bshashikant commented on a change in pull request #1784:
URL: https://github.com/apache/ozone/pull/1784#discussion_r561780511



##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/block/SCMBlockDeletingService.java
##########
@@ -200,4 +219,36 @@ public EmptyTaskResult call() throws Exception {
   public void setBlockDeleteTXNum(int numTXs) {
     blockDeleteLimitSize = numTXs;
   }
+
+
+  @Override
+  public void notifyRaftStatusOrSafeModeStatusChanged(
+      RaftStatus raftStatus, SafeModeStatus safeModeStatus) {

Review comment:
       is safeModeStatus really required. The function seems to care about just 
leader info.

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMService.java
##########
@@ -0,0 +1,85 @@
+/**
+ * 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.ha;
+
+/**
+ * Interface for background services in SCM, including ReplicationManager,
+ * SCMBlockDeletingService and BackgroundPipelineCreator.
+ *
+ * Provide a fine-grained method to manipulate the status of these background
+ * services.
+ */
+public interface SCMService {
+  /**
+   * @param raftStatus latest raft status
+   * @param safeModeStatus latest safe mode status
+   */
+  void notifyRaftStatusOrSafeModeStatusChanged(
+      RaftStatus raftStatus, SafeModeStatus safeModeStatus);
+
+  /**
+   * @param raftStatus latest raft status
+   * @param event latest triggered one time event.
+   */
+  default void notifyOneTimeEventTriggered(
+      RaftStatus raftStatus, OneTimeEvent event) {
+  }
+
+  /**
+   * @return true, if next iteration of Service should take effect,
+   *         false, if next iteration of Service should be skipped.
+   */
+  boolean shouldRun();
+
+  /**
+   * @return name of the Service.
+   */
+  String getServiceName();
+
+  /**
+   * Status of Service.
+   */
+  enum ServiceStatus {
+    RUNNING,
+    PAUSING
+  }
+
+  /**
+   * Raft related status.
+   */
+  enum RaftStatus {
+    LEADER,
+    NOT_LEADER
+  }
+
+  /**
+   * Safe mode related status.
+   */
+  enum SafeModeStatus {
+    IN_SAFE_MODE,
+    OUT_OF_SAFE_MODE
+  }
+
+  /**
+   * One time event.
+   */
+  enum OneTimeEvent {
+    PRE_CHECK_COMPLETED,
+    NEW_NODE_HANDLER_TRIGGERED,
+    NON_HEALTHY_TO_HEALTHY_NODE_HANDLER_TRIGGERED

Review comment:
       NON_HEALTHY_TO_HEALTHY_NODE_HANDLER_TRIGGERED ----> 
UNHEALTHY_TO_HEALTHY_NODE_HANDLER_TRIGGERED

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/block/SCMBlockDeletingService.java
##########
@@ -200,4 +219,36 @@ public EmptyTaskResult call() throws Exception {
   public void setBlockDeleteTXNum(int numTXs) {
     blockDeleteLimitSize = numTXs;
   }
+
+
+  @Override
+  public void notifyRaftStatusOrSafeModeStatusChanged(
+      RaftStatus raftStatus, SafeModeStatus safeModeStatus) {

Review comment:
       Can we separate the the two notifications?

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMServiceManager.java
##########
@@ -0,0 +1,148 @@
+/**
+ * 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.ha;
+
+import com.google.common.base.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.hadoop.hdds.scm.ha.SCMService.*;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Manipulate background services in SCM, including ReplicationManager,
+ * SCMBlockDeletingService and BackgroundPipelineCreator.
+ */
+public final class SCMServiceManager {
+  private static final Logger LOG =
+      LoggerFactory.getLogger(SCMServiceManager.class);
+
+  // Cached latest raft status and safe mode status.
+  // If raftStatus equals null, it means current SCM is running without Ratis.

Review comment:
       I think its better to use SCMContext object info here instead of 
maintaing raft status as well as safe mode status. 

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/pipeline/BackgroundPipelineCreatorV2.java
##########
@@ -0,0 +1,299 @@
+/*
+ * 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.hdds.scm.pipeline;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import org.apache.commons.collections.iterators.LoopingIterator;
+import org.apache.hadoop.hdds.HddsConfigKeys;
+import org.apache.hadoop.hdds.conf.ConfigurationSource;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
+import org.apache.hadoop.hdds.scm.ScmConfigKeys;
+import org.apache.hadoop.hdds.scm.ha.SCMService;
+import org.apache.hadoop.hdds.scm.ha.SCMServiceManager;
+import org.apache.hadoop.ozone.OzoneConfigKeys;
+import org.apache.hadoop.util.Time;
+import org.apache.ratis.util.ExitUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+import static 
org.apache.hadoop.hdds.scm.ha.SCMService.OneTimeEvent.NON_HEALTHY_TO_HEALTHY_NODE_HANDLER_TRIGGERED;
+import static 
org.apache.hadoop.hdds.scm.ha.SCMService.OneTimeEvent.NEW_NODE_HANDLER_TRIGGERED;
+import static 
org.apache.hadoop.hdds.scm.ha.SCMService.OneTimeEvent.PRE_CHECK_COMPLETED;
+
+/**
+ * Implements api for running background pipeline creation jobs.
+ */
+class BackgroundPipelineCreatorV2 implements SCMService {
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(BackgroundPipelineCreator.class);
+
+  private final PipelineManager pipelineManager;
+  private final ConfigurationSource conf;
+
+  /**
+   * SCMService related variables.
+   * 1) after leaving safe mode, BackgroundPipelineCreator needs to
+   *    wait for a while before really take effect.
+   * 2) NewNodeHandler, NonHealthyToHealthyNodeHandler, PreCheckComplete
+   *    will trigger a one-shot run of BackgroundPipelineCreator,
+   *    no matter in safe mode or not.
+   */
+  private final Lock serviceLock = new ReentrantLock();
+  private ServiceStatus serviceStatus = ServiceStatus.PAUSING;
+  private final boolean createPipelineInSafeMode;
+  private final long waitTimeInMillis;
+  private long lastTimeToBeReadyInMillis = 0;
+  private boolean oneShotRun = false;
+
+  /**
+   * RatisPipelineUtilsThread is the one which wakes up at
+   * configured interval and tries to create pipelines.
+   */
+  private Thread thread;
+  private final Object monitor = new Object();
+  private static final String THREAD_NAME = "RatisPipelineUtilsThread";
+  private final AtomicBoolean running = new AtomicBoolean(false);
+  private final long intervalInMillis;
+
+
+  BackgroundPipelineCreatorV2(PipelineManager pipelineManager,
+      ConfigurationSource conf, SCMServiceManager serviceManager) {
+    this.pipelineManager = pipelineManager;
+    this.conf = conf;
+
+    this.createPipelineInSafeMode = conf.getBoolean(
+        HddsConfigKeys.HDDS_SCM_SAFEMODE_PIPELINE_CREATION,
+        HddsConfigKeys.HDDS_SCM_SAFEMODE_PIPELINE_CREATION_DEFAULT);
+
+    this.waitTimeInMillis = conf.getTimeDuration(
+        HddsConfigKeys.HDDS_SCM_WAIT_TIME_AFTER_SAFE_MODE_EXIT,
+        HddsConfigKeys.HDDS_SCM_WAIT_TIME_AFTER_SAFE_MODE_EXIT_DEFAULT,
+        TimeUnit.MILLISECONDS);
+
+    this.intervalInMillis = conf.getTimeDuration(
+        ScmConfigKeys.OZONE_SCM_PIPELINE_CREATION_INTERVAL,
+        ScmConfigKeys.OZONE_SCM_PIPELINE_CREATION_INTERVAL_DEFAULT,
+        TimeUnit.MILLISECONDS);
+
+    // register BackgroundPipelineCreator to SCMServiceManager
+    serviceManager.register(this);
+
+    // start RatisPipelineUtilsThread
+    start();
+  }
+
+  /**
+   * Start RatisPipelineUtilsThread.
+   */
+  public void start() {
+    if (!running.compareAndSet(false, true)) {
+      LOG.warn("{} is already started, just ignore.", THREAD_NAME);
+      return;
+    }
+
+    LOG.info("Starting {}.", THREAD_NAME);
+
+    thread = new ThreadFactoryBuilder()
+        .setDaemon(false)
+        .setNameFormat(THREAD_NAME + " - %d")
+        .setUncaughtExceptionHandler((Thread t, Throwable ex) -> {
+          String message = "Terminate SCM, encounter uncaught exception"
+              + " in RatisPipelineUtilsThread";

Review comment:
       instead of terminate , can we gracefully shutdown???

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMServiceManager.java
##########
@@ -0,0 +1,148 @@
+/**
+ * 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.ha;
+
+import com.google.common.base.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.hadoop.hdds.scm.ha.SCMService.*;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Manipulate background services in SCM, including ReplicationManager,
+ * SCMBlockDeletingService and BackgroundPipelineCreator.
+ */
+public final class SCMServiceManager {
+  private static final Logger LOG =
+      LoggerFactory.getLogger(SCMServiceManager.class);
+
+  // Cached latest raft status and safe mode status.
+  // If raftStatus equals null, it means current SCM is running without Ratis.
+  private RaftStatus raftStatus;
+  private SafeModeStatus safeModeStatus;
+
+  private final List<SCMService> services = new ArrayList<>();
+
+  /**
+   * Start as a follower SCM in safe mode.
+   */
+  private SCMServiceManager(RaftStatus raftStatus,
+                            SafeModeStatus safeModeStatus) {
+    this.raftStatus = raftStatus;
+    this.safeModeStatus = safeModeStatus;
+
+    LOG.info("{} starts with RaftStatus {} and SafeModeStatus {}",
+        SCMServiceManager.class.getSimpleName(), raftStatus, safeModeStatus);
+  }
+
+  /**
+   * Register a SCMService to SCMServiceManager.
+   */
+  public synchronized void register(SCMService service) {
+    Preconditions.checkNotNull(service);
+    LOG.info("register Service {} notify with RaftStatus {} SafeModeStatus {}",
+        service.getServiceName(), raftStatus, safeModeStatus);
+
+    services.add(service);
+    service.notifyRaftStatusOrSafeModeStatusChanged(raftStatus,
+        safeModeStatus);
+  }
+
+  /**
+   * Current SCM becomes leader.
+   */
+  public synchronized void becomeLeader() {
+    LOG.info("SCM becomes leader.");
+    raftStatus = RaftStatus.LEADER;
+    notifyRaftStatusOrSafeModeStatusChanged();
+  }
+
+  /**
+   * Current SCM steps down.
+   */
+  public synchronized void stepDown() {
+    LOG.info("SCM steps down.");
+    raftStatus = RaftStatus.NOT_LEADER;
+    notifyRaftStatusOrSafeModeStatusChanged();
+  }
+
+  /**
+   * Current SCM enters into safe mode,
+   * e.g., restart or reload SCMStateMachine.
+   */
+  public synchronized void enteringSafeMode() {
+    LOG.info("SCM enters SafeMode.");
+    safeModeStatus = SafeModeStatus.IN_SAFE_MODE;
+    notifyRaftStatusOrSafeModeStatusChanged();
+  }
+
+  /**
+   * Current SCM leaves safe mode.
+   */
+  public synchronized void leavingSafeMode() {
+    LOG.info("SCM leaves SafeMode.");
+    safeModeStatus = SafeModeStatus.OUT_OF_SAFE_MODE;
+    notifyRaftStatusOrSafeModeStatusChanged();

Review comment:
       Let's separate out the leader change and safe mode notification as these 
will be two independent events,

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/block/SCMBlockDeletingService.java
##########
@@ -200,4 +219,36 @@ public EmptyTaskResult call() throws Exception {
   public void setBlockDeleteTXNum(int numTXs) {
     blockDeleteLimitSize = numTXs;
   }
+
+
+  @Override
+  public void notifyRaftStatusOrSafeModeStatusChanged(
+      RaftStatus raftStatus, SafeModeStatus safeModeStatus) {

Review comment:
       is safeModeStatus really required?. The function seems to care about 
just leader info.

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/block/SCMBlockDeletingService.java
##########
@@ -200,4 +219,36 @@ public EmptyTaskResult call() throws Exception {
   public void setBlockDeleteTXNum(int numTXs) {
     blockDeleteLimitSize = numTXs;
   }
+
+
+  @Override
+  public void notifyRaftStatusOrSafeModeStatusChanged(
+      RaftStatus raftStatus, SafeModeStatus safeModeStatus) {

Review comment:
       > is safeModeStatus really required. The function seems to care about 
just leader info.
   
   

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SCMContext.java
##########
@@ -117,9 +122,13 @@ public boolean isLeader() {
    * @return term
    * @throws NotLeaderException if isLeader is false
    */
-  public long getTerm() throws NotLeaderException {
+  public long getTermOfLeader() throws NotLeaderException {
     lock.readLock().lock();
     try {
+      if (term == INVALID_TERM) {

Review comment:
       Is this check required??




----------------------------------------------------------------
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:
[email protected]



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

Reply via email to