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.Collections;
021import java.util.List;
022import java.util.Map;
023import java.util.stream.Collectors;
024import org.apache.hadoop.hbase.client.CompactionState;
025import org.apache.hadoop.hbase.util.Strings;
026import org.apache.yetus.audience.InterfaceAudience;
027
028import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
029import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations;
030
031import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
032import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos;
033import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos;
034import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
035
036@InterfaceAudience.Private
037public final class RegionMetricsBuilder {
038
039  public static List<RegionMetrics>
040    toRegionMetrics(AdminProtos.GetRegionLoadResponse regionLoadResponse) {
041    return regionLoadResponse.getRegionLoadsList().stream()
042      .map(RegionMetricsBuilder::toRegionMetrics).collect(Collectors.toList());
043  }
044
045  public static RegionMetrics toRegionMetrics(ClusterStatusProtos.RegionLoad regionLoadPB) {
046    return RegionMetricsBuilder
047      .newBuilder(regionLoadPB.getRegionSpecifier().getValue().toByteArray())
048      .setBloomFilterSize(new Size(regionLoadPB.getTotalStaticBloomSizeKB(), Size.Unit.KILOBYTE))
049      .setCompactedCellCount(regionLoadPB.getCurrentCompactedKVs())
050      .setCompactingCellCount(regionLoadPB.getTotalCompactingKVs())
051      .setCompletedSequenceId(regionLoadPB.getCompleteSequenceId())
052      .setDataLocality(regionLoadPB.hasDataLocality() ? regionLoadPB.getDataLocality() : 0.0f)
053      .setDataLocalityForSsd(
054        regionLoadPB.hasDataLocalityForSsd() ? regionLoadPB.getDataLocalityForSsd() : 0.0f)
055      .setBlocksLocalWeight(
056        regionLoadPB.hasBlocksLocalWeight() ? regionLoadPB.getBlocksLocalWeight() : 0)
057      .setBlocksLocalWithSsdWeight(
058        regionLoadPB.hasBlocksLocalWithSsdWeight() ? regionLoadPB.getBlocksLocalWithSsdWeight() : 0)
059      .setBlocksTotalWeight(regionLoadPB.getBlocksTotalWeight())
060      .setCompactionState(
061        ProtobufUtil.createCompactionStateForRegionLoad(regionLoadPB.getCompactionState()))
062      .setFilteredReadRequestCount(regionLoadPB.getFilteredReadRequestsCount())
063      .setStoreFileUncompressedDataIndexSize(
064        new Size(regionLoadPB.getTotalStaticIndexSizeKB(), Size.Unit.KILOBYTE))
065      .setLastMajorCompactionTimestamp(regionLoadPB.getLastMajorCompactionTs())
066      .setMemStoreSize(new Size(regionLoadPB.getMemStoreSizeMB(), Size.Unit.MEGABYTE))
067      .setReadRequestCount(regionLoadPB.getReadRequestsCount())
068      .setWriteRequestCount(regionLoadPB.getWriteRequestsCount())
069      .setStoreFileIndexSize(new Size(regionLoadPB.getStorefileIndexSizeKB(), Size.Unit.KILOBYTE))
070      .setStoreFileRootLevelIndexSize(
071        new Size(regionLoadPB.getRootIndexSizeKB(), Size.Unit.KILOBYTE))
072      .setStoreCount(regionLoadPB.getStores()).setStoreFileCount(regionLoadPB.getStorefiles())
073      .setStoreRefCount(regionLoadPB.getStoreRefCount())
074      .setMaxCompactedStoreFileRefCount(regionLoadPB.getMaxCompactedStoreFileRefCount())
075      .setStoreFileSize(new Size(regionLoadPB.getStorefileSizeMB(), Size.Unit.MEGABYTE))
076      .setStoreSequenceIds(regionLoadPB.getStoreCompleteSequenceIdList().stream()
077        .collect(Collectors.toMap(
078          (ClusterStatusProtos.StoreSequenceId s) -> s.getFamilyName().toByteArray(),
079          ClusterStatusProtos.StoreSequenceId::getSequenceId)))
080      .setUncompressedStoreFileSize(
081        new Size(regionLoadPB.getStoreUncompressedSizeMB(), Size.Unit.MEGABYTE))
082      .build();
083  }
084
085  private static List<ClusterStatusProtos.StoreSequenceId>
086    toStoreSequenceId(Map<byte[], Long> ids) {
087    return ids.entrySet().stream()
088      .map(e -> ClusterStatusProtos.StoreSequenceId.newBuilder()
089        .setFamilyName(UnsafeByteOperations.unsafeWrap(e.getKey())).setSequenceId(e.getValue())
090        .build())
091      .collect(Collectors.toList());
092  }
093
094  public static ClusterStatusProtos.RegionLoad toRegionLoad(RegionMetrics regionMetrics) {
095    return ClusterStatusProtos.RegionLoad.newBuilder()
096      .setRegionSpecifier(HBaseProtos.RegionSpecifier.newBuilder()
097        .setType(HBaseProtos.RegionSpecifier.RegionSpecifierType.REGION_NAME)
098        .setValue(UnsafeByteOperations.unsafeWrap(regionMetrics.getRegionName())).build())
099      .setTotalStaticBloomSizeKB((int) regionMetrics.getBloomFilterSize().get(Size.Unit.KILOBYTE))
100      .setCurrentCompactedKVs(regionMetrics.getCompactedCellCount())
101      .setTotalCompactingKVs(regionMetrics.getCompactingCellCount())
102      .setCompleteSequenceId(regionMetrics.getCompletedSequenceId())
103      .setDataLocality(regionMetrics.getDataLocality())
104      .setFilteredReadRequestsCount(regionMetrics.getFilteredReadRequestCount())
105      .setTotalStaticIndexSizeKB(
106        (int) regionMetrics.getStoreFileUncompressedDataIndexSize().get(Size.Unit.KILOBYTE))
107      .setLastMajorCompactionTs(regionMetrics.getLastMajorCompactionTimestamp())
108      .setMemStoreSizeMB((int) regionMetrics.getMemStoreSize().get(Size.Unit.MEGABYTE))
109      .setReadRequestsCount(regionMetrics.getReadRequestCount())
110      .setWriteRequestsCount(regionMetrics.getWriteRequestCount())
111      .setStorefileIndexSizeKB((long) regionMetrics.getStoreFileIndexSize().get(Size.Unit.KILOBYTE))
112      .setRootIndexSizeKB(
113        (int) regionMetrics.getStoreFileRootLevelIndexSize().get(Size.Unit.KILOBYTE))
114      .setStores(regionMetrics.getStoreCount()).setStorefiles(regionMetrics.getStoreFileCount())
115      .setStoreRefCount(regionMetrics.getStoreRefCount())
116      .setMaxCompactedStoreFileRefCount(regionMetrics.getMaxCompactedStoreFileRefCount())
117      .setStorefileSizeMB((int) regionMetrics.getStoreFileSize().get(Size.Unit.MEGABYTE))
118      .addAllStoreCompleteSequenceId(toStoreSequenceId(regionMetrics.getStoreSequenceId()))
119      .setStoreUncompressedSizeMB(
120        (int) regionMetrics.getUncompressedStoreFileSize().get(Size.Unit.MEGABYTE))
121      .build();
122  }
123
124  public static RegionMetricsBuilder newBuilder(byte[] name) {
125    return new RegionMetricsBuilder(name);
126  }
127
128  private final byte[] name;
129  private int storeCount;
130  private int storeFileCount;
131  private int storeRefCount;
132  private int maxCompactedStoreFileRefCount;
133  private long compactingCellCount;
134  private long compactedCellCount;
135  private Size storeFileSize = Size.ZERO;
136  private Size memStoreSize = Size.ZERO;
137  private Size indexSize = Size.ZERO;
138  private Size rootLevelIndexSize = Size.ZERO;
139  private Size uncompressedDataIndexSize = Size.ZERO;
140  private Size bloomFilterSize = Size.ZERO;
141  private Size uncompressedStoreFileSize = Size.ZERO;
142  private long writeRequestCount;
143  private long readRequestCount;
144  private long filteredReadRequestCount;
145  private long completedSequenceId;
146  private Map<byte[], Long> storeSequenceIds = Collections.emptyMap();
147  private float dataLocality;
148  private long lastMajorCompactionTimestamp;
149  private float dataLocalityForSsd;
150  private long blocksLocalWeight;
151  private long blocksLocalWithSsdWeight;
152  private long blocksTotalWeight;
153  private CompactionState compactionState;
154
155  private RegionMetricsBuilder(byte[] name) {
156    this.name = name;
157  }
158
159  public RegionMetricsBuilder setStoreCount(int value) {
160    this.storeCount = value;
161    return this;
162  }
163
164  public RegionMetricsBuilder setStoreFileCount(int value) {
165    this.storeFileCount = value;
166    return this;
167  }
168
169  public RegionMetricsBuilder setStoreRefCount(int value) {
170    this.storeRefCount = value;
171    return this;
172  }
173
174  public RegionMetricsBuilder setMaxCompactedStoreFileRefCount(int value) {
175    this.maxCompactedStoreFileRefCount = value;
176    return this;
177  }
178
179  public RegionMetricsBuilder setCompactingCellCount(long value) {
180    this.compactingCellCount = value;
181    return this;
182  }
183
184  public RegionMetricsBuilder setCompactedCellCount(long value) {
185    this.compactedCellCount = value;
186    return this;
187  }
188
189  public RegionMetricsBuilder setStoreFileSize(Size value) {
190    this.storeFileSize = value;
191    return this;
192  }
193
194  public RegionMetricsBuilder setMemStoreSize(Size value) {
195    this.memStoreSize = value;
196    return this;
197  }
198
199  public RegionMetricsBuilder setStoreFileIndexSize(Size value) {
200    this.indexSize = value;
201    return this;
202  }
203
204  public RegionMetricsBuilder setStoreFileRootLevelIndexSize(Size value) {
205    this.rootLevelIndexSize = value;
206    return this;
207  }
208
209  public RegionMetricsBuilder setStoreFileUncompressedDataIndexSize(Size value) {
210    this.uncompressedDataIndexSize = value;
211    return this;
212  }
213
214  public RegionMetricsBuilder setBloomFilterSize(Size value) {
215    this.bloomFilterSize = value;
216    return this;
217  }
218
219  public RegionMetricsBuilder setUncompressedStoreFileSize(Size value) {
220    this.uncompressedStoreFileSize = value;
221    return this;
222  }
223
224  public RegionMetricsBuilder setWriteRequestCount(long value) {
225    this.writeRequestCount = value;
226    return this;
227  }
228
229  public RegionMetricsBuilder setReadRequestCount(long value) {
230    this.readRequestCount = value;
231    return this;
232  }
233
234  public RegionMetricsBuilder setFilteredReadRequestCount(long value) {
235    this.filteredReadRequestCount = value;
236    return this;
237  }
238
239  public RegionMetricsBuilder setCompletedSequenceId(long value) {
240    this.completedSequenceId = value;
241    return this;
242  }
243
244  public RegionMetricsBuilder setStoreSequenceIds(Map<byte[], Long> value) {
245    this.storeSequenceIds = value;
246    return this;
247  }
248
249  public RegionMetricsBuilder setDataLocality(float value) {
250    this.dataLocality = value;
251    return this;
252  }
253
254  public RegionMetricsBuilder setLastMajorCompactionTimestamp(long value) {
255    this.lastMajorCompactionTimestamp = value;
256    return this;
257  }
258
259  public RegionMetricsBuilder setDataLocalityForSsd(float value) {
260    this.dataLocalityForSsd = value;
261    return this;
262  }
263
264  public RegionMetricsBuilder setBlocksLocalWeight(long value) {
265    this.blocksLocalWeight = value;
266    return this;
267  }
268
269  public RegionMetricsBuilder setBlocksLocalWithSsdWeight(long value) {
270    this.blocksLocalWithSsdWeight = value;
271    return this;
272  }
273
274  public RegionMetricsBuilder setBlocksTotalWeight(long value) {
275    this.blocksTotalWeight = value;
276    return this;
277  }
278
279  public RegionMetricsBuilder setCompactionState(CompactionState compactionState) {
280    this.compactionState = compactionState;
281    return this;
282  }
283
284  public RegionMetrics build() {
285    return new RegionMetricsImpl(name, storeCount, storeFileCount, storeRefCount,
286      maxCompactedStoreFileRefCount, compactingCellCount, compactedCellCount, storeFileSize,
287      memStoreSize, indexSize, rootLevelIndexSize, uncompressedDataIndexSize, bloomFilterSize,
288      uncompressedStoreFileSize, writeRequestCount, readRequestCount, filteredReadRequestCount,
289      completedSequenceId, storeSequenceIds, dataLocality, lastMajorCompactionTimestamp,
290      dataLocalityForSsd, blocksLocalWeight, blocksLocalWithSsdWeight, blocksTotalWeight,
291      compactionState);
292  }
293
294  private static class RegionMetricsImpl implements RegionMetrics {
295    private final byte[] name;
296    private final int storeCount;
297    private final int storeFileCount;
298    private final int storeRefCount;
299    private final int maxCompactedStoreFileRefCount;
300    private final long compactingCellCount;
301    private final long compactedCellCount;
302    private final Size storeFileSize;
303    private final Size memStoreSize;
304    private final Size indexSize;
305    private final Size rootLevelIndexSize;
306    private final Size uncompressedDataIndexSize;
307    private final Size bloomFilterSize;
308    private final Size uncompressedStoreFileSize;
309    private final long writeRequestCount;
310    private final long readRequestCount;
311    private final long filteredReadRequestCount;
312    private final long completedSequenceId;
313    private final Map<byte[], Long> storeSequenceIds;
314    private final float dataLocality;
315    private final long lastMajorCompactionTimestamp;
316    private final float dataLocalityForSsd;
317    private final long blocksLocalWeight;
318    private final long blocksLocalWithSsdWeight;
319    private final long blocksTotalWeight;
320    private final CompactionState compactionState;
321
322    RegionMetricsImpl(byte[] name, int storeCount, int storeFileCount, int storeRefCount,
323      int maxCompactedStoreFileRefCount, final long compactingCellCount, long compactedCellCount,
324      Size storeFileSize, Size memStoreSize, Size indexSize, Size rootLevelIndexSize,
325      Size uncompressedDataIndexSize, Size bloomFilterSize, Size uncompressedStoreFileSize,
326      long writeRequestCount, long readRequestCount, long filteredReadRequestCount,
327      long completedSequenceId, Map<byte[], Long> storeSequenceIds, float dataLocality,
328      long lastMajorCompactionTimestamp, float dataLocalityForSsd, long blocksLocalWeight,
329      long blocksLocalWithSsdWeight, long blocksTotalWeight, CompactionState compactionState) {
330      this.name = Preconditions.checkNotNull(name);
331      this.storeCount = storeCount;
332      this.storeFileCount = storeFileCount;
333      this.storeRefCount = storeRefCount;
334      this.maxCompactedStoreFileRefCount = maxCompactedStoreFileRefCount;
335      this.compactingCellCount = compactingCellCount;
336      this.compactedCellCount = compactedCellCount;
337      this.storeFileSize = Preconditions.checkNotNull(storeFileSize);
338      this.memStoreSize = Preconditions.checkNotNull(memStoreSize);
339      this.indexSize = Preconditions.checkNotNull(indexSize);
340      this.rootLevelIndexSize = Preconditions.checkNotNull(rootLevelIndexSize);
341      this.uncompressedDataIndexSize = Preconditions.checkNotNull(uncompressedDataIndexSize);
342      this.bloomFilterSize = Preconditions.checkNotNull(bloomFilterSize);
343      this.uncompressedStoreFileSize = Preconditions.checkNotNull(uncompressedStoreFileSize);
344      this.writeRequestCount = writeRequestCount;
345      this.readRequestCount = readRequestCount;
346      this.filteredReadRequestCount = filteredReadRequestCount;
347      this.completedSequenceId = completedSequenceId;
348      this.storeSequenceIds = Preconditions.checkNotNull(storeSequenceIds);
349      this.dataLocality = dataLocality;
350      this.lastMajorCompactionTimestamp = lastMajorCompactionTimestamp;
351      this.dataLocalityForSsd = dataLocalityForSsd;
352      this.blocksLocalWeight = blocksLocalWeight;
353      this.blocksLocalWithSsdWeight = blocksLocalWithSsdWeight;
354      this.blocksTotalWeight = blocksTotalWeight;
355      this.compactionState = compactionState;
356    }
357
358    @Override
359    public byte[] getRegionName() {
360      return name;
361    }
362
363    @Override
364    public int getStoreCount() {
365      return storeCount;
366    }
367
368    @Override
369    public int getStoreFileCount() {
370      return storeFileCount;
371    }
372
373    @Override
374    public int getStoreRefCount() {
375      return storeRefCount;
376    }
377
378    @Override
379    public int getMaxCompactedStoreFileRefCount() {
380      return maxCompactedStoreFileRefCount;
381    }
382
383    @Override
384    public Size getStoreFileSize() {
385      return storeFileSize;
386    }
387
388    @Override
389    public Size getMemStoreSize() {
390      return memStoreSize;
391    }
392
393    @Override
394    public long getReadRequestCount() {
395      return readRequestCount;
396    }
397
398    @Override
399    public long getFilteredReadRequestCount() {
400      return filteredReadRequestCount;
401    }
402
403    @Override
404    public long getWriteRequestCount() {
405      return writeRequestCount;
406    }
407
408    @Override
409    public Size getStoreFileIndexSize() {
410      return indexSize;
411    }
412
413    @Override
414    public Size getStoreFileRootLevelIndexSize() {
415      return rootLevelIndexSize;
416    }
417
418    @Override
419    public Size getStoreFileUncompressedDataIndexSize() {
420      return uncompressedDataIndexSize;
421    }
422
423    @Override
424    public Size getBloomFilterSize() {
425      return bloomFilterSize;
426    }
427
428    @Override
429    public long getCompactingCellCount() {
430      return compactingCellCount;
431    }
432
433    @Override
434    public long getCompactedCellCount() {
435      return compactedCellCount;
436    }
437
438    @Override
439    public long getCompletedSequenceId() {
440      return completedSequenceId;
441    }
442
443    @Override
444    public Map<byte[], Long> getStoreSequenceId() {
445      return Collections.unmodifiableMap(storeSequenceIds);
446    }
447
448    @Override
449    public Size getUncompressedStoreFileSize() {
450      return uncompressedStoreFileSize;
451    }
452
453    @Override
454    public float getDataLocality() {
455      return dataLocality;
456    }
457
458    @Override
459    public long getLastMajorCompactionTimestamp() {
460      return lastMajorCompactionTimestamp;
461    }
462
463    @Override
464    public float getDataLocalityForSsd() {
465      return dataLocalityForSsd;
466    }
467
468    @Override
469    public long getBlocksLocalWeight() {
470      return blocksLocalWeight;
471    }
472
473    @Override
474    public long getBlocksLocalWithSsdWeight() {
475      return blocksLocalWithSsdWeight;
476    }
477
478    @Override
479    public long getBlocksTotalWeight() {
480      return blocksTotalWeight;
481    }
482
483    @Override
484    public CompactionState getCompactionState() {
485      return compactionState;
486    }
487
488    @Override
489    public String toString() {
490      StringBuilder sb =
491        Strings.appendKeyValue(new StringBuilder(), "storeCount", this.getStoreCount());
492      Strings.appendKeyValue(sb, "storeFileCount", this.getStoreFileCount());
493      Strings.appendKeyValue(sb, "storeRefCount", this.getStoreRefCount());
494      Strings.appendKeyValue(sb, "maxCompactedStoreFileRefCount",
495        this.getMaxCompactedStoreFileRefCount());
496      Strings.appendKeyValue(sb, "uncompressedStoreFileSize", this.getUncompressedStoreFileSize());
497      Strings.appendKeyValue(sb, "lastMajorCompactionTimestamp",
498        this.getLastMajorCompactionTimestamp());
499      Strings.appendKeyValue(sb, "storeFileSize", this.getStoreFileSize());
500      if (this.getUncompressedStoreFileSize().get() != 0) {
501        Strings.appendKeyValue(sb, "compressionRatio",
502          String.format("%.4f", (float) this.getStoreFileSize().get(Size.Unit.MEGABYTE)
503            / (float) this.getUncompressedStoreFileSize().get(Size.Unit.MEGABYTE)));
504      }
505      Strings.appendKeyValue(sb, "memStoreSize", this.getMemStoreSize());
506      Strings.appendKeyValue(sb, "readRequestCount", this.getReadRequestCount());
507      Strings.appendKeyValue(sb, "writeRequestCount", this.getWriteRequestCount());
508      Strings.appendKeyValue(sb, "rootLevelIndexSize", this.getStoreFileRootLevelIndexSize());
509      Strings.appendKeyValue(sb, "uncompressedDataIndexSize",
510        this.getStoreFileUncompressedDataIndexSize());
511      Strings.appendKeyValue(sb, "bloomFilterSize", this.getBloomFilterSize());
512      Strings.appendKeyValue(sb, "compactingCellCount", this.getCompactingCellCount());
513      Strings.appendKeyValue(sb, "compactedCellCount", this.getCompactedCellCount());
514      float compactionProgressPct = Float.NaN;
515      if (this.getCompactingCellCount() > 0) {
516        compactionProgressPct =
517          ((float) this.getCompactedCellCount() / (float) this.getCompactingCellCount());
518      }
519      Strings.appendKeyValue(sb, "compactionProgressPct", compactionProgressPct);
520      Strings.appendKeyValue(sb, "completedSequenceId", this.getCompletedSequenceId());
521      Strings.appendKeyValue(sb, "dataLocality", this.getDataLocality());
522      Strings.appendKeyValue(sb, "dataLocalityForSsd", this.getDataLocalityForSsd());
523      Strings.appendKeyValue(sb, "blocksLocalWeight", blocksLocalWeight);
524      Strings.appendKeyValue(sb, "blocksLocalWithSsdWeight", blocksLocalWithSsdWeight);
525      Strings.appendKeyValue(sb, "blocksTotalWeight", blocksTotalWeight);
526      Strings.appendKeyValue(sb, "compactionState", compactionState);
527      return sb.toString();
528    }
529  }
530
531}