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.io.hfile;
019
020import java.io.IOException;
021import java.nio.ByteBuffer;
022import java.util.NavigableMap;
023import java.util.NavigableSet;
024import java.util.concurrent.ConcurrentSkipListMap;
025import java.util.concurrent.ConcurrentSkipListSet;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.metrics.impl.FastLongHistogram;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.apache.hadoop.hbase.util.GsonUtil;
030import org.apache.yetus.audience.InterfaceAudience;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034import org.apache.hbase.thirdparty.com.google.gson.Gson;
035import org.apache.hbase.thirdparty.com.google.gson.TypeAdapter;
036import org.apache.hbase.thirdparty.com.google.gson.stream.JsonReader;
037import org.apache.hbase.thirdparty.com.google.gson.stream.JsonWriter;
038
039/**
040 * Utilty for aggregating counts in CachedBlocks and toString/toJSON CachedBlocks and BlockCaches.
041 * No attempt has been made at making this thread safe.
042 */
043@InterfaceAudience.Private
044public class BlockCacheUtil {
045
046  private static final Logger LOG = LoggerFactory.getLogger(BlockCacheUtil.class);
047
048  public static final long NANOS_PER_SECOND = 1000000000;
049
050  /**
051   * Needed generating JSON.
052   */
053  private static final Gson GSON = GsonUtil.createGson()
054    .registerTypeAdapter(FastLongHistogram.class, new TypeAdapter<FastLongHistogram>() {
055
056      @Override
057      public void write(JsonWriter out, FastLongHistogram value) throws IOException {
058        AgeSnapshot snapshot = new AgeSnapshot(value);
059        out.beginObject();
060        out.name("mean").value(snapshot.getMean());
061        out.name("min").value(snapshot.getMin());
062        out.name("max").value(snapshot.getMax());
063        out.name("75thPercentile").value(snapshot.get75thPercentile());
064        out.name("95thPercentile").value(snapshot.get95thPercentile());
065        out.name("98thPercentile").value(snapshot.get98thPercentile());
066        out.name("99thPercentile").value(snapshot.get99thPercentile());
067        out.name("999thPercentile").value(snapshot.get999thPercentile());
068        out.endObject();
069      }
070
071      @Override
072      public FastLongHistogram read(JsonReader in) throws IOException {
073        throw new UnsupportedOperationException();
074      }
075    }).setPrettyPrinting().create();
076
077  /**
078   * n * @return The block content as String.
079   */
080  public static String toString(final CachedBlock cb, final long now) {
081    return "filename=" + cb.getFilename() + ", " + toStringMinusFileName(cb, now);
082  }
083
084  /**
085   * Little data structure to hold counts for a file. Used doing a toJSON.
086   */
087  static class CachedBlockCountsPerFile {
088    private int count = 0;
089    private long size = 0;
090    private int countData = 0;
091    private long sizeData = 0;
092    private final String filename;
093
094    CachedBlockCountsPerFile(final String filename) {
095      this.filename = filename;
096    }
097
098    public int getCount() {
099      return count;
100    }
101
102    public long getSize() {
103      return size;
104    }
105
106    public int getCountData() {
107      return countData;
108    }
109
110    public long getSizeData() {
111      return sizeData;
112    }
113
114    public String getFilename() {
115      return filename;
116    }
117  }
118
119  /**
120   * @return A JSON String of <code>filename</code> and counts of <code>blocks</code>
121   */
122  public static String toJSON(String filename, NavigableSet<CachedBlock> blocks)
123    throws IOException {
124    CachedBlockCountsPerFile counts = new CachedBlockCountsPerFile(filename);
125    for (CachedBlock cb : blocks) {
126      counts.count++;
127      counts.size += cb.getSize();
128      BlockType bt = cb.getBlockType();
129      if (bt != null && bt.isData()) {
130        counts.countData++;
131        counts.sizeData += cb.getSize();
132      }
133    }
134    return GSON.toJson(counts);
135  }
136
137  /**
138   * @return JSON string of <code>cbsf</code> aggregated
139   */
140  public static String toJSON(CachedBlocksByFile cbsbf) throws IOException {
141    return GSON.toJson(cbsbf);
142  }
143
144  /**
145   * @return JSON string of <code>bc</code> content.
146   */
147  public static String toJSON(BlockCache bc) throws IOException {
148    return GSON.toJson(bc);
149  }
150
151  /**
152   * n * @return The block content of <code>bc</code> as a String minus the filename.
153   */
154  public static String toStringMinusFileName(final CachedBlock cb, final long now) {
155    return "offset=" + cb.getOffset() + ", size=" + cb.getSize() + ", age="
156      + (now - cb.getCachedTime()) + ", type=" + cb.getBlockType() + ", priority="
157      + cb.getBlockPriority();
158  }
159
160  /**
161   * Get a {@link CachedBlocksByFile} instance and load it up by iterating content in
162   * {@link BlockCache}.
163   * @param conf Used to read configurations
164   * @param bc   Block Cache to iterate.
165   * @return Laoded up instance of CachedBlocksByFile
166   */
167  public static CachedBlocksByFile getLoadedCachedBlocksByFile(final Configuration conf,
168    final BlockCache bc) {
169    CachedBlocksByFile cbsbf = new CachedBlocksByFile(conf);
170    for (CachedBlock cb : bc) {
171      if (cbsbf.update(cb)) break;
172    }
173    return cbsbf;
174  }
175
176  private static int compareCacheBlock(Cacheable left, Cacheable right,
177    boolean includeNextBlockMetadata) {
178    ByteBuffer l = ByteBuffer.allocate(left.getSerializedLength());
179    left.serialize(l, includeNextBlockMetadata);
180    ByteBuffer r = ByteBuffer.allocate(right.getSerializedLength());
181    right.serialize(r, includeNextBlockMetadata);
182    return Bytes.compareTo(l.array(), l.arrayOffset(), l.limit(), r.array(), r.arrayOffset(),
183      r.limit());
184  }
185
186  /**
187   * Validate that the existing and newBlock are the same without including the nextBlockMetadata,
188   * if not, throw an exception. If they are the same without the nextBlockMetadata, return the
189   * comparison.
190   * @param existing block that is existing in the cache.
191   * @param newBlock block that is trying to be cached.
192   * @param cacheKey the cache key of the blocks.
193   * @return comparison of the existing block to the newBlock.
194   */
195  public static int validateBlockAddition(Cacheable existing, Cacheable newBlock,
196    BlockCacheKey cacheKey) {
197    int comparison = compareCacheBlock(existing, newBlock, false);
198    if (comparison != 0) {
199      throw new RuntimeException(
200        "Cached block contents differ, which should not have happened." + "cacheKey:" + cacheKey);
201    }
202    if ((existing instanceof HFileBlock) && (newBlock instanceof HFileBlock)) {
203      comparison = ((HFileBlock) existing).getNextBlockOnDiskSize()
204        - ((HFileBlock) newBlock).getNextBlockOnDiskSize();
205    }
206    return comparison;
207  }
208
209  /**
210   * Because of the region splitting, it's possible that the split key locate in the middle of a
211   * block. So it's possible that both the daughter regions load the same block from their parent
212   * HFile. When pread, we don't force the read to read all of the next block header. So when two
213   * threads try to cache the same block, it's possible that one thread read all of the next block
214   * header but the other one didn't. if the already cached block hasn't next block header but the
215   * new block to cache has, then we can replace the existing block with the new block for better
216   * performance.(HBASE-20447)
217   * @param blockCache BlockCache to check
218   * @param cacheKey   the block cache key
219   * @param newBlock   the new block which try to put into the block cache.
220   * @return true means need to replace existing block with new block for the same block cache key.
221   *         false means just keep the existing block.
222   */
223  public static boolean shouldReplaceExistingCacheBlock(BlockCache blockCache,
224    BlockCacheKey cacheKey, Cacheable newBlock) {
225    // NOTICE: The getBlock has retained the existingBlock inside.
226    Cacheable existingBlock = blockCache.getBlock(cacheKey, false, false, false);
227    if (existingBlock == null) {
228      return true;
229    }
230    try {
231      int comparison = BlockCacheUtil.validateBlockAddition(existingBlock, newBlock, cacheKey);
232      if (comparison < 0) {
233        LOG.warn("Cached block contents differ by nextBlockOnDiskSize, the new block has "
234          + "nextBlockOnDiskSize set. Caching new block.");
235        return true;
236      } else if (comparison > 0) {
237        LOG.warn("Cached block contents differ by nextBlockOnDiskSize, the existing block has "
238          + "nextBlockOnDiskSize set, Keeping cached block.");
239        return false;
240      } else {
241        LOG.debug("Caching an already cached block: {}. This is harmless and can happen in rare "
242          + "cases (see HBASE-8547)", cacheKey);
243        return false;
244      }
245    } finally {
246      // Release this block to decrement the reference count.
247      existingBlock.release();
248    }
249  }
250
251  /**
252   * Use one of these to keep a running account of cached blocks by file. Throw it away when done.
253   * This is different than metrics in that it is stats on current state of a cache. See
254   * getLoadedCachedBlocksByFile
255   */
256  public static class CachedBlocksByFile {
257    private int count;
258    private int dataBlockCount;
259    private long size;
260    private long dataSize;
261    private final long now = System.nanoTime();
262    /**
263     * How many blocks to look at before we give up. There could be many millions of blocks. We
264     * don't want the ui to freeze while we run through 1B blocks... users will think hbase dead. UI
265     * displays warning in red when stats are incomplete.
266     */
267    private final int max;
268    public static final int DEFAULT_MAX = 1000000;
269
270    CachedBlocksByFile() {
271      this(null);
272    }
273
274    CachedBlocksByFile(final Configuration c) {
275      this.max = c == null ? DEFAULT_MAX : c.getInt("hbase.ui.blockcache.by.file.max", DEFAULT_MAX);
276    }
277
278    /**
279     * Map by filename. use concurent utils because we want our Map and contained blocks sorted.
280     */
281    private transient NavigableMap<String, NavigableSet<CachedBlock>> cachedBlockByFile =
282      new ConcurrentSkipListMap<>();
283    FastLongHistogram hist = new FastLongHistogram();
284
285    /**
286     * n * @return True if full.... if we won't be adding any more.
287     */
288    public boolean update(final CachedBlock cb) {
289      if (isFull()) return true;
290      NavigableSet<CachedBlock> set = this.cachedBlockByFile.get(cb.getFilename());
291      if (set == null) {
292        set = new ConcurrentSkipListSet<>();
293        this.cachedBlockByFile.put(cb.getFilename(), set);
294      }
295      set.add(cb);
296      this.size += cb.getSize();
297      this.count++;
298      BlockType bt = cb.getBlockType();
299      if (bt != null && bt.isData()) {
300        this.dataBlockCount++;
301        this.dataSize += cb.getSize();
302      }
303      long age = (this.now - cb.getCachedTime()) / NANOS_PER_SECOND;
304      this.hist.add(age, 1);
305      return false;
306    }
307
308    /**
309     * @return True if full; i.e. there are more items in the cache but we only loaded up the
310     *         maximum set in configuration <code>hbase.ui.blockcache.by.file.max</code> (Default:
311     *         DEFAULT_MAX).
312     */
313    public boolean isFull() {
314      return this.count >= this.max;
315    }
316
317    public NavigableMap<String, NavigableSet<CachedBlock>> getCachedBlockStatsByFile() {
318      return this.cachedBlockByFile;
319    }
320
321    /**
322     * @return count of blocks in the cache
323     */
324    public int getCount() {
325      return count;
326    }
327
328    public int getDataCount() {
329      return dataBlockCount;
330    }
331
332    /**
333     * @return size of blocks in the cache
334     */
335    public long getSize() {
336      return size;
337    }
338
339    /**
340     * @return Size of data.
341     */
342    public long getDataSize() {
343      return dataSize;
344    }
345
346    public AgeSnapshot getAgeInCacheSnapshot() {
347      return new AgeSnapshot(this.hist);
348    }
349
350    @Override
351    public String toString() {
352      AgeSnapshot snapshot = getAgeInCacheSnapshot();
353      return "count=" + count + ", dataBlockCount=" + dataBlockCount + ", size=" + size
354        + ", dataSize=" + getDataSize() + ", mean age=" + snapshot.getMean() + ", min age="
355        + snapshot.getMin() + ", max age=" + snapshot.getMax() + ", 75th percentile age="
356        + snapshot.get75thPercentile() + ", 95th percentile age=" + snapshot.get95thPercentile()
357        + ", 98th percentile age=" + snapshot.get98thPercentile() + ", 99th percentile age="
358        + snapshot.get99thPercentile() + ", 99.9th percentile age=" + snapshot.get99thPercentile();
359    }
360  }
361}