1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.hadoop.hbase.regionserver.wal;
19
20 import com.google.common.cache.Cache;
21 import com.google.common.cache.CacheBuilder;
22 import java.util.concurrent.TimeUnit;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.HConstants;
25
26 /**
27 * A cache of {@link SyncFuture}s. This class supports two methods
28 * {@link SyncFutureCache#getIfPresentOrNew()} and {@link SyncFutureCache#offer(
29 * org.apache.hadoop.hbase.regionserver.wal.SyncFuture)}.
30 *
31 * Usage pattern:
32 * SyncFuture sf = syncFutureCache.getIfPresentOrNew();
33 * sf.reset(...);
34 * // Use the sync future
35 * finally: syncFutureCache.offer(sf);
36 *
37 * Offering the sync future back to the cache makes it eligible for reuse within the same thread
38 * context. Cache keyed by the accessing thread instance and automatically invalidated if it remains
39 * unused for {@link SyncFutureCache#SYNC_FUTURE_INVALIDATION_TIMEOUT_MINS} minutes.
40 */
41 public final class SyncFutureCache {
42
43 private static final long SYNC_FUTURE_INVALIDATION_TIMEOUT_MINS = 2;
44
45 private final Cache<Thread, SyncFuture> syncFutureCache;
46
47 public SyncFutureCache(final Configuration conf) {
48 final int handlerCount = conf.getInt(HConstants.REGION_SERVER_HANDLER_COUNT,
49 HConstants.DEFAULT_REGION_SERVER_HANDLER_COUNT);
50 syncFutureCache = CacheBuilder.newBuilder().initialCapacity(handlerCount)
51 .expireAfterWrite(SYNC_FUTURE_INVALIDATION_TIMEOUT_MINS, TimeUnit.MINUTES).build();
52 }
53
54 public SyncFuture getIfPresentOrNew() {
55 // Invalidate the entry if a mapping exists. We do not want it to be reused at the same time.
56 SyncFuture future = syncFutureCache.asMap().remove(Thread.currentThread());
57 return (future == null) ? new SyncFuture() : future;
58 }
59
60 /**
61 * Offers the sync future back to the cache for reuse.
62 */
63 public void offer(SyncFuture syncFuture) {
64 // It is ok to overwrite an existing mapping.
65 syncFutureCache.asMap().put(syncFuture.getThread(), syncFuture);
66 }
67
68 public void clear() {
69 if (syncFutureCache != null) {
70 syncFutureCache.invalidateAll();
71 }
72 }
73 }