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
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  @Override
434  public Map<String, Integer> getRegionCachedInfo() {
435    return metrics.getRegionCachedInfo();
436  }
437
438  /**
439   * Originally, this method factored in the effect of requests going to the server as well.
440   * However, this does not interact very well with the current region rebalancing code, which only
441   * factors number of regions. For the interim, until we can figure out how to make rebalancing use
442   * all the info available, we're just going to make load purely the number of regions.
443   * @return load factor for this server.
444   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0 Use
445   *             {@link #getNumberOfRegions} instead.
446   */
447  @Deprecated
448  public int getLoad() {
449    // See above comment
450    // int load = numberOfRequests == 0 ? 1 : numberOfRequests;
451    // load *= numberOfRegions == 0 ? 1 : numberOfRegions;
452    // return load;
453    return getNumberOfRegions();
454  }
455
456  /**
457   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0 Use
458   *             {@link #getRegionMetrics} instead.
459   */
460  @Deprecated
461  public Map<byte[], RegionLoad> getRegionsLoad() {
462    return getRegionMetrics().entrySet().stream()
463      .collect(Collectors.toMap(Map.Entry::getKey, e -> new RegionLoad(e.getValue()), (v1, v2) -> {
464        throw new RuntimeException("key collisions?");
465      }, () -> new TreeMap<>(Bytes.BYTES_COMPARATOR)));
466  }
467
468  /**
469   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0 Use
470   *             {@link #getCoprocessorNames} instead.
471   */
472  @Deprecated
473  public String[] getRegionServerCoprocessors() {
474    return getCoprocessorNames().toArray(new String[getCoprocessorNames().size()]);
475  }
476
477  /**
478   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0 Use
479   *             {@link #getCoprocessorNames} instead.
480   */
481  @Deprecated
482  public String[] getRsCoprocessors() {
483    return getRegionServerCoprocessors();
484  }
485
486  /**
487   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0 Use
488   *             {@link #getRequestCountPerSecond} instead.
489   */
490  @Deprecated
491  public double getRequestsPerSecond() {
492    return getRequestCountPerSecond();
493  }
494
495  /**
496   * @see java.lang.Object#toString()
497   */
498  @Override
499  public String toString() {
500    StringBuilder sb = Strings.appendKeyValue(new StringBuilder(), "requestsPerSecond",
501      Double.valueOf(getRequestsPerSecond()));
502    Strings.appendKeyValue(sb, "numberOfOnlineRegions", Integer.valueOf(getNumberOfRegions()));
503    Strings.appendKeyValue(sb, "usedHeapMB", Integer.valueOf(this.getUsedHeapMB()));
504    Strings.appendKeyValue(sb, "maxHeapMB", Integer.valueOf(getMaxHeapMB()));
505    Strings.appendKeyValue(sb, "numberOfStores", Integer.valueOf(this.stores));
506    Strings.appendKeyValue(sb, "numberOfStorefiles", Integer.valueOf(this.storefiles));
507    Strings.appendKeyValue(sb, "storefileUncompressedSizeMB",
508      Integer.valueOf(this.storeUncompressedSizeMB));
509    Strings.appendKeyValue(sb, "storefileSizeMB", Integer.valueOf(this.storefileSizeMB));
510    if (this.storeUncompressedSizeMB != 0) {
511      Strings.appendKeyValue(sb, "compressionRatio",
512        String.format("%.4f", (float) this.storefileSizeMB / (float) this.storeUncompressedSizeMB));
513    }
514    Strings.appendKeyValue(sb, "memstoreSizeMB", Integer.valueOf(this.memstoreSizeMB));
515    Strings.appendKeyValue(sb, "storefileIndexSizeKB", 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 Use
543   *             {@link ServerMetricsBuilder#of(ServerName)} instead.
544   */
545  @Deprecated
546  public static final ServerLoad EMPTY_SERVERLOAD = new ServerLoad(
547    ServerName.valueOf("localhost,1,1"), ClusterStatusProtos.ServerLoad.newBuilder().build());
548
549  /**
550   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0 Use
551   *             {@link #getReportTimestamp} instead.
552   */
553  @Deprecated
554  public long getReportTime() {
555    return getReportTimestamp();
556  }
557
558  @Override
559  public int hashCode() {
560    return Objects.hashCode(stores, storefiles, storeUncompressedSizeMB, storefileSizeMB,
561      memstoreSizeMB, storefileIndexSizeKB, readRequestsCount, filteredReadRequestsCount,
562      writeRequestsCount, rootIndexSizeKB, totalStaticIndexSizeKB, totalStaticBloomSizeKB,
563      totalCompactingKVs, currentCompactedKVs);
564  }
565
566  @Override
567  public boolean equals(Object other) {
568    if (other == this) return true;
569    if (other instanceof ServerLoad) {
570      ServerLoad sl = ((ServerLoad) other);
571      return stores == sl.stores && storefiles == sl.storefiles
572        && storeUncompressedSizeMB == sl.storeUncompressedSizeMB
573        && storefileSizeMB == sl.storefileSizeMB && memstoreSizeMB == sl.memstoreSizeMB
574        && storefileIndexSizeKB == sl.storefileIndexSizeKB
575        && readRequestsCount == sl.readRequestsCount
576        && filteredReadRequestsCount == sl.filteredReadRequestsCount
577        && writeRequestsCount == sl.writeRequestsCount && rootIndexSizeKB == sl.rootIndexSizeKB
578        && totalStaticIndexSizeKB == sl.totalStaticIndexSizeKB
579        && totalStaticBloomSizeKB == sl.totalStaticBloomSizeKB
580        && totalCompactingKVs == sl.totalCompactingKVs
581        && currentCompactedKVs == sl.currentCompactedKVs;
582    }
583    return false;
584  }
585}