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