You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by GitBox <gi...@apache.org> on 2022/10/12 16:14:58 UTC

[GitHub] [ozone] sodonnel commented on a diff in pull request #3788: HDDS-7095. allow on demand scanning for containers

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


##########
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();
+        logScanCompleted(containerData, now);
+        instance.containerController.updateDataScanTimestamp(containerId, now);
+      } else {
+        instance.containerController.markContainerUnhealthy(containerId);
+        instance.metrics.incNumUnHealthyContainers();
+      }
+      instance.metrics.incNumContainersScanned();
+    } catch (IOException e) {
+      LOG.warn("Unexpected exception while scanning container "
+          + containerId, e);
+    }
+  }
+
+  private static void logScanStart(ContainerData containerData) {
+    if (LOG.isDebugEnabled()) {
+      Optional<Instant> scanTimestamp = containerData.lastDataScanTime();
+      Object lastScanTime = scanTimestamp.map(ts -> "at " + ts).orElse("never");
+      LOG.debug("Scanning container {}, last scanned {}",
+          containerData.getContainerID(), lastScanTime);
+    }
+  }
+
+  private static void logScanCompleted(
+      ContainerData containerData, Instant timestamp) {
+    if (LOG.isDebugEnabled()) {

Review Comment:
   When there are simple parameters being passed to the `LOG.debug`, you don't need to wrap it in `if (LOG.isDebugEnabled())` as the underlying logger has this check anyway.
   
   Note that above, where you creating a string to pass to debug, it does make sense to wrap the log in an IF statement, to avoid creating the string when its not going to be logged anyway.



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org