joshelser commented on a change in pull request #3667:
URL: https://github.com/apache/hbase/pull/3667#discussion_r718961561



##########
File path: 
hbase-server/src/main/java/org/apache/hadoop/hbase/master/startupprogress/StartupProgressView.java
##########
@@ -0,0 +1,118 @@
+/**
+ *
+ * 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.hbase.master.startupprogress;
+
+import static org.apache.hadoop.hbase.util.EnvironmentEdgeManager.currentTime;
+
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.hadoop.hbase.monitoring.MonitoredTask;
+import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
+import org.apache.yetus.audience.InterfaceAudience;
+
+/*
+  Creates a view for Startup Progress phases.
+ */
[email protected]
+public class StartupProgressView {
+  private MonitoredTask monitoredTask;
+  private  DateTimeFormatter formatter =
+    DateTimeFormatter.ofPattern("yyyy-MM-dd 
HH:mm:ss.SSS").withZone(ZoneId.of("UTC"));
+  private static final String COMPLETE = "COMPLETE";
+  private static final String RUNNING = "RUNNING";
+
+  public StartupProgressView(MonitoredTask monitoredTask) {
+    this.monitoredTask = monitoredTask;
+  }
+
+  /**
+   * Return list of phase with starttime, endtime and status
+   * @return list of phases
+   */
+  public List<Phase> getPhaseStatus() {
+    List<Phase> phases = new ArrayList<>();
+    // Make a copy of the list to avoid ConcurrentModificationException.
+    List<MonitoredTask.StatusJournalEntry> journalEntries =
+      ImmutableList.copyOf(this.monitoredTask.getStatusJournal());
+    for (MonitoredTask.StatusJournalEntry entry: journalEntries) {
+      Phase phase = new Phase(entry.getStatus());
+      
phase.setStartTime(formatter.format(Instant.ofEpochMilli(entry.getStartTimeStamp())));
+      if (entry.getEndTimeStamp() == Long.MAX_VALUE) {
+        // This means this task is still RUNNING.
+        phase.setStatus(RUNNING);
+        phase.setEndTime("-");
+      } else {
+        phase.setStatus(COMPLETE);
+        
phase.setEndTime(formatter.format(Instant.ofEpochMilli(entry.getEndTimeStamp())));
+      }
+      long elapsedTime = calculateElapsedTime(entry);
+      phase.setElapsedTime(convertElapsedTimeHumanReadableTime(elapsedTime));
+      phases.add(phase);
+    }
+    return phases;
+  }
+
+  /**
+   * Calculate the elapsedTime. If this status is complete then return diff of 
start and end time.
+   * If the status is still running, then return diff of current time and 
start time.
+   * @param entry journal entry
+   * @return elapsed time.
+   */
+  private long calculateElapsedTime(MonitoredTask.StatusJournalEntry entry) {
+    if (entry.getEndTimeStamp() != Long.MAX_VALUE) {
+      return entry.getEndTimeStamp() - entry.getStartTimeStamp();
+    } else {
+      return currentTime() - entry.getStartTimeStamp();
+    }
+  }
+
+  /**
+   * Convert the elapsedTime to human readable form (i.e 1 hour 10 mins 20 
secs 10 ms)
+   * @param elapsedTime elapsedTime
+   * @return human readable format
+   */
+  private String convertElapsedTimeHumanReadableTime(long elapsedTime) {
+    if (elapsedTime == Long.MAX_VALUE) {
+      return "-";
+    }
+    long hours, minutes, seconds, milliseconds;

Review comment:
       HBase follows 
https://www.oracle.com/java/technologies/javase/codeconventions-declarations.html
 which avoids multiple initializations on the same line. I believe we do not 
care if they are done in a block at the start of the method (each on their own 
line) or if you declare them as you use them the first time.

##########
File path: 
hbase-server/src/main/java/org/apache/hadoop/hbase/master/startupprogress/StartupProgressView.java
##########
@@ -0,0 +1,118 @@
+/**
+ *
+ * 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.hbase.master.startupprogress;
+
+import static org.apache.hadoop.hbase.util.EnvironmentEdgeManager.currentTime;
+
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.hadoop.hbase.monitoring.MonitoredTask;
+import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
+import org.apache.yetus.audience.InterfaceAudience;
+
+/*
+  Creates a view for Startup Progress phases.
+ */
[email protected]
+public class StartupProgressView {
+  private MonitoredTask monitoredTask;
+  private  DateTimeFormatter formatter =
+    DateTimeFormatter.ofPattern("yyyy-MM-dd 
HH:mm:ss.SSS").withZone(ZoneId.of("UTC"));
+  private static final String COMPLETE = "COMPLETE";
+  private static final String RUNNING = "RUNNING";
+
+  public StartupProgressView(MonitoredTask monitoredTask) {
+    this.monitoredTask = monitoredTask;
+  }
+
+  /**
+   * Return list of phase with starttime, endtime and status
+   * @return list of phases
+   */
+  public List<Phase> getPhaseStatus() {
+    List<Phase> phases = new ArrayList<>();
+    // Make a copy of the list to avoid ConcurrentModificationException.
+    List<MonitoredTask.StatusJournalEntry> journalEntries =
+      ImmutableList.copyOf(this.monitoredTask.getStatusJournal());

Review comment:
       I wouldn't expect the caller to be modifying this list (especially given 
that `Phase` should also be immutable).
   
   How about avoid the copy by using `Collections.unmodifiableList()`?




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


Reply via email to