joshelser commented on a change in pull request #476: HBASE-11062 hbtop
URL: https://github.com/apache/hbase/pull/476#discussion_r317664758
 
 

 ##########
 File path: 
hbase-hbtop/src/main/java/org/apache/hadoop/hbase/hbtop/screen/top/TopScreenModel.java
 ##########
 @@ -0,0 +1,202 @@
+/**
+ * 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.hbtop.screen.top;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import org.apache.commons.lang3.time.DateFormatUtils;
+import org.apache.hadoop.hbase.ClusterMetrics;
+import org.apache.hadoop.hbase.client.Admin;
+import org.apache.hadoop.hbase.hbtop.Record;
+import org.apache.hadoop.hbase.hbtop.RecordFilter;
+import org.apache.hadoop.hbase.hbtop.field.Field;
+import org.apache.hadoop.hbase.hbtop.field.FieldInfo;
+import org.apache.hadoop.hbase.hbtop.field.FieldValue;
+import org.apache.hadoop.hbase.hbtop.mode.DrillDownInfo;
+import org.apache.hadoop.hbase.hbtop.mode.Mode;
+import org.apache.yetus.audience.InterfaceAudience;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The data and business logic for the top screen.
+ */
[email protected]
+public class TopScreenModel {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(TopScreenModel.class);
+
+  private final Admin admin;
+
+  private Mode currentMode;
+  private Field currentSortField;
+  private List<FieldInfo> fieldInfos;
+  private List<Field> fields;
+
+  private Summary summary;
+  private List<Record> records;
+
+  private final List<RecordFilter> filters = new ArrayList<>();
+  private final List<String> filterHistories = new ArrayList<>();
+
+  private boolean ascendingSort;
+
+  public TopScreenModel(Admin admin, Mode initialMode) {
+    this.admin = Objects.requireNonNull(admin);
+    switchMode(Objects.requireNonNull(initialMode), null, false);
+  }
+
+  public void switchMode(Mode nextMode, List<RecordFilter> initialFilters,
+    boolean keepSortFieldAndSortOrderIfPossible) {
+
+    currentMode = nextMode;
+    fieldInfos = Collections.unmodifiableList(new 
ArrayList<>(currentMode.getFieldInfos()));
+    fields = Collections.unmodifiableList(new 
ArrayList<>(currentMode.getFieldInfos().stream()
+      .map(FieldInfo::getField).collect(Collectors.toList())));
+
+    if (keepSortFieldAndSortOrderIfPossible) {
+      boolean match = fields.stream().anyMatch(f -> f == currentSortField);
+      if (!match) {
+        currentSortField = nextMode.getDefaultSortField();
+        ascendingSort = false;
+      }
+    } else {
+      currentSortField = nextMode.getDefaultSortField();
+      ascendingSort = false;
+    }
+
+    clearFilters();
+    if (initialFilters != null) {
+      filters.addAll(initialFilters);
+    }
+  }
+
+  public void setSortFieldAndFields(Field sortField, List<Field> fields) {
+    this.currentSortField = sortField;
+    this.fields = Collections.unmodifiableList(new ArrayList<>(fields));
+  }
+
+  public void refreshMetricsData() {
 
 Review comment:
   I was tracing through the call trace of how we can get here. I would have 
normally expected this to be guarded by a lock, but it seems like we only call 
it from the "main thread" of the application.
   
   For example, if we ever switched to refreshing on some timer (instead of 
driven by user action), we would have to make sure that we don't start backing 
up on `admin.getClusterMetrics()` calls (e.g. if the Master is having problems).
   
   Assuming I got it right, I think a comment here explaining that "HBTop only 
calls this from a single thread, and if that ever changes, this needs 
synchronization" would be really helpful!

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


With regards,
Apache Git Services

Reply via email to