diff --git a/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/s3guard/S3GuardTool.java b/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/s3guard/S3GuardTool.java index 527697f00f3..19dc32a896e 100644 --- a/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/s3guard/S3GuardTool.java +++ b/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/s3guard/S3GuardTool.java @@ -439,6 +439,10 @@ public abstract class S3GuardTool extends Configured implements Tool { static class SetCapacity extends S3GuardTool { public static final String NAME = "set-capacity"; public static final String PURPOSE = "Alter metadata store IO capacity"; + public static final String READ_CAP_INVALID = "Read capacity must have " + + "value greater than or equal to 1."; + public static final String WRITE_CAP_INVALID = "Write capacity must have " + + "value greater than or equal to 1."; private static final String USAGE = NAME + " [OPTIONS] [s3a://BUCKET]\n" + "\t" + PURPOSE + "\n\n" + "Common options:\n" + @@ -478,11 +482,17 @@ public abstract class S3GuardTool extends Configured implements Tool { String readCap = getCommandFormat().getOptValue(READ_FLAG); if (StringUtils.isNotEmpty(readCap)) { + Preconditions.checkArgument(Integer.parseInt(readCap) > 0, + READ_CAP_INVALID); + S3GuardTool.println(out, "Read capacity set to %s", readCap); options.put(S3GUARD_DDB_TABLE_CAPACITY_READ_KEY, readCap); } String writeCap = getCommandFormat().getOptValue(WRITE_FLAG); if (StringUtils.isNotEmpty(writeCap)) { + Preconditions.checkArgument(Integer.parseInt(writeCap) > 0, + WRITE_CAP_INVALID); + S3GuardTool.println(out, "Write capacity set to %s", writeCap); options.put(S3GUARD_DDB_TABLE_CAPACITY_WRITE_KEY, writeCap); } diff --git a/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/s3guard/AbstractS3GuardToolTestBase.java b/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/s3guard/AbstractS3GuardToolTestBase.java index 7d75f5233a4..f591e3258cf 100644 --- a/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/s3guard/AbstractS3GuardToolTestBase.java +++ b/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/s3guard/AbstractS3GuardToolTestBase.java @@ -51,6 +51,7 @@ import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.util.ExitUtil; import org.apache.hadoop.util.StringUtils; +import static org.apache.hadoop.fs.s3a.Constants.S3GUARD_DDB_TABLE_NAME_KEY; import static org.apache.hadoop.fs.s3a.Constants.S3GUARD_METASTORE_NULL; import static org.apache.hadoop.fs.s3a.Constants.S3_METADATA_STORE_IMPL; import static org.apache.hadoop.fs.s3a.s3guard.S3GuardTool.E_BAD_STATE; @@ -286,6 +287,23 @@ public abstract class AbstractS3GuardToolTestBase extends AbstractS3ATestBase { "prune", testPath.toString()); } + @Test + public void testSetCapacityFailFast() throws Exception{ + Configuration conf = getConfiguration(); + conf.set(S3GUARD_DDB_TABLE_NAME_KEY, getFileSystem().getBucket()); + + S3GuardTool.SetCapacity cmdR = new S3GuardTool.SetCapacity(conf); + String[] argsR = new String[]{cmdR.getName(), "-read", "0", "s3a://bucket"}; + intercept(IllegalArgumentException.class, + S3GuardTool.SetCapacity.READ_CAP_INVALID, () -> cmdR.run(argsR)); + + S3GuardTool.SetCapacity cmdW = new S3GuardTool.SetCapacity(conf); + String[] argsW = new String[]{cmdW.getName(), "-write", "0", + "s3a://bucket"}; + intercept(IllegalArgumentException.class, + S3GuardTool.SetCapacity.WRITE_CAP_INVALID, () -> cmdW.run(argsW)); + } + @Test public void testDestroyNoBucket() throws Throwable { intercept(FileNotFoundException.class,