Search in sources :

Example 16 with Segment

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");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Segment(com.cinchapi.concourse.server.storage.db.kernel.Segment) StoreTest(com.cinchapi.concourse.server.storage.StoreTest) Test(org.junit.Test)

Aggregations

Segment (com.cinchapi.concourse.server.storage.db.kernel.Segment)16 Path (java.nio.file.Path)5 Composite (com.cinchapi.concourse.server.io.Composite)3 Identifier (com.cinchapi.concourse.server.model.Identifier)3 SegmentLoadingException (com.cinchapi.concourse.server.storage.db.kernel.SegmentLoadingException)3 List (java.util.List)3 Test (org.junit.Test)3 AwaitableExecutorService (com.cinchapi.concourse.server.concurrent.AwaitableExecutorService)2 Text (com.cinchapi.concourse.server.model.Text)2 Value (com.cinchapi.concourse.server.model.Value)2 StoreTest (com.cinchapi.concourse.server.storage.StoreTest)2 WriteStreamProfiler (com.cinchapi.concourse.server.storage.WriteStreamProfiler)2 SegmentStorageSystem (com.cinchapi.concourse.server.storage.db.SegmentStorageSystem)2 SimilarityCompactor (com.cinchapi.concourse.server.storage.db.compaction.similarity.SimilarityCompactor)2 Write (com.cinchapi.concourse.server.storage.temp.Write)2 ImmutableList (com.google.common.collect.ImmutableList)2 IOException (java.io.IOException)2 Paths (java.nio.file.Paths)2 Collectors (java.util.stream.Collectors)2 AnyStrings (com.cinchapi.common.base.AnyStrings)1