sodonnel commented on code in PR #3788:
URL: https://github.com/apache/ozone/pull/3788#discussion_r993672113


##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/ozoneimpl/OnDemandContainerScanner.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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.hadoop.ozone.container.ozoneimpl;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.hadoop.hdfs.util.Canceler;
+import org.apache.hadoop.hdfs.util.DataTransferThrottler;
+import org.apache.hadoop.ozone.container.common.impl.ContainerData;
+import org.apache.hadoop.ozone.container.common.interfaces.Container;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.time.Instant;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Class for performing on demand scans of containers.
+ */
+public final class OnDemandContainerScanner {
+  public static final Logger LOG =
+      LoggerFactory.getLogger(OnDemandContainerScanner.class);
+
+  private static volatile OnDemandContainerScanner instance;
+
+  private final ExecutorService scanExecutor;
+  private final ContainerController containerController;
+  private final DataTransferThrottler throttler;
+  private final Canceler canceler;
+  private final ConcurrentHashMap
+      .KeySetView<Container<?>, Boolean> toBeScannedContainers;
+  private final OnDemandScannerMetrics metrics;
+  @VisibleForTesting
+  private static Future<?> lastScanFuture;
+
+  private OnDemandContainerScanner(
+      ContainerScannerConfiguration conf, ContainerController controller) {
+    containerController = controller;
+    throttler = new DataTransferThrottler(
+        conf.getOnDemandBandwidthPerVolume());
+    canceler = new Canceler();
+    metrics = OnDemandScannerMetrics.create();
+    scanExecutor = Executors.newSingleThreadExecutor();
+    toBeScannedContainers = ConcurrentHashMap.newKeySet();
+  }
+
+  public static synchronized void init(
+      ContainerScannerConfiguration conf, ContainerController controller) {
+    if (instance != null) {
+      LOG.warn("Trying to initialize on demand scanner" +
+          " a second time on a datanode.");
+      return;
+    }
+    instance = new OnDemandContainerScanner(conf, controller);
+  }
+
+  public static void scanContainer(Container<?> container) {
+    if (instance == null) {
+      return;
+    }
+    if (container.shouldScanData() &&
+        instance.toBeScannedContainers.add(container)) {
+      lastScanFuture = instance.scanExecutor.submit(() -> {
+        instance.toBeScannedContainers.remove(container);
+        if (container.shouldScanData()) {
+          performOnDemandScan(container);
+        }
+      });
+    }
+  }
+
+  private static void performOnDemandScan(Container<?> container) {
+    long containerId = container.getContainerData().getContainerID();
+    try {
+      ContainerData containerData = container.getContainerData();
+      logScanStart(containerData);
+      if (container.scanData(instance.throttler, instance.canceler)) {
+        Instant now = Instant.now();

Review Comment:
   I am not sure what all `now` will be used for, but it may make sense to pass 
a clock to this object when creating it. In a few places in the code, we use 
the `MonotonicClock` class for this. At runtime it really just wraps 
`Instant.now` etc, but in tests you can pass in TestClock instead, which lets 
you manipulate the time without sleeps. If you have any need to create tests 
when the passage of time affects things, it would make sense to use this 
approach.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to