ChenSammi commented on code in PR #10183:
URL: https://github.com/apache/ozone/pull/10183#discussion_r4236557127
##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyRequest.java:
##########
@@ -1369,46 +1369,96 @@ protected void validateEncryptionKeyInfo(OmBucketInfo
bucketInfo, KeyArgs keyArg
}
/**
- * Validates atomic rewrite conditions for conditional writes.
- * <p>
- * For If-None-Match: * (expectedDataGeneration =
EXPECTED_GEN_CREATE_IF_NOT_EXISTS),
- * the key must NOT exist.
- * <p>
- * For If-Match with a specific generation, the key must exist with matching
updateID.
+ * Resolves conditional write state at request admission time.
*
- * @param dbKeyInfo the existing key info from the database (null if key
doesn't exist)
- * @param keyArgs the key arguments containing expected generation
- * @throws OMException if validation fails
+ * This validates expected generations as admission-time preconditions and
+ * rewrites matching ETags into expected generations for commit-time checks.
*/
- protected void validateAtomicRewrite(OmKeyInfo dbKeyInfo, KeyArgs keyArgs)
- throws OMException {
+ protected KeyArgs resolveConditionalWriteAtAdmission(OmKeyInfo dbKeyInfo,
+ KeyArgs keyArgs, Map<String, String> auditMap) throws OMException {
if (keyArgs.hasExpectedDataGeneration()) {
- long expectedGen = keyArgs.getExpectedDataGeneration();
- // If expectedGen is EXPECTED_GEN_CREATE_IF_NOT_EXISTS, it means the key
MUST NOT exist (If-None-Match)
- if (expectedGen == OzoneConsts.EXPECTED_GEN_CREATE_IF_ABSENT) {
- if (dbKeyInfo != null) {
- throw new OMException("Key already exists",
- OMException.ResultCodes.KEY_ALREADY_EXISTS);
- }
- } else {
- // If a key does not exist, or if it exists but the updateID do not
match, then fail this request.
- if (dbKeyInfo == null) {
- throw new OMException("Key not found during expected rewrite",
- OMException.ResultCodes.KEY_NOT_FOUND);
- }
- if (dbKeyInfo.getUpdateID() != expectedGen) {
- throw new OMException("Generation mismatch during expected rewrite",
- OMException.ResultCodes.KEY_NOT_FOUND);
- }
+ validateAtomicRewriteAtAdmission(dbKeyInfo,
keyArgs.getExpectedDataGeneration(), auditMap);
+ return keyArgs;
+ }
+
+ return validateAndResolveIfMatchAsExpectedGeneration(keyArgs, dbKeyInfo,
auditMap);
+ }
+
+ /**
+ * Validates the condition at request admission time.
+ *
+ * This form is for callers that resolve the admitted generation from
+ * persisted open-key state rather than from request key arguments.
+ */
+ protected void validateAtomicRewriteAtAdmission(OmKeyInfo dbKeyInfo,
+ Long expectedGen, Map<String, String> auditMap) throws OMException {
+ if (expectedGen == null) {
+ return;
+ }
+ addRewriteGenerationToAuditMap(expectedGen, auditMap);
+
+ if (expectedGen == OzoneConsts.EXPECTED_GEN_CREATE_IF_ABSENT) {
+ if (dbKeyInfo != null) {
+ throw new OMException("Key already exists",
OMException.ResultCodes.KEY_ALREADY_EXISTS);
+ }
+ return;
+ }
+
+ if (dbKeyInfo == null) {
+ throw new OMException("Key not found during expected rewrite",
OMException.ResultCodes.KEY_NOT_FOUND);
+ }
+ if (expectedGen != dbKeyInfo.getUpdateID()) {
+ throw new OMException("Generation mismatch during expected rewrite",
OMException.ResultCodes.KEY_NOT_FOUND);
+ }
+ }
+
+ /**
+ * Validates an already admitted condition at serialized commit time.
+ *
+ * This form is for callers that must check the admitted generation before
+ * building the final key info to commit.
+ */
+ protected void validateAtomicRewriteAtCommit(OmKeyInfo existing,
+ Long expectedGen, Map<String, String> auditMap) throws OMException {
+ if (expectedGen == null) {
+ return;
+ }
+ addRewriteGenerationToAuditMap(expectedGen, auditMap);
+
+ if (expectedGen == OzoneConsts.EXPECTED_GEN_CREATE_IF_ABSENT) {
+ if (existing != null) {
+ throw new OMException("Atomic create-if-not-exists conflicted with "
+ + "an existing key",
+ OMException.ResultCodes.ATOMIC_WRITE_CONFLICT);
}
+ return;
+ }
+
+ if (existing == null) {
+ throw new OMException("Atomic rewrite conflicted because the key no "
+ + "longer exists",
+ OMException.ResultCodes.ATOMIC_WRITE_CONFLICT);
Review Comment:
Shall we return OMException.ResultCodes.KEY_NOT_FOUND instead of
ATOMIC_WRITE_CONFLICT here?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]