You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "rpuch (via GitHub)" <gi...@apache.org> on 2023/08/29 14:27:15 UTC

[GitHub] [ignite-3] rpuch opened a new pull request, #2513: IGNITE-20300 Metastorage command reordering wrt Safe Time on Raft Group entry

rpuch opened a new pull request, #2513:
URL: https://github.com/apache/ignite-3/pull/2513

   https://issues.apache.org/jira/browse/IGNITE-20300
   
   Thank you for submitting the pull request.
   
   To streamline the review process of the patch and ensure better code quality
   we ask both an author and a reviewer to verify the following:
   
   ### The Review Checklist
   - [ ] **Formal criteria:** TC status, codestyle, mandatory documentation. Also make sure to complete the following:  
   \- There is a single JIRA ticket related to the pull request.  
   \- The web-link to the pull request is attached to the JIRA ticket.  
   \- The JIRA ticket has the Patch Available state.  
   \- The description of the JIRA ticket explains WHAT was made, WHY and HOW.  
   \- The pull request title is treated as the final commit message. The following pattern must be used: IGNITE-XXXX Change summary where XXXX - number of JIRA issue.
   - [ ] **Design:** new code conforms with the design principles of the components it is added to.
   - [ ] **Patch quality:** patch cannot be split into smaller pieces, its size must be reasonable.
   - [ ] **Code quality:** code is clean and readable, necessary developer documentation is added if needed.
   - [ ] **Tests code quality:** test set covers positive/negative scenarios, happy/edge cases. Tests are effective in terms of execution time and resources.
   
   ### Notes
   - [Apache Ignite Coding Guidelines](https://cwiki.apache.org/confluence/display/IGNITE/Java+Code+Style+Guide)


-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] sashapolo commented on a diff in pull request #2513: IGNITE-20300 Metastorage command reordering wrt Safe Time on Raft Group entry

Posted by "sashapolo (via GitHub)" <gi...@apache.org>.
sashapolo commented on code in PR #2513:
URL: https://github.com/apache/ignite-3/pull/2513#discussion_r1309862777


##########
modules/raft/src/main/java/org/apache/ignite/raft/jraft/rpc/impl/ActionRequestProcessor.java:
##########
@@ -71,15 +81,32 @@ public void handleRequest(RpcContext rpcCtx, ActionRequest request) {
 
         JraftServerImpl.DelegatingStateMachine fsm = (JraftServerImpl.DelegatingStateMachine) node.getOptions().getFsm();
 
-        // Apply a filter before committing to STM.
-        fsm.getListener().onBeforeApply(request.command());
-
         if (request.command() instanceof WriteCommand) {
-            applyWrite(node, request, rpcCtx);
+            if (fsm.getListener() instanceof BeforeApplyHandler) {
+                synchronized (groupIdSyncMonitor(request.groupId())) {
+                    callOnBeforeApply(request, fsm);
+                    applyWrite(node, request, rpcCtx);
+                }
+            } else {
+                applyWrite(node, request, rpcCtx);
+            }
         } else {
+            if (fsm.getListener() instanceof BeforeApplyHandler) {
+                callOnBeforeApply(request, fsm);
+            }
+
             applyRead(node, request, rpcCtx);
         }
     }
