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 2021/04/09 12:26:44 UTC
svn commit: r1888559 - in /jackrabbit/oak/trunk:
oak-it/src/test/java/org/apache/jackrabbit/oak/plugins/document/
oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/
oak-store-document/src/main/java/org/apache/jackrabbit/oak/pl...
Author: mreutegg
Date: Fri Apr 9 12:26:44 2021
New Revision: 1888559
URL: http://svn.apache.org/viewvc?rev=1888559&view=rev
Log:
OAK-9305: Create checkpoint with given revision
Commit changes from PR #274. Contribution by José Andrés Cordero Benítez
Added:
jackrabbit/oak/trunk/oak-it/src/test/java/org/apache/jackrabbit/oak/plugins/document/CompositeCheckpointTest.java (with props)
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBeanTest.java (with props)
Modified:
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/Checkpoints.java
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBuilder.java
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBean.java
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBeanImpl.java
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/CheckpointsTest.java
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/util/UtilsTest.java
Added: jackrabbit/oak/trunk/oak-it/src/test/java/org/apache/jackrabbit/oak/plugins/document/CompositeCheckpointTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-it/src/test/java/org/apache/jackrabbit/oak/plugins/document/CompositeCheckpointTest.java?rev=1888559&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-it/src/test/java/org/apache/jackrabbit/oak/plugins/document/CompositeCheckpointTest.java (added)
+++ jackrabbit/oak/trunk/oak-it/src/test/java/org/apache/jackrabbit/oak/plugins/document/CompositeCheckpointTest.java Fri Apr 9 12:26:44 2021
@@ -0,0 +1,65 @@
+/*
+ * 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.concurrent.TimeUnit;
+
+import org.apache.jackrabbit.oak.composite.CompositeNodeStore;
+import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore;
+import org.apache.jackrabbit.oak.spi.mount.MountInfoProvider;
+import org.apache.jackrabbit.oak.spi.mount.Mounts;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNotNull;
+
+public class CompositeCheckpointTest {
+
+ private DocumentNodeStore global;
+
+ private NodeStore composite;
+
+ @Before
+ public void init() {
+ MountInfoProvider mip = Mounts.newBuilder()
+ .readOnlyMount("mnt", "/mnt")
+ .build();
+
+ global = new DocumentNodeStoreBuilder<>().build();
+
+ composite = new CompositeNodeStore.Builder(mip, global)
+ .addMount("mnt", new MemoryNodeStore())
+ .build();
+ }
+
+ @After
+ public void dispose() {
+ global.dispose();
+ }
+
+ @Test
+ public void createCheckpointOnGlobal() {
+ Revision r = global.getHeadRevision().getRevision(global.getClusterId());
+ assertNotNull(r);
+ String result = global.getMBean().createCheckpoint(
+ r.toString(), TimeUnit.HOURS.toMillis(1), false);
+ String checkpoint = result.substring(result.indexOf("[") + 1, result.indexOf("]"));
+ assertNotNull(composite.retrieve(checkpoint));
+ }
+}
Propchange: jackrabbit/oak/trunk/oak-it/src/test/java/org/apache/jackrabbit/oak/plugins/document/CompositeCheckpointTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/Checkpoints.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/Checkpoints.java?rev=1888559&r1=1888558&r2=1888559&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/Checkpoints.java (original)
+++ jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/Checkpoints.java Fri Apr 9 12:26:44 2021
@@ -19,7 +19,6 @@
package org.apache.jackrabbit.oak.plugins.document;
-import java.math.BigInteger;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -67,8 +66,6 @@ class Checkpoints {
private final DocumentStore store;
- private final Logger log = LoggerFactory.getLogger(getClass());
-
private final AtomicInteger createCounter = new AtomicInteger();
private final Object cleanupLock = new Object();
@@ -89,17 +86,22 @@ class Checkpoints {
rv[0] = nodeStore.getHeadRevision();
}
});
- createCounter.getAndIncrement();
- performCleanupIfRequired();
- UpdateOp op = new UpdateOp(ID, false);
- long endTime = BigInteger.valueOf(nodeStore.getClock().getTime())
- .add(BigInteger.valueOf(lifetimeInMillis))
- .min(BigInteger.valueOf(Long.MAX_VALUE)).longValue();
- op.setMapEntry(PROP_CHECKPOINT, r, new Info(endTime, rv[0], info).toString());
- store.createOrUpdate(Collection.SETTINGS, op);
+ long endTime = Utils.sum(nodeStore.getClock().getTime(), lifetimeInMillis);
+ create(r, new Info(endTime, rv[0], info));
return r;
}
+ public Revision create(long lifetimeInMillis,
+ @NotNull Map<String, String> info,
+ @NotNull Revision revision) {
+ if (revision.getTimestamp() > nodeStore.getClock().getTime()) {
+ throw new IllegalArgumentException("Cannot create checkpoint with a revision in the future: " + revision);
+ }
+ long endTime = Utils.sum(nodeStore.getClock().getTime(), lifetimeInMillis);
+ create(revision, new Info(endTime, null, info));
+ return revision;
+ }
+
public void release(String checkpoint) {
UpdateOp op = new UpdateOp(ID, false);
op.removeMapEntry(PROP_CHECKPOINT, Revision.fromString(checkpoint));
@@ -121,7 +123,7 @@ class Checkpoints {
SortedMap<Revision, Info> checkpoints = getCheckpoints();
if(checkpoints.isEmpty()){
- log.debug("No checkpoint registered so far");
+ LOG.debug("No checkpoint registered so far");
return null;
}
@@ -145,7 +147,7 @@ class Checkpoints {
if (op.hasChanges()) {
store.findAndUpdate(Collection.SETTINGS, op);
- log.debug("Purged {} expired checkpoints", op.getChanges().size());
+ LOG.debug("Purged {} expired checkpoints", op.getChanges().size());
}
return lastAliveRevision;
@@ -239,6 +241,14 @@ class Checkpoints {
}
}
+ private void create(@NotNull Revision revision, @NotNull Info info) {
+ createCounter.getAndIncrement();
+ performCleanupIfRequired();
+ UpdateOp op = new UpdateOp(ID, false);
+ op.setMapEntry(PROP_CHECKPOINT, revision, info.toString());
+ store.createOrUpdate(Collection.SETTINGS, op);
+ }
+
private RevisionVector expand(Revision checkpoint) {
LOG.warn("Expanding {} single revision checkpoint into a " +
"RevisionVector. Please make sure all cluster nodes run " +
Modified: jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java?rev=1888559&r1=1888558&r2=1888559&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java (original)
+++ jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java Fri Apr 9 12:26:44 2021
@@ -65,7 +65,6 @@ import java.util.concurrent.locks.ReadWr
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.jcr.PropertyType;
-import javax.management.NotCompliantMBeanException;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
@@ -3486,13 +3485,10 @@ public final class DocumentNodeStore
}
private DocumentNodeStoreMBean createMBean(DocumentNodeStoreBuilder<?> builder) {
- try {
- return new DocumentNodeStoreMBeanImpl(this,
- builder.getStatisticsProvider().getStats(),
- clusterNodes.values());
- } catch (NotCompliantMBeanException e) {
- throw new IllegalStateException(e);
- }
+ return new DocumentNodeStoreMBeanImpl(this,
+ builder.getStatisticsProvider().getStats(),
+ clusterNodes.values(),
+ builder.getRevisionGCMaxAge());
}
private static abstract class NodeStoreTask implements Runnable {
Modified: jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBuilder.java?rev=1888559&r1=1888558&r2=1888559&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBuilder.java (original)
+++ jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBuilder.java Fri Apr 9 12:26:44 2021
@@ -21,6 +21,7 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Executor;
+import java.util.concurrent.TimeUnit;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
@@ -65,6 +66,7 @@ import static com.google.common.base.Pre
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Suppliers.ofInstance;
import static org.apache.jackrabbit.oak.plugins.document.DocumentNodeStoreService.DEFAULT_JOURNAL_GC_MAX_AGE_MILLIS;
+import static org.apache.jackrabbit.oak.plugins.document.DocumentNodeStoreService.DEFAULT_VER_GC_MAX_AGE;
/**
* A generic builder for a {@link DocumentNodeStore}. By default the builder
@@ -148,6 +150,7 @@ public class DocumentNodeStoreBuilder<T
private int commitValueCacheSize = 10000;
private boolean cacheEmptyCommitValue = false;
private long maxRevisionAgeMillis = DEFAULT_JOURNAL_GC_MAX_AGE_MILLIS;
+ private long maxRevisionGCAgeMillis = TimeUnit.SECONDS.toMillis(DEFAULT_VER_GC_MAX_AGE);
private GCMonitor gcMonitor = new LoggingGCMonitor(
LoggerFactory.getLogger(VersionGarbageCollector.class));
private Predicate<Path> nodeCachePredicate = Predicates.alwaysTrue();
@@ -611,6 +614,21 @@ public class DocumentNodeStoreBuilder<T
return maxRevisionAgeMillis;
}
+ public T setRevisionGCMaxAge(long maxRevisionGCAgeMillis) {
+ this.maxRevisionGCAgeMillis = maxRevisionGCAgeMillis;
+ return thisBuilder();
+ }
+
+ /**
+ * The maximum age for changes in milliseconds. Older changes are candidates
+ * for revision garbage collection.
+ *
+ * @return maximum age in milliseconds.
+ */
+ public long getRevisionGCMaxAge() {
+ return maxRevisionGCAgeMillis;
+ }
+
public T setGCMonitor(@NotNull GCMonitor gcMonitor) {
this.gcMonitor = checkNotNull(gcMonitor);
return thisBuilder();
Modified: jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBean.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBean.java?rev=1888559&r1=1888558&r2=1888559&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBean.java (original)
+++ jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBean.java Fri Apr 9 12:26:44 2021
@@ -86,4 +86,18 @@ public interface DocumentNodeStoreMBean
@Description("Possible values are: DIFF, NODE, NODECHILDREN")
@Name("name")
String name);
+
+ @Description("Creates a checkpoint with the given revision and lifetime.")
+ String createCheckpoint(
+ @Description("The revision of the checkpoint to create.")
+ @Name("revision")
+ String revision,
+ @Description("The lifetime of the checkpoint in milliseconds.")
+ @Name("lifetime")
+ long lifetime,
+ @Description("Force create the checkpoint even when the revision " +
+ "garbage collector may have cleaned up changes already " +
+ "that are more recent than the revision checkpoint.")
+ @Name("force")
+ boolean force);
}
Modified: jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBeanImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBeanImpl.java?rev=1888559&r1=1888558&r2=1888559&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBeanImpl.java (original)
+++ jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBeanImpl.java Fri Apr 9 12:26:44 2021
@@ -17,10 +17,11 @@
package org.apache.jackrabbit.oak.plugins.document;
import java.text.SimpleDateFormat;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.TimeZone;
-import javax.management.NotCompliantMBeanException;
import javax.management.openmbean.CompositeData;
import com.google.common.base.Function;
@@ -48,20 +49,23 @@ final class DocumentNodeStoreMBeanImpl e
private static final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS zzz";
private static final TimeZone TZ_UTC = TimeZone.getTimeZone("UTC");
+ private static final String COMPOSITE_INFO = "composite.checkpoint.";
private final DocumentNodeStore nodeStore;
private final RepositoryStatistics repoStats;
private final Iterable<ClusterNodeInfoDocument> clusterNodes;
+ private final long revisionGCMaxAgeMillis;
private final Logger log = LoggerFactory.getLogger(this.getClass());
DocumentNodeStoreMBeanImpl(DocumentNodeStore nodeStore,
RepositoryStatistics repoStats,
- Iterable<ClusterNodeInfoDocument> clusterNodes)
- throws NotCompliantMBeanException {
+ Iterable<ClusterNodeInfoDocument> clusterNodes,
+ long revisionGCMaxAgeMillis) {
super(DocumentNodeStoreMBean.class);
this.nodeStore = nodeStore;
this.repoStats = repoStats;
this.clusterNodes = clusterNodes;
+ this.revisionGCMaxAgeMillis = revisionGCMaxAgeMillis;
}
@Override
@@ -264,4 +268,34 @@ final class DocumentNodeStoreMBeanImpl e
return "ERROR: Invalid cache name received.";
}
}
+
+ @Override
+ public String createCheckpoint(String revision, long lifetime, boolean force) {
+ Revision rev = Revision.fromString(revision);
+ long oldestTimestamp = nodeStore.getClock().getTime() - revisionGCMaxAgeMillis;
+ Revision oldestCheckpoint = nodeStore.getCheckpoints().getOldestRevisionToKeep();
+ if (oldestCheckpoint != null) {
+ oldestTimestamp = Math.min(oldestTimestamp, oldestCheckpoint.getTimestamp());
+ }
+ if (force || oldestTimestamp < rev.getTimestamp()) {
+ Map<String, String> info = new HashMap<>();
+ // Below properties are only needed when the DocumentNodeStore is
+ // used in a composite NodeStore setup. This is a bit ugly because
+ // it introduces a dependency on an implementation detail of the
+ // CompositeNodeStore implementation. The module oak-it therefore
+ // has a CompositeCheckpointTest to prevent a regression should the
+ // implementation ever change.
+ info.put(COMPOSITE_INFO + "created", Long.toString(rev.getTimestamp()));
+ info.put(COMPOSITE_INFO + "expires", Long.toString(Utils.sum(rev.getTimestamp() + lifetime)));
+
+ String cp = nodeStore.getCheckpoints().create(lifetime, info, rev).toString();
+ log.info("Created checkpoint [{}] with lifetime {} for Revision {}", cp, lifetime, revision);
+ return String.format("Created checkpoint [%s] with lifetime %d for Revision %s", cp, lifetime, revision);
+ } else {
+ throw new IllegalArgumentException(String.format("Cannot create a checkpoint for revision %s. " +
+ "Revision timestamp is %d and oldest timestamp to keep is %d",
+ revision, rev.getTimestamp(), oldestTimestamp));
+ }
+ }
+
}
Modified: jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java?rev=1888559&r1=1888558&r2=1888559&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java (original)
+++ jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreService.java Fri Apr 9 12:26:44 2021
@@ -325,6 +325,7 @@ public class DocumentNodeStoreService {
}
mkBuilder.setGCMonitor(new DelegatingGCMonitor(
newArrayList(gcMonitor, loggingGCMonitor)));
+ mkBuilder.setRevisionGCMaxAge(TimeUnit.SECONDS.toMillis(config.versionGcMaxAgeInSecs()));
nodeStore = mkBuilder.build();
Modified: jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java?rev=1888559&r1=1888558&r2=1888559&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java (original)
+++ jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java Fri Apr 9 12:26:44 2021
@@ -18,6 +18,7 @@ package org.apache.jackrabbit.oak.plugin
import java.io.Closeable;
import java.io.IOException;
+import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@@ -1062,4 +1063,23 @@ public class Utils {
throw new DocumentStoreException(msg);
}
}
+
+ /**
+ * Calculates the sum of the given long values. The implementation protects
+ * against overflow by returning {@code Long#MAX_VALUE} when the result
+ * would actually be bigger than that. Similarly, {@code Long#MIN_VALUE} is
+ * returned when the result would actually be smaller than that.
+ *
+ * @param addends the values.
+ * @return the sum of the values.
+ */
+ public static long sum(long... addends) {
+ BigInteger result = BigInteger.ZERO;
+ for (long value : addends) {
+ result = result.add(BigInteger.valueOf(value));
+ }
+ result = result.max(BigInteger.valueOf(Long.MIN_VALUE));
+ result = result.min(BigInteger.valueOf(Long.MAX_VALUE));
+ return result.longValue();
+ }
}
Modified: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/CheckpointsTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/CheckpointsTest.java?rev=1888559&r1=1888558&r2=1888559&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/CheckpointsTest.java (original)
+++ jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/CheckpointsTest.java Fri Apr 9 12:26:44 2021
@@ -28,22 +28,26 @@ import org.apache.jackrabbit.oak.spi.com
import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
import org.apache.jackrabbit.oak.spi.state.NodeState;
import org.apache.jackrabbit.oak.stats.Clock;
+import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import com.google.common.collect.ImmutableMap;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class CheckpointsTest {
+ private static final long ONE_HOUR = TimeUnit.HOURS.toMillis(1);
+
@Rule
public DocumentMKBuilderProvider builderProvider = new DocumentMKBuilderProvider();
@@ -51,10 +55,14 @@ public class CheckpointsTest {
private DocumentNodeStore store;
+ private Checkpoints checkpoints;
+
@Before
public void setUp() throws InterruptedException {
clock = new Clock.Virtual();
+ clock.waitUntil(System.currentTimeMillis());
store = builderProvider.newBuilder().clock(clock).getNodeStore();
+ checkpoints = new Checkpoints(store);
}
@Test
@@ -418,4 +426,32 @@ public class CheckpointsTest {
assertTrue(root.hasChildNode("foo"));
assertFalse(root.hasChildNode("bar"));
}
+
+ @Test
+ public void createCheckpointWithRevision() {
+ Revision r = new Revision(clock.getTime(), 0, store.getClusterId());
+ assertNotNull(r);
+ assertEquals(r, checkpoints.create(ONE_HOUR, Collections.emptyMap(), r));
+ RevisionVector rv = checkpoints.retrieve(r.toString());
+ assertNotNull(rv);
+ }
+
+ @Test
+ public void createCheckpointWithRevisionTwice() {
+ Revision r = new Revision(clock.getTime(), 0, store.getClusterId());
+ assertNotNull(r);
+ assertEquals(r, checkpoints.create(ONE_HOUR, Collections.emptyMap(), r));
+ assertEquals(r, checkpoints.create(ONE_HOUR * 3, Collections.emptyMap(), r));
+ Checkpoints.Info info = checkpoints.getCheckpoints().get(r);
+ assertNotNull(info);
+ assertThat(info.getExpiryTime(), greaterThan(store.getClock().getTime() + ONE_HOUR * 2));
+ }
+
+ @Test
+ public void createCheckpointWithRevisionInFuture() {
+ long time = store.getClock().getTime() + ONE_HOUR;
+ Revision r = new Revision(time, 0, store.getClusterId());
+ Map<String, String> info = Collections.emptyMap();
+ Assert.assertThrows(IllegalArgumentException.class, () -> checkpoints.create(ONE_HOUR, info, r));
+ }
}
Added: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBeanTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBeanTest.java?rev=1888559&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBeanTest.java (added)
+++ jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBeanTest.java Fri Apr 9 12:26:44 2021
@@ -0,0 +1,114 @@
+/*
+ * 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.concurrent.TimeUnit;
+
+import org.apache.jackrabbit.oak.stats.Clock;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.apache.jackrabbit.oak.plugins.document.DocumentNodeStoreService.DEFAULT_VER_GC_MAX_AGE;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
+
+public class DocumentNodeStoreMBeanTest {
+
+ private static final long ONE_HOUR = TimeUnit.HOURS.toMillis(1);
+
+ private static final long REVISION_GC_MAX_AGE_MILLIS = TimeUnit.SECONDS.toMillis(DEFAULT_VER_GC_MAX_AGE);
+
+ private static final long SLIGHTLY_OLDER_THAN_REVISION_GC_MAX_AGE_MILLIS = TimeUnit.SECONDS.toMillis(DEFAULT_VER_GC_MAX_AGE) + 5000;
+
+ @Rule
+ public DocumentMKBuilderProvider builderProvider = new DocumentMKBuilderProvider();
+
+ private Clock clock;
+
+ private DocumentNodeStore ns;
+
+ private DocumentNodeStoreMBean bean;
+
+ @Before
+ public void init() throws Exception {
+ clock = new Clock.Virtual();
+ clock.waitUntil(System.currentTimeMillis());
+ Revision.setClock(clock);
+ ClusterNodeInfo.setClock(clock);
+ ns = builderProvider.newBuilder()
+ .setRevisionGCMaxAge(REVISION_GC_MAX_AGE_MILLIS)
+ .setLeaseCheckMode(LeaseCheckMode.DISABLED)
+ .clock(clock)
+ .getNodeStore();
+ bean = ns.getMBean();
+ clock.waitUntil(clock.getTime() + REVISION_GC_MAX_AGE_MILLIS * 2);
+ ns.runBackgroundOperations();
+ }
+
+ @AfterClass
+ public static void after() {
+ Revision.resetClockToDefault();
+ ClusterNodeInfo.resetClockToDefault();
+ }
+
+ @Test
+ public void createCheckpointWithInvalidRevisionFormat() {
+ assertThrows(IllegalArgumentException.class, () ->
+ bean.createCheckpoint("invalid", ONE_HOUR, false));
+ }
+
+ @Test
+ public void createCheckpointWithFutureRevision() {
+ Revision r = ns.getHeadRevision().getRevision(ns.getClusterId());
+ assertNotNull(r);
+ String revisionString = new Revision(r.getTimestamp() + TimeUnit.HOURS.toMillis(1), 0, r.getClusterId()).toString();
+ assertThrows(IllegalArgumentException.class, () ->
+ bean.createCheckpoint(revisionString, ONE_HOUR, false));
+ }
+
+ @Test
+ public void createCheckpointWithRevisionTooOld() {
+ Revision r = ns.getHeadRevision().getRevision(ns.getClusterId());
+ assertNotNull(r);
+ String revisionString = new Revision(r.getTimestamp() - SLIGHTLY_OLDER_THAN_REVISION_GC_MAX_AGE_MILLIS, 0, r.getClusterId()).toString();
+ assertThrows(IllegalArgumentException.class, () ->
+ bean.createCheckpoint(revisionString, ONE_HOUR, false));
+ }
+
+ @Test
+ public void createCheckpointForceWithRevisionTooOld() {
+ Revision r = ns.getHeadRevision().getRevision(ns.getClusterId());
+ assertNotNull(r);
+ String revisionString = new Revision(r.getTimestamp() - SLIGHTLY_OLDER_THAN_REVISION_GC_MAX_AGE_MILLIS, 0, r.getClusterId()).toString();
+ String result = bean.createCheckpoint(revisionString, ONE_HOUR, true);
+ String checkpoint = result.substring(result.indexOf("[") + 1, result.indexOf("]"));
+ assertNotNull(ns.retrieve(checkpoint));
+ }
+
+ @Test
+ public void createCheckpointBeforeExistingCheckpoint() {
+ ns.checkpoint(ONE_HOUR);
+ Revision r = ns.getHeadRevision().getRevision(ns.getClusterId());
+ assertNotNull(r);
+ String revisionString = new Revision(r.getTimestamp() - ONE_HOUR, 0, r.getClusterId()).toString();
+ String result = bean.createCheckpoint(revisionString, ONE_HOUR, false);
+ String checkpoint = result.substring(result.indexOf("[") + 1, result.indexOf("]"));
+ assertNotNull(ns.retrieve(checkpoint));
+ }
+}
Propchange: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreMBeanTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java?rev=1888559&r1=1888558&r2=1888559&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java (original)
+++ jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java Fri Apr 9 12:26:44 2021
@@ -136,6 +136,8 @@ public class DocumentNodeStoreTest {
private static final Logger LOG = LoggerFactory.getLogger(DocumentNodeStoreTest.class);
+ private static final long ONE_MINUTE = TimeUnit.MINUTES.toMillis(1);
+
@Rule
public DocumentMKBuilderProvider builderProvider = new DocumentMKBuilderProvider();
@@ -4122,6 +4124,52 @@ public class DocumentNodeStoreTest {
assertEquals(0, store.getNumQueryCalls(NODES));
}
+ @Test
+ public void createCheckpointWithRevision() throws Exception {
+ DocumentNodeStore ns = builderProvider.newBuilder().getNodeStore();
+ RevisionVector head = ns.getHeadRevision();
+ NodeBuilder builder = ns.getRoot().builder();
+ builder.child("foo");
+ merge(ns, builder);
+ Revision r = head.getRevision(ns.getClusterId());
+ assertNotNull(r);
+ String ref = ns.getCheckpoints().create(ONE_MINUTE, Collections.emptyMap(), r).toString();
+ NodeState root = ns.retrieve(ref);
+ assertNotNull(root);
+ assertFalse(root.hasChildNode("foo"));
+ }
+
+ @Test
+ public void expandCheckpointWithRevision() throws Exception {
+ DocumentStore store = new MemoryDocumentStore();
+ DocumentNodeStore ns1 = builderProvider.newBuilder()
+ .setDocumentStore(store).setAsyncDelay(0).setClusterId(1)
+ .getNodeStore();
+ NodeBuilder builder = ns1.getRoot().builder();
+ builder.child("foo");
+ merge(ns1, builder);
+ ns1.runBackgroundOperations();
+ RevisionVector head = ns1.getHeadRevision();
+
+ DocumentNodeStore ns2 = builderProvider.newBuilder()
+ .setDocumentStore(store).setAsyncDelay(0).setClusterId(2)
+ .getNodeStore();
+ builder = ns2.getRoot().builder();
+ builder.child("bar");
+ merge(ns2, builder);
+ ns2.runBackgroundOperations();
+ ns1.runBackgroundOperations();
+ assertTrue(ns1.getRoot().hasChildNode("bar"));
+
+ Revision r = head.getRevision(ns1.getClusterId());
+ assertNotNull(r);
+ String ref = ns1.getCheckpoints().create(ONE_MINUTE, Collections.emptyMap(), r).toString();
+ NodeState root = ns1.retrieve(ref);
+ assertNotNull(root);
+ assertTrue(root.hasChildNode("foo"));
+ assertFalse(root.hasChildNode("bar"));
+ }
+
private void getChildNodeCountTest(int numChildren,
Iterable<Long> maxValues,
Iterable<Long> expectedValues)
Modified: jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/util/UtilsTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/util/UtilsTest.java?rev=1888559&r1=1888558&r2=1888559&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/util/UtilsTest.java (original)
+++ jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/util/UtilsTest.java Fri Apr 9 12:26:44 2021
@@ -443,6 +443,22 @@ public class UtilsTest {
assertEquals(startTime2, r2.getTimestamp());
}
+ @Test
+ public void sum() {
+ assertEquals(0, Utils.sum());
+ assertEquals(42, Utils.sum(7, 15, 20));
+ assertEquals(-12, Utils.sum(-7, 15, -20));
+ assertEquals(42, Utils.sum(Long.MAX_VALUE, 43, Long.MIN_VALUE));
+
+ assertEquals(Long.MAX_VALUE, Utils.sum(Long.MAX_VALUE));
+ assertEquals(Long.MAX_VALUE - 1, Utils.sum(Long.MAX_VALUE, -1));
+ assertEquals(Long.MAX_VALUE, Utils.sum(Long.MAX_VALUE, 1));
+
+ assertEquals(Long.MIN_VALUE, Utils.sum(Long.MIN_VALUE));
+ assertEquals(Long.MIN_VALUE + 1, Utils.sum(Long.MIN_VALUE, 1));
+ assertEquals(Long.MIN_VALUE, Utils.sum(Long.MIN_VALUE, -1));
+ }
+
private static ClusterNodeInfoDocument mockedClusterNodeInfo(int clusterId,
long startTime) {
ClusterNodeInfoDocument info = Mockito.mock(ClusterNodeInfoDocument.class);