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.regionserver.wal; 019 020import java.util.concurrent.TimeUnit; 021import org.apache.hadoop.conf.Configuration; 022import org.apache.hadoop.hbase.HConstants; 023import org.apache.yetus.audience.InterfaceAudience; 024 025import org.apache.hbase.thirdparty.com.google.common.cache.Cache; 026import org.apache.hbase.thirdparty.com.google.common.cache.CacheBuilder; 027 028/** 029 * A cache of {@link SyncFuture}s. This class supports two methods 030 * {@link SyncFutureCache#getIfPresentOrNew()} and {@link SyncFutureCache#offer(SyncFuture)}. Usage 031 * pattern: SyncFuture sf = syncFutureCache.getIfPresentOrNew(); sf.reset(...); // Use the sync 032 * future finally: syncFutureCache.offer(sf); Offering the sync future back to the cache makes it 033 * eligible for reuse within the same thread context. Cache keyed by the accessing thread instance 034 * and automatically invalidated if it remains unused for 035 * {@link SyncFutureCache#SYNC_FUTURE_INVALIDATION_TIMEOUT_MINS} minutes. 036 */ 037@InterfaceAudience.Private 038public final class SyncFutureCache { 039 040 private static final long SYNC_FUTURE_INVALIDATION_TIMEOUT_MINS = 2; 041 042 private final Cache<Thread, SyncFuture> syncFutureCache; 043 044 public SyncFutureCache(final Configuration conf) { 045 final int handlerCount = conf.getInt(HConstants.REGION_SERVER_HANDLER_COUNT, 046 HConstants.DEFAULT_REGION_SERVER_HANDLER_COUNT); 047 syncFutureCache = CacheBuilder.newBuilder().initialCapacity(handlerCount) 048 .expireAfterWrite(SYNC_FUTURE_INVALIDATION_TIMEOUT_MINS, TimeUnit.MINUTES).build(); 049 } 050 051 public SyncFuture getIfPresentOrNew() { 052 // Invalidate the entry if a mapping exists. We do not want it to be reused at the same time. 053 SyncFuture future = syncFutureCache.asMap().remove(Thread.currentThread()); 054 return (future == null) ? new SyncFuture() : future; 055 } 056 057 /** 058 * Offers the sync future back to the cache for reuse. 059 */ 060 public void offer(SyncFuture syncFuture) { 061 // It is ok to overwrite an existing mapping. 062 syncFutureCache.asMap().put(syncFuture.getThread(), syncFuture); 063 } 064 065 public void clear() { 066 if (syncFutureCache != null) { 067 syncFutureCache.invalidateAll(); 068 } 069 } 070}