sodonnel commented on a change in pull request #994:
URL: https://github.com/apache/hadoop-ozone/pull/994#discussion_r433164232



##########
File path: 
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/fsck/ContainerHealthTask.java
##########
@@ -0,0 +1,345 @@
+/*
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.recon.fsck;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import org.apache.hadoop.hdds.scm.PlacementPolicy;
+import org.apache.hadoop.hdds.scm.container.ContainerID;
+import org.apache.hadoop.hdds.scm.container.ContainerInfo;
+import org.apache.hadoop.hdds.scm.container.ContainerManager;
+import org.apache.hadoop.hdds.scm.container.ContainerNotFoundException;
+import org.apache.hadoop.hdds.scm.container.ContainerReplica;
+import org.apache.hadoop.ozone.recon.persistence.ContainerSchemaManager;
+import org.apache.hadoop.ozone.recon.scm.ReconScmTask;
+import org.apache.hadoop.ozone.recon.tasks.ReconTaskConfig;
+import org.apache.hadoop.util.Time;
+import 
org.hadoop.ozone.recon.schema.ContainerSchemaDefinition.UnHealthyContainerStates;
+import org.hadoop.ozone.recon.schema.tables.daos.ReconTaskStatusDao;
+import org.hadoop.ozone.recon.schema.tables.pojos.UnhealthyContainers;
+import org.hadoop.ozone.recon.schema.tables.records.UnhealthyContainersRecord;
+import org.jooq.Cursor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Class that scans the list of containers and keeps track of containers with
+ * no replicas in a SQL table.
+ */
+public class ContainerHealthTask extends ReconScmTask {
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(ContainerHealthTask.class);
+
+  private ContainerManager containerManager;
+  private ContainerSchemaManager containerSchemaManager;
+  private PlacementPolicy placementPolicy;
+  private final long interval;
+  private Set<ContainerInfo> processedContainers = new HashSet<>();
+
+  public ContainerHealthTask(
+      ContainerManager containerManager,
+      ReconTaskStatusDao reconTaskStatusDao,
+      ContainerSchemaManager containerSchemaManager,
+      PlacementPolicy placementPolicy,
+      ReconTaskConfig reconTaskConfig) {
+    super(reconTaskStatusDao);
+    this.containerSchemaManager = containerSchemaManager;
+    this.placementPolicy = placementPolicy;
+    this.containerManager = containerManager;
+    this.interval = TimeUnit.SECONDS.toMillis(
+        reconTaskConfig.getMissingContainerTaskInterval());
+  }
+
+  public synchronized void run() {
+    try {
+      while (canRun()) {
+        long start = Time.monotonicNow();
+        long currentTime = System.currentTimeMillis();
+        long existingCount = processExistingDBRecords(currentTime);
+        LOG.info("Missing Container task thread took {} milliseconds to" +
+                " process {} existing database records.",
+            Time.monotonicNow() - start, existingCount);
+        start = Time.monotonicNow();
+        final List<ContainerInfo> containers = 
containerManager.getContainers();
+        containers.forEach(container ->
+            processContainer(container, currentTime));
+        recordSingleRunCompletion();
+        LOG.info("Missing Container task Thread took {} milliseconds for" +
+                " processing {} containers.", Time.monotonicNow() - start,
+            containers.size());
+        processedContainers.clear();
+        wait(interval);
+      }
+    } catch (Throwable t) {
+      LOG.error("Exception in Missing Container task Thread.", t);
+    }
+  }
+
+  private ContainerHealthStatus setCurrentContainer(long recordId)
+      throws ContainerNotFoundException {
+    ContainerInfo container =
+        containerManager.getContainer(new ContainerID(recordId));
+    Set<ContainerReplica> replicas =
+        containerManager.getContainerReplicas(container.containerID());
+    return new ContainerHealthStatus(container, replicas, placementPolicy);
+  }
+
+  private void completeProcessingContainer(ContainerHealthStatus container,
+      Set<String> existingRecords, long currentTime) {
+    if (container != null) {
+      containerSchemaManager.insertUnhealthyContainerRecords(
+          ContainerHealthRecords.generateUnhealthyRecords(
+              container, existingRecords, currentTime));
+      processedContainers.add(container.getContainer());
+    }
+  }
+
+  private long processExistingDBRecords(long currentTime) {
+    long recordCount = 0;
+    try (Cursor<UnhealthyContainersRecord> cursor =
+             containerSchemaManager.getAllUnhealthyRecordsCursor()) {
+      ContainerHealthStatus currentContainer = null;
+      Set<String> existingRecords = new HashSet<>();
+      while(cursor.hasNext()) {
+        recordCount++;
+        UnhealthyContainersRecord rec = cursor.fetchNext();
+        try {
+          if (currentContainer == null
+              || currentContainer.getContainerID() != rec.getContainerId()) {
+            completeProcessingContainer(
+                currentContainer, existingRecords, currentTime);
+            existingRecords.clear();
+            currentContainer = setCurrentContainer(rec.getContainerId());
+          }
+          if (ContainerHealthRecords
+              .retainOrUpdateRecord(currentContainer, rec)) {
+            existingRecords.add(rec.getContainerState());
+            if (rec.changed()) {
+              rec.update();
+            }
+          } else {
+            rec.delete();
+          }
+        } catch (ContainerNotFoundException cnf) {
+          rec.delete();
+          currentContainer = null;
+        }
+      }
+      // Remember to finish processing the last container
+      completeProcessingContainer(
+          currentContainer, existingRecords, currentTime);
+    }
+    return recordCount;
+  }
+
+  private void processContainer(ContainerInfo container, long currentTime) {
+    try {
+      if (processedContainers.contains(container)) {
+        // Was already handled when processing the existing DB records, so skip
+        return;
+      }
+      Set<ContainerReplica> containerReplicas =
+          containerManager.getContainerReplicas(container.containerID());
+      ContainerHealthStatus h = new ContainerHealthStatus(
+          container, containerReplicas, placementPolicy);
+      if (h.isHealthy()) {
+        return;
+      }
+      containerSchemaManager.insertUnhealthyContainerRecords(
+          ContainerHealthRecords.generateUnhealthyRecords(h, currentTime));
+    } catch (ContainerNotFoundException e) {
+      LOG.error("Container not found while finding missing containers", e);
+    }
+  }
+
+  /**
+   * Helper methods to generate and update the required database records for
+   * unhealthy containers.
+   */
+  static public class ContainerHealthRecords {
+
+    static public boolean retainOrUpdateRecord(

Review comment:
       Done.




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

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



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

Reply via email to