shodaaan commented on code in PR #2429: URL: https://github.com/apache/jackrabbit-oak/pull/2429#discussion_r2253577322
########## oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java: ########## @@ -367,12 +361,12 @@ public MongoDocumentStore(MongoClient connection, MongoDatabase db, if (ol.isPresent()) { // oplog window based on current oplog filling rate - final AtomicReference<Double> oplogWindow = new AtomicReference<>((double) MAX_VALUE); - throttler = exponentialThrottler(DEFAULT_THROTTLING_THRESHOLD, oplogWindow, DEFAULT_THROTTLING_TIME_MS); - throttlingMetricsUpdater = new MongoDocumentStoreThrottlingMetricsUpdater(localDb, oplogWindow); - throttlingMetricsUpdater.scheduleUpdateMetrics(); - LOG.info("Started MongoDB throttling metrics with threshold {}, throttling time {}", - DEFAULT_THROTTLING_THRESHOLD, DEFAULT_THROTTLING_TIME_MS); + final AtomicReference<Integer> factor = new AtomicReference<>(0); + throttler = MongoThrottlerFactory.extFactorThrottler(factor, builder.getThrottlingTimeMillis()); + throttlingFactorUpdater = new MongoDocumentStoreThrottlingFactorUpdater(localDb, factor, builder.getThrottlingJobSchedulePeriodSecs()); + throttlingFactorUpdater.scheduleFactorUpdates(); + LOG.info("Started MongoDB throttling with factor {}, throttling time {}, schedule period {}", Review Comment: Suggestion: also add the default throttling time millis (final value: factor x default millis) to the log message. ########## oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBuilder.java: ########## @@ -312,6 +314,24 @@ public boolean isThrottlingEnabled() { return this.throttlingEnabled; } + public T setThrottlingTimeMillis(int v) { Review Comment: Suggestion: this should be named DefaultThrottlingTimeMillis: in the setters / getters and the private variable ########## oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStoreThrottlingFactorUpdater.java: ########## @@ -0,0 +1,101 @@ +/* + * 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.mongo; + +import com.mongodb.client.MongoDatabase; +import org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser; +import org.bson.Document; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.Closeable; +import java.io.IOException; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.atomic.AtomicReference; + +import static java.util.concurrent.TimeUnit.SECONDS; + +/** + * Reads throttling values from the MongoDB settings collection. + * <p> + * This class provides methods to fetch the throttling factor and related settings + * from the MongoDB database for use in throttling logic. + */ +public class MongoDocumentStoreThrottlingFactorUpdater implements Closeable { + + private static final Logger LOG = LoggerFactory.getLogger(MongoDocumentStoreThrottlingFactorUpdater.class); + private static final String SETTINGS = "settings"; + private static final String ENABLE = "enable"; + private static final String FACTOR = "factor"; + private static final String TS_TIME = "ts"; + public static final String SIZE = "size"; + private final ScheduledExecutorService throttlingFactorExecutor; + private final AtomicReference<Integer> factor; + private final MongoDatabase localDb; + private final int period; + + public MongoDocumentStoreThrottlingFactorUpdater(final @NotNull MongoDatabase localDb, + final @NotNull AtomicReference<Integer> factor, + int period) { + this.throttlingFactorExecutor = Executors.newSingleThreadScheduledExecutor(); + this.factor = factor; + this.localDb = localDb; + this.period = period; + } + + public void scheduleFactorUpdates() { + throttlingFactorExecutor.scheduleAtFixedRate(() -> factor.set(updateFactor()), 10, period, SECONDS); + } + + // visible for testing only + public int updateFactor() { + final Document document = localDb.runCommand(new Document("throttling", SETTINGS)); + + if (!document.containsKey(ENABLE) || !document.containsKey(FACTOR) || !document.containsKey(TS_TIME)) { + LOG.warn("Could not get values for settings.{} collection. Document returned: {}. Setting throttling factor to 0", "throttling", document); + return 0; + } + if (!document.getBoolean(ENABLE)) { + LOG.debug("Throttling has been disabled. Setting throttling factor to 0."); + return 0; + } + + long ts = document.getLong(TS_TIME); + long now = System.currentTimeMillis(); + if (now - ts > 3600000) { // 1 hour in ms Review Comment: This should be configurable via OSGI config variable, default can be 1h. ########## oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoThrottlerFactory.java: ########## @@ -93,4 +105,31 @@ public long throttlingTime() { return throttleTime; } } + + private static class ExtFactorThrottler implements Throttler { + + @NotNull + private final AtomicReference<Integer> factor; + private final long time; + + public ExtFactorThrottler(final @NotNull AtomicReference<Integer> factor, final long time) { + this.factor = factor; + this.time = time; Review Comment: suggestion: rename these to throttlingFactor and throttlingTime, we may have additional variables in the future. -- 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: oak-dev-unsubscr...@jackrabbit.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org