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