taklwu commented on code in PR #8653: URL: https://github.com/apache/hbase/pull/8653#discussion_r4068219661
########## hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/cache/LruCacheEngine.java: ########## @@ -0,0 +1,1623 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.io.hfile.cache; + +import java.lang.ref.WeakReference; +import java.util.EnumMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.PriorityQueue; +import java.util.SortedSet; +import java.util.TreeSet; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.atomic.LongAdder; +import java.util.concurrent.locks.ReentrantLock; +import org.apache.commons.lang3.mutable.MutableBoolean; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseInterfaceAudience; +import org.apache.hadoop.hbase.io.HeapSize; +import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding; +import org.apache.hadoop.hbase.io.hfile.BlockCacheKey; +import org.apache.hadoop.hbase.io.hfile.BlockCacheUtil; +import org.apache.hadoop.hbase.io.hfile.BlockPriority; +import org.apache.hadoop.hbase.io.hfile.BlockType; +import org.apache.hadoop.hbase.io.hfile.CacheStats; +import org.apache.hadoop.hbase.io.hfile.Cacheable; +import org.apache.hadoop.hbase.io.hfile.CachedBlock; +import org.apache.hadoop.hbase.io.hfile.HFileBlock; +import org.apache.hadoop.hbase.io.hfile.LruCachedBlock; +import org.apache.hadoop.hbase.io.hfile.LruCachedBlockQueue; +import org.apache.hadoop.hbase.util.ClassSize; +import org.apache.hadoop.util.StringUtils; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.hbase.thirdparty.com.google.common.base.MoreObjects; +import org.apache.hbase.thirdparty.com.google.common.base.Objects; +import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder; + +/** + * Native LRU {@link CacheEngine} implementation. + * <p> + * This cache is memory-aware using {@link HeapSize}, memory-bound using an LRU eviction algorithm, + * and concurrent. It is backed by a {@link ConcurrentHashMap} and can use a non-blocking eviction + * thread, providing constant-time {@link #cacheBlock(BlockCacheKey, Cacheable, boolean)} and + * {@link #getBlock(BlockCacheKey, boolean, boolean, boolean)} operations. + * </p> + * <p> + * The cache maintains three block-priority levels to provide scan resistance and support in-memory + * column families: + * </p> + * <ul> + * <li>single-access blocks</li> + * <li>multiple-access blocks</li> + * <li>in-memory blocks</li> + * </ul> + * <p> + * Each priority is assigned a portion of the total cache capacity. During eviction the cache tries + * to preserve the configured relative sizes while allowing unused capacity in one priority to be + * consumed by another. + * </p> + * <p> + * This class is a storage engine only. It does not perform L1/L2 orchestration, victim-cache + * delegation, tier placement, admission control, promotion, or demotion. Those responsibilities + * belong to the cache topology and policy layers. + * </p> + */ [email protected] +public class LruCacheEngine implements CacheEngine, HeapSize, Iterable<CachedBlock> { Review Comment: LruBlockCache and LruCacheEngine are very similar, what is the plan for `LruBlockCache` in the future? ########## hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCacheFactory.java: ########## @@ -135,6 +138,62 @@ public static BlockCache createBlockCache(Configuration conf) { return createBlockCache(conf, null); } + /** + * Creates the configured first-level cache as a cache engine. + * <p> + * Cache implementations that have been migrated to {@link CacheEngine} are instantiated directly. + * Legacy {@link FirstLevelBlockCache} implementations are adapted until their migration is + * complete. + * @param c cache configuration + * @return first-level cache engine, or {@code null} when the on-heap cache is disabled + */ + public static CacheEngine createFirstLevelCacheEngine(final Configuration c) { + final long cacheSize = MemorySizeUtil.getOnHeapCacheSize(c); + if (cacheSize < 0) { + return null; + } + + String policy = c.get(BLOCKCACHE_POLICY_KEY, BLOCKCACHE_POLICY_DEFAULT); + int blockSize = c.getInt(BLOCKCACHE_BLOCKSIZE_KEY, HConstants.DEFAULT_BLOCKSIZE); + LOG.info("Allocating CacheEngine size=" + StringUtils.byteDesc(cacheSize) + ", blockSize=" + + StringUtils.byteDesc(blockSize)); + + if (policy.equalsIgnoreCase("LRU")) { Review Comment: should we add other supported policy in BlockCacheFactory or other constants class? I found those have been inline plaintext in few other classes as well. ```suggestion if (policy.equalsIgnoreCase(BlockCacheFactory.BLOCKCACHE_POLICY_DEFAULT)) { ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
