Skip to content

Commit 964e089

Browse files
authored
HADOOP-19346. ViewFileSystem.InnerCache: Replaced ReentrantReadWriteLock with ConcurrentHashMap/putIfAbsent() (#7187)
* HADOOP-19346 ViewFileSystem.InnerCache: Replaced ReentrantReadWriteLock with ConcurrentHashMap/putIfAbsent()
1 parent 65a5bf3 commit 964e089

File tree

1 file changed

+20
-30
lines changed
  • hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs

1 file changed

+20
-30
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFileSystem.java

+20-30
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import static org.apache.hadoop.fs.viewfs.Constants.CONFIG_VIEWFS_TRASH_FORCE_INSIDE_MOUNT_POINT;
2929
import static org.apache.hadoop.fs.viewfs.Constants.CONFIG_VIEWFS_TRASH_FORCE_INSIDE_MOUNT_POINT_DEFAULT;
3030

31+
import java.util.concurrent.ConcurrentHashMap;
32+
import java.util.concurrent.ConcurrentMap;
3133
import java.util.function.Function;
3234
import java.io.FileNotFoundException;
3335
import java.io.IOException;
@@ -45,7 +47,6 @@
4547
import java.util.Map.Entry;
4648
import java.util.Objects;
4749
import java.util.Set;
48-
import java.util.concurrent.locks.ReentrantReadWriteLock;
4950

5051
import org.apache.hadoop.util.Preconditions;
5152
import org.apache.hadoop.classification.InterfaceAudience;
@@ -118,38 +119,32 @@ protected FsGetter fsGetter() {
118119
* Caching children filesystems. HADOOP-15565.
119120
*/
120121
static class InnerCache {
121-
private Map<Key, FileSystem> map = new HashMap<>();
122+
private ConcurrentMap<Key, FileSystem> map = new ConcurrentHashMap<>();
122123
private FsGetter fsCreator;
123-
private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
124124

125125
InnerCache(FsGetter fsCreator) {
126126
this.fsCreator = fsCreator;
127127
}
128128

129-
FileSystem get(URI uri, Configuration config) throws IOException {
130-
Key key = new Key(uri);
131-
FileSystem fs = null;
129+
// computeIfAbsent() does not support a mapping function which throws IOException.
130+
// Wrap fsCreator.getNewInstance() to not throw IOException and return null instead.
131+
FileSystem getNewFileSystem(URI uri, Configuration config) {
132132
try {
133-
rwLock.readLock().lock();
134-
fs = map.get(key);
135-
if (fs != null) {
136-
return fs;
137-
}
138-
} finally {
139-
rwLock.readLock().unlock();
133+
return fsCreator.getNewInstance(uri, config);
134+
} catch (IOException e) {
135+
LOG.error("Failed to create new FileSystem instance for " + uri, e);
136+
return null;
140137
}
141-
try {
142-
rwLock.writeLock().lock();
143-
fs = map.get(key);
144-
if (fs != null) {
145-
return fs;
146-
}
147-
fs = fsCreator.getNewInstance(uri, config);
148-
map.put(key, fs);
149-
return fs;
150-
} finally {
151-
rwLock.writeLock().unlock();
138+
}
139+
140+
FileSystem get(URI uri, Configuration config) throws IOException {
141+
Key key = new Key(uri);
142+
143+
FileSystem fs = map.computeIfAbsent(key, k -> getNewFileSystem(uri, config));
144+
if (fs == null) {
145+
throw new IOException("Failed to create new FileSystem instance for " + uri);
152146
}
147+
return fs;
153148
}
154149

155150
void closeAll() {
@@ -163,12 +158,7 @@ void closeAll() {
163158
}
164159

165160
void clear() {
166-
try {
167-
rwLock.writeLock().lock();
168-
map.clear();
169-
} finally {
170-
rwLock.writeLock().unlock();
171-
}
161+
map.clear();
172162
}
173163

174164
/**

0 commit comments

Comments
 (0)