Copilot commented on code in PR #7763:
URL: https://github.com/apache/ignite-3/pull/7763#discussion_r2922748257


##########
modules/metastorage/src/test/java/org/apache/ignite/internal/metastorage/server/raft/MetaStorageListenerTest.java:
##########
@@ -0,0 +1,289 @@
+/*
+ * 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 org.apache.ignite.internal.metastorage.server.raft;
+
+import static org.apache.ignite.internal.metastorage.dsl.Conditions.notExists;
+import static org.apache.ignite.internal.metastorage.dsl.Operations.noop;
+import static org.apache.ignite.internal.metastorage.dsl.Operations.ops;
+import static org.apache.ignite.internal.util.IgniteUtils.closeAllManually;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.util.List;
+import java.util.UUID;
+import org.apache.ignite.internal.hlc.HybridClock;
+import org.apache.ignite.internal.hlc.HybridClockImpl;
+import org.apache.ignite.internal.hlc.HybridTimestamp;
+import org.apache.ignite.internal.lang.ByteArray;
+import org.apache.ignite.internal.metastorage.command.InvokeCommand;
+import 
org.apache.ignite.internal.metastorage.command.MetaStorageCommandsFactory;
+import org.apache.ignite.internal.metastorage.command.MetaStorageWriteCommand;
+import org.apache.ignite.internal.metastorage.command.MultiInvokeCommand;
+import org.apache.ignite.internal.metastorage.command.PutCommand;
+import org.apache.ignite.internal.metastorage.dsl.MetaStorageMessagesFactory;
+import org.apache.ignite.internal.metastorage.dsl.Statements;
+import org.apache.ignite.internal.metastorage.server.KeyValueStorage;
+import 
org.apache.ignite.internal.metastorage.server.SimpleInMemoryKeyValueStorage;
+import org.apache.ignite.internal.metastorage.server.time.ClusterTimeImpl;
+import org.apache.ignite.internal.raft.IndexWithTerm;
+import org.apache.ignite.internal.raft.WriteCommand;
+import org.apache.ignite.internal.raft.service.CommandClosure;
+import org.apache.ignite.internal.util.IgniteSpinBusyLock;
+import org.jetbrains.annotations.Nullable;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+class MetaStorageListenerTest {
+    private static final MetaStorageCommandsFactory commandsFactory = new 
MetaStorageCommandsFactory();
+    private static final MetaStorageMessagesFactory messagesFactory = new 
MetaStorageMessagesFactory();
+
+    private static final long COMMAND_TERM = 3;
+
+    private static final long PRE_TARGET_COMMAND_INDEX = 1;
+    private static final long TARGET_COMMAND_INDEX = 2;
+
+    private static final HybridClock clock = new HybridClockImpl();
+
+    private KeyValueStorage storage;
+    private ClusterTimeImpl clusterTime;
+
+    private MetaStorageListener listener;
+
+    @BeforeEach
+    void setUp() {
+        storage = new SimpleInMemoryKeyValueStorage("test");
+        clusterTime = new ClusterTimeImpl("test", new IgniteSpinBusyLock(), 
clock);
+
+        listener = new MetaStorageListener(storage, clock, clusterTime);
+
+        storage.start();
+    }
+
+    @AfterEach
+    void cleanup() throws Exception {
+        if (listener != null) {
+            listener.onShutdown();
+        }
+
+        closeAllManually(clusterTime, storage);
+    }
+
+    @ParameterizedTest
+    @MethodSource("commandsVariationsForIndexAdvanceTesting")
+    void commandsAdvanceLastAppliedIndex(MetaStorageWriteCommand command) {
+        testCommandAdvancedLastAppliedIndex(command);
+    }
+
+    private static List<MetaStorageWriteCommand> 
commandsVariationsForIndexAdvanceTesting() {
+        return List.of(
+                somePutCommand(),
+                commandsFactory.putAllCommand()
+                        .keys(List.of(ByteBuffer.allocate(3)))
+                        .values(List.of(ByteBuffer.allocate(3)))
+                        .initiatorTime(clock.now())
+                        .safeTime(clock.now())
+                        .build(),
+                commandsFactory.removeCommand()
+                        .key(ByteBuffer.allocate(3))
+                        .initiatorTime(clock.now())
+                        .safeTime(clock.now())
+                        .build(),
+                commandsFactory.removeAllCommand()
+                        .keys(List.of(ByteBuffer.allocate(3)))
+                        .initiatorTime(clock.now())
+                        .safeTime(clock.now())
+                        .build(),
+                commandsFactory.removeByPrefixCommand()
+                        .prefix(ByteBuffer.allocate(3))
+                        .initiatorTime(clock.now())
+                        .safeTime(clock.now())
+                        .build(),
+                someInvokeCommand(),
+                someMultiInvokeCommand(),
+
+                // Normal command that gets applied.
+                commandsFactory.syncTimeCommand()
+                        .initiatorTime(clock.now())
+                        .safeTime(clock.now())
+                        .initiatorTerm(COMMAND_TERM)
+                        .build(),
+
+                // Command that gets discarded.
+                commandsFactory.syncTimeCommand()
+                        .initiatorTime(clock.now())
+                        .safeTime(clock.now())
+                        .initiatorTerm(COMMAND_TERM - 1)
+                        .build()
+        );
+    }
+
+    private void testCommandAdvancedLastAppliedIndex(MetaStorageWriteCommand 
command) {
+        applySingleCommand(command, TARGET_COMMAND_INDEX, COMMAND_TERM);
+
+        IndexWithTerm indexWithTerm = storage.getIndexWithTerm();
+
+        assertThat(indexWithTerm, is(notNullValue()));
+        assertThat(indexWithTerm.index(), is(TARGET_COMMAND_INDEX));
+        assertThat(indexWithTerm.term(), is(COMMAND_TERM));
+    }
+
+    private void applySingleCommand(MetaStorageWriteCommand command, long 
index, long term) {
+        listener.onBeforeApply(command);
+        listener.onWrite(List.of(commandClosure(command, index, 
term)).iterator());
+    }

Review Comment:
   In applySingleCommand, the return value of listener.onBeforeApply(command) 
is ignored. In real Raft flow the (potentially cloned/modified) command 
returned from onBeforeApply is what gets applied in onWrite; skipping it makes 
the test diverge from production behavior (e.g., safeTime adjustment/clone 
semantics). Consider using the returned Command (cast back to 
MetaStorageWriteCommand) when building the CommandClosure.



##########
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/server/raft/MetaStorageWriteHandler.java:
##########
@@ -114,6 +115,14 @@ public class MetaStorageWriteHandler {
      * Processes a given {@link WriteCommand}.
      */
     void handleWriteCommand(CommandClosure<WriteCommand> clo) {
+        handleWriteCommandInternal(clo);
+
+        assert lastAppliedIndex() == clo.index() : "Last applied index after 
command application is not equal to the command index "
+                + "[lastAppliedIndex=" + lastAppliedIndex() + ", 
commandIndex=" + clo.index()

Review Comment:
   The assertion message computes lastAppliedIndex() again, which can produce a 
different value than the one used in the assert condition (and does an extra 
storage read). Consider storing lastAppliedIndex() in a local variable and 
using it both for the condition and for the message to keep diagnostics 
consistent and avoid duplicate work when assertions are enabled.
   ```suggestion
           long lastAppliedIndex = lastAppliedIndex();
   
           assert lastAppliedIndex == clo.index() : "Last applied index after 
command application is not equal to the command index "
                   + "[lastAppliedIndex=" + lastAppliedIndex + ", 
commandIndex=" + clo.index()
   ```



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