loadDataHelper.cs
7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using Google.Cloud.Bigtable.V2;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using HdrHistogram;
using System.Threading.Tasks;
using Google.Api.Gax.Grpc;
namespace Google.Cloud.Bigtable.ScanTest
{
class loadDataHelper
{
private AppSettings _appSettings;
private V2.TableName _table;
private BigtableClient _client;
private Random _rnd = new Random();
public loadDataHelper(V2.TableName _tableName)
{
_appSettings = Program.AppSetting;
_table = _tableName;
_client = BigtableClient.Create();
Program.IsDataLoaded = true;
}
public void loadTable()
{
//List<Thread> threads = new List<Thread>();
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Loading {_appSettings.Records} records into table {_appSettings.TableName}. Threads: {_appSettings.LoadThreads}. Cluster: {_appSettings.InstanceId}");
try
{
var tasks = new List<Task<int>>();
long recordsPerThread = _appSettings.Records / _appSettings.LoadThreads;
long reminder = _appSettings.Records % _appSettings.LoadThreads;
//initialize load histogram again
//_loadHistogram = new LongConcurrentHistogram(3, TimeStamp.Hours(1), 3);
for (int threadCount = 0; threadCount < _appSettings.LoadThreads; threadCount++)
{
int _position = threadCount;
if (threadCount == _appSettings.LoadThreads - 1)
{
//threads.Add(new Thread(() => processLoad(_position, recordsPerThread, recordsPerThread + reminder)));
string _input = _position.ToString() + "/" + recordsPerThread.ToString() + "/" + (recordsPerThread + reminder).ToString();
tasks.Add(Task<int>.Factory.StartNew(processLoad, _input));
}
else
{
// threads.Add(new Thread(() => processLoad(_position, recordsPerThread, recordsPerThread)));
string _input = _position.ToString() + "/" + recordsPerThread.ToString() + "/" + recordsPerThread.ToString();
tasks.Add(Task<int>.Factory.StartNew(processLoad, _input));
}
}
Task.WaitAll(tasks.ToArray());
//threads.ForEach(a => a.Start());
//threads.ForEach(a => a.Join());
//threads.Clear();
//threads = null;
tasks.ForEach(a => a.Dispose());
tasks.Clear();
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Data loaded successfully into table {_appSettings.TableName}");
}
catch (Exception ex)
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Exception while loading data, error message: {ex.Message}");
}
}
private int processLoad(object input)
{
try
{
string[] _array = input.ToString().Split('/');
int threadNumber = Convert.ToInt32(_array[0]);
long startPosition = Convert.ToInt64(_array[1]);
long totalRecords = Convert.ToInt64(_array[2]);
int recordCount = 0;
MutateRowsRequest request = new MutateRowsRequest() { TableNameAsTableName = _table };
MutateRowsRequest.Types.Entry entry = new MutateRowsRequest.Types.Entry();
// iterating through number of records assigned to a thread
long recordNumber;
for (int insertCount = 0; insertCount < totalRecords; insertCount++)
{
recordCount++;
entry = new MutateRowsRequest.Types.Entry();
entry.Mutations.Add(MutationsBuilder());
// building a batch of defined size
recordNumber = threadNumber * startPosition + insertCount;
StringBuilder sbRowKey = new StringBuilder(_appSettings.RowKeyPrefix + recordNumber.ToString("D" + _appSettings.RowKeySize));
entry.RowKey = Protobuf.ByteString.CopyFromUtf8(sbRowKey.ToString());
request.Entries.Add(entry);
if (recordCount == _appSettings.BatchSize || insertCount == totalRecords - 1)
{
long startTime = Stopwatch.GetTimestamp();
long responseTime;
recordCount = 0;
try
{
#if DEBUG
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Thread: {threadNumber + 1} Inserting record {insertCount + 1}. Last rowkey {sbRowKey.ToString()} for table {_appSettings.TableName}");
#endif
BigtableClient.MutateRowsStream response = _client.MutateRows(request
, CallSettings.FromCallTiming(CallTiming.FromTimeout(TimeSpan.FromMilliseconds(_appSettings.MutateRowTimeOutInMilliSeconds))));
response.GrpcCall.ResponseHeadersAsync.Wait();
}
catch (Exception ex)
{
#if DEBUG
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Exception inserting record {insertCount + 1}. Last rowkey {sbRowKey.ToString()} for table {_appSettings.TableName}");
#endif
}
responseTime = (Stopwatch.GetTimestamp() - startTime) / (Stopwatch.Frequency / 100000);
histogramHelper.LoadHistogram.RecordValue(responseTime);
request = new MutateRowsRequest
{
TableNameAsTableName = _table
};
}
}
}
catch (Exception ex)
{
#if DEBUG
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} Exception to load data for table {_appSettings.TableName}. Error Message: {ex.Message}");
#endif
}
return 0;
}
// Constructing mutation for 1 row
private Mutation[] MutationsBuilder()
{
Mutation[] _columns = new Mutation[_appSettings.Columns];
for (int cellCount = 0; cellCount < _columns.Length; cellCount++)
{
_columns[cellCount] = Mutations.SetCell(_appSettings.ColumnFamily, $"{_appSettings.ColumnPrefix}{cellCount}", new BigtableByteString(RandomData()));
}
return _columns;
}
private Byte[] RandomData()
{
Byte[] b = new Byte[_appSettings.ColumnLength];
_rnd.NextBytes(b);
return b;
}
}
}