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


##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/BackgroundSCMService.java:
##########
@@ -0,0 +1,208 @@
+/*
+ * 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.ha;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.ratis.util.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.time.Clock;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * A common implementation for background SCMService.
+ * */
+public final class BackgroundSCMService implements SCMService {
+  private final Logger log;
+  private final AtomicBoolean running = new AtomicBoolean(false);
+  private Thread backgroundThread;
+  private final Lock serviceLock = new ReentrantLock();
+  private final SCMContext scmContext;
+  private ServiceStatus serviceStatus = ServiceStatus.PAUSING;
+  private final long intervalInMillis;
+  private final long waitTimeInMillis;
+  private long lastTimeToBeReadyInMillis = 0;
+  private final Clock clock;
+  private String serviceName;
+  private Runnable periodicalTask;
+  private volatile boolean runImmediately = false;
+
+  private BackgroundSCMService(
+      final Clock clock, final SCMContext scmContext,
+      final String serviceName, final long intervalInMillis,
+      final long waitTimeInMillis, final Runnable task) {
+    this.scmContext = scmContext;
+    this.clock = clock;
+    this.periodicalTask = task;
+    this.serviceName = serviceName;
+    this.log = LoggerFactory.getLogger(serviceName);
+    this.intervalInMillis = intervalInMillis;
+    this.waitTimeInMillis = waitTimeInMillis;
+    start();
+  }
+
+  @Override
+  public void start() {
+    if (!running.compareAndSet(false, true)) {
+      log.info("{} Service is already running, skip start.", getServiceName());
+      return;
+    }
+    log.info("Starting {} Service.", getServiceName());
+
+    backgroundThread = new Thread(this::run);
+    backgroundThread.setName(serviceName + "Thread");
+    backgroundThread.setDaemon(true);
+    backgroundThread.start();
+  }
+
+  @Override
+  public void notifyStatusChanged() {
+    serviceLock.lock();
+    try {
+      if (scmContext.isLeaderReady() && !scmContext.isInSafeMode()) {
+        if (serviceStatus != ServiceStatus.RUNNING) {
+          log.info("Service {} transitions to RUNNING.", getServiceName());
+          serviceStatus = ServiceStatus.RUNNING;
+          lastTimeToBeReadyInMillis = clock.millis();
+        }
+      } else {
+        if (serviceStatus != ServiceStatus.PAUSING) {
+          log.info("Service {} transitions to PAUSING.", getServiceName());
+          serviceStatus = ServiceStatus.PAUSING;
+        }
+      }
+    } finally {
+      serviceLock.unlock();
+    }
+  }
+
+  private void run() {
+    while (running.get()) {
+      try {
+        if (shouldRun()) {
+          periodicalTask.run();
+        }
+        synchronized (this) {
+          if (!runImmediately) {
+            wait(intervalInMillis);
+          }
+          runImmediately = false;
+        }
+      } catch (InterruptedException e) {

Review Comment:
   Also I am not sure if a runnable is allowed to throw checked exceptions? Can 
it only throw RuntimeExceptions? Maybe its enough to catch and log 
RuntimeException and then re-throw? I am not sure.



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

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