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