forked from datastax/csharp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargeDataTests.cs
418 lines (367 loc) · 13.7 KB
/
LargeDataTests.cs
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//
// Copyright (C) 2012 DataStax Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Threading;
#if MYTEST
using MyTest;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Cassandra.MSTest;
#endif
namespace Cassandra.MSTest
{
[TestClass]
public class LargeDataTests
{
string ksname = "large_data";
int key = 0;
Cluster cluster;
Session session;
CCMBridge.CCMCluster CCMCluster;
[TestInitialize]
public void SetFixture()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
}
private void SetupDefaultCluster()
{
if (CCMCluster != null)
CCMCluster.Discard();
CCMCluster = CCMBridge.CCMCluster.Create(2, Cluster.Builder());
session = CCMCluster.Session;
cluster = CCMCluster.Cluster;
session.CreateKeyspaceIfNotExists(ksname);
session.ChangeKeyspace(ksname);
}
[TestCleanup]
public void Dispose()
{
if(CCMCluster!=null)
CCMCluster.Discard();
}
/*
* Test a wide row of size 1,000,000
* @param c The cluster object
* @param key The key value that will receive the data
* @throws Exception
*/
private void testWideRows()
{
// Write data
for (int i = 0; i < 500; ++i) //1000000
session.Execute("INSERT INTO wide_rows(k,i) VALUES(" + key + "," + i + ")", ConsistencyLevel.Quorum);
// Read data
using (var rs = session.Execute("SELECT i FROM wide_rows WHERE k = " + key.ToString(), ConsistencyLevel.Quorum))
{
// Verify data
int j = 0;
foreach (Row row in rs.GetRows())
Assert.True((int)row["i"] == j++);
}
}
/*
* Test a batch that writes a row of size 10,000
* @param c The cluster object
* @param key The key value that will receive the data
* @throws Throwable
*/
private void testWideBatchRows()
{
// Write data
StringBuilder sb = new StringBuilder("BEGIN BATCH ");
for (int i = 0; i < 500; ++i) // 10000
sb.AppendLine(string.Format("INSERT INTO wide_batch_rows(k,i) VALUES({0},{1})", key, i));
sb.Append("APPLY BATCH");
session.Execute(sb.ToString(), ConsistencyLevel.Quorum);
// Read data
using (var rs = session.Execute("SELECT i FROM wide_batch_rows WHERE k = " + key.ToString(), ConsistencyLevel.Quorum))
{
// Verify data
int j = 0;
foreach (Row row in rs.GetRows())
Assert.True((int)row["i"] == j++);
}
}
/*
* Test a wide row of size 1,000,000 consisting of a ByteBuffer
*/
private void testByteRows()
{
// Build small ByteBuffer sample
BEBinaryWriter bw = new BEBinaryWriter();
for (int i = 0; i < 56; i++)
bw.WriteByte(0);
bw.WriteUInt16((ushort)0xCAFE);
byte[] bb = new byte[58];
Array.Copy(bw.GetBuffer(), bb, 58);
// Write data
for (int i = 0; i < 500; ++i)//1000000
session.Execute(string.Format("INSERT INTO wide_byte_rows(k,i) values({0},0x{1})", key, Cassandra.CqlQueryTools.ToHex(bb)), ConsistencyLevel.Quorum);
// Read data
using (var rs = session.Execute("SELECT i FROM wide_byte_rows WHERE k = " + key.ToString(), ConsistencyLevel.Quorum))
{
// Verify data
foreach (Row row in rs.GetRows())
Assert.ArrEqual((byte[])row["i"], bb);
}
}
/*
* Test a row with a single extra large text value
*/
private void testLargeText()
{
// Write data
StringBuilder b = new StringBuilder();
for (int i = 0; i < 1000; ++i)
b.Append(i.ToString());// Create ultra-long text
session.Execute(string.Format("INSERT INTO large_text(k,i) VALUES({0},'{1}')", key, b.ToString()), ConsistencyLevel.Quorum);
// Read data
using (var rs = session.Execute("SELECT * FROM large_text WHERE k = " + key.ToString(), ConsistencyLevel.Quorum))
{
Row row = rs.GetRows().FirstOrDefault();// select().all().from("large_text").where(eq("k", key))).one();
// Verify data
Assert.True(b.ToString().Equals(row["i"]));
}
}
/*
* Converts an integer to an string of letters
*/
private static String createColumnName(int i)
{
String[] letters = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
StringBuilder columnName;
int currentI;
currentI = i;
columnName = new StringBuilder();
while (true)
{
columnName.Append(letters[currentI % 10]);
currentI /= 10;
if (currentI == 0)
break;
}
return columnName.ToString();
}
/*
* Creates a table with 330 columns
*/
private void testWideTable()
{
// Write data
StringBuilder insrt = new StringBuilder("INSERT INTO wide_table(k");
StringBuilder valus = new StringBuilder(" VALUES(" + key.ToString());
for (int i = 0; i < 330; ++i)
{
insrt.Append(",\"" + createColumnName(i)+"\"");
valus.Append("," + i.ToString());
}
insrt.Append(") " + valus.ToString() + ")");
session.Execute(insrt.ToString(), ConsistencyLevel.Quorum);
// Read data
using (var rs = session.Execute("SELECT * FROM wide_table WHERE k = " + key.ToString(), ConsistencyLevel.Quorum))
{
Row row = rs.GetRows().FirstOrDefault();
Assert.True(row != null, "row is null");
Assert.True(row.Length >= 330, "not enough columns");
// Verify data
for (int i = 0; i < 330; ++i)
{
var cn = createColumnName(i);
Assert.True(row[cn] != null, "column is null");
Assert.True(row[cn] is int, "column is not int");
Assert.True((int)row[cn] == i);
}
}
}
private void largeDataTest(string tableName, string cqlType = "int")
{
try
{
session.WaitForSchemaAgreement(
session.Execute("DROP TABLE " + tableName));
}
catch (InvalidConfigurationInQueryException ex) { }
if (tableName == "wide_table")
session.WaitForSchemaAgreement(
session.Execute(GetTableDeclaration()));
else
session.WaitForSchemaAgreement(
session.Execute(String.Format("CREATE TABLE {0} (k INT, i {1}, PRIMARY KEY(k,i))", tableName, cqlType)));
try
{
switch (tableName)
{
case "wide_rows":
testWideRows();
break;
case "wide_batch_rows":
testWideBatchRows();
break;
case "wide_byte_rows":
testByteRows();
break;
case "large_text":
testLargeText();
break;
case "wide_table":
testWideTable();
break;
default:
throw new InvalidOperationException();
}
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// Test a wide row of size 1,000,000
/// </summary>
/// <throws name="Exception"></throws>
[TestMethod]
[WorksForMe]
public void wideRows()
{
SetupDefaultCluster();
session.ChangeKeyspace(ksname);
largeDataTest("wide_rows");
}
/// <summary>
/// Test a batch that writes a row of size 10,000
/// </summary>
/// <throws name="Exception"></throws>
[TestMethod]
[WorksForMe]
public void wideBatchRows()
{
SetupDefaultCluster();
session.ChangeKeyspace(ksname);
largeDataTest("wide_batch_rows");
}
/// <summary>
/// Test a wide row of size 1,000,000 consisting of a ByteBuffer
/// </summary>
/// <throws name="Exception"></throws>
[TestMethod]
[WorksForMe]
public void wideByteRows()
{
SetupDefaultCluster();
session.ChangeKeyspace(ksname);
largeDataTest("wide_byte_rows", "blob");
}
/// <summary>
/// Test a row with a single extra large text value
/// </summary>
/// <throws name="Exception"></throws>
[TestMethod]
[WorksForMe]
public void largeText()
{
SetupDefaultCluster();
largeDataTest("large_text", "text");
}
/// <summary>
/// Creates a table with 330 columns
/// </summary>
/// <throws name="Exception"></throws>
[TestMethod]
[WorksForMe]
public void wideTable()
{
SetupDefaultCluster();
largeDataTest("wide_table");
}
private static String GetTableDeclaration()
{
StringBuilder tableDeclaration = new StringBuilder();
tableDeclaration.Append("CREATE TABLE wide_table (");
tableDeclaration.Append("k INT PRIMARY KEY");
for (int i = 0; i < 330; ++i)
{
tableDeclaration.Append(String.Format(", \"{0}\" INT", createColumnName(i)));
}
tableDeclaration.Append(")");
return tableDeclaration.ToString();
}
/// <summary>
/// Tests 10 random tests consisting of the other methods in this class
/// </summary>
/// <throws name="Exception"></throws>
[TestMethod]
[WorksForMe]
public void mixedDurationTestCCM()
{
if (CCMCluster != null)
CCMCluster.Discard();
CCMCluster = CCMBridge.CCMCluster.Create(3, Cluster.Builder());
cluster = CCMCluster.Cluster;
session = CCMCluster.Session;
session.CreateKeyspace("large_data", ReplicationStrategies.CreateSimpleStrategyReplicationProperty(3));
session.ChangeKeyspace("large_data");
session.WaitForSchemaAgreement(
session.Execute(String.Format("CREATE TABLE {0} (k INT, i INT, PRIMARY KEY(k, i))", "wide_rows")));
session.WaitForSchemaAgreement(
session.Execute(String.Format("CREATE TABLE {0} (k INT, i INT, PRIMARY KEY(k, i))", "wide_batch_rows")));
session.WaitForSchemaAgreement(
session.Execute(String.Format("CREATE TABLE {0} (k INT, i BLOB, PRIMARY KEY(k, i))", "wide_byte_rows")));
session.WaitForSchemaAgreement(
session.Execute(String.Format("CREATE TABLE {0} (k int PRIMARY KEY, i text)", "large_text")));
// Create the extra wide table definition
StringBuilder tableDeclaration = new StringBuilder();
tableDeclaration.Append("CREATE TABLE wide_table (");
tableDeclaration.Append("k INT PRIMARY KEY");
for (int i = 0; i < 330; ++i)
{
tableDeclaration.Append(String.Format(", \"{0}\" INT", createColumnName(i)));
}
tableDeclaration.Append(")");
session.WaitForSchemaAgreement(
session.Execute(tableDeclaration.ToString())
);
Random rndm = new Random(DateTime.Now.Millisecond);
try
{
for (int i = 0; i < 10; ++i)
{
switch ((int)rndm.Next(0,5))
{
case 0: testWideRows(); break;
case 1: testWideBatchRows(); break;
case 2: testByteRows(); break;
case 3: testLargeText(); break;
case 4: testWideTable(); break;
default: break;
}
}
}
catch (Exception e)
{
CCMCluster.ErrorOut();
throw e;
}
finally
{
CCMCluster.Discard();
}
}
}
}