keith-turner commented on code in PR #4524:
URL: https://github.com/apache/accumulo/pull/4524#discussion_r1702174500
##########
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:
I looked into adding a test for this in elasticity branch, but found it was
not worthwhile if the behavior will change. Could add at test to
`FateStoreIT.testAbsent()` in this PR that does the following.
```java
var optTxStore = store.tryReserve(fateId);
assertTrue(optTxStore.isEmpty());
```
--
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]