Search in sources :

Example 1 with TimingDocumentStoreWrapper

use of org.apache.jackrabbit.oak.plugins.document.util.TimingDocumentStoreWrapper in project jackrabbit-oak by apache.

the class DocumentNodeStoreIT method modifiedResetWithDiff.

@Test
public void modifiedResetWithDiff() throws Exception {
    Clock clock = new Clock.Virtual();
    clock.waitUntil(System.currentTimeMillis());
    Revision.setClock(clock);
    DocumentStore docStore = new TimingDocumentStoreWrapper(ds) {

        @Override
        public void dispose() {
        // do not dispose yet
        }
    };
    DocumentNodeStore ns1 = new DocumentMK.Builder().setDocumentStore(docStore).setClusterId(1).setAsyncDelay(0).clock(clock).setDiffCache(AmnesiaDiffCache.INSTANCE).getNodeStore();
    NodeBuilder builder1 = ns1.getRoot().builder();
    builder1.child("node");
    removeMe.add(getIdFromPath("/node"));
    for (int i = 0; i < DocumentMK.MANY_CHILDREN_THRESHOLD; i++) {
        builder1.child("node-" + i);
        removeMe.add(getIdFromPath("/node/node-" + i));
    }
    ns1.merge(builder1, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    // make sure commit is visible to other node store instance
    ns1.runBackgroundOperations();
    DocumentNodeStore ns2 = new DocumentMK.Builder().setDocumentStore(docStore).setClusterId(2).setAsyncDelay(0).clock(clock).getNodeStore();
    NodeBuilder builder2 = ns2.getRoot().builder();
    builder2.child("node").child("child-a");
    removeMe.add(getIdFromPath("/node/child-a"));
    ns2.merge(builder2, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    // wait at least _modified resolution. in reality the wait may
    // not be necessary. e.g. when the clock passes the resolution boundary
    // exactly at this time
    clock.waitUntil(System.currentTimeMillis() + SECONDS.toMillis(MODIFIED_IN_SECS_RESOLUTION + 1));
    builder1 = ns1.getRoot().builder();
    builder1.child("node").child("child-b");
    removeMe.add(getIdFromPath("/node/child-b"));
    ns1.merge(builder1, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    // remember root for diff
    DocumentNodeState root1 = ns1.getRoot();
    builder1 = root1.builder();
    builder1.child("node").child("child-c");
    removeMe.add(getIdFromPath("/node/child-c"));
    ns1.merge(builder1, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    // remember root for diff
    DocumentNodeState root2 = ns1.getRoot();
    ns1.runBackgroundOperations();
    ns2.runBackgroundOperations();
    JsopDiff diff = new JsopDiff("", 0);
    ns1.compare(root2, root1, diff);
    // must report /node as changed
    assertEquals("^\"node\":{}", diff.toString());
    ns1.dispose();
    ns2.dispose();
}
Also used : TimingDocumentStoreWrapper(org.apache.jackrabbit.oak.plugins.document.util.TimingDocumentStoreWrapper) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) JsopDiff(org.apache.jackrabbit.oak.json.JsopDiff) Clock(org.apache.jackrabbit.oak.stats.Clock) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Test(org.junit.Test)

Example 2 with TimingDocumentStoreWrapper

use of org.apache.jackrabbit.oak.plugins.document.util.TimingDocumentStoreWrapper in project jackrabbit-oak by apache.

the class DocumentNodeStoreTest method backgroundRead.

// OAK-1254
@Test
public void backgroundRead() throws Exception {
    final Semaphore semaphore = new Semaphore(1);
    DocumentStore docStore = new MemoryDocumentStore();
    DocumentStore testStore = new TimingDocumentStoreWrapper(docStore) {

        @Override
        public CacheInvalidationStats invalidateCache(Iterable<String> keys) {
            super.invalidateCache(keys);
            semaphore.acquireUninterruptibly();
            semaphore.release();
            return null;
        }
    };
    final DocumentNodeStore store1 = builderProvider.newBuilder().setAsyncDelay(0).setDocumentStore(testStore).setClusterId(1).getNodeStore();
    DocumentNodeStore store2 = builderProvider.newBuilder().setAsyncDelay(0).setDocumentStore(docStore).setClusterId(2).getNodeStore();
    NodeBuilder builder = store2.getRoot().builder();
    builder.child("node2");
    store2.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    // force update of _lastRevs
    store2.runBackgroundOperations();
    // at this point only node2 must not be visible
    assertFalse(store1.getRoot().hasChildNode("node2"));
    builder = store1.getRoot().builder();
    builder.child("node1");
    NodeState root = store1.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    semaphore.acquireUninterruptibly();
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            store1.runBackgroundOperations();
        }
    });
    t.start();
    // and is waiting for semaphore
    while (!semaphore.hasQueuedThreads()) {
        Thread.sleep(10);
    }
    // must still not be visible at this state
    try {
        assertFalse(root.hasChildNode("node2"));
    } finally {
        semaphore.release();
    }
    t.join();
    // background operations completed
    root = store1.getRoot();
    // now node2 is visible
    assertTrue(root.hasChildNode("node2"));
}
Also used : TimingDocumentStoreWrapper(org.apache.jackrabbit.oak.plugins.document.util.TimingDocumentStoreWrapper) MemoryDocumentStore(org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) MemoryDocumentStore(org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore) Semaphore(java.util.concurrent.Semaphore) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Test(org.junit.Test)

Aggregations

TimingDocumentStoreWrapper (org.apache.jackrabbit.oak.plugins.document.util.TimingDocumentStoreWrapper)2 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)2 Test (org.junit.Test)2 Semaphore (java.util.concurrent.Semaphore)1 JsopDiff (org.apache.jackrabbit.oak.json.JsopDiff)1 MemoryDocumentStore (org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore)1 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)1 Clock (org.apache.jackrabbit.oak.stats.Clock)1