smengcl commented on code in PR #11611:
URL: https://github.com/apache/ozone/pull/11611#discussion_r4332787430


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/StartupProgress.java:
##########
@@ -0,0 +1,298 @@
+/*
+ * 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.server;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.ArrayList;
+import java.util.EnumMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.LongSupplier;
+import javax.management.ObjectName;
+import org.apache.hadoop.metrics2.util.MBeans;
+import org.apache.hadoop.util.Time;
+
+/**
+ * Tracks the phases of a service's startup, so that its progress can be 
observed while it is starting.
+ * <p>
+ * Phases are the constants of an enum, in the order they are executed. {@link 
Enum#toString()} is used as the
+ * name of the phase. A phase may be divided into steps that run concurrently, 
e.g. one per volume. Each step
+ * counts the work items done out of a known total, which is used to estimate 
the remaining time.
+ *
+ * @param <P> the enum defining the phases
+ */
+public class StartupProgress<P extends Enum<P>> implements 
StartupProgressMXBean {
+
+  private final Map<P, Phase> phases;
+  private final LongSupplier clock;
+  private ObjectName jmxBean;
+
+  public StartupProgress(Class<P> phaseType) {
+    this(phaseType, Time::monotonicNow);
+  }
+
+  @VisibleForTesting
+  StartupProgress(Class<P> phaseType, LongSupplier clock) {
+    this.clock = clock;
+    phases = new EnumMap<>(phaseType);
+    for (P phase : phaseType.getEnumConstants()) {
+      phases.put(phase, new Phase(phase.toString(), clock));
+    }
+  }
+
+  /**
+   * Begins the phase, ending all earlier phases. Has no effect if the phase 
has already begun.
+   */
+  public void beginPhase(P phase) {
+    for (Map.Entry<P, Phase> entry : phases.entrySet()) {
+      if (entry.getKey() == phase) {
+        entry.getValue().begin();
+        return;
+      }
+      entry.getValue().end();
+    }
+  }

Review Comment:
   Throwing here would break SCM HA. `VersionEndpointTask` runs once per SCM, 
so the 2nd and 3rd SCM call `beginPhase(PREPARING_VOLUMES)` after the phase has 
ended, and an exception there shuts down the datanode.
   
   I'd keep it as a no-op. Updated javadoc and added a test.



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