forked from JanusGraph/janusgraph
/
InMemoryColumnValueStoreTest.java
522 lines (415 loc) · 21.9 KB
/
InMemoryColumnValueStoreTest.java
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
// Copyright 2019 JanusGraph Authors
//
// 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.
package org.janusgraph.diskstorage.inmemory;
import org.janusgraph.diskstorage.BaseTransactionConfig;
import org.janusgraph.diskstorage.Entry;
import org.janusgraph.diskstorage.EntryList;
import org.janusgraph.diskstorage.StaticBuffer;
import org.janusgraph.diskstorage.keycolumnvalue.KeySliceQuery;
import org.janusgraph.diskstorage.keycolumnvalue.StoreTransaction;
import org.janusgraph.diskstorage.locking.TemporaryLockingException;
import org.janusgraph.diskstorage.util.StaticArrayBuffer;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static org.janusgraph.diskstorage.inmemory.BufferPageTest.makeEntry;
import static org.janusgraph.diskstorage.inmemory.BufferPageTest.makeStaticBuffer;
import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.STORAGE_TRANSACTIONAL;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* This is a unit test for custom override of JanusGraph ColumnValueStore. Required to verify the added logic and
* the correctness of its integration with standard logic
*/
public class InMemoryColumnValueStoreTest
{
public static final String VERY_START = Character.valueOf((char)0).toString();
public static final String COL_START = "00colStart";
public static final String COL_END = "99colEnd";
public static final String VERY_END = "zz";
@Test
public void testRoundtrip()
{
InMemoryColumnValueStore[] cvStores = new InMemoryColumnValueStore[] {
new InMemoryColumnValueStore(),
//same as above but page size = 3 to catch multi-page cases which otherwise
// go untested with default page size of 500 due to small sample data set
new TestPagedBufferColumnValueStore(3)
};
//first, add some columns
List<Entry> additions1 = Arrays.asList(
makeEntry("02col2", "val2"),
makeEntry("01col1", "val1"),
makeEntry("03col3", "val3"),
makeEntry(COL_END, "valEnd"),
makeEntry(COL_START, "valStart"),
makeEntry("04emptycol", "")
);
List<StaticBuffer> deletions1 = Collections.emptyList();
StoreTransaction txh = mock(StoreTransaction.class);
BaseTransactionConfig mockConfig = mock(BaseTransactionConfig.class);
when(txh.getConfiguration()).thenReturn(mockConfig);
when(mockConfig.getCustomOption(eq(STORAGE_TRANSACTIONAL))).thenReturn(true);
for(InMemoryColumnValueStore cvs : cvStores)
{
cvs.mutate(additions1, deletions1, txh);
}
EntryList[] results = new EntryList[cvStores.length];
//check the added/retrieved columns against originals
for(int i=0; i<cvStores.length; i++)
{
results[i] = cvStores[i].getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
makeStaticBuffer(COL_START), makeStaticBuffer(VERY_END)),
txh //if we pass COL_END, it doesn't get included
);
assertEquals(additions1.size(), results[i].size());
}
//we'll get the results back sorted, so we sort the initial entries as well, for ease of comparison
additions1.sort(Comparator.naturalOrder());
for(int j=0; j<cvStores.length; j++)
{
for (int i=0; i< results[j].size(); i++)
{
assertEquals(additions1.get(i), results[j].get(i));
assertEquals(additions1.get(i), results[j].get(i));
assertEquals(additions1.get(i).getColumn(), results[j].get(i).getColumn());
assertEquals(additions1.get(i).getValue(), results[j].get(i).getValue());
}
}
//now delete and update some columns
List<Entry> additions2 = Arrays.asList(
makeEntry("02col2", "val2_updated"),
makeEntry("03col3", "val3_updated")
);
List<StaticBuffer> deletions2 = Arrays.asList(
makeStaticBuffer("01col1"),
makeStaticBuffer("02col2")
);
for(InMemoryColumnValueStore cvs : cvStores)
{
cvs.mutate(additions2, deletions2, txh);
}
for(int i=0; i<cvStores.length; i++)
{
results[i] = cvStores[i].getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
makeStaticBuffer(COL_START), makeStaticBuffer(VERY_END)),
txh //if we pass COL_END, it doesn't get included
);
//col2 was in both deletes AND updates, JanusGraph logic is to honour update over delete, so we expect only col1 to be deleted
assertEquals(additions1.size()-1, results[i].size());
}
for(int i=0; i<cvStores.length; i++)
{
final Map<StaticBuffer, StaticBuffer> seenValues = new HashMap<>(additions1.size()-1);
StaticArrayBuffer expectDeleted = makeStaticBuffer("01col1");
results[i].forEach(entry -> seenValues.put(entry.getColumn(), entry.getValue()));
assertFalse(seenValues.containsKey(expectDeleted));
additions2.forEach(entry ->
{
assertTrue(seenValues.containsKey(entry.getColumn()));
assertEquals(seenValues.get(entry.getColumn()), entry.getValue());
});
}
for(int i=0; i<cvStores.length; i++)
{
results[i] = cvStores[i].getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
additions1.get(1).getColumn(),
additions1.get(3).getColumn()),
txh //if we pass COL_END, it doesn't get included
);
assertEquals(1, results[i].size());
}
}
@Test
public void testVolumeSlidingWindows() throws TemporaryLockingException
{
int pageSize = InMemoryColumnValueStore.DEF_PAGE_SIZE;
int maxNumEntries = 7*pageSize + pageSize/2;
StoreTransaction txh = mock(StoreTransaction.class);
BaseTransactionConfig mockConfig = mock(BaseTransactionConfig.class);
when(txh.getConfiguration()).thenReturn(mockConfig);
when(mockConfig.getCustomOption(eq(STORAGE_TRANSACTIONAL))).thenReturn(true);
InMemoryColumnValueStore cvs = new InMemoryColumnValueStore();
for(int numEntries=1;numEntries<maxNumEntries;numEntries+=pageSize + pageSize/3)
{
List<Entry> additions = generateEntries(0, numEntries,"orig");
cvs.mutate(additions, Collections.emptyList(), txh);
EntryList result = cvs.getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
makeStaticBuffer(VERY_START), makeStaticBuffer(VERY_END)), //if we pass COL_END, it doesn't get included
txh
);
assertEquals(additions.size(), result.size());
int stepSize = numEntries < 100 ? 1 : 21;
for (int windowSize=0; windowSize<numEntries; windowSize+=stepSize)
{
for (int windowStart=0; windowStart<numEntries; windowStart+=stepSize)
{
int windowEnd = Math.min(windowStart+windowSize, numEntries-1);
result = cvs.getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
additions.get(windowStart).getColumn(),
additions.get(windowEnd).getColumn()
),
txh);
assertEquals(windowEnd - windowStart, result.size());
}
}
}
}
@Test
public void testMultipageSlicing() throws TemporaryLockingException
{
int numEntries = 502;
int windowStart = 494;
int windowEnd = 501;
StoreTransaction txh = mock(StoreTransaction.class);
BaseTransactionConfig mockConfig = mock(BaseTransactionConfig.class);
when(txh.getConfiguration()).thenReturn(mockConfig);
when(mockConfig.getCustomOption(eq(STORAGE_TRANSACTIONAL))).thenReturn(true);
InMemoryColumnValueStore cvs = new InMemoryColumnValueStore();
//ColumnValueStore cvs = new DeflatedEntryColumnValueStore(false);
List<Entry> additions = generateEntries(0, numEntries,"orig");
cvs.mutate(additions, Collections.emptyList(), txh);
EntryList result = cvs.getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
makeStaticBuffer(VERY_START),
makeStaticBuffer(VERY_END)), txh //if we pass COL_END, it doesn't get included
);
assertEquals(additions.size(), result.size());
//this getSlice spans two pages and doesn't retrieve either page in full
result = cvs.getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
additions.get(windowStart).getColumn(),
additions.get(windowEnd).getColumn()
), txh);
assertEquals(windowEnd - windowStart, result.size());
}
@Test
public void testMultipageDelete() throws TemporaryLockingException
{
int numEntries = 1001;
StoreTransaction txh = mock(StoreTransaction.class);
BaseTransactionConfig mockConfig = mock(BaseTransactionConfig.class);
when(txh.getConfiguration()).thenReturn(mockConfig);
when(mockConfig.getCustomOption(eq(STORAGE_TRANSACTIONAL))).thenReturn(true);
InMemoryColumnValueStore cvs = new InMemoryColumnValueStore();
//ColumnValueStore cvs = new DeflatedEntryColumnValueStore(false);
List<Entry> additions = generateEntries(0, numEntries,"orig");
cvs.mutate(additions, Collections.emptyList(), txh);
EntryList result = cvs.getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
makeStaticBuffer(VERY_START), makeStaticBuffer(VERY_END)), //if we pass COL_END, it doesn't get included
txh);
assertEquals(additions.size(), result.size());
int windowStart = 494;
int windowEnd = 501;
List<StaticBuffer> deletions = new ArrayList<>(windowEnd-windowStart);
deletions.addAll(additions.subList(windowStart,windowEnd).stream().map(Entry::getColumn).collect(Collectors.toList()));
cvs.mutate(Collections.emptyList(), deletions, txh);
result = cvs.getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
makeStaticBuffer(VERY_START), makeStaticBuffer(VERY_END)), //if we pass COL_END, it doesn't get included
txh);
assertEquals(additions.size()-deletions.size(), result.size());
}
@Test
public void testMultipageUpdateDelete() throws TemporaryLockingException
{
int numEntries = 2511;
StoreTransaction txh = mock(StoreTransaction.class);
BaseTransactionConfig mockConfig = mock(BaseTransactionConfig.class);
when(txh.getConfiguration()).thenReturn(mockConfig);
when(mockConfig.getCustomOption(eq(STORAGE_TRANSACTIONAL))).thenReturn(true);
InMemoryColumnValueStore cvs = new InMemoryColumnValueStore();
//ColumnValueStore cvs = new DeflatedEntryColumnValueStore(false);
List<Entry> additions = generateEntries(0, numEntries,"orig");
cvs.mutate(additions, Collections.emptyList(), txh);
EntryList result = cvs.getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
makeStaticBuffer(VERY_START), makeStaticBuffer(VERY_END)), //if we pass COL_END, it doesn't get included
txh);
assertEquals(additions.size(), result.size());
int windowStart = 494;
int windowEnd = 2002;
//update
List<Entry> updates = generateEntries(windowStart, windowEnd, "updated");
cvs.mutate(updates, Collections.emptyList(), txh);
result = cvs.getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
makeStaticBuffer(VERY_START), makeStaticBuffer(VERY_END)), //if we pass COL_END, it doesn't get included
txh);
assertEquals(additions.size(), result.size());
for(int i=0; i<result.size(); i++)
{
if (windowStart < i && i < windowEnd)
{
assertEquals(updates.get(i-windowStart), result.get(i));
}
else
{
assertEquals(additions.get(i), result.get(i));
}
}
//delete
List<StaticBuffer> deletions = new ArrayList<>(windowEnd-windowStart);
deletions.addAll(additions.subList(windowStart,windowEnd).stream().map(Entry::getColumn).collect(Collectors.toList()));
cvs.mutate(Collections.emptyList(), deletions, txh);
result = cvs.getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
makeStaticBuffer(VERY_START), makeStaticBuffer(VERY_END)), //if we pass COL_END, it doesn't get included
txh);
assertEquals(additions.size()-deletions.size(), result.size());
}
@Test
public void testAddInterleaved() throws TemporaryLockingException
{
int pageSize = InMemoryColumnValueStore.DEF_PAGE_SIZE;
StoreTransaction txh = mock(StoreTransaction.class);
BaseTransactionConfig mockConfig = mock(BaseTransactionConfig.class);
when(txh.getConfiguration()).thenReturn(mockConfig);
when(mockConfig.getCustomOption(eq(STORAGE_TRANSACTIONAL))).thenReturn(true);
InMemoryColumnValueStore cvs = new InMemoryColumnValueStore();
List<Entry> additions1 = generateEntries(0, pageSize*2 + pageSize/2,"orig");
cvs.mutate(additions1, Collections.emptyList(), txh);
List<Entry> additions2 = generateEntries(pageSize*10, pageSize*10 + pageSize*2 + pageSize/2,"orig");
cvs.mutate(additions2, Collections.emptyList(), txh);
List<Entry> additions3 = generateEntries(pageSize*5, pageSize*5 + pageSize*2 + pageSize/2,"orig");
cvs.mutate(additions3, Collections.emptyList(), txh);
EntryList result = cvs.getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
makeStaticBuffer(VERY_START), makeStaticBuffer(VERY_END)), //if we pass COL_END, it doesn't get included
txh);
assertEquals(additions1.size()+additions2.size()+additions3.size(), result.size());
for(int i=0; i< result.size() -1; i++)
{
assertTrue(result.get(i).compareTo(result.get(i+1)) < 0);
}
}
@Test
public void testPagingAndFragmentation() throws TemporaryLockingException
{
int pageSize = InMemoryColumnValueStore.DEF_PAGE_SIZE;
StoreTransaction txh = mock(StoreTransaction.class);
BaseTransactionConfig mockConfig = mock(BaseTransactionConfig.class);
when(txh.getConfiguration()).thenReturn(mockConfig);
when(mockConfig.getCustomOption(eq(STORAGE_TRANSACTIONAL))).thenReturn(true);
InMemoryColumnValueStore cvs = new InMemoryColumnValueStore();
List<Entry> additions = generateEntries(0, pageSize*5 + pageSize/2,"orig");
cvs.mutate(additions, Collections.emptyList(), txh);
//We inserted more than pagesize in one go, so the store should switch to multipage buffer - but, as currently implemented,
// this doesn't get immediately broken up into multiple pages, instead it will hold a single "oversized" page.
//The break-up will only happen if there is a consequent update
assertEquals(1, cvs.numPages(txh));
//emulate update so that the single "oversized" page will be broken up into multiple pages of correct size
cvs.mutate(additions.subList(1, 3), Collections.emptyList(), txh);
assertEquals(6, cvs.numPages(txh));
int numDeleted = 0;
int windowStart = pageSize - pageSize/3;
int windowEnd = pageSize + pageSize/3;
//this should remove parts of page0 and page1
List<StaticBuffer> deletions = new ArrayList<>(windowEnd-windowStart);
deletions.addAll(additions.subList(windowStart,windowEnd).stream().map(Entry::getColumn).collect(Collectors.toList()));
cvs.mutate(Collections.emptyList(), deletions, txh);
numDeleted += windowEnd - windowStart;
EntryList result = cvs.getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
makeStaticBuffer(VERY_START), makeStaticBuffer(VERY_END)), //if we pass COL_END, it doesn't get included
txh);
assertEquals(additions.size() - numDeleted, result.size());
SharedEntryBufferFragmentationReport report = cvs.createFragmentationReport(txh);
assertEquals(6, report.getPageCount());
assertEquals(3, report.getFragmentedPageCount());
//since only 1/3 of each page is removed, the remains won't fit into one page anyway, so not deemed compressable
assertEquals(0, report.getCompressableChunksCount());
assertEquals(0, report.getCompressablePageCount());
windowStart = pageSize * 4 - pageSize/3;
windowEnd = pageSize * 4 + pageSize/3;
//this should remove parts of page3 and page 4
deletions.clear();
deletions.addAll(additions.subList(windowStart,windowEnd).stream().map(Entry::getColumn).collect(Collectors.toList()));
cvs.mutate(Collections.emptyList(), deletions, txh);
numDeleted += windowEnd - windowStart;
result = cvs.getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
makeStaticBuffer(VERY_START), makeStaticBuffer(VERY_END)), //if we pass COL_END, it doesn't get included
txh);
assertEquals(additions.size() - numDeleted, result.size());
report = cvs.createFragmentationReport(txh);
assertEquals(6, report.getPageCount());
assertEquals(5, report.getFragmentedPageCount());
//we now have pages 3 & 4 which are 2/3 full, PLUS page 5 which is half full => 3 pages compressable into 2
assertEquals(1, report.getCompressableChunksCount());
assertEquals(3, report.getCompressablePageCount());
assertEquals(1, report.getAchievablePageReduction());
cvs.quickDefragment(txh);
EntryList result2 = cvs.getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
makeStaticBuffer(VERY_START), makeStaticBuffer(VERY_END)), //if we pass COL_END, it doesn't get included
txh);
assertEquals(additions.size() - numDeleted, result2.size());
for(int i=0; i< result2.size(); i++)
{
assertEquals(result.get(i), result2.get(i));
}
//after quick defrag, we should have 5 pages in total, page 0 & 1 still fragmented, page 4 also not full
report = cvs.createFragmentationReport(txh);
assertEquals(5, report.getPageCount());
assertEquals(3, report.getFragmentedPageCount());
assertEquals(0, report.getCompressableChunksCount());
assertEquals(0, report.getCompressablePageCount());
assertEquals(0, report.getAchievablePageReduction());
windowStart = pageSize - pageSize/2;
windowEnd = pageSize + pageSize/2+1;
//this should remove half of page0 and page1 each
deletions.clear();
deletions.addAll(additions.subList(windowStart,windowEnd).stream().map(Entry::getColumn).collect(Collectors.toList()));
cvs.mutate(Collections.emptyList(), deletions, txh);
numDeleted += (pageSize/2 - pageSize/3) * 2 + 1;
result = cvs.getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
makeStaticBuffer(VERY_START), makeStaticBuffer(VERY_END)), //if we pass COL_END, it doesn't get included
txh);
assertEquals(additions.size() - numDeleted, result.size());
//now two first pages should become collapsible into one
report = cvs.createFragmentationReport(txh);
assertEquals(5, report.getPageCount());
assertEquals(3, report.getFragmentedPageCount());
assertEquals(1, report.getCompressableChunksCount());
assertEquals(2, report.getCompressablePageCount());
assertEquals(1, report.getAchievablePageReduction());
cvs.quickDefragment(txh);
result2 = cvs.getSlice(new KeySliceQuery(makeStaticBuffer("someRow"),
makeStaticBuffer(VERY_START), makeStaticBuffer(VERY_END)), //if we pass COL_END, it doesn't get included
txh);
assertEquals(additions.size() - numDeleted, result2.size());
for(int i=0; i< result2.size(); i++)
{
assertEquals(result.get(i), result2.get(i));
}
//two first pages collapsed into one which is one entry short of full
report = cvs.createFragmentationReport(txh);
assertEquals(4, report.getPageCount());
assertEquals(2, report.getFragmentedPageCount());
assertEquals(0, report.getCompressableChunksCount());
assertEquals(0, report.getCompressablePageCount());
assertEquals(0, report.getAchievablePageReduction());
}
public static List<Entry> generateEntries(int start, int end, String suffix)
{
List<Entry> entries = new ArrayList<>(end-start);
for(int i=start; i<end; i++)
{
entries.add(makeEntry(String.format("%012dcol",i),
String.format("val%d_%s",i,suffix)));
}
return entries;
}
}