Skip to content

Commit

Permalink
HDDS-11441. ozone sh key put should only accept positive expectedGene…
Browse files Browse the repository at this point in the history
…ration (apache#7180)
  • Loading branch information
adoroszlai authored Sep 10, 2024
1 parent 33dbd4a commit 883a63f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -131,9 +131,14 @@ private void async(
private OzoneOutputStream createOrReplaceKey(OzoneBucket bucket, String keyName,
long size, Map<String, String> 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(
Expand Down

0 comments on commit 883a63f

Please sign in to comment.