use of com.cinchapi.concourse.server.storage.db.kernel.Segment in project concourse by cinchapi.
the class DatabaseTest method testDatabaseRemovesDuplicateSegmentsOnStartup.
@Test
public void testDatabaseRemovesDuplicateSegmentsOnStartup() {
Database db = (Database) store;
int expected = TestData.getScaleCount();
for (int i = 0; i < expected; ++i) {
db.accept(Write.add(TestData.getString(), TestData.getTObject(), Time.now()));
db.sync();
}
List<Segment> segments = Reflection.get("segments", db);
// size includes
Assert.assertEquals(expected + 1, segments.size());
// seg0
int a = TestData.getScaleCount() % (segments.size() - 2);
int b = a + 1;
Segment merged = Segment.create();
Segment seg1 = segments.get(a);
Segment seg2 = segments.get(b);
seg1.writes().forEach(write -> merged.acquire(write));
seg2.writes().forEach(write -> merged.acquire(write));
merged.transfer(Paths.get(current).resolve("segments").resolve(UUID.randomUUID() + ".seg"));
// Because #seg1 and #seg2 were merged
expected -= 1;
db.stop();
AtomicInteger duplicates = new AtomicInteger(0);
FileSystem.ls(Paths.get(current).resolve("segments")).forEach(file -> {
if (Random.getInt() % 3 == 0 && !file.toString().endsWith(merged.id())) {
FileSystem.copyBytes(file.toString(), file.getParent().resolve(UUID.randomUUID() + ".seg").toString());
duplicates.incrementAndGet();
}
});
// simulate server restart
db = new Database(db.getBackingStore());
db.start();
segments = Reflection.get("segments", db);
// size includes
Assert.assertEquals(expected + 1, segments.size());
// seg0
Assert.assertTrue(segments.stream().map(Segment::id).collect(Collectors.toList()).contains(merged.id()));
for (Segment segment : segments) {
int count = 0;
for (Segment seg : segments) {
if (seg != segment && seg.intersects(segment)) {
++count;
}
}
if (count > 0) {
Assert.fail(segment + " has " + count + " duplicates");
}
}
System.out.println("The database discarded " + duplicates.get() + 1 + " overlapping segments");
}
Aggregations