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