Skip to content

Commit

Permalink
add closed flag
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvinKirs committed Aug 1, 2024
1 parent 2a4fafb commit 3810637
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 3810637

Please sign in to comment.