1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.hbtop.screen.top;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.List;
24 import java.util.Objects;
25 import org.apache.commons.lang3.time.DateFormatUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.hbase.ClusterStatus;
29 import org.apache.hadoop.hbase.ServerLoad;
30 import org.apache.hadoop.hbase.ServerName;
31 import org.apache.hadoop.hbase.classification.InterfaceAudience;
32 import org.apache.hadoop.hbase.client.Admin;
33 import org.apache.hadoop.hbase.hbtop.Record;
34 import org.apache.hadoop.hbase.hbtop.RecordFilter;
35 import org.apache.hadoop.hbase.hbtop.field.Field;
36 import org.apache.hadoop.hbase.hbtop.field.FieldInfo;
37 import org.apache.hadoop.hbase.hbtop.field.FieldValue;
38 import org.apache.hadoop.hbase.hbtop.mode.DrillDownInfo;
39 import org.apache.hadoop.hbase.hbtop.mode.Mode;
40
41
42
43
44 @InterfaceAudience.Private
45 public class TopScreenModel {
46
47 private static final Log LOG = LogFactory.getLog(TopScreenModel.class);
48
49 private final Admin admin;
50
51 private Mode currentMode;
52 private Field currentSortField;
53 private List<FieldInfo> fieldInfos;
54 private List<Field> fields;
55
56 private Summary summary;
57 private List<Record> records;
58
59 private final List<RecordFilter> filters = new ArrayList<>();
60 private final List<String> filterHistories = new ArrayList<>();
61
62 private boolean ascendingSort;
63
64 public TopScreenModel(Admin admin, Mode initialMode) {
65 this.admin = Objects.requireNonNull(admin);
66 switchMode(Objects.requireNonNull(initialMode), null, false);
67 }
68
69 public void switchMode(Mode nextMode, List<RecordFilter> initialFilters,
70 boolean keepSortFieldAndSortOrderIfPossible) {
71
72 currentMode = nextMode;
73 fieldInfos = Collections.unmodifiableList(new ArrayList<>(currentMode.getFieldInfos()));
74
75 fields = new ArrayList<>();
76 for (FieldInfo fieldInfo : currentMode.getFieldInfos()) {
77 fields.add(fieldInfo.getField());
78 }
79 fields = Collections.unmodifiableList(fields);
80
81 if (keepSortFieldAndSortOrderIfPossible) {
82 boolean match = false;
83 for (Field field : fields) {
84 if (field == currentSortField) {
85 match = true;
86 break;
87 }
88 }
89
90 if (!match) {
91 currentSortField = nextMode.getDefaultSortField();
92 ascendingSort = false;
93 }
94
95 } else {
96 currentSortField = nextMode.getDefaultSortField();
97 ascendingSort = false;
98 }
99
100 clearFilters();
101 if (initialFilters != null) {
102 filters.addAll(initialFilters);
103 }
104 }
105
106 public void setSortFieldAndFields(Field sortField, List<Field> fields) {
107 this.currentSortField = sortField;
108 this.fields = Collections.unmodifiableList(new ArrayList<>(fields));
109 }
110
111
112
113
114
115 public void refreshMetricsData() {
116 ClusterStatus clusterStatus;
117 try {
118 clusterStatus = admin.getClusterStatus();
119 } catch (Exception e) {
120 LOG.error("Unable to get cluster status", e);
121 return;
122 }
123
124 refreshSummary(clusterStatus);
125 refreshRecords(clusterStatus);
126 }
127
128 private void refreshSummary(ClusterStatus clusterStatus) {
129 String currentTime = DateFormatUtils.ISO_8601_EXTENDED_TIME_FORMAT
130 .format(System.currentTimeMillis());
131 String version = clusterStatus.getHBaseVersion();
132 String clusterId = clusterStatus.getClusterId();
133 int liveServers = clusterStatus.getServersSize();
134 int deadServers = clusterStatus.getDeadServerNames().size();
135 int regionCount = clusterStatus.getRegionsCount();
136 int ritCount = clusterStatus.getRegionsInTransition().size();
137 double averageLoad = clusterStatus.getAverageLoad();
138 long aggregateRequestPerSecond = 0;
139 for (ServerName sn: clusterStatus.getServers()) {
140 ServerLoad sl = clusterStatus.getLoad(sn);
141 aggregateRequestPerSecond += sl.getNumberOfRequests();
142 }
143 summary = new Summary(currentTime, version, clusterId, liveServers + deadServers,
144 liveServers, deadServers, regionCount, ritCount, averageLoad, aggregateRequestPerSecond);
145 }
146
147 private void refreshRecords(ClusterStatus clusterStatus) {
148
149 List<Record> records = new ArrayList<>();
150 for (Record record : currentMode.getRecords(clusterStatus)) {
151 boolean filter = false;
152 for (RecordFilter recordFilter : filters) {
153 if (!recordFilter.execute(record)) {
154 filter = true;
155 break;
156 }
157 }
158 if (!filter) {
159 records.add(record);
160 }
161 }
162
163
164 Collections.sort(records, new Comparator<Record>() {
165 @Override
166 public int compare(Record recordLeft, Record recordRight) {
167 FieldValue left = recordLeft.get(currentSortField);
168 FieldValue right = recordRight.get(currentSortField);
169 return (ascendingSort ? 1 : -1) * left.compareTo(right);
170 }
171 });
172
173 this.records = Collections.unmodifiableList(records);
174 }
175
176 public void switchSortOrder() {
177 ascendingSort = !ascendingSort;
178 }
179
180 public boolean addFilter(String filterString, boolean ignoreCase) {
181 RecordFilter filter = RecordFilter.parse(filterString, fields, ignoreCase);
182 if (filter == null) {
183 return false;
184 }
185
186 filters.add(filter);
187 filterHistories.add(filterString);
188 return true;
189 }
190
191 public void clearFilters() {
192 filters.clear();
193 }
194
195 public boolean drillDown(Record selectedRecord) {
196 DrillDownInfo drillDownInfo = currentMode.drillDown(selectedRecord);
197 if (drillDownInfo == null) {
198 return false;
199 }
200 switchMode(drillDownInfo.getNextMode(), drillDownInfo.getInitialFilters(), true);
201 return true;
202 }
203
204 public Mode getCurrentMode() {
205 return currentMode;
206 }
207
208 public Field getCurrentSortField() {
209 return currentSortField;
210 }
211
212 public List<FieldInfo> getFieldInfos() {
213 return fieldInfos;
214 }
215
216 public List<Field> getFields() {
217 return fields;
218 }
219
220 public Summary getSummary() {
221 return summary;
222 }
223
224 public List<Record> getRecords() {
225 return records;
226 }
227
228 public List<RecordFilter> getFilters() {
229 return Collections.unmodifiableList(filters);
230 }
231
232 public List<String> getFilterHistories() {
233 return Collections.unmodifiableList(filterHistories);
234 }
235 }