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