kevinrr888 commented on code in PR #4524:
URL: https://github.com/apache/accumulo/pull/4524#discussion_r1700582782


##########
core/src/main/java/org/apache/accumulo/core/fate/MetaFateStore.java:
##########
@@ -104,19 +109,108 @@ public FateId create() {
   }
 
   @Override
-  protected void create(FateId fateId, FateKey key) {
+  public Optional<FateTxStore<T>> createAndReserve(FateKey fateKey) {
+    final var reservation = FateReservation.from(lockID, UUID.randomUUID());
+    final var fateId = fateIdGenerator.fromTypeAndKey(type(), fateKey);
+
     try {
-      zk.putPersistentData(getTXPath(fateId), new NodeValue(TStatus.NEW, 
key).serialize(),
-          NodeExistsPolicy.FAIL);
-    } catch (KeeperException | InterruptedException e) {
+      byte[] nodeVal = zk.mutateOrCreate(getTXPath(fateId),
+          new NodeValue(TStatus.NEW, reservation, fateKey).serialize(), 
currSerNodeVal -> {
+            // We are only returning a non-null value for the following cases:
+            // 1) The existing NodeValue for fateId is exactly the same as the 
value set for the
+            // node if it doesn't yet exist:
+            // TStatus = TStatus.NEW, FateReservation = reservation, FateKey = 
fateKey
+            // This might occur if there was a ZK server fault and the same 
write is running a 2nd
+            // time
+            // 2) The existing NodeValue for fateId has:
+            // TStatus = TStatus.NEW, no FateReservation present, FateKey = 
fateKey
+            // The fateId is NEW/unseeded and not reserved, so we can allow it 
to be reserved
+            NodeValue currNodeVal = new NodeValue(currSerNodeVal);
+            if (currNodeVal.status == TStatus.NEW && 
currNodeVal.isReservedBy(reservation)) {
+              verifyFateKey(fateId, currNodeVal.fateKey, fateKey);
+              return currSerNodeVal;
+            } else if (currNodeVal.status == TStatus.NEW && 
!currNodeVal.isReserved()) {
+              verifyFateKey(fateId, currNodeVal.fateKey, fateKey);
+              // NEW/unseeded transaction and not reserved, so we can allow it 
to be reserved
+              return new NodeValue(TStatus.NEW, reservation, 
fateKey).serialize();
+            } else {
+              log.trace(
+                  "fate id {} tstatus {} fate key {} is reserved {} is either 
currently reserved "
+                      + "or has already been seeded with work (non-NEW 
status), or both",
+                  fateId, currNodeVal.status, currNodeVal.fateKey.orElse(null),
+                  currNodeVal.isReserved());
+              // This will not change the value to null but will return null
+              return null;
+            }
+          });
+      if (nodeVal != null) {
+        return Optional.of(new FateTxStoreImpl(fateId, reservation));
+      } else {
+        return Optional.empty();
+      }
+    } catch (InterruptedException | KeeperException | 
AcceptableThriftTableOperationException e) {
+      throw new IllegalStateException(e);
+    }
+  }
+
+  @Override
+  public Optional<FateTxStore<T>> tryReserve(FateId fateId) {
+    // uniquely identify this attempt to reserve the fate operation data
+    FateReservation reservation = FateReservation.from(lockID, 
UUID.randomUUID());
+
+    try {
+      byte[] newSerNodeVal = zk.mutateExisting(getTXPath(fateId), 
currSerNodeVal -> {

Review Comment:
   Ah okay, I thought we would want to throw the exception in the case the node 
doesn't exist. Changed and added check to 
MultipleStoresIT.testReserveUnreserve(). I see now that the tryReserve() for 
UFS and MFS were different for this case: UFS would return empty optional, MFS 
would throw an error, so this change is good and keeps them consistent. I 
remember noticing this difference a while ago when I was writing this, but must 
have forgot to write down/ask which functionality is expected. Glad this was 
spotted again.



-- 
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]

Reply via email to