Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OAK-10313: Counter for DocumentStore check #996

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public class DocumentStoreCheck {

private final boolean summary;

private final boolean counter;

private final int numThreads;

private final ExecutorService executorService;
Expand All @@ -93,6 +95,7 @@ private DocumentStoreCheck(DocumentNodeStore ns,
boolean progress,
boolean silent,
boolean summary,
boolean counter,
int numThreads,
String output,
boolean orphan,
Expand All @@ -107,6 +110,7 @@ private DocumentStoreCheck(DocumentNodeStore ns,
this.progress = progress;
this.silent = silent;
this.summary = summary;
this.counter = counter;
this.numThreads = numThreads;
this.executorService = new ThreadPoolExecutor(
numThreads, numThreads, 1, TimeUnit.MINUTES,
Expand Down Expand Up @@ -176,6 +180,9 @@ private void scheduleResultWriter(BlockingQueue<Result> results)

private DocumentProcessor createDocumentProcessor() {
List<DocumentProcessor> processors = new ArrayList<>();
if (counter) {
processors.add(new NodeCounter(ns, ns.getHeadRevision(), executorService));
}
if (summary) {
processors.add(new Summary(numThreads));
}
Expand Down Expand Up @@ -233,6 +240,8 @@ public static class Builder {

private boolean summary;

private boolean counter;

private int numThreads = Runtime.getRuntime().availableProcessors();

private String output;
Expand Down Expand Up @@ -272,6 +281,11 @@ public Builder withSummary(boolean enable) {
return this;
}

public Builder withCounter(boolean enable) {
this.counter = enable;
return this;
}

public Builder withNumThreads(int numThreads) {
this.numThreads = numThreads;
return this;
Expand Down Expand Up @@ -314,7 +328,7 @@ public Builder withUuid(boolean enable) {

public DocumentStoreCheck build() {
return new DocumentStoreCheck(ns, store, closer, progress, silent,
summary, numThreads, output, orphan, baseVersion,
summary, counter, numThreads, output, orphan, baseVersion,
versionHistory, predecessors, successors, uuid);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.check;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;

import org.apache.jackrabbit.oak.commons.json.JsopBuilder;
import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore;
import org.apache.jackrabbit.oak.plugins.document.Path;
import org.apache.jackrabbit.oak.plugins.document.RevisionVector;
import org.apache.jackrabbit.oak.spi.state.NodeState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Count documents and nodes that exist.
*/
public class NodeCounter extends AsyncNodeStateProcessor {

private final AtomicLong numDocuments = new AtomicLong();

private final AtomicLong numNodes = new AtomicLong();

public NodeCounter(DocumentNodeStore ns,
RevisionVector headRevision,
ExecutorService executorService) {
super(ns, headRevision, executorService);
}

@Override
protected void runTask(@NotNull Path path,
@Nullable NodeState state,
@NotNull Consumer<Result> resultConsumer) {
numDocuments.incrementAndGet();
if (state != null) {
numNodes.incrementAndGet();
}
}

@Override
public void end(@NotNull BlockingQueue<Result> results)
throws InterruptedException {
JsopBuilder json = new JsopBuilder();
json.object();
json.key("type").value("counter");
json.key("documents").value(numDocuments.get());
json.key("nodes").value(numNodes.get());
json.key("exist").value(getPercentageExist());
json.endObject();
results.put(json::toString);
}

private String getPercentageExist() {
float p = 0.0f;
if (numDocuments.get() != 0) {
p = 100f * numNodes.get() / numDocuments.get();
}
return String.format("%.2f%%", p);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void execute(String... args) throws Exception {
.withProgress(options.withProgress())
.isSilent(options.isSilent())
.withSummary(options.withSummary())
.withCounter(options.withCounter())
.withNumThreads(options.getNumThreads())
.build().run();

Expand All @@ -91,6 +92,8 @@ private static final class CheckOptions extends Utils.NodeStoreOptions {

final OptionSpec<Boolean> summary;

final OptionSpec<Boolean> counter;

final OptionSpec<Boolean> orphan;

final OptionSpec<Boolean> baseVersion;
Expand All @@ -115,6 +118,8 @@ public CheckOptions(String usage) {
.withOptionalArg().ofType(Boolean.class).defaultsTo(Boolean.TRUE);
summary = parser.accepts("summary", "Write a summary message at the end")
.withOptionalArg().ofType(Boolean.class).defaultsTo(Boolean.TRUE);
counter = parser.accepts("counter", "Count documents and nodes that exist")
.withOptionalArg().ofType(Boolean.class).defaultsTo(Boolean.TRUE);
orphan = parser.accepts("orphan", "Check for orphaned nodes")
.withOptionalArg().ofType(Boolean.class).defaultsTo(Boolean.TRUE);
baseVersion = parser.accepts("baseVersion", "Check jcr:baseVersion reference")
Expand Down Expand Up @@ -153,6 +158,10 @@ public boolean withSummary() {
return summary.value(options);
}

public boolean withCounter() {
return counter.value(options);
}

public boolean withOrphan() {
return orphan.value(options);
}
Expand Down