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 static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.util.Arrays;
024import java.util.Map;
025import java.util.Random;
026import java.util.concurrent.Callable;
027import java.util.concurrent.ConcurrentHashMap;
028import java.util.concurrent.ExecutorCompletionService;
029import java.util.concurrent.ExecutorService;
030import java.util.concurrent.Executors;
031import java.util.concurrent.Future;
032import java.util.concurrent.ThreadLocalRandom;
033import java.util.concurrent.TimeUnit;
034import java.util.concurrent.locks.Lock;
035import java.util.concurrent.locks.ReentrantReadWriteLock;
036import org.apache.hadoop.hbase.HBaseClassTestRule;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.testclassification.MiscTests;
039import org.apache.hadoop.hbase.util.IdReadWriteLockWithObjectPool.ReferenceType;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.junit.runner.RunWith;
044import org.junit.runners.Parameterized;
045import org.slf4j.Logger;
046import org.slf4j.LoggerFactory;
047
048@RunWith(Parameterized.class)
049@Category({ MiscTests.class, MediumTests.class })
050// Medium as it creates 100 threads; seems better to run it isolated
051public class TestIdReadWriteLockWithObjectPool {
052
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055    HBaseClassTestRule.forClass(TestIdReadWriteLockWithObjectPool.class);
056
057  private static final Logger LOG =
058    LoggerFactory.getLogger(TestIdReadWriteLockWithObjectPool.class);
059
060  private static final int NUM_IDS = 16;
061  private static final int NUM_THREADS = 128;
062  private static final int NUM_SECONDS = 15;
063
064  @Parameterized.Parameter
065  public IdReadWriteLockWithObjectPool<Long> idLock;
066
067  @Parameterized.Parameters
068  public static Iterable<Object[]> data() {
069    return Arrays
070      .asList(new Object[][] { { new IdReadWriteLockWithObjectPool<Long>(ReferenceType.WEAK) },
071        { new IdReadWriteLockWithObjectPool<Long>(ReferenceType.SOFT) } });
072  }
073
074  private Map<Long, String> idOwner = new ConcurrentHashMap<>();
075
076  private class IdLockTestThread implements Callable<Boolean> {
077
078    private String clientId;
079
080    public IdLockTestThread(String clientId) {
081      this.clientId = clientId;
082    }
083
084    @Override
085    public Boolean call() throws Exception {
086      Thread.currentThread().setName(clientId);
087      Random rand = ThreadLocalRandom.current();
088      long endTime = EnvironmentEdgeManager.currentTime() + NUM_SECONDS * 1000;
089      while (EnvironmentEdgeManager.currentTime() < endTime) {
090        long id = rand.nextInt(NUM_IDS);
091        boolean readLock = rand.nextBoolean();
092
093        ReentrantReadWriteLock readWriteLock = idLock.getLock(id);
094        Lock lock = readLock ? readWriteLock.readLock() : readWriteLock.writeLock();
095        try {
096          lock.lock();
097          int sleepMs = 1 + rand.nextInt(4);
098          String owner = idOwner.get(id);
099          if (owner != null && LOG.isDebugEnabled()) {
100            LOG.debug((readLock ? "Read" : "Write") + "lock of Id " + id + " already taken by "
101              + owner + ", we are " + clientId);
102          }
103
104          idOwner.put(id, clientId);
105          Thread.sleep(sleepMs);
106          idOwner.remove(id);
107
108        } finally {
109          lock.unlock();
110          if (LOG.isDebugEnabled()) {
111            LOG.debug("Release " + (readLock ? "Read" : "Write") + " lock of Id" + id + ", we are "
112              + clientId);
113          }
114        }
115      }
116      return true;
117    }
118
119  }
120
121  @Test
122  public void testMultipleClients() throws Exception {
123    ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS);
124    try {
125      ExecutorCompletionService<Boolean> ecs = new ExecutorCompletionService<>(exec);
126      for (int i = 0; i < NUM_THREADS; ++i)
127        ecs.submit(new IdLockTestThread("client_" + i));
128      for (int i = 0; i < NUM_THREADS; ++i) {
129        Future<Boolean> result = ecs.take();
130        assertTrue(result.get());
131      }
132      int entryPoolSize = idLock.purgeAndGetEntryPoolSize();
133      LOG.debug("Size of entry pool after gc and purge: " + entryPoolSize);
134      ReferenceType refType = idLock.getReferenceType();
135      switch (refType) {
136        case WEAK:
137          // make sure the entry pool will be cleared after GC and purge call
138          assertEquals(0, entryPoolSize);
139          break;
140        case SOFT:
141          // make sure the entry pool won't be cleared when JVM memory is enough
142          // even after GC and purge call
143          assertEquals(NUM_IDS, entryPoolSize);
144          break;
145        default:
146          break;
147      }
148    } finally {
149      exec.shutdown();
150      exec.awaitTermination(5000, TimeUnit.MILLISECONDS);
151    }
152  }
153
154}