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.List;
024import java.util.Map;
025import java.util.stream.Collectors;
026import org.apache.hadoop.hbase.util.Strings;
027import org.apache.yetus.audience.InterfaceAudience;
028
029import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations;
030import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos;
031
032/**
033 * Encapsulates per-region load metrics.
034 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
035 *             Use {@link RegionMetrics} instead.
036 */
037@InterfaceAudience.Public
038@Deprecated
039public class RegionLoad implements RegionMetrics {
040  // DONT use this pb object since the byte array backed may be modified in rpc layer
041  // we keep this pb object for BC.
042  protected ClusterStatusProtos.RegionLoad regionLoadPB;
043  private final RegionMetrics metrics;
044
045  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
046  public RegionLoad(ClusterStatusProtos.RegionLoad regionLoadPB) {
047    this.regionLoadPB = regionLoadPB;
048    this.metrics = RegionMetricsBuilder.toRegionMetrics(regionLoadPB);
049  }
050
051  RegionLoad(RegionMetrics metrics) {
052    this.metrics = metrics;
053    this.regionLoadPB = RegionMetricsBuilder.toRegionLoad(metrics);
054  }
055
056  /**
057   * @return the region name
058   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
059   *             Use {@link #getRegionName} instead.
060   */
061  @Deprecated
062  public byte[] getName() {
063    return metrics.getRegionName();
064  }
065
066  @Override
067  public byte[] getRegionName() {
068    return metrics.getRegionName();
069  }
070
071  @Override
072  public int getStoreCount() {
073    return metrics.getStoreCount();
074  }
075
076  @Override
077  public int getStoreFileCount() {
078    return metrics.getStoreFileCount();
079  }
080
081  @Override
082  public Size getStoreFileSize() {
083    return metrics.getStoreFileSize();
084  }
085
086  @Override
087  public Size getMemStoreSize() {
088    return metrics.getMemStoreSize();
089  }
090
091  @Override
092  public long getReadRequestCount() {
093    return metrics.getReadRequestCount();
094  }
095
096  @Override
097  public long getFilteredReadRequestCount() {
098    return metrics.getFilteredReadRequestCount();
099  }
100
101  @Override
102  public Size getStoreFileIndexSize() {
103    return metrics.getStoreFileIndexSize();
104  }
105
106  @Override
107  public long getWriteRequestCount() {
108    return metrics.getWriteRequestCount();
109  }
110
111  @Override
112  public Size getStoreFileRootLevelIndexSize() {
113    return metrics.getStoreFileRootLevelIndexSize();
114  }
115
116  @Override
117  public Size getStoreFileUncompressedDataIndexSize() {
118    return metrics.getStoreFileUncompressedDataIndexSize();
119  }
120
121  @Override
122  public Size getBloomFilterSize() {
123    return metrics.getBloomFilterSize();
124  }
125
126  @Override
127  public long getCompactingCellCount() {
128    return metrics.getCompactingCellCount();
129  }
130
131  @Override
132  public long getCompactedCellCount() {
133    return metrics.getCompactedCellCount();
134  }
135
136  @Override
137  public long getCompletedSequenceId() {
138    return metrics.getCompletedSequenceId();
139  }
140
141  @Override
142  public Map<byte[], Long> getStoreSequenceId() {
143    return metrics.getStoreSequenceId();
144  }
145
146  @Override
147  public Size getUncompressedStoreFileSize() {
148    return metrics.getUncompressedStoreFileSize();
149  }
150
151  /**
152   * @return the number of stores
153   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
154   *             Use {@link #getStoreCount} instead.
155   */
156  @Deprecated
157  public int getStores() {
158    return metrics.getStoreCount();
159  }
160
161  /**
162   * @return the number of storefiles
163   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
164   *             Use {@link #getStoreFileCount} instead.
165   */
166  @Deprecated
167  public int getStorefiles() {
168    return metrics.getStoreFileCount();
169  }
170
171  /**
172   * @return the total size of the storefiles, in MB
173   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
174   *             Use {@link #getStoreFileSize} instead.
175   */
176  @Deprecated
177  public int getStorefileSizeMB() {
178    return (int) metrics.getStoreFileSize().get(Size.Unit.MEGABYTE);
179  }
180
181  /**
182   * @return the memstore size, in MB
183   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
184   *             Use {@link #getMemStoreSize} instead.
185   */
186  @Deprecated
187  public int getMemStoreSizeMB() {
188    return (int) metrics.getMemStoreSize().get(Size.Unit.MEGABYTE);
189  }
190
191  /**
192   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
193   *             ((<a href="https://issues.apache.org/jira/browse/HBASE-3935">HBASE-3935</a>)).
194   *             Use {@link #getStoreFileRootLevelIndexSize} instead.
195   */
196  @Deprecated
197  public int getStorefileIndexSizeMB() {
198    // Return value divided by 1024
199    return (getRootIndexSizeKB() >> 10);
200  }
201
202  /**
203   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
204   *             Use {@link #getStoreFileRootLevelIndexSize()} instead.
205   */
206  @Deprecated
207  public int getStorefileIndexSizeKB() {
208    return getRootIndexSizeKB();
209  }
210
211  /**
212   * @return the number of requests made to region
213   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
214   *             Use {@link #getRequestCount()} instead.
215   */
216  @Deprecated
217  public long getRequestsCount() {
218    return metrics.getRequestCount();
219  }
220
221  /**
222   * @return the number of read requests made to region
223   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
224   *             Use {@link #getReadRequestCount} instead.
225   */
226  @Deprecated
227  public long getReadRequestsCount() {
228    return metrics.getReadRequestCount();
229  }
230
231  /**
232   * @return the number of filtered read requests made to region
233   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
234   *             Use {@link #getFilteredReadRequestCount} instead.
235   */
236  @Deprecated
237  public long getFilteredReadRequestsCount() {
238    return metrics.getFilteredReadRequestCount();
239  }
240
241  /**
242   * @return the number of write requests made to region
243   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
244   *             Use {@link #getWriteRequestCount} instead.
245   */
246  @Deprecated
247  public long getWriteRequestsCount() {
248    return metrics.getWriteRequestCount();
249  }
250
251  /**
252   * @return The current total size of root-level indexes for the region, in KB.
253   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
254   *             Use {@link #getStoreFileRootLevelIndexSize} instead.
255   */
256  @Deprecated
257  public int getRootIndexSizeKB() {
258    return (int) metrics.getStoreFileRootLevelIndexSize().get(Size.Unit.KILOBYTE);
259  }
260
261  /**
262   * @return The total size of all index blocks, not just the root level, in KB.
263   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
264   *             Use {@link #getStoreFileUncompressedDataIndexSize} instead.
265   */
266  @Deprecated
267  public int getTotalStaticIndexSizeKB() {
268    return (int) metrics.getStoreFileUncompressedDataIndexSize().get(Size.Unit.KILOBYTE);
269  }
270
271  /**
272   * @return The total size of all Bloom filter blocks, not just loaded into the
273   * block cache, in KB.
274   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
275   *             Use {@link #getBloomFilterSize} instead.
276   */
277  @Deprecated
278  public int getTotalStaticBloomSizeKB() {
279    return (int) metrics.getBloomFilterSize().get(Size.Unit.KILOBYTE);
280  }
281
282  /**
283   * @return the total number of kvs in current compaction
284   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
285   *             Use {@link #getCompactingCellCount} instead.
286   */
287  @Deprecated
288  public long getTotalCompactingKVs() {
289    return metrics.getCompactingCellCount();
290  }
291
292  /**
293   * @return the number of already compacted kvs in current compaction
294   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
295   *             Use {@link #getCompactedCellCount} instead.
296   */
297  @Deprecated
298  public long getCurrentCompactedKVs() {
299    return metrics.getCompactedCellCount();
300  }
301
302  /**
303   * This does not really belong inside RegionLoad but its being done in the name of expediency.
304   * @return the completed sequence Id for the region
305   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
306   *             Use {@link #getCompletedSequenceId} instead.
307   */
308  @Deprecated
309  public long getCompleteSequenceId() {
310    return metrics.getCompletedSequenceId();
311  }
312
313  /**
314   * @return completed sequence id per store.
315   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
316   *             Use {@link #getStoreSequenceId} instead.
317   */
318  @Deprecated
319  public List<ClusterStatusProtos.StoreSequenceId> getStoreCompleteSequenceId() {
320    return metrics.getStoreSequenceId().entrySet().stream()
321        .map(s -> ClusterStatusProtos.StoreSequenceId.newBuilder()
322                  .setFamilyName(UnsafeByteOperations.unsafeWrap(s.getKey()))
323                  .setSequenceId(s.getValue())
324                  .build())
325        .collect(Collectors.toList());
326  }
327
328  /**
329   * @return the uncompressed size of the storefiles in MB.
330   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
331   *             Use {@link #getUncompressedStoreFileSize} instead.
332   */
333  @Deprecated
334  public int getStoreUncompressedSizeMB() {
335    return (int) metrics.getUncompressedStoreFileSize().get(Size.Unit.KILOBYTE);
336  }
337
338  /**
339   * @return the data locality of region in the regionserver.
340   */
341  @Override
342  public float getDataLocality() {
343    return metrics.getDataLocality();
344  }
345
346  @Override
347  public long getLastMajorCompactionTimestamp() {
348    return metrics.getLastMajorCompactionTimestamp();
349  }
350
351  /**
352   * @return the timestamp of the oldest hfile for any store of this region.
353   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
354   *             Use {@link #getLastMajorCompactionTimestamp} instead.
355   */
356  @Deprecated
357  public long getLastMajorCompactionTs() {
358    return metrics.getLastMajorCompactionTimestamp();
359  }
360
361  /**
362   * @return the reference count for the stores of this region
363   */
364  public int getStoreRefCount() {
365    return metrics.getStoreRefCount();
366  }
367
368  @Override
369  public int getMaxCompactedStoreFileRefCount() {
370    return metrics.getMaxCompactedStoreFileRefCount();
371  }
372
373  /**
374   * @see java.lang.Object#toString()
375   */
376  @Override
377  public String toString() {
378    StringBuilder sb = Strings.appendKeyValue(new StringBuilder(), "numberOfStores",
379        this.getStores());
380    Strings.appendKeyValue(sb, "numberOfStorefiles", this.getStorefiles());
381    Strings.appendKeyValue(sb, "storeRefCount", this.getStoreRefCount());
382    Strings.appendKeyValue(sb, "storefileUncompressedSizeMB",
383        this.getStoreUncompressedSizeMB());
384    Strings.appendKeyValue(sb, "lastMajorCompactionTimestamp",
385        this.getLastMajorCompactionTs());
386    Strings.appendKeyValue(sb, "storefileSizeMB", this.getStorefileSizeMB());
387    if (this.getStoreUncompressedSizeMB() != 0) {
388      Strings.appendKeyValue(sb, "compressionRatio",
389          String.format("%.4f", (float) this.getStorefileSizeMB() /
390              (float) this.getStoreUncompressedSizeMB()));
391    }
392    Strings.appendKeyValue(sb, "memstoreSizeMB",
393        this.getMemStoreSizeMB());
394    Strings.appendKeyValue(sb, "readRequestsCount",
395        this.getReadRequestsCount());
396    Strings.appendKeyValue(sb, "writeRequestsCount",
397        this.getWriteRequestsCount());
398    Strings.appendKeyValue(sb, "rootIndexSizeKB",
399        this.getRootIndexSizeKB());
400    Strings.appendKeyValue(sb, "totalStaticIndexSizeKB",
401        this.getTotalStaticIndexSizeKB());
402    Strings.appendKeyValue(sb, "totalStaticBloomSizeKB",
403        this.getTotalStaticBloomSizeKB());
404    Strings.appendKeyValue(sb, "totalCompactingKVs",
405        this.getTotalCompactingKVs());
406    Strings.appendKeyValue(sb, "currentCompactedKVs",
407        this.getCurrentCompactedKVs());
408    float compactionProgressPct = Float.NaN;
409    if (this.getTotalCompactingKVs() > 0) {
410      compactionProgressPct = ((float) this.getCurrentCompactedKVs() /
411          (float) this.getTotalCompactingKVs());
412    }
413    Strings.appendKeyValue(sb, "compactionProgressPct",
414        compactionProgressPct);
415    Strings.appendKeyValue(sb, "completeSequenceId",
416        this.getCompleteSequenceId());
417    Strings.appendKeyValue(sb, "dataLocality",
418        this.getDataLocality());
419    return sb.toString();
420  }
421}