Skip to content

Commit

Permalink
add static function to get lockIndex of some block
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingfei committed Jul 30, 2015
1 parent 7c19c04 commit 80e4bd5
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions servers/src/main/java/tachyon/worker/block/BlockLockManager.java
Expand Up @@ -45,6 +45,8 @@ public class BlockLockManager {
private static final int NUM_LOCKS = 1000;
/** The unique id of each lock */
private static final AtomicLong LOCK_ID_GEN = new AtomicLong(0);
/** A hashing function to map blockId to one of the locks */
private static final HashFunction HASH_FUNC = Hashing.murmur3_32();

/** The object that serves all metadata requests for the block store */
private final BlockMetadataManager mMetaManager;
Expand All @@ -56,8 +58,6 @@ public class BlockLockManager {
private final Map<Long, LockRecord> mLockIdToRecordMap = new HashMap<Long, LockRecord>();
/** To guard access on mLockIdToRecordMap and mUserIdToLockIdsMap */
private final Object mSharedMapsLock = new Object();
/** A hashing function to map blockId to one of the locks */
private final HashFunction mHashFunc = Hashing.murmur3_32();

public BlockLockManager(BlockMetadataManager metaManager) {
mMetaManager = Preconditions.checkNotNull(metaManager);
Expand All @@ -66,6 +66,10 @@ public BlockLockManager(BlockMetadataManager metaManager) {
}
}

public static int blockHashIndex(long blockId) {
return Math.abs(HASH_FUNC.hashLong(blockId).asInt()) % NUM_LOCKS;
}

/**
* Locks a block if it exists, throws IOException otherwise.
*
Expand All @@ -77,8 +81,8 @@ public BlockLockManager(BlockMetadataManager metaManager) {
*/
public long lockBlock(long userId, long blockId, BlockLockType blockLockType) throws IOException {
// hashing blockId into the range of [0, NUM_LOCKS-1]
int hashValue = Math.abs(mHashFunc.hashLong(blockId).asInt()) % NUM_LOCKS;
ClientRWLock blockLock = mLockArray[hashValue];
int index = blockHashIndex(blockId);
ClientRWLock blockLock = mLockArray[index];
Lock lock;
if (blockLockType == BlockLockType.READ) {
lock = blockLock.readLock();
Expand Down

0 comments on commit 80e4bd5

Please sign in to comment.