From ceb8a8c4d876006c12c595b926d4c0d7cdafaeb8 Mon Sep 17 00:00:00 2001 From: Alexandru Parvulescu Date: Mon, 18 May 2015 11:58:22 +0000 Subject: [PATCH] OAK-2870 Introduce a SegmentNodeStoreBuilder to help wire a SegmentNodeStore git-svn-id: https://svn.apache.org/repos/asf/jackrabbit/oak/trunk@1679998 13f79535-47bb-0310-9956-ffa450edef68 Conflicts: oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStore.java oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java --- .../oak/plugins/segment/SegmentNodeStore.java | 15 +++ .../plugins/segment/SegmentNodeStoreBuilder.java | 103 +++++++++++++++++++++ .../plugins/segment/SegmentNodeStoreService.java | 33 +++---- 3 files changed, 130 insertions(+), 21 deletions(-) create mode 100644 oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreBuilder.java diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStore.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStore.java index b26e17f..c4cb574 100644 --- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStore.java +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStore.java @@ -99,7 +99,22 @@ public class SegmentNodeStore implements NodeStore, Observable { private boolean commitFairLock = Boolean .getBoolean("oak.segmentNodeStore.commitFairLock"); + @Nonnull + public static SegmentNodeStoreBuilder newSegmentNodeStore( + @Nonnull SegmentStore store) { + return SegmentNodeStoreBuilder.newSegmentNodeStore(checkNotNull(store)); + } + + /** + * @deprecated Use {@link SegmentNodeStore#newSegmentNodeStore(SegmentStore)} instead + * + */ + @Deprecated public SegmentNodeStore(SegmentStore store) { + this(store, false); + } + + SegmentNodeStore(SegmentStore store, boolean internal) { if (commitFairLock) { log.info("initializing SegmentNodeStore with the commitFairLock option enabled."); } diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreBuilder.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreBuilder.java new file mode 100644 index 0000000..787daef --- /dev/null +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreBuilder.java @@ -0,0 +1,103 @@ +/* + * 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.segment; + +import static com.google.common.base.Preconditions.checkState; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.apache.jackrabbit.oak.plugins.segment.compaction.CompactionStrategy.NO_COMPACTION; + +import java.io.IOException; +import java.util.concurrent.Callable; + +import javax.annotation.Nonnull; + +import org.apache.jackrabbit.oak.plugins.segment.compaction.CompactionStrategy; +import org.apache.jackrabbit.oak.plugins.segment.compaction.CompactionStrategy.CleanupType; + +public class SegmentNodeStoreBuilder { + + private final SegmentStore store; + + private boolean isCreated; + + private boolean hasCompactionStrategy; + private boolean pauseCompaction; + private boolean cloneBinaries; + private String cleanup; + private long cleanupTs; + private byte memoryThreshold; + private int lockWaitTime; + private int retryCount; + private boolean forceAfterFail; + private CompactionStrategy compactionStrategy; + + static SegmentNodeStoreBuilder newSegmentNodeStore(SegmentStore store) { + return new SegmentNodeStoreBuilder(store); + } + + private SegmentNodeStoreBuilder(@Nonnull SegmentStore store) { + this.store = store; + } + + public SegmentNodeStoreBuilder withCompactionStrategy( + boolean pauseCompaction, boolean cloneBinaries, String cleanup, + long cleanupTs, byte memoryThreshold, final int lockWaitTime, + int retryCount, boolean forceAfterFail) { + this.hasCompactionStrategy = true; + this.pauseCompaction = pauseCompaction; + this.cloneBinaries = cloneBinaries; + this.cleanup = cleanup; + this.cleanupTs = cleanupTs; + this.memoryThreshold = memoryThreshold; + this.lockWaitTime = lockWaitTime; + this.retryCount = retryCount; + this.forceAfterFail = forceAfterFail; + return this; + } + + public CompactionStrategy getCompactionStrategy() { + checkState(isCreated); + return compactionStrategy; + } + + @Nonnull + public SegmentNodeStore create() throws IOException { + checkState(!isCreated); + isCreated = true; + final SegmentNodeStore segmentStore = new SegmentNodeStore(store, true); + if (hasCompactionStrategy) { + compactionStrategy = new CompactionStrategy(pauseCompaction, + cloneBinaries, CleanupType.valueOf(cleanup), cleanupTs, + memoryThreshold) { + + @Override + public boolean compacted(Callable setHead) + throws Exception { + // Need to guard against concurrent commits to avoid + // mixed segments. See OAK-2192. + return segmentStore.locked(setHead, lockWaitTime, SECONDS); + } + }; + compactionStrategy.setRetryCount(retryCount); + compactionStrategy.setForceAfterFail(forceAfterFail); + } else { + compactionStrategy = NO_COMPACTION; + } + return segmentStore; + } + +} diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java index ee20e19..bef14a3 100644 --- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStoreService.java @@ -18,7 +18,6 @@ package org.apache.jackrabbit.oak.plugins.segment; import static com.google.common.base.Preconditions.checkState; import static java.util.Collections.emptyMap; -import static java.util.concurrent.TimeUnit.SECONDS; import static org.apache.jackrabbit.oak.commons.PropertiesUtil.toBoolean; import static org.apache.jackrabbit.oak.commons.PropertiesUtil.toInteger; import static org.apache.jackrabbit.oak.commons.PropertiesUtil.toLong; @@ -38,7 +37,6 @@ import java.io.File; import java.io.IOException; import java.util.Dictionary; import java.util.Hashtable; -import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FilenameUtils; @@ -60,7 +58,6 @@ import org.apache.jackrabbit.oak.plugins.blob.BlobGCMBean; import org.apache.jackrabbit.oak.plugins.blob.BlobGarbageCollector; import org.apache.jackrabbit.oak.plugins.blob.MarkSweepGarbageCollector; import org.apache.jackrabbit.oak.plugins.segment.compaction.CompactionStrategy; -import org.apache.jackrabbit.oak.plugins.segment.compaction.CompactionStrategy.CleanupType; import org.apache.jackrabbit.oak.plugins.segment.compaction.CompactionStrategyMBean; import org.apache.jackrabbit.oak.plugins.segment.compaction.DefaultCompactionStrategyMBean; import org.apache.jackrabbit.oak.plugins.segment.file.FileStore; @@ -357,20 +354,6 @@ public class SegmentNodeStoreService extends ProxyNodeStore final long blobGcMaxAgeInSecs = toLong(lookup(context, PROP_BLOB_GC_MAX_AGE), DEFAULT_BLOB_GC_MAX_AGE); - CompactionStrategy compactionStrategy = new CompactionStrategy( - pauseCompaction, cloneBinaries, CleanupType.valueOf(cleanup), cleanupTs, - memoryThreshold) { - @Override - public boolean compacted(Callable setHead) throws Exception { - // Need to guard against concurrent commits to avoid - // mixed segments. See OAK-2192. - return delegate.locked(setHead, lockWaitTime, SECONDS); - } - }; - compactionStrategy.setRetryCount(retryCount); - compactionStrategy.setForceAfterFail(forceCommit); - compactionStrategy.setPersistCompactionMap(persistCompactionMap); - OsgiWhiteboard whiteboard = new OsgiWhiteboard(context.getBundleContext()); gcMonitor = new GCMonitorTracker(); gcMonitor.start(whiteboard); @@ -381,12 +364,20 @@ public class SegmentNodeStoreService extends ProxyNodeStore .withGCMonitor(gcMonitor); if (customBlobStore) { log.info("Initializing SegmentNodeStore with BlobStore [{}]", blobStore); - store = storeBuilder.withBlobStore(blobStore).create() - .setCompactionStrategy(compactionStrategy); + store = storeBuilder.withBlobStore(blobStore).create(); } else { - store = storeBuilder.create() - .setCompactionStrategy(compactionStrategy); + store = storeBuilder.create(); } + SegmentNodeStoreBuilder nodeStoreBuilder = SegmentNodeStore + .newSegmentNodeStore(store); + nodeStoreBuilder.withCompactionStrategy(pauseCompaction, cloneBinaries, + cleanup, cleanupTs, memoryThreshold, lockWaitTime, retryCount, + forceCommit); + delegate = nodeStoreBuilder.create(); + + CompactionStrategy compactionStrategy = nodeStoreBuilder + .getCompactionStrategy(); + store.setCompactionStrategy(compactionStrategy); delegate = new SegmentNodeStore(store); store.setCompactionStrategy(compactionStrategy); -- 1.8.4.3