/
BaseFileSystem.java
520 lines (480 loc) · 19 KB
/
BaseFileSystem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package alluxio.client.file;
import alluxio.AlluxioURI;
import alluxio.Constants;
import alluxio.annotation.PublicApi;
import alluxio.client.file.options.CreateDirectoryOptions;
import alluxio.client.file.options.CreateFileOptions;
import alluxio.client.file.options.DeleteOptions;
import alluxio.client.file.options.ExistsOptions;
import alluxio.client.file.options.FreeOptions;
import alluxio.client.file.options.GetStatusOptions;
import alluxio.client.file.options.InStreamOptions;
import alluxio.client.file.options.ListStatusOptions;
import alluxio.client.file.options.LoadMetadataOptions;
import alluxio.client.file.options.MountOptions;
import alluxio.client.file.options.OpenFileOptions;
import alluxio.client.file.options.OutStreamOptions;
import alluxio.client.file.options.RenameOptions;
import alluxio.client.file.options.SetAclOptions;
import alluxio.client.file.options.SetAttributeOptions;
import alluxio.client.file.options.UnmountOptions;
import alluxio.exception.AlluxioException;
import alluxio.exception.DirectoryNotEmptyException;
import alluxio.exception.ExceptionMessage;
import alluxio.exception.FileAlreadyExistsException;
import alluxio.exception.FileDoesNotExistException;
import alluxio.exception.InvalidPathException;
import alluxio.exception.status.AlluxioStatusException;
import alluxio.exception.status.AlreadyExistsException;
import alluxio.exception.status.FailedPreconditionException;
import alluxio.exception.status.InvalidArgumentException;
import alluxio.exception.status.NotFoundException;
import alluxio.exception.status.UnavailableException;
import alluxio.master.MasterInquireClient;
import alluxio.security.authorization.AclEntry;
import alluxio.uri.Authority;
import alluxio.wire.LoadMetadataType;
import alluxio.wire.MountPointInfo;
import alluxio.wire.SetAclAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.annotation.concurrent.ThreadSafe;
/**
* Default implementation of the {@link FileSystem} interface. Developers can extend this class
* instead of implementing the interface. This implementation reads and writes data through
* {@link FileInStream} and {@link FileOutStream}. This class is thread safe.
*/
@PublicApi
@ThreadSafe
public class BaseFileSystem implements FileSystem {
private static final Logger LOG = LoggerFactory.getLogger(BaseFileSystem.class);
protected final FileSystemContext mFileSystemContext;
/**
* @param context file system context
* @return a {@link BaseFileSystem}
*/
public static BaseFileSystem get(FileSystemContext context) {
return new BaseFileSystem(context);
}
/**
* Constructs a new base file system.
*
* @param context file system context
*/
protected BaseFileSystem(FileSystemContext context) {
mFileSystemContext = context;
}
@Override
public void createDirectory(AlluxioURI path)
throws FileAlreadyExistsException, InvalidPathException, IOException, AlluxioException {
createDirectory(path, CreateDirectoryOptions.defaults());
}
@Override
public void createDirectory(AlluxioURI path, CreateDirectoryOptions options)
throws FileAlreadyExistsException, InvalidPathException, IOException, AlluxioException {
checkUri(path);
FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient();
try {
masterClient.createDirectory(path, options);
LOG.debug("Created directory {}, options: {}", path.getPath(), options);
} catch (AlreadyExistsException e) {
throw new FileAlreadyExistsException(e.getMessage());
} catch (InvalidArgumentException e) {
throw new InvalidPathException(e.getMessage());
} catch (UnavailableException e) {
throw e;
} catch (AlluxioStatusException e) {
throw e.toAlluxioException();
} finally {
mFileSystemContext.releaseMasterClient(masterClient);
}
}
@Override
public FileOutStream createFile(AlluxioURI path)
throws FileAlreadyExistsException, InvalidPathException, IOException, AlluxioException {
return createFile(path, CreateFileOptions.defaults());
}
@Override
public FileOutStream createFile(AlluxioURI path, CreateFileOptions options)
throws FileAlreadyExistsException, InvalidPathException, IOException, AlluxioException {
checkUri(path);
FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient();
URIStatus status;
try {
masterClient.createFile(path, options);
// Do not sync before this getStatus, since the UFS file is expected to not exist.
GetStatusOptions opts = GetStatusOptions.defaults();
opts.setLoadMetadataType(LoadMetadataType.Never);
opts.getCommonOptions().setSyncIntervalMs(-1);
status = masterClient.getStatus(path, opts);
LOG.debug("Created file {}, options: {}", path.getPath(), options);
} catch (AlreadyExistsException e) {
throw new FileAlreadyExistsException(e.getMessage());
} catch (InvalidArgumentException e) {
throw new InvalidPathException(e.getMessage());
} catch (UnavailableException e) {
throw e;
} catch (AlluxioStatusException e) {
throw e.toAlluxioException();
} finally {
mFileSystemContext.releaseMasterClient(masterClient);
}
OutStreamOptions outStreamOptions = options.toOutStreamOptions();
outStreamOptions.setUfsPath(status.getUfsPath());
outStreamOptions.setMountId(status.getMountId());
outStreamOptions.setAcl(status.getAcl());
try {
return new FileOutStream(path, outStreamOptions, mFileSystemContext);
} catch (Exception e) {
delete(path);
throw e;
}
}
@Override
public void delete(AlluxioURI path)
throws DirectoryNotEmptyException, FileDoesNotExistException, IOException, AlluxioException {
delete(path, DeleteOptions.defaults());
}
@Override
public void delete(AlluxioURI path, DeleteOptions options)
throws DirectoryNotEmptyException, FileDoesNotExistException, IOException, AlluxioException {
checkUri(path);
FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient();
try {
masterClient.delete(path, options);
LOG.debug("Deleted {}, options: {}", path.getPath(), options);
} catch (FailedPreconditionException e) {
// A little sketchy, but this should be the only case that throws FailedPrecondition.
throw new DirectoryNotEmptyException(e.getMessage());
} catch (NotFoundException e) {
throw new FileDoesNotExistException(e.getMessage());
} catch (UnavailableException e) {
throw e;
} catch (AlluxioStatusException e) {
throw e.toAlluxioException();
} finally {
mFileSystemContext.releaseMasterClient(masterClient);
}
}
@Override
public boolean exists(AlluxioURI path)
throws InvalidPathException, IOException, AlluxioException {
return exists(path, ExistsOptions.defaults());
}
@Override
public boolean exists(AlluxioURI path, ExistsOptions options)
throws InvalidPathException, IOException, AlluxioException {
checkUri(path);
FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient();
try {
// TODO(calvin): Make this more efficient
masterClient.getStatus(path, options.toGetStatusOptions());
return true;
} catch (NotFoundException e) {
return false;
} catch (InvalidArgumentException e) {
// The server will throw this when a prefix of the path is a file.
// TODO(andrew): Change the server so that a prefix being a file means the path does not exist
return false;
} catch (UnavailableException e) {
throw e;
} catch (AlluxioStatusException e) {
throw e.toAlluxioException();
} finally {
mFileSystemContext.releaseMasterClient(masterClient);
}
}
@Override
public void free(AlluxioURI path)
throws FileDoesNotExistException, IOException, AlluxioException {
free(path, FreeOptions.defaults());
}
@Override
public void free(AlluxioURI path, FreeOptions options)
throws FileDoesNotExistException, IOException, AlluxioException {
checkUri(path);
FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient();
try {
masterClient.free(path, options);
LOG.debug("Freed {}, options: {}", path.getPath(), options);
} catch (NotFoundException e) {
throw new FileDoesNotExistException(e.getMessage());
} catch (UnavailableException e) {
throw e;
} catch (AlluxioStatusException e) {
throw e.toAlluxioException();
} finally {
mFileSystemContext.releaseMasterClient(masterClient);
}
}
@Override
public URIStatus getStatus(AlluxioURI path)
throws FileDoesNotExistException, IOException, AlluxioException {
return getStatus(path, GetStatusOptions.defaults());
}
@Override
public URIStatus getStatus(AlluxioURI path, GetStatusOptions options)
throws FileDoesNotExistException, IOException, AlluxioException {
checkUri(path);
FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient();
try {
return masterClient.getStatus(path, options);
} catch (NotFoundException e) {
throw new FileDoesNotExistException(ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(path));
} catch (UnavailableException e) {
throw e;
} catch (AlluxioStatusException e) {
throw e.toAlluxioException();
} finally {
mFileSystemContext.releaseMasterClient(masterClient);
}
}
@Override
public List<URIStatus> listStatus(AlluxioURI path)
throws FileDoesNotExistException, IOException, AlluxioException {
return listStatus(path, ListStatusOptions.defaults());
}
@Override
public List<URIStatus> listStatus(AlluxioURI path, ListStatusOptions options)
throws FileDoesNotExistException, IOException, AlluxioException {
checkUri(path);
FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient();
// TODO(calvin): Fix the exception handling in the master
try {
return masterClient.listStatus(path, options);
} catch (NotFoundException e) {
throw new FileDoesNotExistException(ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(path));
} catch (UnavailableException e) {
throw e;
} catch (AlluxioStatusException e) {
throw e.toAlluxioException();
} finally {
mFileSystemContext.releaseMasterClient(masterClient);
}
}
/**
* {@inheritDoc}
*
* @deprecated since version 1.1 and will be removed in version 2.0
*/
@Deprecated
@Override
public void loadMetadata(AlluxioURI path)
throws FileDoesNotExistException, IOException, AlluxioException {
loadMetadata(path, LoadMetadataOptions.defaults());
}
/**
* {@inheritDoc}
*
* @deprecated since version 1.1 and will be removed in version 2.0
*/
@Deprecated
@Override
public void loadMetadata(AlluxioURI path, LoadMetadataOptions options)
throws FileDoesNotExistException, IOException, AlluxioException {
checkUri(path);
FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient();
try {
masterClient.loadMetadata(path, options);
LOG.debug("Loaded metadata {}, options: {}", path.getPath(), options);
} catch (NotFoundException e) {
throw new FileDoesNotExistException(e.getMessage());
} catch (UnavailableException e) {
throw e;
} catch (AlluxioStatusException e) {
throw e.toAlluxioException();
} finally {
mFileSystemContext.releaseMasterClient(masterClient);
}
}
@Override
public void mount(AlluxioURI alluxioPath, AlluxioURI ufsPath)
throws IOException, AlluxioException {
mount(alluxioPath, ufsPath, MountOptions.defaults());
}
@Override
public void mount(AlluxioURI alluxioPath, AlluxioURI ufsPath, MountOptions options)
throws IOException, AlluxioException {
checkUri(alluxioPath);
FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient();
try {
// TODO(calvin): Make this fail on the master side
masterClient.mount(alluxioPath, ufsPath, options);
LOG.info("Mount " + ufsPath.toString() + " to " + alluxioPath.getPath());
} catch (UnavailableException e) {
throw e;
} catch (AlluxioStatusException e) {
throw e.toAlluxioException();
} finally {
mFileSystemContext.releaseMasterClient(masterClient);
}
}
@Override
public Map<String, MountPointInfo> getMountTable() throws IOException, AlluxioException {
FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient();
try {
return masterClient.getMountTable();
} catch (UnavailableException e) {
throw e;
} catch (AlluxioStatusException e) {
throw e.toAlluxioException();
} finally {
mFileSystemContext.releaseMasterClient(masterClient);
}
}
@Override
public FileInStream openFile(AlluxioURI path)
throws FileDoesNotExistException, IOException, AlluxioException {
return openFile(path, OpenFileOptions.defaults());
}
@Override
public FileInStream openFile(AlluxioURI path, OpenFileOptions options)
throws FileDoesNotExistException, IOException, AlluxioException {
checkUri(path);
URIStatus status = getStatus(path);
if (status.isFolder()) {
throw new FileDoesNotExistException(
ExceptionMessage.CANNOT_READ_DIRECTORY.getMessage(status.getName()));
}
InStreamOptions inStreamOptions = options.toInStreamOptions(status);
return new FileInStream(status, inStreamOptions, mFileSystemContext);
}
@Override
public void rename(AlluxioURI src, AlluxioURI dst)
throws FileDoesNotExistException, IOException, AlluxioException {
rename(src, dst, RenameOptions.defaults());
}
@Override
public void rename(AlluxioURI src, AlluxioURI dst, RenameOptions options)
throws FileDoesNotExistException, IOException, AlluxioException {
checkUri(src);
checkUri(dst);
FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient();
try {
// TODO(calvin): Update this code on the master side.
masterClient.rename(src, dst, options);
LOG.debug("Renamed {} to {}, options: {}", src.getPath(), dst.getPath(), options);
} catch (NotFoundException e) {
throw new FileDoesNotExistException(e.getMessage());
} catch (UnavailableException e) {
throw e;
} catch (AlluxioStatusException e) {
throw e.toAlluxioException();
} finally {
mFileSystemContext.releaseMasterClient(masterClient);
}
}
@Override
public void setAcl(AlluxioURI path, SetAclAction action, List<AclEntry> entries)
throws FileDoesNotExistException, IOException, AlluxioException {
setAcl(path, action, entries, SetAclOptions.defaults());
}
@Override
public void setAcl(AlluxioURI path, SetAclAction action, List<AclEntry> entries,
SetAclOptions options) throws FileDoesNotExistException, IOException, AlluxioException {
checkUri(path);
FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient();
try {
masterClient.setAcl(path, action, entries, options);
LOG.debug("Set ACL for {}, entries: {} options: {}", path.getPath(), entries, options);
} catch (NotFoundException e) {
throw new FileDoesNotExistException(e.getMessage());
} catch (UnavailableException e) {
throw e;
} catch (AlluxioStatusException e) {
throw e.toAlluxioException();
} finally {
mFileSystemContext.releaseMasterClient(masterClient);
}
}
@Override
public void setAttribute(AlluxioURI path)
throws FileDoesNotExistException, IOException, AlluxioException {
setAttribute(path, SetAttributeOptions.defaults());
}
@Override
public void setAttribute(AlluxioURI path, SetAttributeOptions options)
throws FileDoesNotExistException, IOException, AlluxioException {
checkUri(path);
FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient();
try {
masterClient.setAttribute(path, options);
LOG.debug("Set attributes for {}, options: {}", path.getPath(), options);
} catch (NotFoundException e) {
throw new FileDoesNotExistException(e.getMessage());
} catch (UnavailableException e) {
throw e;
} catch (AlluxioStatusException e) {
throw e.toAlluxioException();
} finally {
mFileSystemContext.releaseMasterClient(masterClient);
}
}
@Override
public void unmount(AlluxioURI path) throws IOException, AlluxioException {
unmount(path, UnmountOptions.defaults());
}
@Override
public void unmount(AlluxioURI path, UnmountOptions options)
throws IOException, AlluxioException {
checkUri(path);
FileSystemMasterClient masterClient = mFileSystemContext.acquireMasterClient();
try {
masterClient.unmount(path);
LOG.debug("Unmounted {}, options: {}", path.getPath(), options);
} catch (UnavailableException e) {
throw e;
} catch (AlluxioStatusException e) {
throw e.toAlluxioException();
} finally {
mFileSystemContext.releaseMasterClient(masterClient);
}
}
/**
* Checks an {@link AlluxioURI} for scheme and authority information. Warn the user and throw an
* exception if necessary.
*/
private static void checkUri(AlluxioURI uri) {
if (uri.hasScheme()) {
String warnMsg = "The URI scheme \"{}\" is ignored and not required in URIs passed to"
+ " the Alluxio Filesystem client.";
if (uri.getScheme().equals(Constants.SCHEME)) {
LOG.warn(warnMsg, Constants.SCHEME);
} else if (uri.getScheme().equals(Constants.SCHEME_FT)) {
LOG.warn(warnMsg, Constants.SCHEME_FT);
} else {
throw new IllegalArgumentException(
String.format("Scheme %s:// in AlluxioURI is invalid. Schemes in filesystem"
+ " operations are ignored. \"alluxio://\" or no scheme at all is valid.",
uri.getScheme()));
}
}
if (uri.hasAuthority()) {
LOG.warn("The URI authority (hostname and port) is ignored and not required in URIs passed "
+ "to the Alluxio Filesystem client.");
/* Even if we choose to log the warning, check if the Configuration host matches what the
* user passes. If not, throw an exception letting the user know they don't match.
*/
Authority configured = MasterInquireClient.Factory.create().getConnectDetails().toAuthority();
if (!configured.equals(uri.getAuthority())) {
throw new IllegalArgumentException(
String.format("The URI authority %s does not match the configured " + "value of %s.",
uri.getAuthority(), configured));
}
}
return;
}
}