28
28
import static org .apache .hadoop .fs .viewfs .Constants .CONFIG_VIEWFS_TRASH_FORCE_INSIDE_MOUNT_POINT ;
29
29
import static org .apache .hadoop .fs .viewfs .Constants .CONFIG_VIEWFS_TRASH_FORCE_INSIDE_MOUNT_POINT_DEFAULT ;
30
30
31
+ import java .util .concurrent .ConcurrentHashMap ;
32
+ import java .util .concurrent .ConcurrentMap ;
31
33
import java .util .function .Function ;
32
34
import java .io .FileNotFoundException ;
33
35
import java .io .IOException ;
45
47
import java .util .Map .Entry ;
46
48
import java .util .Objects ;
47
49
import java .util .Set ;
48
- import java .util .concurrent .locks .ReentrantReadWriteLock ;
49
50
50
51
import org .apache .hadoop .util .Preconditions ;
51
52
import org .apache .hadoop .classification .InterfaceAudience ;
@@ -118,38 +119,32 @@ protected FsGetter fsGetter() {
118
119
* Caching children filesystems. HADOOP-15565.
119
120
*/
120
121
static class InnerCache {
121
- private Map <Key , FileSystem > map = new HashMap <>();
122
+ private ConcurrentMap <Key , FileSystem > map = new ConcurrentHashMap <>();
122
123
private FsGetter fsCreator ;
123
- private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock ();
124
124
125
125
InnerCache (FsGetter fsCreator ) {
126
126
this .fsCreator = fsCreator ;
127
127
}
128
128
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 ) {
132
132
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 ;
140
137
}
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 );
152
146
}
147
+ return fs ;
153
148
}
154
149
155
150
void closeAll () {
@@ -163,12 +158,7 @@ void closeAll() {
163
158
}
164
159
165
160
void clear () {
166
- try {
167
- rwLock .writeLock ().lock ();
168
- map .clear ();
169
- } finally {
170
- rwLock .writeLock ().unlock ();
171
- }
161
+ map .clear ();
172
162
}
173
163
174
164
/**
0 commit comments