You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by mr...@apache.org on 2022/07/01 07:32:13 UTC

[jackrabbit-oak] 02/02: OAK-9780: PoC for DocumentNodeStore cache warming

This is an automated email from the ASF dual-hosted git repository.

mreutegg pushed a commit to branch OAK-9780
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git

commit a6ed7b1a2cd651812b0114fffe0aca9e1bf6aefa
Author: Marcel Reutegger <ma...@gmail.com>
AuthorDate: Thu Jun 30 16:50:31 2022 +0200

    OAK-9780: PoC for DocumentNodeStore cache warming
    
    Test prefetch with concurrent update
---
 .../document/ConcurrentPrefetchAndUpdateIT.java    | 150 +++++++++++++++++++++
 1 file changed, 150 insertions(+)

diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/ConcurrentPrefetchAndUpdateIT.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/ConcurrentPrefetchAndUpdateIT.java
new file mode 100755
index 0000000000..c4ed1046eb
--- /dev/null
+++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/ConcurrentPrefetchAndUpdateIT.java
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.plugins.document;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.atomic.AtomicLong;
+
+import com.google.common.collect.Lists;
+import com.mongodb.DBObject;
+import com.mongodb.MongoClient;
+import com.mongodb.client.MongoDatabase;
+
+import org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore;
+import org.apache.jackrabbit.oak.plugins.document.util.Utils;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.apache.jackrabbit.oak.plugins.document.Collection.NODES;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class ConcurrentPrefetchAndUpdateIT extends AbstractMongoConnectionTest {
+
+    protected static final int NUM_NODES = 50;
+
+    private static final Random RAND = new Random();
+
+    private TestStore store;
+
+    private final AtomicLong counter = new AtomicLong();
+
+    private final List<String> ids = new ArrayList<>();
+
+    @Before
+    @Override
+    public void setUpConnection() throws Exception {
+        mongoConnection = connectionFactory.getConnection();
+        assertNotNull(mongoConnection);
+        MongoDatabase db = mongoConnection.getDatabase();
+        MongoUtils.dropCollections(db);
+        DocumentMK.Builder builder = new DocumentMK.Builder()
+                .clock(getTestClock()).setAsyncDelay(0);
+        store = new TestStore(mongoConnection.getMongoClient(), db, builder);
+        mk = builder.setDocumentStore(store).open();
+    }
+
+    @Test
+    public void cacheConsistency() throws Exception {
+        Revision r = newRevision();
+        List<UpdateOp> ops = Lists.newArrayList();
+        for (int i = 0; i < NUM_NODES; i++) {
+            String id = Utils.getIdFromPath("/node-" + i);
+            ids.add(id);
+            UpdateOp op = new UpdateOp(id, true);
+            NodeDocument.setLastRev(op, r);
+            ops.add(op);
+        }
+        store.create(NODES, ops);
+
+        for (int i = 0; i < 100; i++) {
+            Thread q = new Thread(this::prefetchDocuments);
+            Thread u = new Thread(this::updateDocuments);
+            Thread iv = new Thread(this::invalidate);
+            q.start();
+            u.start();
+            iv.start();
+            q.join();
+            u.join();
+            iv.join();
+            for (String id : ids) {
+                NodeDocument doc = store.find(NODES, id);
+                assertNotNull(doc);
+                assertEquals("Unexpected revision timestamp for " + doc.getId(),
+                        counter.get(), doc.getLastRev().get(1).getTimestamp());
+            }
+        }
+    }
+
+    private Revision newRevision() {
+        return new Revision(counter.incrementAndGet(), 0, 1);
+    }
+
+    private void prefetchDocuments() {
+        randomWait();
+        store.prefetch(NODES, ids);
+    }
+
+    private void updateDocuments() {
+        randomWait();
+        UpdateOp op = new UpdateOp("foo", false);
+        NodeDocument.setLastRev(op, newRevision());
+        List<UpdateOp> ops = Lists.newArrayList();
+        for (String id : ids) {
+            ops.add(op.shallowCopy(id));
+        }
+        store.createOrUpdate(NODES, ops);
+    }
+
+    private void invalidate() {
+        randomWait();
+        List<String> invalidate = new ArrayList<>(ids);
+        Collections.shuffle(invalidate);
+        // evict random 80% of the docs from the cache
+        for (String id : invalidate.subList(0, (int) (NUM_NODES * 0.8))) {
+            store.invalidateCache(NODES, id);
+        }
+    }
+
+    private static void randomWait() {
+        try {
+            Thread.sleep(0, RAND.nextInt(100_000));
+        } catch (InterruptedException e) {
+            // ignore
+        }
+    }
+
+    private static final class TestStore extends MongoDocumentStore {
+
+        TestStore(MongoClient client, MongoDatabase db, DocumentMK.Builder builder) {
+            super(client, db, builder);
+        }
+
+        @Override
+        protected <T extends Document> T convertFromDBObject(
+                @NotNull Collection<T> collection, @Nullable DBObject n) {
+            randomWait();
+            return super.convertFromDBObject(collection, n);
+        }
+    }
+}
+