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


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

Review Comment:
   Can we use `String.format` here?



##########
modules/metastorage/src/integrationTest/java/org/apache/ignite/internal/metastorage/impl/ItMetaStorageServiceTest.java:
##########
@@ -395,7 +401,7 @@ public void testGetWithUpperBoundRevision() {
     public void testGetAll() {
         Node node = prepareNodes(1).get(0);
 
-        
when(node.mockStorage.getAll(anyList())).thenReturn(EXPECTED_SRV_RESULT_COLL);
+        
doReturn(EXPECTED_SRV_RESULT_COLL).when(node.mockStorage).getAll(anyList());

Review Comment:
   Since we are now using real storage implementation, why do we need to mock 
this? Can we just insert the values?



##########
modules/metastorage/src/test/java/org/apache/ignite/internal/metastorage/server/raft/MetaStorageListenerTest.java:
##########
@@ -0,0 +1,306 @@
+/*
+ * 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.Command;
+import org.apache.ignite.internal.raft.IndexWithTerm;
+import org.apache.ignite.internal.raft.RaftGroupConfiguration;
+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())

Review Comment:
   Minor nitpick, but I think we can extract `clock.now` into a variable)



##########
modules/metastorage/src/integrationTest/java/org/apache/ignite/internal/metastorage/impl/ItMetaStorageServiceTest.java:
##########
@@ -736,7 +754,12 @@ public void testInvoke() {
 
         byte[] expVal = {2};
 
-        when(node.mockStorage.invoke(any(), any(), any(), any(), 
any())).thenReturn(true);
+        doAnswer(invocation -> {

Review Comment:
   What's the point of this mocking?



##########
modules/metastorage/src/integrationTest/java/org/apache/ignite/internal/metastorage/impl/ItIdempotentCommandCacheTest.java:
##########
@@ -329,23 +339,63 @@ boolean checkValueInStorage(byte[] testKey, byte[] 
testValueExpected) {
 
     @BeforeEach
     void setUp(TestInfo testInfo) {
-        startCluster(testInfo);
+        this.testInfo = testInfo;
+
+        if 
(testInfo.getTestMethod().orElseThrow().isAnnotationPresent(DisableIdleSafeTimePropagation.class))
 {
+            assertThat(
+                    
systemDistributedConfiguration.idleSafeTimeSyncIntervalMillis().update(HOURS.toMillis(1)),
+                    willCompleteSuccessfully()
+            );
+        }
+
+        startCluster();
     }
 
     @AfterEach
     void tearDown() throws Exception {
-        closeAll(nodes.stream());
+        closeAllManually(nodes.stream());
     }
 
-    @Test
-    public void testIdempotentInvoke() throws InterruptedException {
+    @ParameterizedTest
+    @EnumSource(Invoker.class)
+    void testIdempotentInvoke(Invoker invoker) {
+        Node leader = leader(raftClient());
+
+        boolean result = doRetriedInvoke(invoker, leader);
+
+        assertTrue(result);
+        assertTrue(leader.checkValueInStorage(TEST_KEY.bytes(), TEST_VALUE));
+    }
+
+    private boolean doRetriedInvoke(Invoker invoker, Node leader) {
         AtomicInteger writeActionReqCount = new AtomicInteger();
         CompletableFuture<Void> retryBlockingFuture = new 
CompletableFuture<>();
 
         log.info("Test: blocking messages.");
 
-        Node leader = leader(raftClient());
+        dropResponseToFirstInvoke(leader, writeActionReqCount, 
retryBlockingFuture);
+
+        MetaStorageManager metaStorageManager = leader.metaStorageManager;
+
+        CompletableFuture<Boolean> fut = invoker.invokeOn(metaStorageManager);
+
+        await().until(() -> leader.checkValueInStorage(TEST_KEY.bytes(), 
TEST_VALUE));
+
+        log.info("Test: value appeared in storage.");
+
+        assertTrue(retryBlockingFuture.complete(null));

Review Comment:
   Why do we need an assertion here?



##########
modules/metastorage/src/integrationTest/java/org/apache/ignite/internal/metastorage/impl/ItMetaStorageServiceTest.java:
##########
@@ -685,7 +691,19 @@ public void testMultiInvoke() {
 
         var ifCaptor = ArgumentCaptor.forClass(If.class);
 
-        when(node.mockStorage.invoke(any(), any(), 
any())).thenReturn(ops().yield(true).result(), null, null);
+        doAnswer(new Answer() {

Review Comment:
   and this?



##########
modules/metastorage/src/integrationTest/java/org/apache/ignite/internal/metastorage/impl/ItIdempotentCommandCacheTest.java:
##########
@@ -329,23 +339,63 @@ boolean checkValueInStorage(byte[] testKey, byte[] 
testValueExpected) {
 
     @BeforeEach
     void setUp(TestInfo testInfo) {
-        startCluster(testInfo);
+        this.testInfo = testInfo;
+
+        if 
(testInfo.getTestMethod().orElseThrow().isAnnotationPresent(DisableIdleSafeTimePropagation.class))
 {

Review Comment:
   Could you please leave a comment about why this is needed?



##########
modules/metastorage/src/integrationTest/java/org/apache/ignite/internal/metastorage/impl/ItIdempotentCommandCacheTest.java:
##########
@@ -582,7 +625,7 @@ private static IdempotentCommand 
buildKeyNotExistsMultiInvokeCommand(
                 .build();
     }
 
-    private void startCluster(TestInfo testInfo) {
+    private void startCluster() {

Review Comment:
   Not related to this PR, but I think making `Node` class non-static may make 
the code shorter and simpler



##########
modules/metastorage/src/integrationTest/java/org/apache/ignite/internal/metastorage/impl/ItIdempotentCommandCacheTest.java:
##########
@@ -607,4 +650,34 @@ private void startCluster(TestInfo testInfo) {
 
         nodes.forEach(Node::deployWatches);
     }
+
+    @Target(ElementType.METHOD)
+    @Retention(RetentionPolicy.RUNTIME)
+    private @interface DisableIdleSafeTimePropagation {
+    }
+
+    private enum Invoker {
+        INVOKE {
+            @Override
+            CompletableFuture<Boolean> invokeOn(MetaStorageManager 
metaStorageManager) {
+                return metaStorageManager.invoke(
+                        notExists(TEST_KEY),
+                        put(TEST_KEY, TEST_VALUE),
+                        put(TEST_KEY, ANOTHER_VALUE)
+                );
+            }
+        },
+        MULTI_INVOKE {

Review Comment:
   Why is this a "multi-invoke"? It does the same thing just through a 
different API. Or do we call that API "multi-invoke"?



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