Search in sources :

Example 1 with NodeDocumentCache

use of org.apache.jackrabbit.oak.plugins.document.cache.NodeDocumentCache in project jackrabbit-oak by apache.

the class CacheConsistencyIT method runTest.

private void runTest() throws Throwable {
    addNodes(null, "/test", "/test/foo");
    final List<Throwable> exceptions = Collections.synchronizedList(new ArrayList<Throwable>());
    Thread t1 = new Thread(new Runnable() {

        @Override
        public void run() {
            String id = Utils.getIdFromPath("/test/foo");
            List<String> ids = Lists.newArrayList();
            ids.add(id);
            long v = 0;
            while (exceptions.isEmpty()) {
                try {
                    UpdateOp op = new UpdateOp(ids.get(0), false);
                    op.set("p", ++v);
                    store.update(NODES, ids, op);
                    NodeDocument doc = store.find(NODES, id);
                    Object p = doc.get("p");
                    assertEquals(v, ((Long) p).longValue());
                } catch (Throwable e) {
                    exceptions.add(e);
                }
            }
        }
    }, "update");
    t1.start();
    Thread t2 = new Thread(new Runnable() {

        @Override
        public void run() {
            String id = Utils.getIdFromPath("/test/foo");
            long v = 0;
            while (exceptions.isEmpty()) {
                try {
                    UpdateOp op = new UpdateOp(id, false);
                    op.set("q", ++v);
                    NodeDocument old = store.findAndUpdate(NODES, op);
                    Object q = old.get("q");
                    if (q != null) {
                        assertEquals(v - 1, ((Long) q).longValue());
                    }
                } catch (Throwable e) {
                    exceptions.add(e);
                }
            }
        }
    }, "findAndUpdate");
    t2.start();
    Thread t3 = new Thread(new Runnable() {

        @Override
        public void run() {
            String id = Utils.getIdFromPath("/test/foo");
            long p = 0;
            long q = 0;
            while (exceptions.isEmpty()) {
                try {
                    NodeDocument doc = store.find(NODES, id);
                    if (doc != null) {
                        Object value = doc.get("p");
                        if (value != null) {
                            assertTrue((Long) value >= p);
                            p = (Long) value;
                        }
                        value = doc.get("q");
                        if (value != null) {
                            assertTrue("previous: " + q + ", now: " + value, (Long) value >= q);
                            q = (Long) value;
                        }
                    }
                } catch (Throwable e) {
                    exceptions.add(e);
                }
            }
        }
    }, "reader");
    t3.start();
    NodeDocumentCache cache = store.getNodeDocumentCache();
    // run for at most five seconds
    long end = System.currentTimeMillis() + 1000;
    String id = Utils.getIdFromPath("/test/foo");
    while (t1.isAlive() && t2.isAlive() && t3.isAlive() && System.currentTimeMillis() < end) {
        if (cache.getIfPresent(id) != null) {
            Thread.sleep(0, (int) (Math.random() * 100));
            // simulate eviction
            cache.invalidate(id);
        }
    }
    for (Throwable e : exceptions) {
        throw e;
    }
    exceptions.add(new Exception("end"));
    t1.join();
    t2.join();
    t3.join();
}
Also used : NodeDocumentCache(org.apache.jackrabbit.oak.plugins.document.cache.NodeDocumentCache) UpdateOp(org.apache.jackrabbit.oak.plugins.document.UpdateOp) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with NodeDocumentCache

use of org.apache.jackrabbit.oak.plugins.document.cache.NodeDocumentCache in project jackrabbit-oak by apache.

the class RDBCacheConsistencyIT method runTest.

