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)}}.
031 * <p>
032 * Usage pattern:
033 *
034 * <pre>
035 *   SyncFuture sf = syncFutureCache.getIfPresentOrNew();
036 *   sf.reset(...);
037 *   // Use the sync future
038 *   finally: syncFutureCache.offer(sf);
039 * </pre>
040 *
041 * Offering the sync future back to the cache makes it eligible for reuse within the same thread
042 * context. Cache keyed by the accessing thread instance and automatically invalidated if it remains
043 * unused for {@link SyncFutureCache#SYNC_FUTURE_INVALIDATION_TIMEOUT_MINS} minutes.
044 */
045@InterfaceAudience.Private
046public final class SyncFutureCache {
047
048  private static final long SYNC_FUTURE_INVALIDATION_TIMEOUT_MINS = 2;
049
050  private final Cache<Thread, SyncFuture> syncFutureCache;
051
052  public SyncFutureCache(final Configuration conf) {
053    final int handlerCount = conf.getInt(HConstants.REGION_SERVER_HANDLER_COUNT,
054      HConstants.DEFAULT_REGION_SERVER_HANDLER_COUNT);
055    syncFutureCache = CacheBuilder.newBuilder().initialCapacity(handlerCount)
056      .expireAfterWrite(SYNC_FUTURE_INVALIDATION_TIMEOUT_MINS, TimeUnit.MINUTES).build();
057  }
058
059  public SyncFuture getIfPresentOrNew() {
060    // Invalidate the entry if a mapping exists. We do not want it to be reused at the same time.
061    SyncFuture future = syncFutureCache.asMap().remove(Thread.currentThread());
062    return (future == null) ? new SyncFuture() : future;
063  }
064
065  /**
066   * Offers the sync future back to the cache for reuse.
067   */
068  public void offer(SyncFuture syncFuture) {
069    // It is ok to overwrite an existing mapping.
070    syncFutureCache.asMap().put(syncFuture.getThread(), syncFuture);
071  }
072
073  public void clear() {
074    if (syncFutureCache != null) {
075      syncFutureCache.invalidateAll();
076    }
077  }
078}