Skip to content

Commit

Permalink
Resolve deadlocks in FileSystemContext by removing 'synchronized'
Browse files Browse the repository at this point in the history
  • Loading branch information
elega committed Oct 8, 2022
1 parent d5b314c commit f3b2c60
Showing 1 changed file with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
Expand Down Expand Up @@ -158,9 +161,11 @@ public class FileSystemContext implements Closeable {
/** Whether to do URI scheme validation for file systems using this context. */
private boolean mUriValidationEnabled = true;

private final Lock mWorkerInfoListLock = new ReentrantLock();

/** Cached map for workers. */
@GuardedBy("this")
private volatile List<BlockWorkerInfo> mWorkerInfoList = null;
@GuardedBy("mWorkerInfoList")
private final AtomicReference<List<BlockWorkerInfo>> mWorkerInfoList = new AtomicReference<>();

/** The policy to refresh workers list. */
@GuardedBy("this")
Expand Down Expand Up @@ -636,11 +641,14 @@ public synchronized WorkerNetAddress getNodeLocalWorker() throws IOException {
*
* @return the info of all block workers eligible for reads and writes
*/
public synchronized List<BlockWorkerInfo> getCachedWorkers() throws IOException {
if (mWorkerInfoList == null || mWorkerInfoList.isEmpty() || mWorkerRefreshPolicy.attempt()) {
mWorkerInfoList = getAllWorkers();
public List<BlockWorkerInfo> getCachedWorkers() throws IOException {
synchronized (mWorkerInfoList) {
if (mWorkerInfoList.get() == null || mWorkerInfoList.get().isEmpty()
|| mWorkerRefreshPolicy.attempt()) {
mWorkerInfoList.set(getAllWorkers());
}
return mWorkerInfoList.get();
}
return mWorkerInfoList;
}

/**
Expand Down

0 comments on commit f3b2c60

Please sign in to comment.