diff --git a/hadoop-ozone/dist/src/main/smoketest/ozone-lib/shell_tests.robot b/hadoop-ozone/dist/src/main/smoketest/ozone-lib/shell_tests.robot index 22805efcb1b..651cda016f2 100644 --- a/hadoop-ozone/dist/src/main/smoketest/ozone-lib/shell_tests.robot +++ b/hadoop-ozone/dist/src/main/smoketest/ozone-lib/shell_tests.robot @@ -56,3 +56,11 @@ Compare Key With Local File with Different File Compare Key With Local File if File Does Not Exist ${matches} = Compare Key With Local File o3://${OM_SERVICE_ID}/vol1/bucket/passwd /no-such-file Should Be Equal ${matches} ${FALSE} + +Rejects Put Key With Zero Expected Generation + ${output} = Execute and checkrc ozone sh key put --expectedGeneration 0 o3://${OM_SERVICE_ID}/vol1/bucket/passwd /etc/passwd 255 + Should Contain ${output} must be positive + +Rejects Put Key With Negative Expected Generation + ${output} = Execute and checkrc ozone sh key put --expectedGeneration -1 o3://${OM_SERVICE_ID}/vol1/bucket/passwd /etc/passwd 255 + Should Contain ${output} must be positive diff --git a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/keys/PutKeyHandler.java b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/keys/PutKeyHandler.java index 833f4f7e779..35095dd7ff2 100644 --- a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/keys/PutKeyHandler.java +++ b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/shell/keys/PutKeyHandler.java @@ -71,7 +71,7 @@ public class PutKeyHandler extends KeyHandler { @Option(names = "--expectedGeneration", description = "Store key only if it already exists and its generation matches the value provided") - private long expectedGeneration; + private Long expectedGeneration; @Override protected void execute(OzoneClient client, OzoneAddress address) @@ -131,9 +131,14 @@ private void async( private OzoneOutputStream createOrReplaceKey(OzoneBucket bucket, String keyName, long size, Map keyMetadata, ReplicationConfig replicationConfig ) throws IOException { - return expectedGeneration > 0 - ? bucket.rewriteKey(keyName, size, expectedGeneration, replicationConfig, keyMetadata) - : bucket.createKey(keyName, size, replicationConfig, keyMetadata); + if (expectedGeneration != null) { + final long existingGeneration = expectedGeneration; + Preconditions.checkArgument(existingGeneration > 0, + "expectedGeneration must be positive, but was %s", existingGeneration); + return bucket.rewriteKey(keyName, size, existingGeneration, replicationConfig, keyMetadata); + } + + return bucket.createKey(keyName, size, replicationConfig, keyMetadata); } private void stream(