/
LocalPaginatedClusterWithWAL.java
executable file
·472 lines (356 loc) · 15.3 KB
/
LocalPaginatedClusterWithWAL.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
package com.orientechnologies.orient.core.storage.impl.local.paginated;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import com.orientechnologies.orient.core.compression.OCompression;
import com.orientechnologies.orient.core.config.*;
import com.orientechnologies.orient.core.db.record.OCurrentStorageComponentsFactory;
import com.orientechnologies.orient.core.storage.impl.local.OStorageVariableParser;
import com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperationsManager;
import com.orientechnologies.orient.core.storage.impl.local.paginated.base.ODurablePage;
import org.testng.Assert;
import org.testng.annotations.*;
import com.orientechnologies.orient.core.index.hashindex.local.cache.OCacheEntry;
import com.orientechnologies.orient.core.index.hashindex.local.cache.ODiskCache;
import com.orientechnologies.orient.core.index.hashindex.local.cache.OReadWriteDiskCache;
import com.orientechnologies.orient.core.storage.fs.OAbstractFile;
import com.orientechnologies.orient.core.storage.impl.local.paginated.wal.*;
/**
* @author Andrey Lomakin
* @since 5/8/13
*/
@Test
public class LocalPaginatedClusterWithWAL extends LocalPaginatedClusterTest {
private ODiskWriteAheadLog writeAheadLog;
private OPaginatedCluster testCluster;
private ODiskCache testDiskCache;
private OLocalPaginatedStorage testStorage;
private String storageDir;
private String testStorageDir;
private OLocalPaginatedStorage storage;
@BeforeMethod
@Override
public void beforeMethod() throws IOException {
buildDirectory = System.getProperty("buildDirectory", ".");
buildDirectory += "/localPaginatedClusterWithWALTest";
createPaginatedCluster();
createTestPaginatedCluster();
}
private void createPaginatedCluster() throws IOException {
storage = mock(OLocalPaginatedStorage.class);
OStorageConfiguration storageConfiguration = mock(OStorageConfiguration.class);
when(storageConfiguration.getContextConfiguration()).thenReturn(new OContextConfiguration());
storageConfiguration.clusters = new ArrayList<OStorageClusterConfiguration>();
storageConfiguration.fileTemplate = new OStorageSegmentConfiguration();
storageConfiguration.binaryFormatVersion = Integer.MAX_VALUE;
storageDir = buildDirectory + "/localPaginatedClusterWithWALTestOne";
when(storage.getStoragePath()).thenReturn(storageDir);
when(storage.getName()).thenReturn("localPaginatedClusterWithWALTestOne");
when(storage.getComponentsFactory()).thenReturn(new OCurrentStorageComponentsFactory(storageConfiguration));
when(storage.getVariableParser()).thenReturn(new OStorageVariableParser(storageDir));
File buildDir = new File(buildDirectory);
if (!buildDir.exists())
buildDir.mkdirs();
File storageDirOneFile = new File(storageDir);
if (!storageDirOneFile.exists())
storageDirOneFile.mkdirs();
writeAheadLog = new ODiskWriteAheadLog(6000, -1, 10 * 1024L * OWALPage.PAGE_SIZE, storage);
diskCache = new OReadWriteDiskCache(400L * 1024 * 1024 * 1024, 1648L * 1024 * 1024,
OGlobalConfiguration.DISK_CACHE_PAGE_SIZE.getValueAsInteger() * 1024, 1000000, 100, storage, null, false, false);
when(storage.getStorageTransaction()).thenReturn(null);
when(storage.getWALInstance()).thenReturn(writeAheadLog);
atomicOperationsManager = new OAtomicOperationsManager(storage);
when(storage.getAtomicOperationsManager()).thenReturn(atomicOperationsManager);
when(storage.getDiskCache()).thenReturn(diskCache);
when(storage.getConfiguration()).thenReturn(storageConfiguration);
when(storage.getMode()).thenReturn("rw");
when(storageConfiguration.getDirectory()).thenReturn(storageDir);
paginatedCluster = new OPaginatedCluster();
paginatedCluster.configure(storage, 6, "testPaginatedClusterWithWALTest", buildDirectory, -1);
paginatedCluster.create(-1);
}
private void createTestPaginatedCluster() throws IOException {
testStorage = mock(OLocalPaginatedStorage.class);
OStorageConfiguration storageConfiguration = mock(OStorageConfiguration.class);
storageConfiguration.clusters = new ArrayList<OStorageClusterConfiguration>();
storageConfiguration.fileTemplate = new OStorageSegmentConfiguration();
storageConfiguration.binaryFormatVersion = Integer.MAX_VALUE;
when(storageConfiguration.getContextConfiguration()).thenReturn(new OContextConfiguration());
testStorageDir = buildDirectory + "/localPaginatedClusterWithWALTestTwo";
when(testStorage.getStoragePath()).thenReturn(testStorageDir);
when(testStorage.getComponentsFactory()).thenReturn(new OCurrentStorageComponentsFactory(storageConfiguration));
when(testStorage.getName()).thenReturn("localPaginatedClusterWithWALTestTwo");
when(testStorage.getVariableParser()).thenReturn(new OStorageVariableParser(testStorageDir));
File buildDir = new File(buildDirectory);
if (!buildDir.exists())
buildDir.mkdirs();
File storageDirTwoFile = new File(testStorageDir);
if (!storageDirTwoFile.exists())
storageDirTwoFile.mkdirs();
testDiskCache = new OReadWriteDiskCache(400L * 1024 * 1024 * 1024, 1648L * 1024 * 1024,
OGlobalConfiguration.DISK_CACHE_PAGE_SIZE.getValueAsInteger() * 1024, 1000000, 100, testStorage, null, false, false);
OStorageVariableParser variableParser = new OStorageVariableParser(testStorageDir);
final OAtomicOperationsManager testAtomicOperationsManager = new OAtomicOperationsManager(testStorage);
when(testStorage.getWALInstance()).thenReturn(null);
when(testStorage.getStorageTransaction()).thenReturn(null);
when(testStorage.getAtomicOperationsManager()).thenReturn(testAtomicOperationsManager);
when(testStorage.getDiskCache()).thenReturn(testDiskCache);
when(testStorage.getVariableParser()).thenReturn(variableParser);
when(testStorage.getConfiguration()).thenReturn(storageConfiguration);
when(testStorage.getMode()).thenReturn("rw");
when(storageConfiguration.getDirectory()).thenReturn(testStorageDir);
testCluster = new OPaginatedCluster();
testCluster.configure(testStorage, 6, "testPaginatedClusterWithWALTest", buildDirectory, -1);
testCluster.create(-1);
}
@AfterMethod
public void afterMethod() throws IOException {
Assert.assertNull(atomicOperationsManager.getCurrentOperation());
writeAheadLog.delete();
paginatedCluster.delete();
diskCache.delete();
testCluster.delete();
testDiskCache.delete();
File file = new File(storageDir);
Assert.assertTrue(file.delete());
file = new File(testStorageDir);
Assert.assertTrue(file.delete());
file = new File(buildDirectory);
Assert.assertTrue(file.delete());
}
@BeforeClass
@Override
public void beforeClass() throws IOException {
System.out.println("Start LocalPaginatedClusterWithWALTest");
}
@AfterClass
@Override
public void afterClass() throws IOException {
System.out.println("End LocalPaginatedClusterWithWALTest");
}
@Override
public void testAddOneSmallRecord() throws IOException {
super.testAddOneSmallRecord();
assertFileRestoreFromWAL();
}
@Override
public void testAddOneBigRecord() throws IOException {
super.testAddOneBigRecord();
assertFileRestoreFromWAL();
}
@Override
public void testUpdateOneSmallRecord() throws IOException {
super.testUpdateOneSmallRecord();
assertFileRestoreFromWAL();
}
@Override
public void testUpdateOneBigRecord() throws IOException {
super.testUpdateOneBigRecord();
assertFileRestoreFromWAL();
}
@Override
public void testDeleteRecordAndAddNewOnItsPlace() throws IOException {
super.testDeleteRecordAndAddNewOnItsPlace();
assertFileRestoreFromWAL();
}
@Override
public void testAddManySmallRecords() throws IOException {
super.testAddManySmallRecords();
assertFileRestoreFromWAL();
}
@Override
public void testUpdateOneSmallRecordVersionIsLowerCurrentOne() throws IOException {
super.testUpdateOneSmallRecordVersionIsLowerCurrentOne();
assertFileRestoreFromWAL();
}
@Override
public void testUpdateOneSmallRecordVersionIsMinusTwo() throws IOException {
super.testUpdateOneSmallRecordVersionIsMinusTwo();
assertFileRestoreFromWAL();
}
@Override
public void testUpdateManySmallRecords() throws IOException {
super.testUpdateManySmallRecords();
assertFileRestoreFromWAL();
}
@Override
public void testAddManyRecords() throws IOException {
super.testAddManyRecords();
assertFileRestoreFromWAL();
}
@Override
public void testAddManyBigRecords() throws IOException {
super.testAddManyBigRecords();
assertFileRestoreFromWAL();
}
@Override
public void testUpdateManyRecords() throws IOException {
super.testUpdateManyRecords();
assertFileRestoreFromWAL();
}
@Override
public void testUpdateManyBigRecords() throws IOException {
super.testUpdateManyBigRecords();
assertFileRestoreFromWAL();
}
@Override
public void testRemoveHalfSmallRecords() throws IOException {
super.testRemoveHalfSmallRecords();
assertFileRestoreFromWAL();
}
@Override
public void testRemoveHalfRecords() throws IOException {
super.testRemoveHalfRecords();
assertFileRestoreFromWAL();
}
@Override
public void testRemoveHalfBigRecords() throws IOException {
super.testRemoveHalfBigRecords();
assertFileRestoreFromWAL();
}
@Override
public void testRemoveHalfRecordsAndAddAnotherHalfAgain() throws IOException {
super.testRemoveHalfRecordsAndAddAnotherHalfAgain();
assertFileRestoreFromWAL();
}
@Override
public void testHideHalfSmallRecords() throws IOException {
super.testHideHalfSmallRecords();
assertFileRestoreFromWAL();
}
@Override
public void testHideHalfBigRecords() throws IOException {
super.testHideHalfBigRecords();
assertFileRestoreFromWAL();
}
@Override
public void testHideHalfRecords() throws IOException {
super.testHideHalfRecords();
assertFileRestoreFromWAL();
}
@Override
public void testHideHalfRecordsAndAddAnotherHalfAgain() throws IOException {
super.testHideHalfRecordsAndAddAnotherHalfAgain();
assertFileRestoreFromWAL();
}
@Override
@Test(enabled = false)
public void testForwardIteration() throws IOException {
super.testForwardIteration();
}
@Override
@Test(enabled = false)
public void testBackwardIteration() throws IOException {
super.testBackwardIteration();
}
@Override
@Test(enabled = false)
public void testGetPhysicalPosition() throws IOException {
super.testGetPhysicalPosition();
}
@Override
@Test(enabled = false)
public void testRecordGrowFactor() throws Exception {
super.testRecordGrowFactor();
}
@Override
@Test(enabled = false)
public void testRecordOverflowGrowFactor() throws Exception {
super.testRecordOverflowGrowFactor();
}
private void assertFileRestoreFromWAL() throws IOException {
paginatedCluster.close();
writeAheadLog.close();
diskCache.clear();
restoreClusterFromWAL();
testCluster.close();
assertClusterContentIsTheSame(testCluster.getName(), paginatedCluster.getName());
testCluster.open();
paginatedCluster.open();
}
private void restoreClusterFromWAL() throws IOException {
ODiskWriteAheadLog log = new ODiskWriteAheadLog(4, -1, 10 * 1024L * OWALPage.PAGE_SIZE, storage);
OLogSequenceNumber lsn = log.begin();
List<OWALRecord> atomicUnit = new ArrayList<OWALRecord>();
boolean atomicChangeIsProcessed = false;
while (lsn != null) {
OWALRecord walRecord = log.read(lsn);
atomicUnit.add(walRecord);
if (!atomicChangeIsProcessed) {
Assert.assertTrue(walRecord instanceof OAtomicUnitStartRecord);
atomicChangeIsProcessed = true;
} else if (walRecord instanceof OAtomicUnitEndRecord) {
atomicChangeIsProcessed = false;
for (OWALRecord restoreRecord : atomicUnit) {
if (restoreRecord instanceof OAtomicUnitStartRecord || restoreRecord instanceof OAtomicUnitEndRecord
|| restoreRecord instanceof OFileCreatedCreatedWALRecord
|| restoreRecord instanceof ONonTxOperationPerformedWALRecord)
continue;
final OUpdatePageRecord updatePageRecord = (OUpdatePageRecord) restoreRecord;
final long fileId = updatePageRecord.getFileId();
final long pageIndex = updatePageRecord.getPageIndex();
if (!testDiskCache.isOpen(fileId))
testDiskCache.openFile(fileId);
OCacheEntry cacheEntry = testDiskCache.load(fileId, pageIndex, true);
if (cacheEntry == null) {
do {
if (cacheEntry != null)
diskCache.release(cacheEntry);
cacheEntry = testDiskCache.allocateNewPage(fileId);
} while (cacheEntry.getPageIndex() != pageIndex);
}
cacheEntry.acquireExclusiveLock();
try {
ODurablePage durablePage = new ODurablePage(cacheEntry, null);
durablePage.restoreChanges(updatePageRecord.getChanges());
durablePage.setLsn(updatePageRecord.getLsn());
cacheEntry.markDirty();
} finally {
cacheEntry.releaseExclusiveLock();
testDiskCache.release(cacheEntry);
}
}
atomicUnit.clear();
} else {
Assert.assertTrue(walRecord instanceof OUpdatePageRecord || walRecord instanceof OFileCreatedCreatedWALRecord
|| walRecord instanceof ONonTxOperationPerformedWALRecord);
}
lsn = log.next(lsn);
}
Assert.assertTrue(atomicUnit.isEmpty());
log.close();
}
private void assertClusterContentIsTheSame(String expectedCluster, String actualCluster) throws IOException {
File expectedDataFile = new File(testStorageDir, expectedCluster + ".pcl");
RandomAccessFile datFileOne = new RandomAccessFile(expectedDataFile, "r");
RandomAccessFile datFileTwo = new RandomAccessFile(new File(storageDir, actualCluster + ".pcl"), "r");
assertFileContentIsTheSame(datFileOne, datFileTwo);
datFileOne.close();
datFileTwo.close();
File expectedRIDMapFile = new File(testStorageDir, expectedCluster + ".cpm");
RandomAccessFile ridMapOne = new RandomAccessFile(expectedRIDMapFile, "r");
RandomAccessFile ridMapTwo = new RandomAccessFile(new File(storageDir, actualCluster + ".cpm"), "r");
assertFileContentIsTheSame(ridMapOne, ridMapTwo);
ridMapOne.close();
ridMapTwo.close();
}
private void assertFileContentIsTheSame(RandomAccessFile datFileOne, RandomAccessFile datFileTwo) throws IOException {
Assert.assertEquals(datFileOne.length(), datFileTwo.length());
byte[] expectedContent = new byte[OClusterPage.PAGE_SIZE];
byte[] actualContent = new byte[OClusterPage.PAGE_SIZE];
datFileOne.seek(OAbstractFile.HEADER_SIZE);
datFileTwo.seek(OAbstractFile.HEADER_SIZE);
int bytesRead = datFileOne.read(expectedContent);
while (bytesRead >= 0) {
datFileTwo.readFully(actualContent, 0, bytesRead);
Assert.assertEquals(expectedContent, actualContent);
expectedContent = new byte[OClusterPage.PAGE_SIZE];
actualContent = new byte[OClusterPage.PAGE_SIZE];
bytesRead = datFileOne.read(expectedContent);
}
}
}