errose28 commented on code in PR #7490:
URL: https://github.com/apache/ozone/pull/7490#discussion_r2109608481


##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/ozoneimpl/ContainerScanHelper.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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 java.io.IOException;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.Optional;
+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.apache.hadoop.ozone.container.common.interfaces.ScanResult;
+import org.apache.hadoop.ozone.container.common.volume.HddsVolume;
+import org.slf4j.Logger;
+
+/**
+ * Mixin to handle common data and metadata scan operations among background 
and on-demand scanners.
+ */
+public class ContainerScanHelper {
+  private final Logger log;
+  private final ContainerController controller;
+  private final AbstractContainerScannerMetrics metrics;
+  private final long minScanGap;
+
+  public ContainerScanHelper(Logger log, ContainerController controller,
+                             AbstractContainerScannerMetrics metrics, 
ContainerScannerConfiguration conf) {
+    this.log = log;
+    this.controller = controller;
+    this.metrics = metrics;
+    this.minScanGap = conf.getContainerScanMinGap();
+  }
+
+  public void scanData(Container<?> container, DataTransferThrottler 
throttler, Canceler canceler)
+      throws IOException, InterruptedException {
+    if (!shouldScanData(container)) {
+      return;
+    }
+    ContainerData containerData = container.getContainerData();
+    long containerId = containerData.getContainerID();
+    logScanStart(containerData);
+    DataScanResult result = container.scanData(throttler, canceler);
+
+    if (result.isDeleted()) {
+      log.debug("Container [{}] has been deleted during the data scan.", 
containerId);
+    } else {
+      try {
+        controller.updateContainerChecksum(containerId, result.getDataTree());
+      } catch (IOException ex) {
+        log.warn("Failed to update container checksum after scan of container 
{}", containerId, ex);
+      }
+      if (!result.isHealthy()) {
+        handleUnhealthyScanResult(containerId, result);
+      }
+      metrics.incNumContainersScanned();
+    }
+
+    Instant now = Instant.now();
+    if (!result.isDeleted()) {
+      controller.updateDataScanTimestamp(containerId, now);
+    }
+    // Even if the container was deleted, mark the scan as completed since we 
already logged it as starting.
+    logScanCompleted(containerData, now);
+  }
+
+  public void handleUnhealthyScanResult(long containerID, ScanResult result) 
throws IOException {
+
+    log.error("Corruption detected in container [{}]. Marking it UNHEALTHY. 
{}", containerID, result);
+    if (log.isDebugEnabled()) {
+      StringBuilder allErrorString = new StringBuilder();
+      result.getErrors().forEach(r -> allErrorString.append(r).append('\n'));
+      log.debug("Complete list of errors detected while scanning container 
{}:\n{}", containerID, allErrorString);
+    }
+
+    // Only increment the number of unhealthy containers if the container was 
not already unhealthy.
+    // TODO HDDS-11593: Scanner counters will start from zero
+    //  at the beginning of each run, so this will need to be incremented for 
every unhealthy container seen
+    //  regardless of its previous state.
+    boolean containerMarkedUnhealthy = 
controller.markContainerUnhealthy(containerID, result);
+    if (containerMarkedUnhealthy) {
+      metrics.incNumUnHealthyContainers();
+    }
+  }
+
+  public boolean shouldScanMetadata(Container<?> container) {
+    if (container == null) {
+      return false;
+    }
+    long containerID = container.getContainerData().getContainerID();
+
+    HddsVolume containerVolume = container.getContainerData().getVolume();
+    if (containerVolume.isFailed()) {
+      log.debug("Skipping scan for container {} since its volume {} has 
failed.", containerID, containerVolume);
+      return false;
+    }
+
+    return !recentlyScanned(container.getContainerData());
+  }
+
+  public boolean shouldScanData(Container<?> container) {
+    return shouldScanMetadata(container) && container.shouldScanData();
+  }
+
+  private boolean recentlyScanned(ContainerData containerData) {
+    Optional<Instant> lastScanTime = containerData.lastDataScanTime();
+    Instant now = Instant.now();
+    // Container is considered recently scanned if it was scanned within the
+    // configured time frame. If the optional is empty, the container was
+    // never scanned.
+    boolean recentlyScanned = lastScanTime.map(scanInstant ->
+            Duration.between(now, scanInstant).abs()
+                .compareTo(Duration.ofMillis(minScanGap)) < 0)

Review Comment:
   Latest commits adds the ability to bypass the scan gap for on-demand scanner.



-- 
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-unsubscr...@ozone.apache.org

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


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

Reply via email to