-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HBASE-23223 Support the offsetLock of bucketCache to use strong ref (#…
…764) Signed-off-by: Wellington Chevreuil <wchevreuil@apache.org>
- Loading branch information
1 parent
77b4e8c
commit 4ea7922
Showing
7 changed files
with
237 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
hbase-server/src/main/java/org/apache/hadoop/hbase/util/IdReadWriteLockStrongRef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.hadoop.hbase.util; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
||
import org.apache.yetus.audience.InterfaceAudience; | ||
import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; | ||
|
||
@InterfaceAudience.Private | ||
public class IdReadWriteLockStrongRef<T> extends IdReadWriteLock<T> { | ||
|
||
final ConcurrentHashMap<T, ReentrantReadWriteLock> map = new ConcurrentHashMap<>(); | ||
|
||
@VisibleForTesting | ||
@Override | ||
public ReentrantReadWriteLock getLock(T id) { | ||
ReentrantReadWriteLock existing = map.get(id); | ||
if (existing != null) { | ||
return existing; | ||
} | ||
|
||
ReentrantReadWriteLock newLock = new ReentrantReadWriteLock(); | ||
existing = map.putIfAbsent(id, newLock); | ||
if (existing == null) { | ||
return newLock; | ||
} else { | ||
return existing; | ||
} | ||
} | ||
|
||
} |
104 changes: 104 additions & 0 deletions
104
hbase-server/src/main/java/org/apache/hadoop/hbase/util/IdReadWriteLockWithObjectPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.hadoop.hbase.util; | ||
|
||
import java.lang.ref.Reference; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
||
import org.apache.yetus.audience.InterfaceAudience; | ||
|
||
import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; | ||
|
||
@InterfaceAudience.Private | ||
public class IdReadWriteLockWithObjectPool<T> extends IdReadWriteLock<T>{ | ||
// The number of lock we want to easily support. It's not a maximum. | ||
private static final int NB_CONCURRENT_LOCKS = 1000; | ||
/** | ||
* The pool to get entry from, entries are mapped by {@link Reference} and will be automatically | ||
* garbage-collected by JVM | ||
*/ | ||
private final ObjectPool<T, ReentrantReadWriteLock> lockPool; | ||
private final ReferenceType refType; | ||
|
||
public IdReadWriteLockWithObjectPool() { | ||
this(ReferenceType.WEAK); | ||
} | ||
|
||
/** | ||
* Constructor of IdReadWriteLockWithObjectPool | ||
* @param referenceType type of the reference used in lock pool, {@link ReferenceType#WEAK} by | ||
* default. Use {@link ReferenceType#SOFT} if the key set is limited and the locks will | ||
* be reused with a high frequency | ||
*/ | ||
public IdReadWriteLockWithObjectPool(ReferenceType referenceType) { | ||
this.refType = referenceType; | ||
switch (referenceType) { | ||
case SOFT: | ||
lockPool = new SoftObjectPool<>(new ObjectPool.ObjectFactory<T, ReentrantReadWriteLock>() { | ||
@Override | ||
public ReentrantReadWriteLock createObject(T id) { | ||
return new ReentrantReadWriteLock(); | ||
} | ||
}, NB_CONCURRENT_LOCKS); | ||
break; | ||
case WEAK: | ||
default: | ||
lockPool = new WeakObjectPool<>(new ObjectPool.ObjectFactory<T, ReentrantReadWriteLock>() { | ||
@Override | ||
public ReentrantReadWriteLock createObject(T id) { | ||
return new ReentrantReadWriteLock(); | ||
} | ||
}, NB_CONCURRENT_LOCKS); | ||
} | ||
} | ||
|
||
public static enum ReferenceType { | ||
WEAK, SOFT | ||
} | ||
|
||
/** | ||
* Get the ReentrantReadWriteLock corresponding to the given id | ||
* @param id an arbitrary number to identify the lock | ||
*/ | ||
@Override | ||
public ReentrantReadWriteLock getLock(T id) { | ||
lockPool.purge(); | ||
ReentrantReadWriteLock readWriteLock = lockPool.get(id); | ||
return readWriteLock; | ||
} | ||
|
||
/** For testing */ | ||
@VisibleForTesting | ||
int purgeAndGetEntryPoolSize() { | ||
gc(); | ||
Threads.sleep(200); | ||
lockPool.purge(); | ||
return lockPool.size(); | ||
} | ||
|
||
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="DM_GC", justification="Intentional") | ||
private void gc() { | ||
System.gc(); | ||
} | ||
|
||
@VisibleForTesting | ||
public ReferenceType getReferenceType() { | ||
return this.refType; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestIdReadWriteLockStrongRef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.hadoop.hbase.util; | ||
|
||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
||
import org.apache.hadoop.hbase.HBaseClassTestRule; | ||
import org.apache.hadoop.hbase.testclassification.SmallTests; | ||
import org.junit.Assert; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
||
@Category({ SmallTests.class }) | ||
public class TestIdReadWriteLockStrongRef { | ||
|
||
@ClassRule | ||
public static final HBaseClassTestRule CLASS_RULE = | ||
HBaseClassTestRule.forClass(TestIdReadWriteLockStrongRef.class); | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(TestIdReadWriteLockStrongRef.class); | ||
|
||
private IdReadWriteLockStrongRef<Long> idLock = new IdReadWriteLockStrongRef<>(); | ||
|
||
@Test | ||
public void testGetLock() throws Exception { | ||
Long offset_1 = 1L; | ||
Long offset_2 = 2L; | ||
ReentrantReadWriteLock offsetLock_1 = idLock.getLock(offset_1); | ||
ReentrantReadWriteLock offsetLock_2 = idLock.getLock(offset_1); | ||
Assert.assertEquals(offsetLock_1,offsetLock_2); | ||
ReentrantReadWriteLock offsetLock_3 = idLock.getLock(offset_2); | ||
Assert.assertNotEquals(offsetLock_1,offsetLock_3); | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.