|
@@ -21,18 +21,26 @@ import org.apache.hadoop.conf.Configuration;
|
|
|
import org.apache.hadoop.fs.CommonConfigurationKeys;
|
|
|
import org.apache.hadoop.fs.CommonPathCapabilities;
|
|
|
import org.apache.hadoop.fs.FileSystem;
|
|
|
+import org.apache.hadoop.fs.LeaseRecoverable;
|
|
|
import org.apache.hadoop.fs.Options;
|
|
|
import org.apache.hadoop.fs.Path;
|
|
|
import org.apache.hadoop.fs.PathHandle;
|
|
|
+import org.apache.hadoop.fs.SafeMode;
|
|
|
+import org.apache.hadoop.fs.SafeModeAction;
|
|
|
import org.apache.hadoop.fs.viewfs.ConfigUtil;
|
|
|
import org.apache.hadoop.hdfs.protocol.HdfsConstants;
|
|
|
import org.apache.hadoop.test.Whitebox;
|
|
|
+import org.apache.hadoop.util.functional.ConsumerRaisingIOE;
|
|
|
+
|
|
|
+import org.assertj.core.api.Assertions;
|
|
|
import org.junit.Assert;
|
|
|
import org.junit.Test;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.net.URI;
|
|
|
|
|
|
+import static org.apache.hadoop.fs.CommonPathCapabilities.LEASE_RECOVERABLE;
|
|
|
+import static org.assertj.core.api.Assertions.assertThat;
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
import static org.junit.Assert.assertFalse;
|
|
|
import static org.junit.Assert.assertTrue;
|
|
@@ -195,18 +203,98 @@ public class TestViewDistributedFileSystem extends TestDistributedFileSystem{
|
|
|
|
|
|
@Test
|
|
|
public void testPathCapabilities() throws IOException {
|
|
|
- Configuration conf = getTestConfiguration();
|
|
|
- try (MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numDataNodes(0).build()) {
|
|
|
- URI defaultUri = URI.create(conf.get(CommonConfigurationKeys.FS_DEFAULT_NAME_KEY));
|
|
|
- conf.set("fs.viewfs.mounttable." + defaultUri.getHost() + ".linkFallback",
|
|
|
- defaultUri.toString());
|
|
|
- try (ViewDistributedFileSystem fileSystem = (ViewDistributedFileSystem) FileSystem.get(
|
|
|
- conf)) {
|
|
|
- final Path testFile = new Path("/test");
|
|
|
- assertTrue("ViewDfs supports truncate",
|
|
|
- fileSystem.hasPathCapability(testFile, CommonPathCapabilities.FS_TRUNCATE));
|
|
|
- }
|
|
|
+ try (MiniDFSCluster cluster = new MiniDFSCluster.Builder(getViewFsConfiguration())
|
|
|
+ .numDataNodes(0).build();
|
|
|
+ ViewDistributedFileSystem fileSystem = (ViewDistributedFileSystem) FileSystem.get(
|
|
|
+ cluster.getConfiguration(0))) {
|
|
|
+ final Path testFile = new Path("/test");
|
|
|
+ assertTrue("ViewDfs supports truncate",
|
|
|
+ fileSystem.hasPathCapability(testFile, CommonPathCapabilities.FS_TRUNCATE));
|
|
|
+ final boolean isLeaseRecoverable = fileSystem.hasPathCapability(testFile, LEASE_RECOVERABLE);
|
|
|
+ assertThat(isLeaseRecoverable).describedAs("path capabilities %s=%s in %s",
|
|
|
+ LEASE_RECOVERABLE, fileSystem.hasPathCapability(testFile, LEASE_RECOVERABLE),
|
|
|
+ fileSystem).isTrue();
|
|
|
+ assertThat(fileSystem).describedAs("filesystem %s", fileSystem)
|
|
|
+ .isInstanceOf(LeaseRecoverable.class);
|
|
|
+ assertThat(fileSystem).describedAs("filesystem %s", fileSystem).isInstanceOf(SafeMode.class);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSafeMode() throws IOException {
|
|
|
+ testSafeMode(this::executeAssertionsWithSafeMode);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSafeModeWithDeprecatedAPIs() throws IOException {
|
|
|
+ testSafeMode(this::executeAssertionsWithDeprecatedAPIs);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void testSafeMode(ConsumerRaisingIOE<ViewDistributedFileSystem> executeAssertionsFunction)
|
|
|
+ throws IOException {
|
|
|
+ try (MiniDFSCluster cluster = new MiniDFSCluster.Builder(getViewFsConfiguration())
|
|
|
+ .numDataNodes(0).build();
|
|
|
+ ViewDistributedFileSystem fileSystem = (ViewDistributedFileSystem) FileSystem.get(
|
|
|
+ cluster.getConfiguration(0))) {
|
|
|
+ executeAssertionsFunction.accept(fileSystem);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private SafeMode verifyAndGetSafeModeInstance(FileSystem fs) {
|
|
|
+ Assertions.assertThat(fs)
|
|
|
+ .describedAs("File system %s must be an instance of %s", fs, SafeMode.class.getClass())
|
|
|
+ .isInstanceOf(SafeMode.class);
|
|
|
+ return (SafeMode) fs;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void executeAssertionsWithSafeMode(ViewDistributedFileSystem fileSystem)
|
|
|
+ throws IOException {
|
|
|
+ SafeMode fsWithSafeMode = verifyAndGetSafeModeInstance(fileSystem);
|
|
|
+ assertSafeModeStatus(fsWithSafeMode, SafeModeAction.GET, false,
|
|
|
+ "Getting the status of safe mode before entering should be off.");
|
|
|
+ assertSafeModeStatus(fsWithSafeMode, SafeModeAction.ENTER, true,
|
|
|
+ "Entering Safe mode and safe mode turns on.");
|
|
|
+ assertSafeModeStatus(fsWithSafeMode, SafeModeAction.GET, true,
|
|
|
+ "Getting the status of safe mode after entering, safe mode should be on.");
|
|
|
+ assertSafeModeStatus(fsWithSafeMode, SafeModeAction.LEAVE, false,
|
|
|
+ "Leaving safe mode, and safe mode switches off.");
|
|
|
+ assertSafeModeStatus(fsWithSafeMode, SafeModeAction.FORCE_EXIT, false,
|
|
|
+ "Force exist safe mode at any time, safe mode should always switches off.");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void executeAssertionsWithDeprecatedAPIs(ViewDistributedFileSystem fileSystem)
|
|
|
+ throws IOException {
|
|
|
+ assertSafeModeStatus(fileSystem, HdfsConstants.SafeModeAction.SAFEMODE_GET, false,
|
|
|
+ "Getting the status of safe mode before entering should be off.");
|
|
|
+ assertSafeModeStatus(fileSystem, HdfsConstants.SafeModeAction.SAFEMODE_ENTER, true,
|
|
|
+ "Entering Safe mode and safe mode turns on.");
|
|
|
+ assertSafeModeStatus(fileSystem, HdfsConstants.SafeModeAction.SAFEMODE_GET, true,
|
|
|
+ "Getting the status of safe mode after entering, safe mode should be on.");
|
|
|
+ assertSafeModeStatus(fileSystem, HdfsConstants.SafeModeAction.SAFEMODE_LEAVE, false,
|
|
|
+ "Leaving safe mode, and safe mode switches off.");
|
|
|
+ assertSafeModeStatus(fileSystem, HdfsConstants.SafeModeAction.SAFEMODE_FORCE_EXIT, false,
|
|
|
+ "Force exist safe mode at any time, safe mode should always switches off.");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void assertSafeModeStatus(SafeMode fsWithSafeMode, SafeModeAction action,
|
|
|
+ boolean expectedStatus, String message) throws IOException {
|
|
|
+ Assertions.assertThat(fsWithSafeMode.setSafeMode(action)).describedAs(message)
|
|
|
+ .isEqualTo(expectedStatus);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void assertSafeModeStatus(ViewDistributedFileSystem fileSystem,
|
|
|
+ HdfsConstants.SafeModeAction action, boolean expectedStatus, String message)
|
|
|
+ throws IOException {
|
|
|
+ Assertions.assertThat(fileSystem.setSafeMode(action)).describedAs(message)
|
|
|
+ .isEqualTo(expectedStatus);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Configuration getViewFsConfiguration() {
|
|
|
+ Configuration conf = getTestConfiguration();
|
|
|
+ URI defaultUri = URI.create(conf.get(CommonConfigurationKeys.FS_DEFAULT_NAME_KEY));
|
|
|
+ conf.set("fs.viewfs.mounttable." + defaultUri.getHost() + ".linkFallback",
|
|
|
+ defaultUri.toString());
|
|
|
+ return conf;
|
|
|
+ }
|
|
|
+
|
|
|
}
|