Search in sources :

Example 1 with IndexRecord

use of com.cinchapi.concourse.server.storage.db.IndexRecord in project concourse by cinchapi.

the class SegmentTest method testConcurrency.

@Test
public void testConcurrency() throws InterruptedException {
    AtomicBoolean succeeded = new AtomicBoolean(true);
    AwaitableExecutorService executor = new AwaitableExecutorService(Executors.newCachedThreadPool());
    try {
        for (int i = 0; i < 1000; ++i) {
            AtomicBoolean done = new AtomicBoolean(false);
            long record = i;
            String key = Long.toString(record);
            TObject value = Convert.javaToThrift(Long.toString(record));
            Write write = Write.add(key, value, record);
            Identifier pk = Identifier.of(record);
            Text text = Text.wrap(key);
            Thread reader = new Thread(() -> {
                while (!done.get()) {
                    TableRecord tr = TableRecord.create(pk);
                    IndexRecord ir = IndexRecord.create(text);
                    CorpusRecord cr = CorpusRecord.create(text);
                    segment.table().seek(Composite.create(pk), tr);
                    segment.index().seek(Composite.create(text), ir);
                    segment.corpus().seek(Composite.create(text), cr);
                    if (!done.get() && tr.isEmpty() != ir.isEmpty()) {
                        if (!tr.isEmpty() && ir.isEmpty()) {
                            // Later read is empty
                            succeeded.set(false);
                            System.out.println(AnyStrings.format("table empty = {} and index empty = {} for {}", tr.isEmpty(), ir.isEmpty(), record));
                        }
                    }
                    if (!done.get() && ir.isEmpty() != cr.isEmpty()) {
                        if (!ir.isEmpty() && cr.isEmpty()) {
                            // Later read is empty
                            succeeded.set(false);
                            System.out.println(AnyStrings.format("index empty = {} and corpus empty = {} for {}", tr.isEmpty(), cr.isEmpty(), record));
                        }
                    }
                }
                TableRecord tr = TableRecord.create(pk);
                IndexRecord ir = IndexRecord.create(text);
                CorpusRecord cr = CorpusRecord.create(text);
                segment.table().seek(Composite.create(pk), tr);
                segment.index().seek(Composite.create(text), ir);
                segment.corpus().seek(Composite.create(text), cr);
                if (tr.isEmpty()) {
                    succeeded.set(false);
                    System.out.println("After write finished, table still empty for " + record);
                }
                if (ir.isEmpty()) {
                    succeeded.set(false);
                    System.out.println("After write finished, index still empty for " + record);
                }
                if (cr.isEmpty()) {
                    succeeded.set(false);
                    System.out.println("After write finished, corpus still empty for " + record);
                }
            });
            Thread writer = new Thread(() -> {
                try {
                    segment.acquire(write, executor);
                    done.set(true);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            reader.start();
            writer.start();
            writer.join();
            reader.join();
        }
        Assert.assertTrue(succeeded.get());
    } finally {
        executor.shutdown();
    }
}
Also used : Write(com.cinchapi.concourse.server.storage.temp.Write) TObject(com.cinchapi.concourse.thrift.TObject) Text(com.cinchapi.concourse.server.model.Text) IndexRecord(com.cinchapi.concourse.server.storage.db.IndexRecord) TableRecord(com.cinchapi.concourse.server.storage.db.TableRecord) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Identifier(com.cinchapi.concourse.server.model.Identifier) CorpusRecord(com.cinchapi.concourse.server.storage.db.CorpusRecord) AwaitableExecutorService(com.cinchapi.concourse.server.concurrent.AwaitableExecutorService) ConcourseBaseTest(com.cinchapi.concourse.test.ConcourseBaseTest) Test(org.junit.Test)

Aggregations

AwaitableExecutorService (com.cinchapi.concourse.server.concurrent.AwaitableExecutorService)1 Identifier (com.cinchapi.concourse.server.model.Identifier)1 Text (com.cinchapi.concourse.server.model.Text)1 CorpusRecord (com.cinchapi.concourse.server.storage.db.CorpusRecord)1 IndexRecord (com.cinchapi.concourse.server.storage.db.IndexRecord)1 TableRecord (com.cinchapi.concourse.server.storage.db.TableRecord)1 Write (com.cinchapi.concourse.server.storage.temp.Write)1 ConcourseBaseTest (com.cinchapi.concourse.test.ConcourseBaseTest)1 TObject (com.cinchapi.concourse.thrift.TObject)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1