/
RavenDB-15080.cs
661 lines (554 loc) · 24.8 KB
/
RavenDB-15080.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FastTests.Server.Replication;
using Raven.Client.Documents;
using Raven.Client.Documents.Operations;
using Raven.Client.Documents.Smuggler;
using Raven.Server.ServerWide.Context;
using SlowTests.Core.Utils.Entities;
using Tests.Infrastructure;
using Xunit;
using Xunit.Abstractions;
namespace SlowTests.Issues
{
public class RavenDB_15080 : ReplicationTestBase
{
public RavenDB_15080(ITestOutputHelper output) : base(output)
{
}
[Fact]
public async Task CanSplitLowerCasedAndUpperCasedCounterNames()
{
using (var storeA = GetDocumentStore())
{
using (var session = storeA.OpenAsyncSession())
{
await session.StoreAsync(new User
{
Name = "Aviv1"
}, "users/1");
var countersFor = session.CountersFor("users/1");
for (int i = 0; i < 500; i++)
{
var str = $"abc{i}";
countersFor.Increment(str);
}
await session.SaveChangesAsync();
}
using (var session = storeA.OpenAsyncSession())
{
var countersFor = session.CountersFor("users/1");
for (int i = 0; i < 500; i++)
{
var str = $"Xyz{i}";
countersFor.Increment(str);
}
await session.SaveChangesAsync();
}
}
}
[Fact]
public async Task SplitCounterShouldRemoveUnusedEntries()
{
using (var store = GetDocumentStore())
{
await store.Maintenance.SendAsync(new CreateSampleDataOperation(DatabaseItemType.CounterGroups | DatabaseItemType.Documents));
using (var session = store.OpenAsyncSession())
{
var counter = session.CountersFor("products/74-A");
for (int i = 0; i < 3000; i++)
{
counter.Increment($"⭐{i}");
}
await session.SaveChangesAsync();
}
var hashSetAll = new HashSet<string>();
var hashSetWithValues = new HashSet<string>();
var db = await GetDocumentDatabaseInstanceFor(store);
using (db.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext ctx))
using (ctx.OpenReadTransaction())
{
foreach (var details in db.DocumentsStorage.CountersStorage.GetCounterValuesForDocument(ctx, "products/74-A"))
{
hashSetAll.Clear();
hashSetWithValues.Clear();
foreach (var item in db.DocumentsStorage.CountersStorage.GetCountersFromCounterGroup(details))
{
var x = item.DbId;
var y = item.Value;
hashSetAll.Add(x);
if (y != 0)
hashSetWithValues.Add(x);
}
Assert.Equal(hashSetWithValues.Count, hashSetAll.Count);
}
}
using (var session = store.OpenAsyncSession())
{
var counter = session.CountersFor("products/74-A");
for (int i = 0; i < 2345; i++)
{
counter.Delete($"⭐{i}");
}
await session.SaveChangesAsync();
}
using (var session = store.OpenAsyncSession())
{
var counter = session.CountersFor("products/74-A");
for (int i = 0; i < 1000; i++)
{
counter.Increment($"{i}⭐");
}
await session.SaveChangesAsync();
}
using (db.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext ctx))
using (ctx.OpenReadTransaction())
{
foreach (var details in db.DocumentsStorage.CountersStorage.GetCounterValuesForDocument(ctx, "products/74-A"))
{
hashSetAll.Clear();
hashSetWithValues.Clear();
foreach (var item in db.DocumentsStorage.CountersStorage.GetCountersFromCounterGroup(details))
{
var x = item.DbId;
var y = item.Value;
hashSetAll.Add(x);
if (y != 0)
hashSetWithValues.Add(x);
}
Assert.Equal(hashSetWithValues.Count, hashSetAll.Count);
}
}
}
}
[Fact]
public async Task CanSplitAndReplicateRandomCounterName()
{
using (var storeA = GetDocumentStore())
using (var storeB = GetDocumentStore())
{
await SetupReplicationAsync(storeA, storeB);
await SetupReplicationAsync(storeB, storeA);
using (var session = storeA.OpenAsyncSession())
{
await session.StoreAsync(new User { Name = "Aviv1" }, "users/1");
await session.SaveChangesAsync();
}
EnsureReplicating(storeA, storeB);
var t1 = Task.Run(async () =>
{
var rand = new Random(42);
for (int i = 0; i < 1_000; i++)
{
using (var session = storeA.OpenAsyncSession())
{
var str = RandomString(8, rand);
session.CountersFor("users/1").Increment(str, 100000);
await session.SaveChangesAsync();
}
}
});
var t2 = Task.Run(async () =>
{
var rand = new Random(357);
for (int i = 0; i < 1_000; i++)
{
using (var session = storeB.OpenAsyncSession())
{
var str = RandomString(8, rand);
session.CountersFor("users/1").Increment(str, 100000);
await session.SaveChangesAsync();
}
}
});
await Task.WhenAll(t1, t2);
Assert.True(EnsureReplicating(storeA, storeB, out var errMsg), errMsg);
Assert.True(EnsureReplicating(storeB, storeA, out errMsg), errMsg);
await EnsureNoReplicationLoop(Server, storeA.Database);
await EnsureNoReplicationLoop(Server, storeB.Database);
}
}
[Fact]
public void CounterOperationsShouldBeCaseInsensitiveToCounterName()
{
using (var store = GetDocumentStore())
{
using (var session = store.OpenSession())
{
session.Store(new User
{
Name = "Aviv"
}, "users/1");
session.CountersFor("users/1").Increment("abc");
session.SaveChanges();
}
using (var session = store.OpenSession())
{
// should NOT create a new counter
session.CountersFor("users/1").Increment("ABc");
session.SaveChanges();
}
var db = GetDocumentDatabaseInstanceFor(store).Result;
using (db.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext ctx))
using (ctx.OpenReadTransaction())
{
var all = db.DocumentsStorage.CountersStorage.GetCountersForDocument(ctx, "users/1")?.ToList();
Assert.Equal(1, all?.Count);
Assert.Equal("abc", all?[0]);
}
using (var session = store.OpenSession())
{
// get should be case-insensitive to counter name
var val = session.CountersFor("users/1").Get("AbC");
Assert.True(val.HasValue);
Assert.Equal(2, val);
var doc = session.Load<User>("users/1");
var counterNames = session.Advanced.GetCountersFor(doc);
Assert.Equal(1, counterNames.Count);
Assert.Equal("abc", counterNames[0]); // metadata counter-names should preserve their original casing
}
using (var session = store.OpenSession())
{
session.CountersFor("users/1").Increment("XyZ");
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var val = session.CountersFor("users/1").Get("xyz");
Assert.True(val.HasValue);
Assert.Equal(1, val);
var doc = session.Load<User>("users/1");
var counterNames = session.Advanced.GetCountersFor(doc);
Assert.Equal(2, counterNames.Count);
// metadata counter-names should preserve their original casing
Assert.Equal("abc", counterNames[0]);
Assert.Equal("XyZ", counterNames[1]);
}
using (var session = store.OpenSession())
{
// delete should be case-insensitive to counter name
session.CountersFor("users/1").Delete("aBC");
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var val = session.CountersFor("users/1").Get("abc");
Assert.Null(val);
var doc = session.Load<User>("users/1");
var counterNames = session.Advanced.GetCountersFor(doc);
Assert.Equal(1, counterNames.Count);
Assert.Equal("XyZ", counterNames[0]);
}
using (var session = store.OpenSession())
{
session.CountersFor("users/1").Delete("xyZ");
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var val = session.CountersFor("users/1").Get("Xyz");
Assert.Null(val);
var doc = session.Load<User>("users/1");
var counterNames = session.Advanced.GetCountersFor(doc);
Assert.Null(counterNames);
}
}
}
[Fact]
public async Task CountersShouldBeCaseInsensitive()
{
// RavenDB-14753
using (var store = GetDocumentStore())
{
using (var session = store.OpenSession())
{
var company = new Company
{
Name = "HR"
};
session.Store(company, "companies/1");
session.CountersFor(company).Increment("Likes", 999);
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var company = session.Load<Company>("companies/1");
session.CountersFor(company).Delete("lIkEs");
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var company = session.Load<Company>("companies/1");
var counters = session.CountersFor(company).GetAll();
Assert.Equal(0, counters.Count);
}
var database = await GetDocumentDatabaseInstanceFor(store);
var countersStorage = database.DocumentsStorage.CountersStorage;
using (database.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
using (context.OpenReadTransaction())
{
var values = countersStorage
.GetCounterPartialValues(context, "companies/1", "Likes")
.ToList();
Assert.Equal(0, values.Count);
}
}
}
[Fact]
public void DeletedCounterShouldNotBePresentInMetadataCounters()
{
// RavenDB-14753
using (var store = GetDocumentStore())
{
using (var session = store.OpenSession())
{
var company = new Company
{
Name = "HR"
};
session.Store(company, "companies/1");
session.CountersFor(company).Increment("Likes", 999);
session.CountersFor(company).Increment("Cats", 999);
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var company = session.Load<Company>("companies/1");
session.CountersFor(company).Delete("lIkEs");
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var company = session.Load<Company>("companies/1");
company.Name = "RavenDB";
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var company = session.Load<Company>("companies/1");
var counters = session.Advanced.GetCountersFor(company);
Assert.Equal(1, counters.Count);
Assert.Equal("Cats", counters[0]);
}
}
}
[Fact]
public void GetCountersForDocumentShouldReturnNamesInTheirOriginalCasing()
{
using (var store = GetDocumentStore())
{
using (var session = store.OpenSession())
{
session.Store(new User(), "users/1");
var countersFor = session.CountersFor("users/1");
countersFor.Increment("AviV");
countersFor.Increment("Karmel");
countersFor.Increment("PAWEL");
session.SaveChanges();
}
var db = GetDocumentDatabaseInstanceFor(store).Result;
using (db.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext ctx))
using (ctx.OpenReadTransaction())
{
var all = db.DocumentsStorage.CountersStorage.GetCountersForDocument(ctx, "users/1")?.ToList();
Assert.Equal(3, all?.Count);
Assert.Equal("AviV", all[0]);
Assert.Equal("Karmel", all[1]);
Assert.Equal("PAWEL", all[2]);
}
using (var session = store.OpenSession())
{
// GetAll should return counter names in their original casing
var all = session.CountersFor("users/1").GetAll();
Assert.Equal(3, all?.Count);
var keys = all.Keys.ToList();
Assert.True(keys.Contains("AviV"));
Assert.True(keys.Contains("Karmel"));
Assert.True(keys.Contains("PAWEL"));
}
}
}
[Fact]
public void CanDeleteAndReInsertCounter()
{
using (var store = GetDocumentStore())
{
using (var session = store.OpenSession())
{
var company = new Company
{
Name = "HR"
};
session.Store(company, "companies/1");
session.CountersFor(company).Increment("Likes", 999);
session.CountersFor(company).Increment("Cats", 999);
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var company = session.Load<Company>("companies/1");
session.CountersFor(company).Delete("Likes");
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var company = session.Load<Company>("companies/1");
var counters = session.Advanced.GetCountersFor(company);
Assert.Equal(1, counters.Count);
Assert.Equal("Cats", counters[0]);
var counter = session.CountersFor(company).Get("Likes");
Assert.Null(counter);
var all = session.CountersFor(company).GetAll();
Assert.Equal(1, all.Count);
}
using (var session = store.OpenSession())
{
session.CountersFor("companies/1").Increment("Likes");
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var company = session.Load<Company>("companies/1");
var counters = session.Advanced.GetCountersFor(company);
Assert.Equal(2, counters.Count);
Assert.Equal("Cats", counters[0]);
Assert.Equal("Likes", counters[1]);
var counter = session.CountersFor(company).Get("Likes");
Assert.NotNull(counter);
Assert.Equal(1, counter.Value);
}
}
}
[Fact]
public void CountersSessionCacheShouldBeCaseInsensitiveToCounterName()
{
using (var store = GetDocumentStore())
{
using (var session = store.OpenSession())
{
var company = new Company
{
Name = "HR"
};
session.Store(company, "companies/1");
session.CountersFor(company).Increment("Likes", 333);
session.CountersFor(company).Increment("Cats", 999);
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var company = session.Load<Company>("companies/1");
// the document is now tracked by the session,
// so now counters-cache has access to '@counters' from metadata
// searching for the counter's name in '@counters' should be done in a case insensitive manner
// counter name should be found in '@counters' => go to server
var counter = session.CountersFor(company).Get("liKes");
Assert.Equal(2, session.Advanced.NumberOfRequests);
Assert.NotNull(counter);
Assert.Equal(333, counter.Value);
counter = session.CountersFor(company).Get("cats");
Assert.Equal(3, session.Advanced.NumberOfRequests);
Assert.NotNull(counter);
Assert.Equal(999, counter.Value);
}
}
}
[Fact]
public async Task ExportAndImportCountersShouldKeepOriginalCasing()
{
var file = GetTempFileName();
try
{
using (var store1 = GetDocumentStore())
using (var store2 = GetDocumentStore())
{
using (var session = store1.OpenAsyncSession())
{
await session.StoreAsync(new User { Name = "Name" }, "users/1");
await session.StoreAsync(new User { Name = "Name2" }, "users/2");
await session.SaveChangesAsync();
}
using (var session = store1.OpenAsyncSession())
{
session.CountersFor("users/1").Increment("Likes", 100);
session.CountersFor("users/1").Increment("Dislikes", 200);
session.CountersFor("users/2").Increment("Downloads", 500);
await session.SaveChangesAsync();
}
var operation = await store1.Smuggler.ExportAsync(new DatabaseSmugglerExportOptions(), file);
await operation.WaitForCompletionAsync(TimeSpan.FromMinutes(1));
operation = await store2.Smuggler.ImportAsync(new DatabaseSmugglerImportOptions(), file);
await operation.WaitForCompletionAsync(TimeSpan.FromMinutes(1));
var stats = await store2.Maintenance.SendAsync(new GetStatisticsOperation());
Assert.Equal(2, stats.CountOfDocuments);
Assert.Equal(2, stats.CountOfCounterEntries);
using (var session = store2.OpenAsyncSession())
{
var user1 = await session.LoadAsync<User>("users/1");
var user2 = await session.LoadAsync<User>("users/2");
Assert.Equal("Name", user1.Name);
Assert.Equal("Name2", user2.Name);
var dic = await session.CountersFor(user1).GetAllAsync();
Assert.Equal(2, dic.Count);
Assert.Equal(100, dic["likes"]);
Assert.Equal(200, dic["dislikes"]);
var val = await session.CountersFor(user2).GetAsync("downloads");
Assert.Equal(500, val);
}
using (var session = store2.OpenAsyncSession())
{
var user1 = await session.LoadAsync<User>("users/1");
var counterNames = session.Advanced.GetCountersFor(user1);
Assert.NotNull(counterNames);
Assert.Equal(2, counterNames.Count);
Assert.Equal("Dislikes", counterNames[0]);
Assert.Equal("Likes", counterNames[1]);
var user2 = await session.LoadAsync<User>("users/2");
counterNames = session.Advanced.GetCountersFor(user2);
Assert.NotNull(counterNames);
Assert.Equal(1, counterNames.Count);
Assert.Equal("Downloads", counterNames[0]);
}
}
}
finally
{
File.Delete(file);
}
}
private static string RandomString(int size, Random random)
{
var builder = new StringBuilder();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(74 * random.NextDouble() + 48)));
builder.Append(ch);
}
return builder.ToString();
}
private bool EnsureReplicating(IDocumentStore src, IDocumentStore dst, out string errorMsg)
{
errorMsg = string.Empty;
var id = "marker/" + Guid.NewGuid();
using (var s = src.OpenSession())
{
s.Store(new { }, id);
s.SaveChanges();
}
if (WaitForDocumentToReplicate<object>(dst, id, 15 * 1000) != null)
return true;
errorMsg = $"Failed to replicate from '{src.Database}' to '{dst.Database}' : ";
var replicationLoader = GetDocumentDatabaseInstanceFor(src).Result.ReplicationLoader;
foreach (var e in replicationLoader.OutgoingFailureInfo)
{
errorMsg += string.Join(", ", e.Value.Errors.Select(x => x.Message));
}
return false;
}
}
}