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 ReplicationLoadSink
395   */
396  @Override
397  public ReplicationLoadSink getReplicationLoadSink() {
398    return metrics.getReplicationLoadSink();
399  }
400
401  @Override
402  public Map<byte[], RegionMetrics> getRegionMetrics() {
403    return metrics.getRegionMetrics();
404  }
405
406  @Override
407  public Set<String> getCoprocessorNames() {
408    return metrics.getCoprocessorNames();
409  }
410
411  @Override
412  public long getReportTimestamp() {
413    return metrics.getReportTimestamp();
414  }
415
416  @Override
417  public long getLastReportTimestamp() {
418    return metrics.getLastReportTimestamp();
419  }
420
421  /**
422   * Originally, this method factored in the effect of requests going to the
423   * server as well. However, this does not interact very well with the current
424   * region rebalancing code, which only factors number of regions. For the
425   * interim, until we can figure out how to make rebalancing use all the info
426   * available, we're just going to make load purely the number of regions.
427   *
428   * @return load factor for this server.
429   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
430   *             Use {@link #getNumberOfRegions} instead.
431   */
432  @Deprecated
433  public int getLoad() {
434    // See above comment
435    // int load = numberOfRequests == 0 ? 1 : numberOfRequests;
436    // load *= numberOfRegions == 0 ? 1 : numberOfRegions;
437    // return load;
438    return getNumberOfRegions();
439  }
440
441  /**
442   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
443   *             Use {@link #getRegionMetrics} instead.
444   */
445  @Deprecated
446  public Map<byte[], RegionLoad> getRegionsLoad() {
447    return getRegionMetrics().entrySet().stream()
448        .collect(Collectors.toMap(Map.Entry::getKey, e -> new RegionLoad(e.getValue()),
449          (v1, v2) -> {
450            throw new RuntimeException("key collisions?");
451          }, () -> new TreeMap<>(Bytes.BYTES_COMPARATOR)));
452  }
453
454  /**
455   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
456   *             Use {@link #getCoprocessorNames} instead.
457   */
458  @Deprecated
459  public String[] getRegionServerCoprocessors() {
460    return getCoprocessorNames().toArray(new String[getCoprocessorNames().size()]);
461  }
462
463  /**
464   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
465   *             Use {@link #getCoprocessorNames} instead.
466   */
467  @Deprecated
468  public String[] getRsCoprocessors() {
469    return getRegionServerCoprocessors();
470  }
471
472  /**
473   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
474   *             Use {@link #getRequestCountPerSecond} instead.
475   */
476  @Deprecated
477  public double getRequestsPerSecond() {
478    return getRequestCountPerSecond();
479  }
480
481  /**
482   * @see java.lang.Object#toString()
483   */
484  @Override
485  public String toString() {
486    StringBuilder sb = Strings.appendKeyValue(new StringBuilder(), "requestsPerSecond",
487      Double.valueOf(getRequestsPerSecond()));
488    Strings.appendKeyValue(sb, "numberOfOnlineRegions", Integer.valueOf(getNumberOfRegions()));
489    Strings.appendKeyValue(sb, "usedHeapMB", Integer.valueOf(this.getUsedHeapMB()));
490    Strings.appendKeyValue(sb, "maxHeapMB", Integer.valueOf(getMaxHeapMB()));
491    Strings.appendKeyValue(sb, "numberOfStores", Integer.valueOf(this.stores));
492    Strings.appendKeyValue(sb, "numberOfStorefiles", Integer.valueOf(this.storefiles));
493    Strings.appendKeyValue(sb, "storefileUncompressedSizeMB",
494        Integer.valueOf(this.storeUncompressedSizeMB));
495    Strings.appendKeyValue(sb, "storefileSizeMB", Integer.valueOf(this.storefileSizeMB));
496    if (this.storeUncompressedSizeMB != 0) {
497      Strings.appendKeyValue(sb, "compressionRatio", String.format("%.4f",
498          (float) this.storefileSizeMB / (float) this.storeUncompressedSizeMB));
499    }
500    Strings.appendKeyValue(sb, "memstoreSizeMB", Integer.valueOf(this.memstoreSizeMB));
501    Strings.appendKeyValue(sb, "storefileIndexSizeKB",
502        Long.valueOf(this.storefileIndexSizeKB));
503    Strings.appendKeyValue(sb, "readRequestsCount", Long.valueOf(this.readRequestsCount));
504    Strings.appendKeyValue(sb, "filteredReadRequestsCount",
505        Long.valueOf(this.filteredReadRequestsCount));
506    Strings.appendKeyValue(sb, "writeRequestsCount", Long.valueOf(this.writeRequestsCount));
507    Strings.appendKeyValue(sb, "rootIndexSizeKB", Integer.valueOf(this.rootIndexSizeKB));
508    Strings.appendKeyValue(sb, "totalStaticIndexSizeKB",
509        Integer.valueOf(this.totalStaticIndexSizeKB));
510    Strings.appendKeyValue(sb, "totalStaticBloomSizeKB",
511        Integer.valueOf(this.totalStaticBloomSizeKB));
512    Strings.appendKeyValue(sb, "totalCompactingKVs", Long.valueOf(this.totalCompactingKVs));
513    Strings.appendKeyValue(sb, "currentCompactedKVs", Long.valueOf(this.currentCompactedKVs));
514    float compactionProgressPct = Float.NaN;
515    if (this.totalCompactingKVs > 0) {
516      compactionProgressPct =
517          Float.valueOf((float) this.currentCompactedKVs / this.totalCompactingKVs);
518    }
519    Strings.appendKeyValue(sb, "compactionProgressPct", compactionProgressPct);
520
521    String[] coprocessorStrings = getRsCoprocessors();
522    if (coprocessorStrings != null) {
523      Strings.appendKeyValue(sb, "coprocessors", Arrays.toString(coprocessorStrings));
524    }
525    return sb.toString();
526  }
527
528  /**
529   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
530   *             Use {@link ServerMetricsBuilder#of(ServerName)} instead.
531   */
532  @Deprecated
533  public static final ServerLoad EMPTY_SERVERLOAD =
534      new ServerLoad(ServerName.valueOf("localhost,1,1"),
535          ClusterStatusProtos.ServerLoad.newBuilder().build());
536
537  /**
538   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
539   *             Use {@link #getReportTimestamp} instead.
540   */
541  @Deprecated
542  public long getReportTime() {
543    return getReportTimestamp();
544  }
545
546  @Override
547  public int hashCode() {
548    return Objects
549        .hashCode(stores, storefiles, storeUncompressedSizeMB, storefileSizeMB, memstoreSizeMB,
550            storefileIndexSizeKB, readRequestsCount, filteredReadRequestsCount, writeRequestsCount,
551            rootIndexSizeKB, totalStaticIndexSizeKB, totalStaticBloomSizeKB, totalCompactingKVs,
552            currentCompactedKVs);
553  }
554
555  @Override
556  public boolean equals(Object other) {
557    if (other == this) return true;
558    if (other instanceof ServerLoad) {
559      ServerLoad sl = ((ServerLoad) other);
560      return stores == sl.stores && storefiles == sl.storefiles
561          && storeUncompressedSizeMB == sl.storeUncompressedSizeMB
562          && storefileSizeMB == sl.storefileSizeMB && memstoreSizeMB == sl.memstoreSizeMB
563          && storefileIndexSizeKB == sl.storefileIndexSizeKB
564          && readRequestsCount == sl.readRequestsCount
565          && filteredReadRequestsCount == sl.filteredReadRequestsCount
566          && writeRequestsCount == sl.writeRequestsCount && rootIndexSizeKB == sl.rootIndexSizeKB
567          && totalStaticIndexSizeKB == sl.totalStaticIndexSizeKB
568          && totalStaticBloomSizeKB == sl.totalStaticBloomSizeKB
569          && totalCompactingKVs == sl.totalCompactingKVs
570          && currentCompactedKVs == sl.currentCompactedKVs;
571    }
572    return false;
573  }
574}