lweitzendorf commented on code in PR #692:
URL: https://github.com/apache/jackrabbit-oak/pull/692#discussion_r995415860


##########
oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/ParallelCompactor.java:
##########
@@ -0,0 +1,373 @@
+/*
+ * 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.segment;
+
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.plugins.index.counter.ApproximateCounter;
+import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeBuilder;
+import org.apache.jackrabbit.oak.segment.file.GCNodeWriteMonitor;
+import org.apache.jackrabbit.oak.segment.file.cancel.Canceller;
+import org.apache.jackrabbit.oak.spi.blob.BlobStore;
+import org.apache.jackrabbit.oak.spi.gc.GCMonitor;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.jackrabbit.oak.spi.state.NodeStateDiff;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.*;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
+import static 
org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.EMPTY_NODE;
+import static 
org.apache.jackrabbit.oak.segment.ClassicCompactor.getStableIdBytes;
+
+/**
+ * This compactor implementation leverages the tree structure of the 
repository for concurrent compaction.
+ * It explores the tree breadth-first until the target node count is reached. 
Every node at this depth will be
+ * an entry point for asynchronous compaction. After the exploration phase, 
the main thread will collect
+ * these compaction results and write their parents' node state to disk.
+ */
+public class ParallelCompactor extends CheckpointCompactor {
+    /**
+     * Expand repository tree until there are this many nodes for each worker 
to compact. Tradeoff
+     * between low efficiency of many small tasks and high risk of at least 
one of the subtrees being
+     * significantly larger than totalSize / numWorkers (unequal work 
distribution).
+     */
+    private static final int MIN_NODES_PER_WORKER = 1000;
+
+    /**
+     * Stop expansion if tree size grows beyond this many nodes per worker at 
the latest.
+     */
+    private static final int MAX_NODES_PER_WORKER = 10_000;
+
+    private final int numWorkers;
+
+    private final long totalSizeEstimate;
+
+    /**
+     * Manages workers for asynchronous compaction.
+     */
+    @Nullable
+    private ExecutorService executorService;
+
+    /**
+     * Create a new instance based on the passed arguments.
+     * @param gcListener listener receiving notifications about the garbage 
collection process
+     * @param reader     segment reader used to read from the segments
+     * @param writer     segment writer used to serialise to segments
+     * @param blobStore  the blob store or {@code null} if none
+     * @param compactionMonitor   notification call back for each compacted 
nodes, properties, and binaries
+     * @param nThreads   number of threads to use for parallel compaction,
+     *                   negative numbers are interpreted relative to the 
number of available processors
+     */
+    public ParallelCompactor(
+            @NotNull GCMonitor gcListener,
+            @NotNull SegmentReader reader,
+            @NotNull SegmentWriter writer,
+            @Nullable BlobStore blobStore,
+            @NotNull GCNodeWriteMonitor compactionMonitor,
+            int nThreads) {
+        super(gcListener, reader, writer, blobStore, compactionMonitor);
+
+        int availableProcessors = Runtime.getRuntime().availableProcessors();
+        if (nThreads < 0) {
+            nThreads += availableProcessors + 1;
+        }
+        numWorkers = Math.max(0, nThreads - 1);

Review Comment:
   This is actually not a mistake but intended behavior. Negative numbers are 
interpreted relative to the number of available processors. So specifying -1 
means using all available cores, -2 all but one and then for your example -4 
means all but 3 of the available processors (8-3 = 5). I've seen these 
semantics used in libraries such as 
[sklearn](https://scikit-learn.org/stable/glossary.html#term-n_jobs). Granted 
this is Python, but I think the semantics still make sense.
   
   Yes you are correct, it degrades to the behavior of `CheckpointCompactor` 
(see first comment).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to