001/**
002 * Copyright The Apache Software Foundation
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package org.apache.hadoop.hbase;
022
023import java.util.Arrays;
024import java.util.List;
025import java.util.Map;
026import java.util.Set;
027import java.util.TreeMap;
028import java.util.stream.Collectors;
029import org.apache.hadoop.hbase.replication.ReplicationLoadSink;
030import org.apache.hadoop.hbase.replication.ReplicationLoadSource;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.apache.hadoop.hbase.util.Strings;
033import org.apache.yetus.audience.InterfaceAudience;
034
035import org.apache.hbase.thirdparty.com.google.common.base.Objects;
036import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos;
037
038/**
039 * This class is used for exporting current state of load on a RegionServer.
040 *
041 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
042 *             Use {@link ServerMetrics} instead.
043 */
044@InterfaceAudience.Public
045@Deprecated
046public class ServerLoad implements ServerMetrics {
047  private final ServerMetrics metrics;
048  private int stores = 0;
049  private int storefiles = 0;
050  private int storeUncompressedSizeMB = 0;
051  private int storefileSizeMB = 0;
052  private int memstoreSizeMB = 0;
053  private long storefileIndexSizeKB = 0;
054  private long readRequestsCount = 0;
055  private long filteredReadRequestsCount = 0;
056  private long writeRequestsCount = 0;
057  private int rootIndexSizeKB = 0;
058  private int totalStaticIndexSizeKB = 0;
059  private int totalStaticBloomSizeKB = 0;
060  private long totalCompactingKVs = 0;
061  private long currentCompactedKVs = 0;
062
063  /**
064   * DONT USE this construction. It make a fake server name;
065   */
066  @InterfaceAudience.Private
067  public ServerLoad(ClusterStatusProtos.ServerLoad serverLoad) {
068    this(ServerName.valueOf("localhost,1,1"), serverLoad);
069  }
070
071  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
072  @InterfaceAudience.Private
073  public ServerLoad(ServerName name, ClusterStatusProtos.ServerLoad serverLoad) {
074    this(ServerMetricsBuilder.toServerMetrics(name, serverLoad));
075    this.serverLoad = serverLoad;
076  }
077
078  @InterfaceAudience.Private
079  public ServerLoad(ServerMetrics metrics) {
080    this.metrics = metrics;
081    this.serverLoad = ServerMetricsBuilder.toServerLoad(metrics);
082    for (RegionMetrics rl : metrics.getRegionMetrics().values()) {
083      stores += rl.getStoreCount();
084      storefiles += rl.getStoreFileCount();
085      storeUncompressedSizeMB += rl.getUncompressedStoreFileSize().get(Size.Unit.MEGABYTE);
086      storefileSizeMB += rl.getStoreFileSize().get(Size.Unit.MEGABYTE);
087      memstoreSizeMB += rl.getMemStoreSize().get(Size.Unit.MEGABYTE);
088      readRequestsCount += rl.getReadRequestCount();
089      filteredReadRequestsCount += rl.getFilteredReadRequestCount();
090      writeRequestsCount += rl.getWriteRequestCount();
091      storefileIndexSizeKB += rl.getStoreFileIndexSize().get(Size.Unit.KILOBYTE);
092      rootIndexSizeKB += rl.getStoreFileRootLevelIndexSize().get(Size.Unit.KILOBYTE);
093      totalStaticIndexSizeKB += rl.getStoreFileUncompressedDataIndexSize().get(Size.Unit.KILOBYTE);
094      totalStaticBloomSizeKB += rl.getBloomFilterSize().get(Size.Unit.KILOBYTE);
095      totalCompactingKVs += rl.getCompactingCellCount();
096      currentCompactedKVs += rl.getCompactedCellCount();
097    }
098  }
099
100  /**
101   * NOTE: Function name cannot start with "get" because then an OpenDataException is thrown because
102   * HBaseProtos.ServerLoad cannot be converted to an open data type(see HBASE-5967).
103   * @return the underlying ServerLoad protobuf object
104   * @deprecated DONT use this pb object since the byte array backed may be modified in rpc layer
105   */
106  @InterfaceAudience.Private
107  @Deprecated
108  public ClusterStatusProtos.ServerLoad obtainServerLoadPB() {
109    return serverLoad;
110  }
111
112  protected ClusterStatusProtos.ServerLoad serverLoad;
113
114  /**
115   * @return number of requests  since last report.
116   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0.
117   *             Use {@link #getRequestCountPerSecond} instead.
118   */
119  @Deprecated
120  public long getNumberOfRequests() {
121    return getRequestCountPerSecond();
122  }
123
124  /**
125   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
126   *             No flag in 2.0
127   */
128  @Deprecated
129  public boolean hasNumberOfRequests() {
130    return true;
131  }
132
133  /**
134   * @return total Number of requests from the start of the region server.
135   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0.
136   *             Use {@link #getRequestCount} instead.
137   */
138  @Deprecated
139  public long getTotalNumberOfRequests() {
140    return getRequestCount();
141  }
142
143  /**
144   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
145   *             No flag in 2.0
146   */
147  @Deprecated
148  public boolean hasTotalNumberOfRequests() {
149    return true;
150  }
151
152  /**
153   * @return the amount of used heap, in MB.
154   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0.
155   *             Use {@link #getUsedHeapSize} instead.
156   */
157  @Deprecated
158  public int getUsedHeapMB() {
159    return (int) getUsedHeapSize().get(Size.Unit.MEGABYTE);
160  }
161
162  /**
163   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
164   *             No flag in 2.0
165   */
166  @Deprecated
167  public boolean hasUsedHeapMB() {
168    return true;
169  }
170
171  /**
172   * @return the maximum allowable size of the heap, in MB.
173   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
174   *             Use {@link #getMaxHeapSize} instead.
175   */
176  @Deprecated
177  public int getMaxHeapMB() {
178    return (int) getMaxHeapSize().get(Size.Unit.MEGABYTE);
179  }
180
181  /**
182   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
183   *             No flag in 2.0
184   */
185  @Deprecated
186  public boolean hasMaxHeapMB() {
187    return true;
188  }
189
190  /**
191   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
192   *             Use {@link #getRegionMetrics} instead.
193   */
194  @Deprecated
195  public int getStores() {
196    return stores;
197  }
198
199  /**
200   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0.
201   *             Use {@link #getRegionMetrics} instead.
202   */
203  @Deprecated
204  public int getStorefiles() {
205    return storefiles;
206  }
207
208  /**
209   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
210   *             Use {@link #getRegionMetrics} instead.
211   */
212  @Deprecated
213  public int getStoreUncompressedSizeMB() {
214    return storeUncompressedSizeMB;
215  }
216
217  /**
218   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
219   *             Use {@link #getRegionMetrics} instead.
220   */
221  @Deprecated
222  public int getStorefileSizeInMB() {
223    return storefileSizeMB;
224  }
225
226  /**
227   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
228   *             Use {@link #getRegionMetrics} instead.
229   */
230  @Deprecated
231  public int getStorefileSizeMB() {
232    return storefileSizeMB;
233  }
234
235  /**
236   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
237   *             Use {@link #getRegionMetrics} instead.
238   */
239  @Deprecated
240  public int getMemstoreSizeInMB() {
241    return memstoreSizeMB;
242  }
243
244  /**
245   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
246   *     Use {@link #getRegionMetrics} instead.
247   */
248  @Deprecated
249  public int getMemStoreSizeMB() {
250    return memstoreSizeMB;
251  }
252
253  /**
254   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
255   *     Use {@link #getRegionMetrics} instead.
256   */
257  @Deprecated
258  public int getStorefileIndexSizeInMB() {
259    // Return value divided by 1024
260    return (int) (getStorefileIndexSizeKB() >> 10);
261  }
262
263  /**
264   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
265   *     Use {@link #getRegionMetrics} instead.
266   */
267  @Deprecated
268  public long getStorefileIndexSizeKB() {
269    return storefileIndexSizeKB;
270  }
271
272  /**
273   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
274   *     Use {@link #getRegionMetrics} instead.
275   */
276  @Deprecated
277  public long getReadRequestsCount() {
278    return readRequestsCount;
279  }
280
281  /**
282   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
283   *     Use {@link #getRegionMetrics} instead.
284   */
285  @Deprecated
286  public long getFilteredReadRequestsCount() {
287    return filteredReadRequestsCount;
288  }
289
290  /**
291   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
292   *     Use {@link #getRegionMetrics} instead.
293   */
294  @Deprecated
295  public long getWriteRequestsCount() {
296    return writeRequestsCount;
297  }
298
299  /**
300   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
301   *     Use {@link #getRegionMetrics} instead.
302   */
303  @Deprecated
304  public int getRootIndexSizeKB() {
305    return rootIndexSizeKB;
306  }
307
308  /**
309   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
310   *     Use {@link #getRegionMetrics} instead.
311   */
312  @Deprecated
313  public int getTotalStaticIndexSizeKB() {
314    return totalStaticIndexSizeKB;
315  }
316
317  /**
318   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
319   *     Use {@link #getRegionMetrics} instead.
320   */
321  @Deprecated
322  public int getTotalStaticBloomSizeKB() {
323    return totalStaticBloomSizeKB;
324  }
325
326  /**
327   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
328   *     Use {@link #getRegionMetrics} instead.
329   */
330  @Deprecated
331  public long getTotalCompactingKVs() {
332    return totalCompactingKVs;
333  }
334
335  /**
336   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
337   *     Use {@link #getRegionMetrics} instead.
338   */
339  @Deprecated
340  public long getCurrentCompactedKVs() {
341    return currentCompactedKVs;
342  }
343
344  /**
345   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
346   *             Use {@link #getRegionMetrics} instead.
347   */
348  @Deprecated
349  public int getNumberOfRegions() {
350    return metrics.getRegionMetrics().size();
351  }
352
353  @Override
354  public ServerName getServerName() {
355    return metrics.getServerName();
356  }
357
358  @Override
359  public long getRequestCountPerSecond() {
360    return metrics.getRequestCountPerSecond();
361  }
362
363  @Override
364  public long getRequestCount() {
365    return metrics.getRequestCount();
366  }
367
368  @Override
369  public Size getUsedHeapSize() {
370    return metrics.getUsedHeapSize();
371  }
372
373  @Override
374  public Size getMaxHeapSize() {
375    return metrics.getMaxHeapSize();
376  }
377
378  @Override
379  public int getInfoServerPort() {
380    return metrics.getInfoServerPort();
381  }
382
383  /**
384   * Call directly from client such as hbase shell
385   * @return the list of ReplicationLoadSource
386   */
387  @Override
388  public List<ReplicationLoadSource> getReplicationLoadSourceList() {
389    return metrics.getReplicationLoadSourceList();
390  }
391
392  /**
393   * Call directly from client such as hbase shell
394   * @return a map of ReplicationLoadSource list per peer id
395   */
396  @Override
397  public Map<String, List<ReplicationLoadSource>> getReplicationLoadSourceMap() {
398    return metrics.getReplicationLoadSourceMap();
399  }
400
401  /**
402   * Call directly from client such as hbase shell
403   * @return ReplicationLoadSink
404   */
405  @Override
406  public ReplicationLoadSink getReplicationLoadSink() {
407    return metrics.getReplicationLoadSink();
408  }
409
410  @Override
411  public Map<byte[], RegionMetrics> getRegionMetrics() {
412    return metrics.getRegionMetrics();
413  }
414
415  @Override public Map<byte[], UserMetrics> getUserMetrics() {
416    return metrics.getUserMetrics();
417  }
418
419  @Override
420  public Set<String> getCoprocessorNames() {
421    return metrics.getCoprocessorNames();
422  }
423
424  @Override
425  public long getReportTimestamp() {
426    return metrics.getReportTimestamp();
427  }
428
429  @Override
430  public long getLastReportTimestamp() {
431    return metrics.getLastReportTimestamp();
432  }
433
434  /**
435   * Originally, this method factored in the effect of requests going to the
436   * server as well. However, this does not interact very well with the current
437   * region rebalancing code, which only factors number of regions. For the
438   * interim, until we can figure out how to make rebalancing use all the info
439   * available, we're just going to make load purely the number of regions.
440   *
441   * @return load factor for this server.
442   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
443   *             Use {@link #getNumberOfRegions} instead.
444   */
445  @Deprecated
446  public int getLoad() {
447    // See above comment
448    // int load = numberOfRequests == 0 ? 1 : numberOfRequests;
449    // load *= numberOfRegions == 0 ? 1 : numberOfRegions;
450    // return load;
451    return getNumberOfRegions();
452  }
453
454  /**
455   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
456   *             Use {@link #getRegionMetrics} instead.
457   */
458  @Deprecated
459  public Map<byte[], RegionLoad> getRegionsLoad() {
460    return getRegionMetrics().entrySet().stream()
461        .collect(Collectors.toMap(Map.Entry::getKey, e -> new RegionLoad(e.getValue()),
462          (v1, v2) -> {
463            throw new RuntimeException("key collisions?");
464          }, () -> new TreeMap<>(Bytes.BYTES_COMPARATOR)));
465  }
466
467  /**
468   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
469   *             Use {@link #getCoprocessorNames} instead.
470   */
471  @Deprecated
472  public String[] getRegionServerCoprocessors() {
473    return getCoprocessorNames().toArray(new String[getCoprocessorNames().size()]);
474  }
475
476  /**
477   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
478   *             Use {@link #getCoprocessorNames} instead.
479   */
480  @Deprecated
481  public String[] getRsCoprocessors() {
482    return getRegionServerCoprocessors();
483  }
484
485  /**
486   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
487   *             Use {@link #getRequestCountPerSecond} instead.
488   */
489  @Deprecated
490  public double getRequestsPerSecond() {
491    return getRequestCountPerSecond();
492  }
493
494  /**
495   * @see java.lang.Object#toString()
496   */
497  @Override
498  public String toString() {
499    StringBuilder sb = Strings.appendKeyValue(new StringBuilder(), "requestsPerSecond",
500      Double.valueOf(getRequestsPerSecond()));
501    Strings.appendKeyValue(sb, "numberOfOnlineRegions", Integer.valueOf(getNumberOfRegions()));
502    Strings.appendKeyValue(sb, "usedHeapMB", Integer.valueOf(this.getUsedHeapMB()));
503    Strings.appendKeyValue(sb, "maxHeapMB", Integer.valueOf(getMaxHeapMB()));
504    Strings.appendKeyValue(sb, "numberOfStores", Integer.valueOf(this.stores));
505    Strings.appendKeyValue(sb, "numberOfStorefiles", Integer.valueOf(this.storefiles));
506    Strings.appendKeyValue(sb, "storefileUncompressedSizeMB",
507        Integer.valueOf(this.storeUncompressedSizeMB));
508    Strings.appendKeyValue(sb, "storefileSizeMB", Integer.valueOf(this.storefileSizeMB));
509    if (this.storeUncompressedSizeMB != 0) {
510      Strings.appendKeyValue(sb, "compressionRatio", String.format("%.4f",
511          (float) this.storefileSizeMB / (float) this.storeUncompressedSizeMB));
512    }
513    Strings.appendKeyValue(sb, "memstoreSizeMB", Integer.valueOf(this.memstoreSizeMB));
514    Strings.appendKeyValue(sb, "storefileIndexSizeKB",
515        Long.valueOf(this.storefileIndexSizeKB));
516    Strings.appendKeyValue(sb, "readRequestsCount", Long.valueOf(this.readRequestsCount));
517    Strings.appendKeyValue(sb, "filteredReadRequestsCount",
518        Long.valueOf(this.filteredReadRequestsCount));
519    Strings.appendKeyValue(sb, "writeRequestsCount", Long.valueOf(this.writeRequestsCount));
520    Strings.appendKeyValue(sb, "rootIndexSizeKB", Integer.valueOf(this.rootIndexSizeKB));
521    Strings.appendKeyValue(sb, "totalStaticIndexSizeKB",
522        Integer.valueOf(this.totalStaticIndexSizeKB));
523    Strings.appendKeyValue(sb, "totalStaticBloomSizeKB",
524        Integer.valueOf(this.totalStaticBloomSizeKB));
525    Strings.appendKeyValue(sb, "totalCompactingKVs", Long.valueOf(this.totalCompactingKVs));
526    Strings.appendKeyValue(sb, "currentCompactedKVs", Long.valueOf(this.currentCompactedKVs));
527    float compactionProgressPct = Float.NaN;
528    if (this.totalCompactingKVs > 0) {
529      compactionProgressPct =
530          Float.valueOf((float) this.currentCompactedKVs / this.totalCompactingKVs);
531    }
532    Strings.appendKeyValue(sb, "compactionProgressPct", compactionProgressPct);
533
534    String[] coprocessorStrings = getRsCoprocessors();
535    if (coprocessorStrings != null) {
536      Strings.appendKeyValue(sb, "coprocessors", Arrays.toString(coprocessorStrings));
537    }
538    return sb.toString();
539  }
540
541  /**
542   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
543   *             Use {@link ServerMetricsBuilder#of(ServerName)} instead.
544   */
545  @Deprecated
546  public static final ServerLoad EMPTY_SERVERLOAD =
547      new ServerLoad(ServerName.valueOf("localhost,1,1"),
548          ClusterStatusProtos.ServerLoad.newBuilder().build());
549
550  /**
551   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
552   *             Use {@link #getReportTimestamp} instead.
553   */
554  @Deprecated
555  public long getReportTime() {
556    return getReportTimestamp();
557  }
558
559  @Override
560  public int hashCode() {
561    return Objects
562        .hashCode(stores, storefiles, storeUncompressedSizeMB, storefileSizeMB, memstoreSizeMB,
563            storefileIndexSizeKB, readRequestsCount, filteredReadRequestsCount, writeRequestsCount,
564            rootIndexSizeKB, totalStaticIndexSizeKB, totalStaticBloomSizeKB, totalCompactingKVs,
565            currentCompactedKVs);
566  }
567
568  @Override
569  public boolean equals(Object other) {
570    if (other == this) return true;
571    if (other instanceof ServerLoad) {
572      ServerLoad sl = ((ServerLoad) other);
573      return stores == sl.stores && storefiles == sl.storefiles
574          && storeUncompressedSizeMB == sl.storeUncompressedSizeMB
575          && storefileSizeMB == sl.storefileSizeMB && memstoreSizeMB == sl.memstoreSizeMB
576          && storefileIndexSizeKB == sl.storefileIndexSizeKB
577          && readRequestsCount == sl.readRequestsCount
578          && filteredReadRequestsCount == sl.filteredReadRequestsCount
579          && writeRequestsCount == sl.writeRequestsCount && rootIndexSizeKB == sl.rootIndexSizeKB
580          && totalStaticIndexSizeKB == sl.totalStaticIndexSizeKB
581          && totalStaticBloomSizeKB == sl.totalStaticBloomSizeKB
582          && totalCompactingKVs == sl.totalCompactingKVs
583          && currentCompactedKVs == sl.currentCompactedKVs;
584    }
585    return false;
586  }
587}