+    private static void callOnBeforeApply(ActionRequest request, DelegatingStateMachine fsm) {
+        ((BeforeApplyHandler) fsm.getListener()).onBeforeApply(request.command());
+    }
+
+    private Object groupIdSyncMonitor(String groupId) {
+        assert groupId != null;
+
+        return groupIdsToMonitors.computeIfAbsent(groupId, k -> groupId);

Review Comment:
   I would have expected a `new Object` call in case the key is not present, but I guess that's not a big deal. Also, we may use `Function.identity` 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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] sashapolo commented on a diff in pull request #2513: IGNITE-20300 Metastorage command reordering wrt Safe Time on Raft Group entry

Posted by "sashapolo (via GitHub)" <gi...@apache.org>.
sashapolo commented on code in PR #2513:
URL: https://github.com/apache/ignite-3/pull/2513#discussion_r1309718668


##########
modules/raft/src/main/java/org/apache/ignite/raft/jraft/rpc/impl/ActionRequestProcessor.java:
##########
@@ -71,13 +75,23 @@ public void handleRequest(RpcContext rpcCtx, ActionRequest request) {
 
         JraftServerImpl.DelegatingStateMachine fsm = (JraftServerImpl.DelegatingStateMachine) node.getOptions().getFsm();
 
-        // Apply a filter before committing to STM.
-        fsm.getListener().onBeforeApply(request.command());
+        if (fsm.getListener().atomicOnBeforeApplyAndWrite(request.command()) && request.command() instanceof WriteCommand) {
+            // Synchronizing per group.
+            synchronized (internizer.intern(request.groupId())) {

Review Comment:
   I really think that this "internizer" class is an overkill. Can we synchronize on the "node" object instead?



##########
modules/raft-api/src/main/java/org/apache/ignite/internal/raft/service/RaftGroupListener.java:
##########
@@ -93,4 +93,18 @@ default void onConfigurationCommitted(CommittedConfiguration config) {
     default void onBeforeApply(Command command) {
         // No-op.
     }
+
+    /**
+     * Returns {@code true} if {@link #onBeforeApply(Command)} and saving of the command to the LogManager
+     * must be executed atomically (that is, these actions cannot intermingle for different commands
+     * on the same Raft group.
+     *
+     * <p>This does not influence read commands: for them no atomicity guarantees are provided.
+     *
+     * @param command The command.
+     * @return {@code true} if {@link #onBeforeApply(Command)} and saving must be executed atomically.
+     */
+    default boolean atomicOnBeforeApplyAndWrite(Command command) {

Review Comment:
   I would suggest to introduce a sub-class, that will declare the `onBeforeApply` method. This way we can always know if `onBeforeApply` is not a no-op and always execute it atomically with `write`



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] sashapolo commented on a diff in pull request #2513: IGNITE-20300 Metastorage command reordering wrt Safe Time on Raft Group entry

Posted by "sashapolo (via GitHub)" <gi...@apache.org>.
sashapolo commented on code in PR #2513:
URL: https://github.com/apache/ignite-3/pull/2513#discussion_r1309766740


##########
modules/raft/src/main/java/org/apache/ignite/raft/jraft/rpc/impl/ActionRequestProcessor.java:
##########
@@ -71,13 +75,23 @@ public void handleRequest(RpcContext rpcCtx, ActionRequest request) {
 
         JraftServerImpl.DelegatingStateMachine fsm = (JraftServerImpl.DelegatingStateMachine) node.getOptions().getFsm();
 
-        // Apply a filter before committing to STM.
-        fsm.getListener().onBeforeApply(request.command());
+        if (fsm.getListener().atomicOnBeforeApplyAndWrite(request.command()) && request.command() instanceof WriteCommand) {
+            // Synchronizing per group.
+            synchronized (internizer.intern(request.groupId())) {

Review Comment:
   > but someone else could do the same in another part of the code which might lead to less parallelizm
   
   I don't think it's an issue
   
   Another solution would be to add an explicit string -> object map to hold per-group locks (which is what this internizer does)



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] rpuch commented on a diff in pull request #2513: IGNITE-20300 Metastorage command reordering wrt Safe Time on Raft Group entry

Posted by "rpuch (via GitHub)" <gi...@apache.org>.
rpuch commented on code in PR #2513:
URL: https://github.com/apache/ignite-3/pull/2513#discussion_r1309867314


##########
modules/raft/src/main/java/org/apache/ignite/raft/jraft/rpc/impl/ActionRequestProcessor.java:
##########
@@ -71,15 +81,32 @@ public void handleRequest(RpcContext rpcCtx, ActionRequest request) {
 
         JraftServerImpl.DelegatingStateMachine fsm = (JraftServerImpl.DelegatingStateMachine) node.getOptions().getFsm();
 
-        // Apply a filter before committing to STM.
-        fsm.getListener().onBeforeApply(request.command());
-
         if (request.command() instanceof WriteCommand) {
-            applyWrite(node, request, rpcCtx);
+            if (fsm.getListener() instanceof BeforeApplyHandler) {
+                synchronized (groupIdSyncMonitor(request.groupId())) {
+                    callOnBeforeApply(request, fsm);
+                    applyWrite(node, request, rpcCtx);
+                }
+            } else {
+                applyWrite(node, request, rpcCtx);
+            }
         } else {
+            if (fsm.getListener() instanceof BeforeApplyHandler) {
+                callOnBeforeApply(request, fsm);
+            }
+
             applyRead(node, request, rpcCtx);
         }
     }
+    private static void callOnBeforeApply(ActionRequest request, DelegatingStateMachine fsm) {
+        ((BeforeApplyHandler) fsm.getListener()).onBeforeApply(request.command());
+    }
+
+    private Object groupIdSyncMonitor(String groupId) {
+        assert groupId != null;
+
+        return groupIdsToMonitors.computeIfAbsent(groupId, k -> groupId);

Review Comment:
   `Function.identify()` would look a bit too cryptic in this particular case.
   
   Also, why would we allocate a new object if we can use the key as a monitor? It's referenced by the map 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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] rpuch commented on a diff in pull request #2513: IGNITE-20300 Metastorage command reordering wrt Safe Time on Raft Group entry

Posted by "rpuch (via GitHub)" <gi...@apache.org>.
rpuch commented on code in PR #2513:
URL: https://github.com/apache/ignite-3/pull/2513#discussion_r1309746628


##########
modules/raft-api/src/main/java/org/apache/ignite/internal/raft/service/RaftGroupListener.java:
##########
@@ -93,4 +93,18 @@ default void onConfigurationCommitted(CommittedConfiguration config) {
     default void onBeforeApply(Command command) {
         // No-op.
     }
+
+    /**
+     * Returns {@code true} if {@link #onBeforeApply(Command)} and saving of the command to the LogManager
+     * must be executed atomically (that is, these actions cannot intermingle for different commands
+     * on the same Raft group.
+     *
+     * <p>This does not influence read commands: for them no atomicity guarantees are provided.
+     *
+     * @param command The command.
+     * @return {@code true} if {@link #onBeforeApply(Command)} and saving must be executed atomically.
+     */
+    default boolean atomicOnBeforeApplyAndWrite(Command command) {

Review Comment:
   But do we need to ALWAYS execute it atomically with the write? I think no



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] sashapolo merged pull request #2513: IGNITE-20300 Metastorage command reordering wrt Safe Time on Raft Group entry

Posted by "sashapolo (via GitHub)" <gi...@apache.org>.
sashapolo merged PR #2513:
URL: https://github.com/apache/ignite-3/pull/2513


-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] sashapolo commented on a diff in pull request #2513: IGNITE-20300 Metastorage command reordering wrt Safe Time on Raft Group entry

Posted by "sashapolo (via GitHub)" <gi...@apache.org>.
sashapolo commented on code in PR #2513:
URL: https://github.com/apache/ignite-3/pull/2513#discussion_r1309769328


##########
modules/raft-api/src/main/java/org/apache/ignite/internal/raft/service/RaftGroupListener.java:
##########
@@ -93,4 +93,18 @@ default void onConfigurationCommitted(CommittedConfiguration config) {
     default void onBeforeApply(Command command) {
         // No-op.
     }
+
+    /**
+     * Returns {@code true} if {@link #onBeforeApply(Command)} and saving of the command to the LogManager
+     * must be executed atomically (that is, these actions cannot intermingle for different commands
+     * on the same Raft group.
+     *
+     * <p>This does not influence read commands: for them no atomicity guarantees are provided.
+     *
+     * @param command The command.
+     * @return {@code true} if {@link #onBeforeApply(Command)} and saving must be executed atomically.
+     */
+    default boolean atomicOnBeforeApplyAndWrite(Command command) {

Review Comment:
   ok, I think my proposed solution doesn't make sense, because we don't have separate listener instances for writes and reads (and we only need to call onBeforeApply for writes)



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] sashapolo commented on a diff in pull request #2513: IGNITE-20300 Metastorage command reordering wrt Safe Time on Raft Group entry

Posted by "sashapolo (via GitHub)" <gi...@apache.org>.
sashapolo commented on code in PR #2513:
URL: https://github.com/apache/ignite-3/pull/2513#discussion_r1309764185


##########
modules/raft-api/src/main/java/org/apache/ignite/internal/raft/service/RaftGroupListener.java:
##########
@@ -93,4 +93,18 @@ default void onConfigurationCommitted(CommittedConfiguration config) {
     default void onBeforeApply(Command command) {
         // No-op.
     }
+
+    /**
+     * Returns {@code true} if {@link #onBeforeApply(Command)} and saving of the command to the LogManager
+     * must be executed atomically (that is, these actions cannot intermingle for different commands
+     * on the same Raft group.
+     *
+     * <p>This does not influence read commands: for them no atomicity guarantees are provided.
+     *
+     * @param command The command.
+     * @return {@code true} if {@link #onBeforeApply(Command)} and saving must be executed atomically.
+     */
+    default boolean atomicOnBeforeApplyAndWrite(Command command) {

Review Comment:
   Why not? I can't imagine a scenario when you need to implement `onBeforeApply` and not have it executed atomically. Having a separate boolean method seems to cryptic to me. Anyway, we can add it after we find out that we need it.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] sashapolo commented on a diff in pull request #2513: IGNITE-20300 Metastorage command reordering wrt Safe Time on Raft Group entry

Posted by "sashapolo (via GitHub)" <gi...@apache.org>.
sashapolo commented on code in PR #2513:
URL: https://github.com/apache/ignite-3/pull/2513#discussion_r1309764185


##########
modules/raft-api/src/main/java/org/apache/ignite/internal/raft/service/RaftGroupListener.java:
##########
@@ -93,4 +93,18 @@ default void onConfigurationCommitted(CommittedConfiguration config) {
     default void onBeforeApply(Command command) {
         // No-op.
     }
+
+    /**
+     * Returns {@code true} if {@link #onBeforeApply(Command)} and saving of the command to the LogManager
+     * must be executed atomically (that is, these actions cannot intermingle for different commands
+     * on the same Raft group.
+     *
+     * <p>This does not influence read commands: for them no atomicity guarantees are provided.
+     *
+     * @param command The command.
+     * @return {@code true} if {@link #onBeforeApply(Command)} and saving must be executed atomically.
+     */
+    default boolean atomicOnBeforeApplyAndWrite(Command command) {

Review Comment:
   Why not? I can't imagine a scenario when you need to implement `onBeforeApply` and not have it executed atomically. Having a separate boolean method seems too cryptic to me. Anyway, we can add it after we find out that we need it.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] rpuch commented on a diff in pull request #2513: IGNITE-20300 Metastorage command reordering wrt Safe Time on Raft Group entry

Posted by "rpuch (via GitHub)" <gi...@apache.org>.
rpuch commented on code in PR #2513:
URL: https://github.com/apache/ignite-3/pull/2513#discussion_r1309857977


##########
modules/raft/src/main/java/org/apache/ignite/raft/jraft/rpc/impl/ActionRequestProcessor.java:
##########
@@ -71,13 +75,23 @@ public void handleRequest(RpcContext rpcCtx, ActionRequest request) {
 
         JraftServerImpl.DelegatingStateMachine fsm = (JraftServerImpl.DelegatingStateMachine) node.getOptions().getFsm();
 
-        // Apply a filter before committing to STM.
-        fsm.getListener().onBeforeApply(request.command());
+        if (fsm.getListener().atomicOnBeforeApplyAndWrite(request.command()) && request.command() instanceof WriteCommand) {
+            // Synchronizing per group.
+            synchronized (internizer.intern(request.groupId())) {

Review Comment:
   I moved the map from internizer to the processor and removed internizer implementation



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] rpuch commented on a diff in pull request #2513: IGNITE-20300 Metastorage command reordering wrt Safe Time on Raft Group entry

Posted by "rpuch (via GitHub)" <gi...@apache.org>.
rpuch commented on code in PR #2513:
URL: https://github.com/apache/ignite-3/pull/2513#discussion_r1309749467


##########
modules/raft/src/main/java/org/apache/ignite/raft/jraft/rpc/impl/ActionRequestProcessor.java:
##########
@@ -71,13 +75,23 @@ public void handleRequest(RpcContext rpcCtx, ActionRequest request) {
 
         JraftServerImpl.DelegatingStateMachine fsm = (JraftServerImpl.DelegatingStateMachine) node.getOptions().getFsm();
 
-        // Apply a filter before committing to STM.
-        fsm.getListener().onBeforeApply(request.command());
+        if (fsm.getListener().atomicOnBeforeApplyAndWrite(request.command()) && request.command() instanceof WriteCommand) {
+            // Synchronizing per group.
+            synchronized (internizer.intern(request.groupId())) {

Review Comment:
   Yes, we could, but someone else could do the same in another part of the code which might lead to less parallelizm (unnecessarily). Another solution is to add a monitor object to the `NodeImpl` (one per instance), but we would have to add an accessor for it to the `Node` interface, and this also seems non-desirable.



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [ignite-3] sashapolo commented on a diff in pull request #2513: IGNITE-20300 Metastorage command reordering wrt Safe Time on Raft Group entry

Posted by "sashapolo (via GitHub)" <gi...@apache.org>.
sashapolo commented on code in PR #2513:
URL: https://github.com/apache/ignite-3/pull/2513#discussion_r1309872433


##########
modules/raft/src/main/java/org/apache/ignite/raft/jraft/rpc/impl/ActionRequestProcessor.java:
##########
@@ -71,15 +81,32 @@ public void handleRequest(RpcContext rpcCtx, ActionRequest request) {
 
         JraftServerImpl.DelegatingStateMachine fsm = (JraftServerImpl.DelegatingStateMachine) node.getOptions().getFsm();
 
-        // Apply a filter before committing to STM.
-        fsm.getListener().onBeforeApply(request.command());
-
         if (request.command() instanceof WriteCommand) {
-            applyWrite(node, request, rpcCtx);
+            if (fsm.getListener() instanceof BeforeApplyHandler) {
+                synchronized (groupIdSyncMonitor(request.groupId())) {
+                    callOnBeforeApply(request, fsm);
+                    applyWrite(node, request, rpcCtx);
+                }
+            } else {
+                applyWrite(node, request, rpcCtx);
+            }
         } else {
+            if (fsm.getListener() instanceof BeforeApplyHandler) {
+                callOnBeforeApply(request, fsm);
+            }
+
             applyRead(node, request, rpcCtx);
         }
     }
+    private static void callOnBeforeApply(ActionRequest request, DelegatingStateMachine fsm) {
+        ((BeforeApplyHandler) fsm.getListener()).onBeforeApply(request.command());
+    }
+
+    private Object groupIdSyncMonitor(String groupId) {
+        assert groupId != null;
+
+        return groupIdsToMonitors.computeIfAbsent(groupId, k -> groupId);

Review Comment:
   That's just a matter of taste, that's why I have approved the PR regardless



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org