From 36f7e7e7660e9813db0ad1dab1d63d51391fbf17 Mon Sep 17 00:00:00 2001 From: elega <445092967@qq.com> Date: Sun, 9 Oct 2022 11:48:15 +0800 Subject: [PATCH] Change synchronized on FileSystemContext to avoid deadlock ### What changes are proposed in this pull request? Please outline the changes and how this PR fixes the issue. ### Why are the changes needed? Please clarify why the changes are needed. For instance, 1. If you propose a new API, clarify the use case for a new API. 2. If you fix a bug, describe the bug. ### Does this PR introduce any user facing changes? Please list the user-facing changes introduced by your change, including 1. change in user-facing APIs 2. addition or removal of property keys 3. webui pr-link: Alluxio/alluxio#16219 change-id: cid-f6e6af4556026e122b2e672fad6ab87a0a9f507e --- .../alluxio/client/file/FileSystemContext.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/core/client/fs/src/main/java/alluxio/client/file/FileSystemContext.java b/core/client/fs/src/main/java/alluxio/client/file/FileSystemContext.java index 7e3ffce0c2f4..5f0372990bd3 100644 --- a/core/client/fs/src/main/java/alluxio/client/file/FileSystemContext.java +++ b/core/client/fs/src/main/java/alluxio/client/file/FileSystemContext.java @@ -60,6 +60,7 @@ import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; import javax.annotation.Nullable; import javax.annotation.concurrent.GuardedBy; import javax.annotation.concurrent.ThreadSafe; @@ -156,11 +157,11 @@ public class FileSystemContext implements Closeable { private boolean mUriValidationEnabled = true; /** Cached map for workers. */ - @GuardedBy("this") - private volatile List mWorkerInfoList = null; + @GuardedBy("mWorkerInfoList") + private final AtomicReference> mWorkerInfoList = new AtomicReference<>(); /** The policy to refresh workers list. */ - @GuardedBy("this") + @GuardedBy("mWorkerInfoList") private final RefreshPolicy mWorkerRefreshPolicy; /** @@ -632,11 +633,14 @@ public synchronized WorkerNetAddress getNodeLocalWorker() throws IOException { * * @return the info of all block workers eligible for reads and writes */ - public synchronized List getCachedWorkers() throws IOException { - if (mWorkerInfoList == null || mWorkerInfoList.isEmpty() || mWorkerRefreshPolicy.attempt()) { - mWorkerInfoList = getAllWorkers(); + public List getCachedWorkers() throws IOException { + synchronized (mWorkerInfoList) { + if (mWorkerInfoList.get() == null || mWorkerInfoList.get().isEmpty() + || mWorkerRefreshPolicy.attempt()) { + mWorkerInfoList.set(getAllWorkers()); + } + return mWorkerInfoList.get(); } - return mWorkerInfoList; } /**