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.util;
019
020import java.lang.ref.Reference;
021import java.util.concurrent.locks.ReentrantReadWriteLock;
022import org.apache.yetus.audience.InterfaceAudience;
023
024@InterfaceAudience.Private
025public class IdReadWriteLockWithObjectPool<T> extends IdReadWriteLock<T> {
026  // The number of lock we want to easily support. It's not a maximum.
027  private static final int NB_CONCURRENT_LOCKS = 1000;
028  /**
029   * The pool to get entry from, entries are mapped by {@link Reference} and will be automatically
030   * garbage-collected by JVM
031   */
032  private final ObjectPool<T, ReentrantReadWriteLock> lockPool;
033  private final ReferenceType refType;
034  private final boolean purgeOnGet;
035
036  public IdReadWriteLockWithObjectPool() {
037    this(ReferenceType.WEAK);
038  }
039
040  /**
041   * Constructor of IdReadWriteLockWithObjectPool
042   * @param referenceType type of the reference used in lock pool, {@link ReferenceType#WEAK} by
043   *                      default. Use {@link ReferenceType#SOFT} if the key set is limited and the
044   *                      locks will be reused with a high frequency
045   */
046  public IdReadWriteLockWithObjectPool(ReferenceType referenceType) {
047    this(referenceType, true);
048  }
049
050  /**
051   * Constructor of IdReadWriteLockWithObjectPool
052   * @param referenceType type of the reference used in lock pool
053   * @param purgeOnGet    whether {@link #getLock(Object)} also purges stale references. if false,
054   *                      the owner must remember to call {@link #purge()} externally
055   */
056  public IdReadWriteLockWithObjectPool(ReferenceType referenceType, boolean purgeOnGet) {
057    this.refType = referenceType;
058    this.purgeOnGet = purgeOnGet;
059    switch (referenceType) {
060      case SOFT:
061        lockPool = new SoftObjectPool<>(new ObjectPool.ObjectFactory<T, ReentrantReadWriteLock>() {
062          @Override
063          public ReentrantReadWriteLock createObject(T id) {
064            return new ReentrantReadWriteLock();
065          }
066        }, NB_CONCURRENT_LOCKS);
067        break;
068      case WEAK:
069      default:
070        lockPool = new WeakObjectPool<>(new ObjectPool.ObjectFactory<T, ReentrantReadWriteLock>() {
071          @Override
072          public ReentrantReadWriteLock createObject(T id) {
073            return new ReentrantReadWriteLock();
074          }
075        }, NB_CONCURRENT_LOCKS);
076    }
077  }
078
079  public static enum ReferenceType {
080    WEAK,
081    SOFT
082  }
083
084  /**
085   * Get the ReentrantReadWriteLock corresponding to the given id
086   * @param id an arbitrary number to identify the lock
087   */
088  @Override
089  public ReentrantReadWriteLock getLock(T id) {
090    if (purgeOnGet) {
091      lockPool.purge();
092    }
093
094    ReentrantReadWriteLock readWriteLock = lockPool.get(id);
095    return readWriteLock;
096  }
097
098  /**
099   * Cleans up the lockPool of stale references
100   */
101  public void purge() {
102    lockPool.purge();
103  }
104
105  /**
106   * Returns the size of the underlying lockPool
107   */
108  public int getPoolSize() {
109    return lockPool.size();
110  }
111
112  /** For testing */
113  int purgeAndGetEntryPoolSize() {
114    gc();
115    Threads.sleep(200);
116    lockPool.purge();
117    return lockPool.size();
118  }
119
120  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "DM_GC", justification = "Intentional")
121  private void gc() {
122    System.gc();
123  }
124
125  public ReferenceType getReferenceType() {
126    return this.refType;
127  }
128}