nandakumar131 commented on a change in pull request #620: HDDS-1205. Refactor 
ReplicationManager to handle QUASI_CLOSED contain…
URL: https://github.com/apache/hadoop/pull/620#discussion_r266797761
 
 

 ##########
 File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/ReplicationManager.java
 ##########
 @@ -0,0 +1,686 @@
+/**
+ * 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.hdds.scm.container;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hdds.protocol.DatanodeDetails;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.LifeCycleState;
+import org.apache.hadoop.hdds.protocol.proto
+    .StorageContainerDatanodeProtocolProtos.ContainerReplicaProto.State;
+import org.apache.hadoop.hdds.scm.ScmConfigKeys;
+import org.apache.hadoop.hdds.scm.container.placement.algorithms
+    .ContainerPlacementPolicy;
+import org.apache.hadoop.hdds.scm.events.SCMEvents;
+import org.apache.hadoop.hdds.server.events.EventPublisher;
+import org.apache.hadoop.ozone.lock.LockManager;
+import org.apache.hadoop.ozone.protocol.commands.CloseContainerCommand;
+import org.apache.hadoop.ozone.protocol.commands.CommandForDatanode;
+import org.apache.hadoop.ozone.protocol.commands.DeleteContainerCommand;
+import org.apache.hadoop.ozone.protocol.commands.ReplicateContainerCommand;
+import org.apache.hadoop.util.ExitUtil;
+import org.apache.hadoop.util.Time;
+import org.apache.ratis.util.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Replication Manager (RM) is the one which is responsible for making sure
+ * that the containers are properly replicated. Replication Manager deals only
+ * with Quasi Closed / Closed container.
+ */
+public class ReplicationManager {
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(ReplicationManager.class);
+
+  /**
+   * Reference to the ContainerManager.
+   */
+  private final ContainerManager containerManager;
+
+  /**
+   * PlacementPolicy which is used to identify where a container
+   * should be copied.
+   */
+  private final ContainerPlacementPolicy containerPlacement;
+
+  /**
+   * EventPublisher to fire Replicate and Delete container commands.
+   */
+  private final EventPublisher eventPublisher;
+
+  /**
+   * Used for locking a container with its ID while processing it.
+   */
+  private final LockManager<ContainerID> lockManager;
+
+  /**
+   * This is used to track container replication commands which are issued
+   * by ReplicationManager and not yet complete.
+   */
+  private final Map<ContainerID, List<InflightAction>> inflightReplication;
+
+  /**
+   * This is used to track container deletion commands which are issued
+   * by ReplicationManager and not yet complete.
+   */
+  private final Map<ContainerID, List<InflightAction>> inflightDeletion;
+
+  /**
+   * ReplicationMonitor thread is the one which wakes up at configured
+   * interval and processes all the containers.
+   */
+  private final Thread replicationMonitor;
+
+  /**
+   * The frequency in which ReplicationMonitor thread should run.
+   */
+  private final long interval;
+
+  /**
+   * Timeout for container replication & deletion command issued by
+   * ReplicationManager.
+   */
+  private final long eventTimeout;
+
+  /**
+   * Flag used to check if ReplicationMonitor thread is running or not.
+   */
+  private volatile boolean running;
+
+  /**
+   * Constructs ReplicationManager instance with the given configuration.
+   *
+   * @param conf OzoneConfiguration
+   * @param containerManager ContainerManager
+   * @param containerPlacement ContainerPlacementPolicy
+   * @param eventPublisher EventPublisher
+   */
+  public ReplicationManager(final Configuration conf,
+                            final ContainerManager containerManager,
+                            final ContainerPlacementPolicy containerPlacement,
+                            final EventPublisher eventPublisher) {
+    this.containerManager = containerManager;
+    this.containerPlacement = containerPlacement;
+    this.eventPublisher = eventPublisher;
+    this.lockManager = new LockManager<>(conf);
+    this.inflightReplication = new HashMap<>();
+    this.inflightDeletion = new HashMap<>();
+    this.replicationMonitor = new Thread(this::run);
+    this.replicationMonitor.setName("ReplicationMonitor");
+    this.interval = conf.getTimeDuration(
+        ScmConfigKeys.HDDS_SCM_REPLICATION_THREAD_INTERVAL,
+        ScmConfigKeys.HDDS_SCM_REPLICATION_THREAD_INTERVAL_DEFAULT,
+        TimeUnit.MILLISECONDS);
+    this.eventTimeout = conf.getTimeDuration(
+        ScmConfigKeys.HDDS_SCM_REPLICATION_EVENT_TIMEOUT,
+        ScmConfigKeys.HDDS_SCM_REPLICATION_EVENT_TIMEOUT_DEFAULT,
+        TimeUnit.MILLISECONDS);
+    this.running = false;
+  }
+
+  /**
+   * Starts Replication Monitor thread.
+   */
+  public void start() {
+    LOG.info("Starting Replication Monitor Thread.");
+    running = true;
+    replicationMonitor.start();
+  }
+
+  /**
+   * Process all the containers immediately.
+   */
+  @VisibleForTesting
+  void processContainersNow() {
+    containerManager.getContainerIDs()
+        .parallelStream().forEach(this::processContainer);
+  }
+
+  /**
+   * Stops Replication Monitor thread.
+   */
+  public void stop() {
+    LOG.info("Stopping Replication Monitor Thread.");
+    running = false;
+    replicationMonitor.interrupt();
+  }
+
+  /**
+   * ReplicationMonitor thread runnable. This wakes up at configured
+   * interval and processes all the containers in the system.
+   */
+  private void run() {
+    try {
+      while (running) {
+        try {
+          final long start = Time.monotonicNow();
+          final List<ContainerID> containerIds =
+              containerManager.getContainerIDs();
+          containerIds.parallelStream().forEach(this::processContainer);
+          LOG.debug("Replication Monitor Thread took {} milliseconds for" +
+                  " processing {} containers.", Time.monotonicNow() - start,
+              containerIds.size());
+          Thread.sleep(interval);
+        } catch (InterruptedException ex) {
+          // Wakeup and process the containers.
+          LOG.debug("Replication Monitor Thread got interrupt exception.");
+        }
+      }
+    } catch (Throwable t) {
+      // When we get runtime exception, we should terminate SCM.
+      LOG.error("Exception in Replication Monitor Thread.", t);
+      ExitUtil.terminate(1, t);
+    }
+  }
+
+  /**
+   * Process the given container.
+   *
+   * @param id ContainerID
+   */
+  private void processContainer(ContainerID id) {
+    lockManager.lock(id);
+    try {
+      final ContainerInfo container = containerManager.getContainer(id);
+      final Set<ContainerReplica> replicas = containerManager
+          .getContainerReplicas(container.containerID());
+      final LifeCycleState state = container.getState();
+
+      /*
+       * We don't take any action if the container is in OPEN state.
+       */
+      if (state == LifeCycleState.OPEN) {
+        return;
+      }
+
+      /*
+       * If the container is in CLOSING state, the replicas can either
+       * be in OPEN or in CLOSING state. In both of this cases
+       * we have to resend close container command to the datanodes.
+       */
+      if (state == LifeCycleState.CLOSING) {
+        replicas.forEach(replica -> sendCloseCommand(
+            container, replica.getDatanodeDetails(), false));
+        return;
+      }
+
+      /*
+       * Before processing the container we have to reconcile the
+       * inflightReplication and inflightDeletion actions.
+       *
+       * We remove the entry from inflightReplication and inflightDeletion
+       * list, if the operation is completed or if it has timed out.
+       */
+      updateInflightAction(container, inflightReplication,
+          action -> replicas.stream()
+              .anyMatch(r -> r.getDatanodeDetails().equals(action.datanode)));
+
+      updateInflightAction(container, inflightDeletion,
+          action -> replicas.stream()
+              .noneMatch(r -> r.getDatanodeDetails().equals(action.datanode)));
+
+
+      /*
+       * If the container is in QUASI_CLOSED state, check and close the
+       * container if possible.
+       */
+      if (state == LifeCycleState.QUASI_CLOSED) {
+        forceCloseContainer(container, replicas);
 
 Review comment:
   True, it will be easy to understand the logic if we return here. Will change 
it.

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


With regards,
Apache Git Services

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

Reply via email to