private void runTest() throws Throwable {
    addNodes(null, "/test", "/test/foo");
    final List<Throwable> exceptions = Collections.synchronizedList(new ArrayList<Throwable>());
    Thread t1 = new Thread(new Runnable() {

        @Override
        public void run() {
            String id = Utils.getIdFromPath("/test/foo");
            List<String> ids = Lists.newArrayList();
            ids.add(id);
            long v = 0;
            while (exceptions.isEmpty()) {
                try {
                    UpdateOp op = new UpdateOp(ids.get(0), false);
                    op.set("p", ++v);
                    op.set("mb", "update");
                    store.update(NODES, ids, op);
                    NodeDocument doc = store.find(NODES, id);
                    Object p = doc.get("p");
                    assertEquals("Unexpected result after update-then-find sequence, last modification of document by '" + doc.get("mb") + "' thread @" + doc.getModCount(), v, ((Long) p).longValue());
                // System.out.println("u @" + doc.getModCount() + " p=" + v + "; q=" + doc.get("q"));
                } catch (Throwable e) {
                    exceptions.add(e);
                }
            }
        }
    }, "update");
    t1.start();
    Thread t2 = new Thread(new Runnable() {

        @Override
        public void run() {
            String id = Utils.getIdFromPath("/test/foo");
            long v = 0;
            long lastWrittenV = 0;
            while (exceptions.isEmpty()) {
                try {
                    UpdateOp op = new UpdateOp(id, false);
                    op.set("q", ++v);
                    op.set("mb", "findAndUpdate");
                    NodeDocument old = store.findAndUpdate(NODES, op);
                    Object q = old.get("q");
                    if (q != null) {
                        assertEquals("Unexpected result after findAndUpdate, last modification of document by '" + old.get("mb") + "' thread @" + old.getModCount(), lastWrittenV, ((Long) q).longValue());
                    }
                    lastWrittenV = v;
                // System.out.println("f @" + old.getModCount() + " p=" + old.get("p") + "; q=" + q);
                } catch (DocumentStoreException e) {
                // System.err.println("f update of v to " + v + " failed: " + e.getMessage());
                // keep going, RDBDocumentStore might have given up due
                // to race conditions
                } catch (Throwable e) {
                    exceptions.add(e);
                }
            }
        }
    }, "findAndUpdate");
    t2.start();
    Thread t3 = new Thread(new Runnable() {

        @Override
        public void run() {
            String id = Utils.getIdFromPath("/test/foo");
            long p = 0;
            long q = 0;
            long mc = 0;
            while (exceptions.isEmpty()) {
                try {
                    NodeDocument doc = store.find(NODES, id);
                    if (doc != null) {
                        Object value = doc.get("p");
                        if (value != null) {
                            assertTrue("reader thread at @" + doc.getModCount() + ": observed property value for 'p' (incremented by 'update' thread) decreased, last change by '" + doc.get("mb") + "' thread; previous: " + p + " (at @" + mc + "), now: " + value, (Long) value >= p);
                            p = (Long) value;
                        }
                        value = doc.get("q");
                        if (value != null) {
                            assertTrue("reader thread at @" + doc.getModCount() + ": observed property value for 'q' (incremented by 'findAndUpdate' thread) decreased, last change by '" + doc.get("mb") + "' thread; previous: " + q + " (at @" + mc + "), now: " + value, (Long) value >= q);
                            q = (Long) value;
                        }
                    }
                    mc = doc.getModCount();
                } catch (Throwable e) {
                    exceptions.add(e);
                }
            }
        }
    }, "reader");
    t3.start();
    NodeDocumentCache cache = store.getNodeDocumentCache();
    // run for at most five seconds
    long end = System.currentTimeMillis() + 1000;
    String id = Utils.getIdFromPath("/test/foo");
    while (t1.isAlive() && t2.isAlive() && t3.isAlive() && System.currentTimeMillis() < end) {
        if (cache.getIfPresent(id) != null) {
            Thread.sleep(0, (int) (Math.random() * 100));
            // simulate eviction
            cache.invalidate(id);
        }
    }
    for (Throwable e : exceptions) {
        throw e;
    }
    exceptions.add(new Exception("end"));
    t1.join();
    t2.join();
    t3.join();
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) NodeDocumentCache(org.apache.jackrabbit.oak.plugins.document.cache.NodeDocumentCache) UpdateOp(org.apache.jackrabbit.oak.plugins.document.UpdateOp) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)2 UpdateOp (org.apache.jackrabbit.oak.plugins.document.UpdateOp)2 NodeDocumentCache (org.apache.jackrabbit.oak.plugins.document.cache.NodeDocumentCache)2 DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)1