From 3810637a3ed276ce69d25498f71b6db026b73fdc Mon Sep 17 00:00:00 2001 From: Calvin Kirs Date: Thu, 1 Aug 2024 10:52:17 +0800 Subject: [PATCH] add closed flag --- .../doris/fs/remote/RemoteFileSystem.java | 20 +++++++++++-------- .../apache/doris/fs/remote/S3FileSystem.java | 7 +++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/fs/remote/RemoteFileSystem.java b/fe/fe-core/src/main/java/org/apache/doris/fs/remote/RemoteFileSystem.java index 045b48573c858a4..08b7e1cde78bef3 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/fs/remote/RemoteFileSystem.java +++ b/fe/fe-core/src/main/java/org/apache/doris/fs/remote/RemoteFileSystem.java @@ -37,11 +37,12 @@ import java.util.List; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.ReentrantLock; public abstract class RemoteFileSystem extends PersistentFileSystem implements Closeable { // this field will be visited by multi-threads, better use volatile qualifier protected volatile org.apache.hadoop.fs.FileSystem dfsFileSystem = null; - + private final ReentrantLock fsLock = new ReentrantLock(); protected static final AtomicBoolean closed = new AtomicBoolean(false); public RemoteFileSystem(String name, StorageBackend.StorageType type) { @@ -126,13 +127,16 @@ public Status renameDir(String origFilePath, } @Override - public synchronized void close() throws IOException { - if (closed.getAndSet(true)) { - return; - } - closed.set(true); - if (dfsFileSystem != null) { - dfsFileSystem.close(); + public void close() throws IOException { + fsLock.lock(); + try { + if (!closed.getAndSet(true)) { + if (dfsFileSystem != null) { + dfsFileSystem.close(); + } + } + } finally { + fsLock.unlock(); } } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/fs/remote/S3FileSystem.java b/fe/fe-core/src/main/java/org/apache/doris/fs/remote/S3FileSystem.java index 525d80d6797b353..aa7b9827de85866 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/fs/remote/S3FileSystem.java +++ b/fe/fe-core/src/main/java/org/apache/doris/fs/remote/S3FileSystem.java @@ -58,8 +58,15 @@ private void initFsProperties() { @Override protected FileSystem nativeFileSystem(String remotePath) throws UserException { + //todo Extracting a common method to achieve logic reuse + if (closed.get()) { + throw new UserException("FileSystem is closed."); + } if (dfsFileSystem == null) { synchronized (this) { + if (closed.get()) { + throw new UserException("FileSystem is closed."); + } if (dfsFileSystem == null) { Configuration conf = DFSFileSystem.getHdfsConf(ifNotSetFallbackToSimpleAuth()); System.setProperty("com.amazonaws.services.s3.enableV4", "true");