HDFS-6229. Merge r1586714 from trunk.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1586715 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jing Zhao 2014-04-11 16:45:06 +00:00
parent 77beb3573c
commit 4d83618ec6
4 changed files with 41 additions and 5 deletions

View File

@ -20,6 +20,7 @@
import java.util.Arrays; import java.util.Arrays;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -183,6 +184,8 @@ public Object getPayload() {
private final long expirationTime; private final long expirationTime;
private String cacheName; private String cacheName;
private final ReentrantLock lock = new ReentrantLock();
/** /**
* Constructor * Constructor
* @param cacheName name to identify the cache by * @param cacheName name to identify the cache by
@ -206,6 +209,13 @@ private static boolean skipRetryCache() {
|| Arrays.equals(Server.getClientId(), RpcConstants.DUMMY_CLIENT_ID); || Arrays.equals(Server.getClientId(), RpcConstants.DUMMY_CLIENT_ID);
} }
public void lock() {
this.lock.lock();
}
public void unlock() {
this.lock.unlock();
}
private void incrCacheClearedCounter() { private void incrCacheClearedCounter() {
retryCacheMetrics.incrCacheCleared(); retryCacheMetrics.incrCacheCleared();
@ -247,7 +257,8 @@ public String getCacheName() {
*/ */
private CacheEntry waitForCompletion(CacheEntry newEntry) { private CacheEntry waitForCompletion(CacheEntry newEntry) {
CacheEntry mapEntry = null; CacheEntry mapEntry = null;
synchronized (this) { lock.lock();
try {
mapEntry = set.get(newEntry); mapEntry = set.get(newEntry);
// If an entry in the cache does not exist, add a new one // If an entry in the cache does not exist, add a new one
if (mapEntry == null) { if (mapEntry == null) {
@ -262,6 +273,8 @@ private CacheEntry waitForCompletion(CacheEntry newEntry) {
} else { } else {
retryCacheMetrics.incrCacheHit(); retryCacheMetrics.incrCacheHit();
} }
} finally {
lock.unlock();
} }
// Entry already exists in cache. Wait for completion and return its state // Entry already exists in cache. Wait for completion and return its state
Preconditions.checkNotNull(mapEntry, Preconditions.checkNotNull(mapEntry,
@ -292,8 +305,11 @@ private CacheEntry waitForCompletion(CacheEntry newEntry) {
public void addCacheEntry(byte[] clientId, int callId) { public void addCacheEntry(byte[] clientId, int callId) {
CacheEntry newEntry = new CacheEntry(clientId, callId, System.nanoTime() CacheEntry newEntry = new CacheEntry(clientId, callId, System.nanoTime()
+ expirationTime, true); + expirationTime, true);
synchronized(this) { lock.lock();
try {
set.put(newEntry); set.put(newEntry);
} finally {
lock.unlock();
} }
retryCacheMetrics.incrCacheUpdated(); retryCacheMetrics.incrCacheUpdated();
} }
@ -303,8 +319,11 @@ public void addCacheEntryWithPayload(byte[] clientId, int callId,
// since the entry is loaded from editlog, we can assume it succeeded. // since the entry is loaded from editlog, we can assume it succeeded.
CacheEntry newEntry = new CacheEntryWithPayload(clientId, callId, payload, CacheEntry newEntry = new CacheEntryWithPayload(clientId, callId, payload,
System.nanoTime() + expirationTime, true); System.nanoTime() + expirationTime, true);
synchronized(this) { lock.lock();
try {
set.put(newEntry); set.put(newEntry);
} finally {
lock.unlock();
} }
retryCacheMetrics.incrCacheUpdated(); retryCacheMetrics.incrCacheUpdated();
} }

View File

@ -131,6 +131,9 @@ Release 2.4.1 - UNRELEASED
HDFS-6235. TestFileJournalManager can fail on Windows due to file locking if HDFS-6235. TestFileJournalManager can fail on Windows due to file locking if
tests run out of order. (cnauroth) tests run out of order. (cnauroth)
HDFS-6229. Race condition in failover can cause RetryCache fail to work.
(jing9)
Release 2.4.0 - 2014-04-07 Release 2.4.0 - 2014-04-07
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -791,7 +791,19 @@ static FSNamesystem loadFromDisk(Configuration conf) throws IOException {
public RetryCache getRetryCache() { public RetryCache getRetryCache() {
return retryCache; return retryCache;
} }
void lockRetryCache() {
if (retryCache != null) {
retryCache.lock();
}
}
void unlockRetryCache() {
if (retryCache != null) {
retryCache.unlock();
}
}
/** Whether or not retry cache is enabled */ /** Whether or not retry cache is enabled */
boolean hasRetryCache() { boolean hasRetryCache() {
return retryCache != null; return retryCache != null;
@ -6922,8 +6934,8 @@ String createSnapshot(String snapshotRoot, String snapshotName)
if (cacheEntry != null && cacheEntry.isSuccess()) { if (cacheEntry != null && cacheEntry.isSuccess()) {
return (String) cacheEntry.getPayload(); return (String) cacheEntry.getPayload();
} }
writeLock();
String snapshotPath = null; String snapshotPath = null;
writeLock();
try { try {
checkOperation(OperationCategory.WRITE); checkOperation(OperationCategory.WRITE);
checkNameNodeSafeMode("Cannot create snapshot for " + snapshotRoot); checkNameNodeSafeMode("Cannot create snapshot for " + snapshotRoot);

View File

@ -1569,10 +1569,12 @@ public void stopStandbyServices() throws IOException {
@Override @Override
public void writeLock() { public void writeLock() {
namesystem.writeLock(); namesystem.writeLock();
namesystem.lockRetryCache();
} }
@Override @Override
public void writeUnlock() { public void writeUnlock() {
namesystem.unlockRetryCache();
namesystem.writeUnlock(); namesystem.writeUnlock();
} }