下面列出了怎么用org.apache.hadoop.fs.PathIsNotEmptyDirectoryException的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testChildDeletion() throws Throwable {
ServiceRecord app = createRecord("app1",
PersistencePolicies.APPLICATION, "app",
null);
ServiceRecord container = createRecord("container1",
PersistencePolicies.CONTAINER, "container",
null);
operations.bind("/app", app, BindFlags.OVERWRITE);
operations.bind("/app/container", container, BindFlags.OVERWRITE);
try {
int p = purge("/",
"app1",
PersistencePolicies.APPLICATION,
RegistryAdminService.PurgePolicy.FailOnChildren);
fail("expected a failure, got a purge count of " + p);
} catch (PathIsNotEmptyDirectoryException expected) {
// expected
}
}
/**
* Remove a file/directory from the namespace.
* <p>
* For large directories, deletion is incremental. The blocks under
* the directory are collected and deleted a small number at a time holding
* the {@link FSNamesystem} lock.
* <p>
* For small directory or file the deletion is done in one shot.
*
*/
static BlocksMapUpdateInfo delete(
FSNamesystem fsn, String src, boolean recursive, boolean logRetryCache)
throws IOException {
FSDirectory fsd = fsn.getFSDirectory();
FSPermissionChecker pc = fsd.getPermissionChecker();
byte[][] pathComponents = FSDirectory.getPathComponentsForReservedPath(src);
src = fsd.resolvePath(pc, src, pathComponents);
final INodesInPath iip = fsd.getINodesInPath4Write(src, false);
if (!recursive && fsd.isNonEmptyDirectory(iip)) {
throw new PathIsNotEmptyDirectoryException(src + " is non empty");
}
if (fsd.isPermissionEnabled()) {
fsd.checkPermission(pc, iip, false, null, FsAction.WRITE, null,
FsAction.ALL, true);
}
return deleteInternal(fsn, src, iip, logRetryCache);
}
@Test
public void testChildDeletion() throws Throwable {
ServiceRecord app = createRecord("app1",
PersistencePolicies.APPLICATION, "app",
null);
ServiceRecord container = createRecord("container1",
PersistencePolicies.CONTAINER, "container",
null);
operations.bind("/app", app, BindFlags.OVERWRITE);
operations.bind("/app/container", container, BindFlags.OVERWRITE);
try {
int p = purge("/",
"app1",
PersistencePolicies.APPLICATION,
RegistryAdminService.PurgePolicy.FailOnChildren);
fail("expected a failure, got a purge count of " + p);
} catch (PathIsNotEmptyDirectoryException expected) {
// expected
}
}
/**
* Remove a file/directory from the namespace.
* <p>
* For large directories, deletion is incremental. The blocks under
* the directory are collected and deleted a small number at a time holding
* the {@link FSNamesystem} lock.
* <p>
* For small directory or file the deletion is done in one shot.
*
*/
static BlocksMapUpdateInfo delete(
FSNamesystem fsn, String src, boolean recursive, boolean logRetryCache)
throws IOException {
FSDirectory fsd = fsn.getFSDirectory();
FSPermissionChecker pc = fsd.getPermissionChecker();
byte[][] pathComponents = FSDirectory.getPathComponentsForReservedPath(src);
src = fsd.resolvePath(pc, src, pathComponents);
final INodesInPath iip = fsd.getINodesInPath4Write(src, false);
if (!recursive && fsd.isNonEmptyDirectory(iip)) {
throw new PathIsNotEmptyDirectoryException(src + " is non empty");
}
if (fsd.isPermissionEnabled()) {
fsd.checkPermission(pc, iip, false, null, FsAction.WRITE, null,
FsAction.ALL, true);
}
return deleteInternal(fsn, src, iip, logRetryCache);
}
/** @throws Exception If failed. */
@Test
public void testDeleteFailsIfNonRecursive() throws Exception {
Path fsHome = new Path(primaryFsUri);
Path someDir3 = new Path(fsHome, "/someDir1/someDir2/someDir3");
FSDataOutputStream os = fs.create(someDir3, EnumSet.noneOf(CreateFlag.class),
Options.CreateOpts.perms(FsPermission.getDefault()));
os.close();
final Path someDir2 = new Path(fsHome, "/someDir1/someDir2");
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override public Object call() throws Exception {
fs.delete(someDir2, false);
return null;
}
}, PathIsNotEmptyDirectoryException.class, null);
assertPathExists(fs, someDir2);
assertPathExists(fs, someDir3);
}
/**
* Perform a delete on a specified type.
* @param deletion a delete
* @param type possible values are 'files', 'subdirs', 'dirs'
* @return true if it deleted successfully, false otherwise
*/
private boolean deleteAction(Action<Boolean> deletion, String type, Path dir) {
boolean deleted;
try {
LOG.trace("Start deleting {} under {}", type, dir);
deleted = deletion.act();
} catch (PathIsNotEmptyDirectoryException exception) {
// N.B. HDFS throws this exception when we try to delete a non-empty directory, but
// LocalFileSystem throws a bare IOException. So some test code will get the verbose
// message below.
LOG.debug("Couldn't delete '{}' yet because it isn't empty w/exception.", dir, exception);
deleted = false;
} catch (IOException ioe) {
LOG.info("Could not delete {} under {}. might be transient; we'll retry. if it keeps "
+ "happening, use following exception when asking on mailing list.",
type, dir, ioe);
deleted = false;
} catch (Exception e) {
LOG.info("unexpected exception: ", e);
deleted = false;
}
LOG.trace("Finish deleting {} under {}, deleted=", type, dir, deleted);
return deleted;
}
/**
* Helper function. Delete a path non-recursively and expect failure.
* @param f Path to delete.
* @throws IOException
*/
private void deleteNonRecursivelyAndFail(Path f) throws IOException {
try {
fs.delete(f, false);
Assert.fail("Should have thrown PathIsNotEmptyDirectoryException!");
} catch (PathIsNotEmptyDirectoryException ignored) {
}
}
DeleteIterator(Path f, boolean recursive)
throws IOException {
super(f);
this.recursive = recursive;
if (getStatus().isDirectory()
&& !this.recursive
&& listStatus(f).length != 0) {
throw new PathIsNotEmptyDirectoryException(f.toString());
}
// Initialize bucket here to reduce number of RPC calls
OFSPath ofsPath = new OFSPath(f);
// TODO: Refactor later.
adapterImpl = (BasicRootedOzoneClientAdapterImpl) adapter;
this.bucket = adapterImpl.getBucket(ofsPath, false);
}
DeleteIterator(Path f, boolean recursive)
throws IOException {
super(f);
this.recursive = recursive;
if (getStatus().isDirectory()
&& !this.recursive
&& listStatus(f).length != 0) {
throw new PathIsNotEmptyDirectoryException(f.toString());
}
}
@Test
public void testDeleteNonEmpty() throws Throwable {
putExampleServiceEntry(ENTRY_PATH, 0);
try {
operations.delete(PARENT_PATH, false);
fail("Expected a failure");
} catch (PathIsNotEmptyDirectoryException expected) {
// expected; ignore
}
operations.delete(PARENT_PATH, true);
}
@Test
public void testRMNonRf() throws Throwable {
mkPath("/rm", CreateMode.PERSISTENT);
mkPath("/rm/child", CreateMode.PERSISTENT);
try {
curatorService.zkDelete("/rm", false, null);
fail("expected a failure");
} catch (PathIsNotEmptyDirectoryException expected) {
}
}
@Override
protected void processPath(PathData item) throws IOException {
if (!item.stat.isDirectory()) {
throw new PathIsNotDirectoryException(item.toString());
}
if (item.fs.listStatus(item.path).length == 0) {
if (!item.fs.delete(item.path, false)) {
throw new PathIOException(item.toString());
}
} else if (!ignoreNonEmpty) {
throw new PathIsNotEmptyDirectoryException(item.toString());
}
}
@Test
public void testDeleteNonEmpty() throws Throwable {
putExampleServiceEntry(ENTRY_PATH, 0);
try {
operations.delete(PARENT_PATH, false);
fail("Expected a failure");
} catch (PathIsNotEmptyDirectoryException expected) {
// expected; ignore
}
operations.delete(PARENT_PATH, true);
}
@Test
public void testRMNonRf() throws Throwable {
mkPath("/rm", CreateMode.PERSISTENT);
mkPath("/rm/child", CreateMode.PERSISTENT);
try {
curatorService.zkDelete("/rm", false, null);
fail("expected a failure");
} catch (PathIsNotEmptyDirectoryException expected) {
}
}
@Override
protected void processPath(PathData item) throws IOException {
if (!item.stat.isDirectory()) {
throw new PathIsNotDirectoryException(item.toString());
}
if (item.fs.listStatus(item.path).length == 0) {
if (!item.fs.delete(item.path, false)) {
throw new PathIOException(item.toString());
}
} else if (!ignoreNonEmpty) {
throw new PathIsNotEmptyDirectoryException(item.toString());
}
}
/**
* Cast IO exception to IGFS exception.
*
* @param msg Error message.
* @param e IO exception.
* @return IGFS exception.
*/
public static IgfsException cast(String msg, IOException e) {
if (e instanceof FileNotFoundException)
return new IgfsPathNotFoundException(e);
else if (e instanceof ParentNotDirectoryException)
return new IgfsParentNotDirectoryException(msg, e);
else if (e instanceof PathIsNotEmptyDirectoryException)
return new IgfsDirectoryNotEmptyException(e);
else if (e instanceof PathExistsException)
return new IgfsPathAlreadyExistsException(msg, e);
else
return new IgfsException(msg, e);
}
/**
* Delete a path.
*
* If the operation returns without an error then the entry has been
* deleted.
* @param path path delete recursively
* @param recursive recursive flag
* @throws PathNotFoundException path is not in the registry.
* @throws InvalidPathnameException the path is invalid.
* @throws PathIsNotEmptyDirectoryException path has child entries, but
* recursive is false.
* @throws IOException Any other IO Exception
*
*/
void delete(String path, boolean recursive)
throws PathNotFoundException,
PathIsNotEmptyDirectoryException,
InvalidPathnameException,
IOException;
/**
* Delete a path.
*
* If the operation returns without an error then the entry has been
* deleted.
* @param path path delete recursively
* @param recursive recursive flag
* @throws PathNotFoundException path is not in the registry.
* @throws InvalidPathnameException the path is invalid.
* @throws PathIsNotEmptyDirectoryException path has child entries, but
* recursive is false.
* @throws IOException Any other IO Exception
*
*/
void delete(String path, boolean recursive)
throws PathNotFoundException,
PathIsNotEmptyDirectoryException,
InvalidPathnameException,
IOException;