You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2022/12/22 07:52:06 UTC

[GitHub] [ignite-3] rpuch commented on a diff in pull request #1464: IGNITE-18019 GC methods in MvPartitionStorage & new tests & old tests reorganization.

rpuch commented on code in PR #1464:
URL: https://github.com/apache/ignite-3/pull/1464#discussion_r1054362896


##########
modules/storage-api/src/main/java/org/apache/ignite/internal/storage/BinaryRowWithRowId.java:
##########
@@ -0,0 +1,51 @@
+/*
+ * 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.storage;
+
+import org.apache.ignite.internal.schema.BinaryRow;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Wrapper that holds both {@link BinaryRow} and {@link RowId}.

Review Comment:
   Should it be noted that it *might* hold a binary row, but it also might be missing?



##########
modules/storage-api/src/main/java/org/apache/ignite/internal/storage/MvPartitionStorage.java:
##########
@@ -209,6 +209,17 @@ public interface MvPartitionStorage extends ManuallyCloseable {
      */
     @Nullable RowId closestRowId(RowId lowerBound) throws StorageException;
 
+    /**
+     * Polls an oldest row in the partition, removing it at the same time.

Review Comment:
   ```suggestion
        * Polls the oldest row in the partition, removing it at the same time.
   ```



##########
modules/storage-api/src/main/java/org/apache/ignite/internal/storage/BinaryRowWithRowId.java:
##########
@@ -0,0 +1,51 @@
+/*
+ * 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.storage;
+
+import org.apache.ignite.internal.schema.BinaryRow;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Wrapper that holds both {@link BinaryRow} and {@link RowId}.
+ */
+public class BinaryRowWithRowId {

Review Comment:
   `BinaryRowWithRowId` makes me think that it is a binary row itself, but here it isn't (as it does not extend it, it has it as a part of it). `BinaryRowAndRowId` does not seem to cause this feeling.
   
   Extremely optional.



##########
modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/AbstractMvPartitionStorageConcurrencyTest.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.storage;
+
+import static org.apache.ignite.internal.testframework.IgniteTestUtils.startRace;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+import org.apache.ignite.internal.hlc.HybridTimestamp;
+import org.apache.ignite.internal.storage.impl.TestStorageEngine;
+import org.junit.jupiter.api.RepeatedTest;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test to check for race conditions in MV partition storage.
+ */
+public abstract class AbstractMvPartitionStorageConcurrencyTest extends BaseMvPartitionStorageTest {
+    /** To be used in a loop. {@link RepeatedTest} has a smaller failure rate for some reasons. */
+    private static final int REPEATS = 100;
+
+    @Test
+    void testAbortAndRead() throws Exception {
+        for (int i = 0; i < REPEATS; i++) {
+            addWrite(ROW_ID, BINARY_ROW, TX_ID);
+
+            startRace(
+                    () -> abortWrite(ROW_ID),
+                    () -> read(ROW_ID, clock.now()),
+                    () -> scanFirstEntry(clock.now())
+            );
+
+            assertNull(read(ROW_ID, clock.now()));
+        }
+    }
+
+    @Test
+    void testCommitAndRead() throws Exception {
+        for (int i = 0; i < REPEATS; i++) {
+            addWrite(ROW_ID, BINARY_ROW, TX_ID);
+
+            startRace(
+                    () -> commitWrite(ROW_ID, clock.now()),
+                    () -> read(ROW_ID, clock.now()),
+                    () -> scanFirstEntry(clock.now())
+            );
+
+            assertRowMatches(read(ROW_ID, clock.now()), BINARY_ROW);
+        }
+    }
+
+    @Test
+    void testUpdateAndRead() throws Exception {
+        for (int i = 0; i < REPEATS; i++) {
+            addWrite(ROW_ID, BINARY_ROW, TX_ID);
+
+            startRace(
+                    () -> addWrite(ROW_ID, BINARY_ROW, TX_ID),

Review Comment:
   Let's make this write another row, not the same row that was written before, so we can distinguish the 'it was updated' case from 'it was not updated' case



##########
modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/AbstractMvPartitionStorageConcurrencyTest.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.storage;
+
+import static org.apache.ignite.internal.testframework.IgniteTestUtils.startRace;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+import org.apache.ignite.internal.hlc.HybridTimestamp;
+import org.apache.ignite.internal.storage.impl.TestStorageEngine;
+import org.junit.jupiter.api.RepeatedTest;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test to check for race conditions in MV partition storage.
+ */
+public abstract class AbstractMvPartitionStorageConcurrencyTest extends BaseMvPartitionStorageTest {
+    /** To be used in a loop. {@link RepeatedTest} has a smaller failure rate for some reasons. */

Review Comment:
   Probably because with `@RepeatedTest` a brand new storage instance is created?



##########
modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/AbstractMvPartitionStorageConcurrencyTest.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.storage;
+
+import static org.apache.ignite.internal.testframework.IgniteTestUtils.startRace;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+import org.apache.ignite.internal.hlc.HybridTimestamp;
+import org.apache.ignite.internal.storage.impl.TestStorageEngine;
+import org.junit.jupiter.api.RepeatedTest;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test to check for race conditions in MV partition storage.
+ */
+public abstract class AbstractMvPartitionStorageConcurrencyTest extends BaseMvPartitionStorageTest {
+    /** To be used in a loop. {@link RepeatedTest} has a smaller failure rate for some reasons. */
+    private static final int REPEATS = 100;
+
+    @Test
+    void testAbortAndRead() throws Exception {
+        for (int i = 0; i < REPEATS; i++) {
+            addWrite(ROW_ID, BINARY_ROW, TX_ID);
+
+            startRace(
+                    () -> abortWrite(ROW_ID),
+                    () -> read(ROW_ID, clock.now()),
+                    () -> scanFirstEntry(clock.now())
+            );
+
+            assertNull(read(ROW_ID, clock.now()));
+        }
+    }
+
+    @Test
+    void testCommitAndRead() throws Exception {
+        for (int i = 0; i < REPEATS; i++) {
+            addWrite(ROW_ID, BINARY_ROW, TX_ID);
+
+            startRace(
+                    () -> commitWrite(ROW_ID, clock.now()),
+                    () -> read(ROW_ID, clock.now()),
+                    () -> scanFirstEntry(clock.now())
+            );
+
+            assertRowMatches(read(ROW_ID, clock.now()), BINARY_ROW);
+        }
+    }
+
+    @Test
+    void testUpdateAndRead() throws Exception {
+        for (int i = 0; i < REPEATS; i++) {
+            addWrite(ROW_ID, BINARY_ROW, TX_ID);
+
+            startRace(
+                    () -> addWrite(ROW_ID, BINARY_ROW, TX_ID),
+                    () -> read(ROW_ID, clock.now()),
+                    () -> scanFirstEntry(clock.now())
+            );
+
+            assertRowMatches(read(ROW_ID, clock.now()), BINARY_ROW);
+        }
+    }
+
+    @Test
+    void testRegularGcAndRead() throws Exception {
+        //TODO https://issues.apache.org/jira/browse/IGNITE-18020
+        assumeTrue(engine instanceof TestStorageEngine);
+
+        for (int i = 0; i < REPEATS; i++) {
+            HybridTimestamp firstCommitTs = addAndCommit(BINARY_ROW);
+
+            addAndCommit(BINARY_ROW);
+
+            startRace(
+                    () -> pollForVacuum(HybridTimestamp.MAX_VALUE),
+                    () -> read(ROW_ID, firstCommitTs),
+                    () -> scanFirstEntry(firstCommitTs)
+            );
+
+            assertNull(pollForVacuum(HybridTimestamp.MAX_VALUE));
+
+            cleanup();
+        }
+    }
+
+    @Test
+    void testTombstoneGcAndRead() throws Exception {
+        //TODO https://issues.apache.org/jira/browse/IGNITE-18020
+        assumeTrue(engine instanceof TestStorageEngine);
+
+        for (int i = 0; i < REPEATS; i++) {
+            HybridTimestamp firstCommitTs = addAndCommit(BINARY_ROW);
+
+            addAndCommit(null);
+
+            startRace(
+                    () -> pollForVacuum(HybridTimestamp.MAX_VALUE),
+                    () -> read(ROW_ID, firstCommitTs),
+                    () -> scanFirstEntry(firstCommitTs)
+            );
+
+            assertNull(pollForVacuum(HybridTimestamp.MAX_VALUE));
+        }
+    }
+
+    @Test
+    void testTombstoneGcAndAddWrite() throws Exception {
+        //TODO https://issues.apache.org/jira/browse/IGNITE-18020
+        assumeTrue(engine instanceof TestStorageEngine);
+
+        for (int i = 0; i < REPEATS; i++) {
+            addAndCommit(BINARY_ROW);
+
+            addAndCommit(null);
+
+            startRace(
+                    () -> pollForVacuum(HybridTimestamp.MAX_VALUE),
+                    () -> addWrite(ROW_ID, BINARY_ROW, TX_ID)

Review Comment:
   Should this row be different from the first one?



##########
modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/AbstractMvPartitionStorageGcTest.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.storage;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+import org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import org.apache.ignite.internal.hlc.HybridTimestamp;
+import org.apache.ignite.internal.schema.BinaryRow;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Abstract test for MV partition storage GC.
+ */
+@ExtendWith(ConfigurationExtension.class)
+public abstract class AbstractMvPartitionStorageGcTest extends BaseMvPartitionStorageTest {
+    @Test
+    void testEmptyStorage() {
+        assertNull(storage.pollForVacuum(clock.now()));
+    }
+
+    @Test
+    void testSingleValueStorage() {
+        addAndCommit(BINARY_ROW);
+
+        assertNull(storage.pollForVacuum(clock.now()));
+    }
+
+    @Test
+    void testRegularPoll() {
+        HybridTimestamp firstCommitTs = addAndCommit(BINARY_ROW);
+
+        HybridTimestamp tsBetweenCommits = clock.now();
+
+        HybridTimestamp secondCommitTs = addAndCommit(BINARY_ROW);
+
+        // Data is still visible for older timestamps.
+        assertNull(storage.pollForVacuum(firstCommitTs));
+
+        assertNull(storage.pollForVacuum(tsBetweenCommits));
+
+        // Once a low watermark value becomes equal to second commit timestamp, previous value
+        // becomes completely inaccessible and should be purged.
+        BinaryRowWithRowId row = storage.pollForVacuum(secondCommitTs);
+
+        assertNotNull(row);
+
+        assertRowMatches(row.binaryRow(), BINARY_ROW);
+
+        // Read from the old timestamp should return null.
+        assertNull(read(ROW_ID, firstCommitTs));
+
+        // Read from the newer timestamp should return last value.
+        assertRowMatches(read(ROW_ID, secondCommitTs), BINARY_ROW);
+    }
+
+    @Test
+    void testPollFromUnderTombstone() {
+        addAndCommit(BINARY_ROW);
+        HybridTimestamp secondCommitTs = addAndCommit(null);
+
+        BinaryRowWithRowId row = storage.pollForVacuum(secondCommitTs);
+
+        assertNotNull(row);
+        assertRowMatches(row.binaryRow(), BINARY_ROW);
+
+        assertNull(read(ROW_ID, secondCommitTs));
+
+        // Check that tombstone is also deleted from the partition. It must be empty at this point.
+        assertNull(storage.closestRowId(ROW_ID));
+    }
+
+    @Test
+    void testDoubleTombstone() {
+        addAndCommit(BINARY_ROW);
+        addAndCommit(null);
+        HybridTimestamp lastCommitTs = addAndCommit(null);
+
+        BinaryRowWithRowId row = storage.pollForVacuum(lastCommitTs);
+
+        assertNotNull(row);
+        assertRowMatches(row.binaryRow(), BINARY_ROW);
+
+        assertNull(read(ROW_ID, lastCommitTs));
+
+        // Check that all tombstones are deleted from the partition. It must be empty at this point.
+        assertNull(storage.closestRowId(ROW_ID));
+    }
+
+    @Test
+    void testManyOldVersions() {
+        addAndCommit(BINARY_ROW);
+
+        BinaryRow binaryRow2 = binaryRow(KEY, new TestValue(50, "50"));
+
+        addAndCommit(binaryRow2);
+
+        HybridTimestamp lowWatermark = addAndCommit(null);
+
+        // Poll the oldest row.
+        BinaryRowWithRowId row = pollForVacuum(lowWatermark);
+
+        assertNotNull(row);
+        assertRowMatches(row.binaryRow(), BINARY_ROW);
+
+        // Poll the next oldest row.
+        row = pollForVacuum(lowWatermark);
+
+        assertNotNull(row);
+        assertRowMatches(row.binaryRow(), binaryRow2);
+
+        // Nothing else to poll.
+        assertNull(pollForVacuum(lowWatermark));
+    }
+}

Review Comment:
   Let's add a test (or a few) that models the real threading model that will be used in production code: one thread modifies the storage, another one acts as a GC. This might help catch visibility bugs (if, for some reason, `pollForVacuum()` never sees changes made by other methods).



##########
modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/impl/TestMvPartitionStorage.java:
##########
@@ -204,7 +215,18 @@ public void commitWrite(RowId rowId, HybridTimestamp timestamp) {
                 return versionChain;
             }
 
-            return VersionChain.forCommitted(timestamp, versionChain);
+            VersionChain committedVersionChain = VersionChain.forCommitted(timestamp, versionChain);
+
+            if (committedVersionChain.next != null) {
+                // Avoid creating tombstones for tombstones.
+                if (committedVersionChain.row == null && committedVersionChain.next.row == null) {
+                    return committedVersionChain.next;
+                }
+
+                gcQueue.add(new IgniteBiTuple<>(committedVersionChain, rowId));

Review Comment:
   Also, access to `gcQueue` is not synchronized explicitly. `ConcurrentMap` guarantees happens-before between actions with the map and actions around these actions (before or after them in the Program Order), but does it make any guarantees about what happens inside remapping functions? Documentation on `ConcurrentSkipListMap#compute()` specifically says that it does NOT guarantee atomicity for remapping functions.



##########
modules/core/src/testFixtures/java/org/apache/ignite/internal/testframework/IgniteTestUtils.java:
##########
@@ -766,4 +771,43 @@ public static <T> T await(CompletionStage<T> stage, long timeout, TimeUnit unit)
     public static <T> T await(CompletionStage<T> stage) {
         return await(stage, TIMEOUT_SEC, TimeUnit.SECONDS);
     }
+
+    /**
+     * Runs all actions, each in a separate thread, having a {@link CyclicBarrier} before calling {@link RunnableX#run()}.
+     *
+     * @throws InterruptedException If failed to {@link Thread#join()} a thread.

Review Comment:
   ```suggestion
        * @throws InterruptedException If interrupted when trying to {@link Thread#join()} a thread.
   ```



##########
modules/core/src/testFixtures/java/org/apache/ignite/internal/testframework/IgniteTestUtils.java:
##########
@@ -766,4 +771,43 @@ public static <T> T await(CompletionStage<T> stage, long timeout, TimeUnit unit)
     public static <T> T await(CompletionStage<T> stage) {
         return await(stage, TIMEOUT_SEC, TimeUnit.SECONDS);
     }
+
+    /**
+     * Runs all actions, each in a separate thread, having a {@link CyclicBarrier} before calling {@link RunnableX#run()}.
+     *
+     * @throws InterruptedException If failed to {@link Thread#join()} a thread.
+     */
+    public static void startRace(RunnableX... actions) throws InterruptedException {

Review Comment:
   `start` makes me think that it just starts the threads, but does not wait for them to finish. But here, the method is fully synchronous as it joins the threads. How about `runRace()`?



##########
modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/AbstractMvPartitionStorageGcTest.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.storage;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+import org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import org.apache.ignite.internal.hlc.HybridTimestamp;
+import org.apache.ignite.internal.schema.BinaryRow;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Abstract test for MV partition storage GC.
+ */
+@ExtendWith(ConfigurationExtension.class)
+public abstract class AbstractMvPartitionStorageGcTest extends BaseMvPartitionStorageTest {
+    @Test
+    void testEmptyStorage() {
+        assertNull(storage.pollForVacuum(clock.now()));
+    }
+
+    @Test
+    void testSingleValueStorage() {
+        addAndCommit(BINARY_ROW);
+
+        assertNull(storage.pollForVacuum(clock.now()));
+    }
+
+    @Test
+    void testRegularPoll() {
+        HybridTimestamp firstCommitTs = addAndCommit(BINARY_ROW);
+
+        HybridTimestamp tsBetweenCommits = clock.now();
+
+        HybridTimestamp secondCommitTs = addAndCommit(BINARY_ROW);

Review Comment:
   Let's use a row with a different value to be able to differentiate the values



##########
modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/AbstractMvPartitionStorageConcurrencyTest.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.storage;
+
+import static org.apache.ignite.internal.testframework.IgniteTestUtils.startRace;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+import org.apache.ignite.internal.hlc.HybridTimestamp;
+import org.apache.ignite.internal.storage.impl.TestStorageEngine;
+import org.junit.jupiter.api.RepeatedTest;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test to check for race conditions in MV partition storage.
+ */
+public abstract class AbstractMvPartitionStorageConcurrencyTest extends BaseMvPartitionStorageTest {
+    /** To be used in a loop. {@link RepeatedTest} has a smaller failure rate for some reasons. */
+    private static final int REPEATS = 100;
+
+    @Test
+    void testAbortAndRead() throws Exception {
+        for (int i = 0; i < REPEATS; i++) {
+            addWrite(ROW_ID, BINARY_ROW, TX_ID);
+
+            startRace(
+                    () -> abortWrite(ROW_ID),
+                    () -> read(ROW_ID, clock.now()),
+                    () -> scanFirstEntry(clock.now())
+            );
+
+            assertNull(read(ROW_ID, clock.now()));
+        }
+    }
+
+    @Test
+    void testCommitAndRead() throws Exception {
+        for (int i = 0; i < REPEATS; i++) {
+            addWrite(ROW_ID, BINARY_ROW, TX_ID);
+
+            startRace(
+                    () -> commitWrite(ROW_ID, clock.now()),
+                    () -> read(ROW_ID, clock.now()),
+                    () -> scanFirstEntry(clock.now())
+            );
+
+            assertRowMatches(read(ROW_ID, clock.now()), BINARY_ROW);
+        }
+    }
+
+    @Test
+    void testUpdateAndRead() throws Exception {
+        for (int i = 0; i < REPEATS; i++) {
+            addWrite(ROW_ID, BINARY_ROW, TX_ID);
+
+            startRace(
+                    () -> addWrite(ROW_ID, BINARY_ROW, TX_ID),
+                    () -> read(ROW_ID, clock.now()),
+                    () -> scanFirstEntry(clock.now())
+            );
+
+            assertRowMatches(read(ROW_ID, clock.now()), BINARY_ROW);
+        }
+    }
+
+    @Test
+    void testRegularGcAndRead() throws Exception {
+        //TODO https://issues.apache.org/jira/browse/IGNITE-18020
+        assumeTrue(engine instanceof TestStorageEngine);

Review Comment:
   Why do we need this assertion? On an engine where GC is not implemented the corresponding exception will demonstrate clearly why this test has failed.



##########
modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/AbstractMvPartitionStorageGcTest.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.storage;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+import org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import org.apache.ignite.internal.hlc.HybridTimestamp;
+import org.apache.ignite.internal.schema.BinaryRow;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Abstract test for MV partition storage GC.
+ */
+@ExtendWith(ConfigurationExtension.class)

Review Comment:
   Why is it needed here?



##########
modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/AbstractMvPartitionStorageGcTest.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.storage;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+import org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import org.apache.ignite.internal.hlc.HybridTimestamp;
+import org.apache.ignite.internal.schema.BinaryRow;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Abstract test for MV partition storage GC.
+ */
+@ExtendWith(ConfigurationExtension.class)
+public abstract class AbstractMvPartitionStorageGcTest extends BaseMvPartitionStorageTest {
+    @Test
+    void testEmptyStorage() {
+        assertNull(storage.pollForVacuum(clock.now()));
+    }
+
+    @Test
+    void testSingleValueStorage() {
+        addAndCommit(BINARY_ROW);
+
+        assertNull(storage.pollForVacuum(clock.now()));
+    }
+
+    @Test
+    void testRegularPoll() {
+        HybridTimestamp firstCommitTs = addAndCommit(BINARY_ROW);
+
+        HybridTimestamp tsBetweenCommits = clock.now();
+
+        HybridTimestamp secondCommitTs = addAndCommit(BINARY_ROW);
+
+        // Data is still visible for older timestamps.
+        assertNull(storage.pollForVacuum(firstCommitTs));
+
+        assertNull(storage.pollForVacuum(tsBetweenCommits));
+
+        // Once a low watermark value becomes equal to second commit timestamp, previous value
+        // becomes completely inaccessible and should be purged.
+        BinaryRowWithRowId row = storage.pollForVacuum(secondCommitTs);

Review Comment:
   ```suggestion
           BinaryRowWithRowId gcedRow = storage.pollForVacuum(secondCommitTs);
   ```



##########
modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/impl/TestMvPartitionStorage.java:
##########
@@ -426,6 +448,51 @@ public ReadResult next() {
         return map.ceilingKey(lowerBound);
     }
 
+    @Override
+    public @Nullable BinaryRowWithRowId pollForVacuum(HybridTimestamp lowWatermark) {
+        Iterator<IgniteBiTuple<VersionChain, RowId>> it = gcQueue.iterator();

Review Comment:
   No synchronization whatsoever. How about changing `gcQueue` to `ConcurrentSkipListSet`?



##########
modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/AbstractMvPartitionStorageConcurrencyTest.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.storage;
+
+import static org.apache.ignite.internal.testframework.IgniteTestUtils.startRace;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+import org.apache.ignite.internal.hlc.HybridTimestamp;
+import org.apache.ignite.internal.storage.impl.TestStorageEngine;
+import org.junit.jupiter.api.RepeatedTest;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test to check for race conditions in MV partition storage.
+ */
+public abstract class AbstractMvPartitionStorageConcurrencyTest extends BaseMvPartitionStorageTest {
+    /** To be used in a loop. {@link RepeatedTest} has a smaller failure rate for some reasons. */
+    private static final int REPEATS = 100;
+
+    @Test
+    void testAbortAndRead() throws Exception {
+        for (int i = 0; i < REPEATS; i++) {
+            addWrite(ROW_ID, BINARY_ROW, TX_ID);
+
+            startRace(
+                    () -> abortWrite(ROW_ID),
+                    () -> read(ROW_ID, clock.now()),
+                    () -> scanFirstEntry(clock.now())
+            );
+
+            assertNull(read(ROW_ID, clock.now()));
+        }
+    }
+
+    @Test
+    void testCommitAndRead() throws Exception {
+        for (int i = 0; i < REPEATS; i++) {

Review Comment:
   Shouldn't the storage be cleaned between iterations? Otherwise, more and more versions of the row are added. Is this ok?
   
   The same question also probably concerns other tests.



##########
modules/storage-api/src/testFixtures/java/org/apache/ignite/internal/storage/impl/TestMvPartitionStorage.java:
##########
@@ -204,7 +215,18 @@ public void commitWrite(RowId rowId, HybridTimestamp timestamp) {
                 return versionChain;
             }
 
-            return VersionChain.forCommitted(timestamp, versionChain);
+            VersionChain committedVersionChain = VersionChain.forCommitted(timestamp, versionChain);
+
+            if (committedVersionChain.next != null) {
+                // Avoid creating tombstones for tombstones.
+                if (committedVersionChain.row == null && committedVersionChain.next.row == null) {
+                    return committedVersionChain.next;
+                }
+
+                gcQueue.add(new IgniteBiTuple<>(committedVersionChain, rowId));

Review Comment:
   `ConcurrentSkipListMap#compute()` may invoke the remapping function more than once due to concurrency. Here, the remapping function has a side effect (addition to the queue). It seems that it can result in multiple insertion to the queue. `VersionChain` does not define `equals()/hashCode()`, so this will really result in a chain being stored twice in the GC queue.



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