HDFS-10183. Prevent race condition during class initialization. Contributed by Pavel Avgustinov.

This commit is contained in:
Sangjin Lee 2018-04-20 20:33:10 -07:00
parent 28e2244390
commit f40969a141
2 changed files with 6 additions and 6 deletions

View File

@ -40,7 +40,7 @@ class FSEditLogAsync extends FSEditLog implements Runnable {
// use separate mutex to avoid possible deadlock when stopping the thread.
private final Object syncThreadLock = new Object();
private Thread syncThread;
private static ThreadLocal<Edit> threadEdit = new ThreadLocal<Edit>();
private static final ThreadLocal<Edit> THREAD_EDIT = new ThreadLocal<Edit>();
// requires concurrent access from caller threads and syncing thread.
private final BlockingQueue<Edit> editPendingQ =
@ -114,16 +114,16 @@ public void close() {
@Override
void logEdit(final FSEditLogOp op) {
Edit edit = getEditInstance(op);
threadEdit.set(edit);
THREAD_EDIT.set(edit);
enqueueEdit(edit);
}
@Override
public void logSync() {
Edit edit = threadEdit.get();
Edit edit = THREAD_EDIT.get();
if (edit != null) {
// do NOT remove to avoid expunge & rehash penalties.
threadEdit.set(null);
THREAD_EDIT.set(null);
if (LOG.isDebugEnabled()) {
LOG.debug("logSync " + edit);
}

View File

@ -157,7 +157,7 @@ public abstract class FSEditLogOp {
int rpcCallId;
public static class OpInstanceCache {
private static ThreadLocal<OpInstanceCacheMap> cache =
private static final ThreadLocal<OpInstanceCacheMap> CACHE =
new ThreadLocal<OpInstanceCacheMap>() {
@Override
protected OpInstanceCacheMap initialValue() {
@ -188,7 +188,7 @@ public OpInstanceCache get() {
@SuppressWarnings("unchecked")
public <T extends FSEditLogOp> T get(FSEditLogOpCodes opCode) {
return useCache ? (T)cache.get().get(opCode) : (T)newInstance(opCode);
return useCache ? (T)CACHE.get().get(opCode) : (T)newInstance(opCode);
}
private static FSEditLogOp newInstance(FSEditLogOpCodes opCode) {