aweisberg commented on code in PR #7:
URL: https://github.com/apache/cassandra-accord/pull/7#discussion_r1014381206
##########
accord-core/src/main/java/accord/local/Command.java:
##########
@@ -399,158 +525,253 @@ private void onChangeInternal(PartialCommand command)
case Committed:
case ReadyToExecute:
- case Executed:
+ case PreApplied:
case Applied:
case Invalidated:
- if (isUnableToApply())
- {
- updatePredecessor(command);
- if (isWaitingOnCommit())
- {
- removeWaitingOnCommit(command);
- }
- }
- else
- {
- command.removeListener(this);
- }
- maybeExecute(false);
+ updatePredecessor(command, false);
+ maybeExecute(safeStore, progressShard(safeStore), false, true);
break;
}
}
- @Override
- public void onChange(Command command)
- {
- onChangeInternal(command);
- }
-
- protected void postApply()
+ protected void postApply(SafeCommandStore safeStore)
{
logger.trace("{} applied, setting status to Applied and notifying
listeners", txnId());
- status(Applied);
- notifyListeners();
+ setStatus(Applied);
+ notifyListeners(safeStore);
}
- private static Function<CommandStore, Void> callPostApply(TxnId txnId)
+ private static Function<SafeCommandStore, Void> callPostApply(TxnId txnId)
{
- return commandStore -> {
- commandStore.command(txnId).postApply();
+ return safeStore -> {
+ safeStore.command(txnId).postApply(safeStore);
return null;
};
}
- protected Future<Void> apply()
+ protected Future<Void> apply(SafeCommandStore safeStore)
{
// important: we can't include a reference to *this* in the lambda,
since the C* implementation may evict
// the command instance from memory between now and the write
completing (and post apply being called)
- return writes().apply(commandStore()).flatMap(unused ->
- commandStore().process(this, callPostApply(txnId()))
+ CommandStore unsafeStore = safeStore.commandStore();
+ return writes().apply(safeStore).flatMap(unused ->
+ unsafeStore.submit(this, callPostApply(txnId()))
);
}
- public Future<Data> read(Keys scope)
+ public Future<Data> read(SafeCommandStore safeStore)
{
- return txn().read(this);
+ return partialTxn().read(safeStore, this);
}
- private Future<Void> maybeExecute(boolean notifyListenersOnNoop)
+ // TODO: maybe split into maybeExecute and maybeApply?
+ private boolean maybeExecute(SafeCommandStore safeStore, ProgressShard
shard, boolean alwaysNotifyListeners, boolean notifyWaitingOn)
{
if (logger.isTraceEnabled())
- logger.trace("{}: Maybe executing with status {}. Will notify
listeners on noop: {}", txnId(), status(), notifyListenersOnNoop);
+ logger.trace("{}: Maybe executing with status {}. Will notify
listeners on noop: {}", txnId(), status(), alwaysNotifyListeners);
- if (status() != Committed && status() != Executed)
+ if (status() != Committed && status() != PreApplied)
{
- if (notifyListenersOnNoop) notifyListeners();
- return Writes.SUCCESS;
+ if (alwaysNotifyListeners)
+ notifyListeners(safeStore);
+ return false;
}
- if (isUnableToApply())
+ if (isUnableToExecute())
{
- BlockedBy blockedBy = blockedBy();
- if (blockedBy != null)
- {
- logger.trace("{}: not executing, blocked on {}", txnId(),
blockedBy.command.txnId());
- commandStore().progressLog().waiting(blockedBy.command,
blockedBy.someKeys);
- if (notifyListenersOnNoop) notifyListeners();
- return Writes.SUCCESS;
- }
- assert !isWaitingOnApply();
+ if (alwaysNotifyListeners)
+ notifyListeners(safeStore);
+
+ if (notifyWaitingOn)
+ new NotifyWaitingOn(this).accept(safeStore);
+ return false;
}
switch (status())
{
case Committed:
// TODO: maintain distinct ReadyToRead and ReadyToWrite states
- status(ReadyToExecute);
+ setStatus(ReadyToExecute);
logger.trace("{}: set to ReadyToExecute", txnId());
- boolean isProgressShard = progressKey() != null &&
handles(txnId().epoch, progressKey());
- commandStore().progressLog().readyToExecute(this,
isProgressShard, isProgressShard && progressKey().equals(homeKey()));
- notifyListeners();
+ safeStore.progressLog().readyToExecute(this, shard);
+ notifyListeners(safeStore);
break;
- case Executed:
- logger.trace("{}: applying", txnId());
- if (notifyListenersOnNoop) notifyListeners();
- return apply();
+
+ case PreApplied:
+ if (executeRanges(safeStore,
executeAt()).intersects(writes().keys,
safeStore.commandStore()::hashIntersects))
+ {
+ logger.trace("{}: applying", txnId());
+ apply(safeStore);
+ }
+ else
+ {
+ logger.trace("{}: applying no-op", txnId());
+ setStatus(Applied);
+ notifyListeners(safeStore);
+ }
}
- return Writes.SUCCESS;
+ return true;
}
/**
* @param dependency is either committed or invalidated
+ * @param isInsert true iff this is initial {@code populateWaitingOn} call
+ * @return true iff {@code maybeExecute} might now have a different outcome
*/
- private void updatePredecessor(PartialCommand dependency)
+ private boolean updatePredecessor(Command dependency, boolean isInsert)
{
Preconditions.checkState(dependency.hasBeen(Committed));
if (dependency.hasBeen(Invalidated))
{
logger.trace("{}: {} is invalidated. Stop listening and removing
from waiting on commit set.", txnId(), dependency.txnId());
dependency.removeListener(this);
- removeWaitingOnCommit(dependency);
+ removeWaitingOnCommit(dependency.txnId()); // TODO (now): this was
missing in partial-replication; might be redundant?
+ return true;
}
else if (dependency.executeAt().compareTo(executeAt()) > 0)
{
// cannot be a predecessor if we execute later
- logger.trace("{}: {} executes after us. Stop listening.", txnId(),
dependency.txnId());
+ logger.trace("{}: {} executes after us. Stop listening and
removing from waiting on apply set.", txnId(), dependency.txnId());
+ removeWaitingOn(dependency.txnId(), dependency.executeAt());
dependency.removeListener(this);
+ return true;
}
else if (dependency.hasBeen(Applied))
{
logger.trace("{}: {} has been applied. Stop listening and removing
from waiting on apply set.", txnId(), dependency.txnId());
- removeWaitingOnApply(dependency);
+ removeWaitingOn(dependency.txnId(), dependency.executeAt());
dependency.removeListener(this);
+ return true;
}
- else
+ else if (isUnableToExecute())
{
logger.trace("{}: adding {} to waiting on apply set.", txnId(),
dependency.txnId());
- addWaitingOnApplyIfAbsent(dependency);
+ addWaitingOnApplyIfAbsent(dependency.txnId(),
dependency.executeAt());
+ removeWaitingOnCommit(dependency.txnId());
+ return false;
+ }
+ else if (isInsert)
Review Comment:
If it's a precondition on method entry the dependency is committed then why
are we `addWaitingOnCommit` for the dependency if `isInsert`? We shouldn't be
waiting on that particular dependency.
--
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]