belliottsmith commented on code in PR #113:
URL: https://github.com/apache/cassandra-accord/pull/113#discussion_r1750218314


##########
accord-core/src/main/java/accord/impl/progresslog/CallbackInvoker.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package accord.impl.progresslog;
+
+import java.util.function.BiConsumer;
+
+import accord.local.SafeCommand;
+import accord.primitives.TxnId;
+import accord.utils.Invariants;
+
+import static accord.impl.progresslog.TxnStateKind.Home;
+import static accord.impl.progresslog.TxnStateKind.Waiting;
+import static accord.local.PreLoadContext.contextFor;
+
+class CallbackInvoker<P, V> implements BiConsumer<V, Throwable>
+{
+    static <P, V> BiConsumer<V, Throwable> 
invokeWaitingCallback(DefaultProgressLog instance, TxnId txnId, P param, 
Callback<P, V> callback)
+    {
+        return invokeCallback(Waiting, instance, txnId, param, callback);
+    }
+
+    static <P, V> BiConsumer<V, Throwable> 
invokeHomeCallback(DefaultProgressLog owner, TxnId txnId, P param, Callback<P, 
V> callback)
+    {
+        return invokeCallback(Home, owner, txnId, param, callback);
+    }
+
+    static <P, V> BiConsumer<V, Throwable> invokeCallback(TxnStateKind kind, 
DefaultProgressLog owner, TxnId txnId, P param, Callback<P, V> callback)
+    {
+        CallbackInvoker<P, V> invoker = new CallbackInvoker<>(owner, kind, 
owner.nextInvokerId(), txnId, param, callback);
+        owner.registerActive(kind, txnId, invoker);
+        return invoker;
+    }
+
+    final DefaultProgressLog owner;
+    final boolean isHome;
+    final long id;
+    final TxnId txnId;
+    final P param;
+    final Callback<P, V> callback;
+
+    CallbackInvoker(DefaultProgressLog owner, TxnStateKind kind, long id, 
TxnId txnId, P param, Callback<P, V> callback)
+    {
+        this.owner = owner;
+        this.isHome = kind == Home;
+        this.id = id;
+        this.txnId = txnId;
+        this.param = param;
+        this.callback = callback;
+    }
+
+    private TxnStateKind kind()
+    {
+        return isHome ? Home : Waiting;
+    }
+
+    @Override
+    public void accept(V success, Throwable fail)
+    {
+        owner.commandStore.execute(contextFor(txnId), safeStore -> {
+
+            // we load safeCommand first so that if it clears the progress log 
we abandon the callback
+            SafeCommand safeCommand = safeStore.ifInitialised(txnId);
+            Invariants.checkState(safeCommand != null);
+
+            if (owner.deregisterActive(kind(), this))

Review Comment:
   So, looking at this again - the point of the active callback registry is to 
be able to logically invalidate callback handlers we have abandoned (so we 
don't have to worry about them competing to modify our state). So, it isn't 
actually a problem not to remove ourselves; if the transaction isn't completed 
the progress log will pick it up again and register new callbacks as necessary. 
But, I have slightly updated the sequence of events anyway.



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

Reply via email to