Phillippko commented on code in PR #4572: URL: https://github.com/apache/ignite-3/pull/4572#discussion_r1802833543
########## modules/metastorage/src/integrationTest/java/org/apache/ignite/internal/metastorage/impl/ItMetaStorageManagerImplTest.java: ########## @@ -338,6 +349,93 @@ public void onError(Throwable e) { assertThat("Safe time is advanced too early", watchCompleted.get(), is(true)); } + @Test + void testReadOperationsFutureWithoutReadOperations() { + assertTrue(metaStorageManager.readOperationsFuture(0).isDone()); + assertTrue(metaStorageManager.readOperationsFuture(1).isDone()); + } + + /** + * Tests {@link MetaStorageManagerImpl#readOperationsFuture} as expected in use. + * <ul> + * <li>Creates read operations from the leader and local ones.</li> + * <li>Set a new compaction revision via {@link MetaStorageManagerImpl#setCompactionRevisionLocally}.</li> + * <li>Wait for the completion of read operations on the new compaction revision.</li> + * </ul> + * + * <p>Due to the difficulty of testing all reading from leader methods at once, we test each of them separately.</p> + */ + @ParameterizedTest + @MethodSource("readFromLeaderOperations") + public void testReadOperationsFuture(ReadFromLeaderAction readFromLeaderAction) { + assertThat(metaStorageManager.put(FOO_KEY, VALUE), willCompleteSuccessfully()); + assertThat(metaStorageManager.put(FOO_KEY, VALUE), willCompleteSuccessfully()); + + var startSendReadActionRequestFuture = new CompletableFuture<Void>(); + var continueSendReadActionRequestFuture = new CompletableFuture<>(); + + ((DefaultMessagingService) clusterService.messagingService()).dropMessages((recipientConsistentId, message) -> { + if (message instanceof ReadActionRequest) { + startSendReadActionRequestFuture.complete(null); + + assertThat(continueSendReadActionRequestFuture, willCompleteSuccessfully()); + } + + return false; + }); + + CompletableFuture<?> readFromLeaderOperationFuture = readFromLeaderAction.read(metaStorageManager, FOO_KEY); + Cursor<Entry> getLocallyCursor = metaStorageManager.getLocally(FOO_KEY, FOO_KEY, 5); + + assertThat(startSendReadActionRequestFuture, willCompleteSuccessfully()); + + metaStorageManager.setCompactionRevisionLocally(1); + + CompletableFuture<Void> readOperationsFuture = metaStorageManager.readOperationsFuture(1); Review Comment: Let's add checks that operations after / equal to revisionExcluded are ignored ########## modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/server/ReadOperationForCompactionTracker.java: ########## @@ -0,0 +1,147 @@ +/* + * 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; + +import static java.util.stream.Collectors.collectingAndThen; +import static java.util.stream.Collectors.toList; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.ignite.internal.metastorage.MetaStorageCompactionManager; +import org.apache.ignite.internal.tostring.IgniteToStringInclude; +import org.apache.ignite.internal.tostring.S; +import org.apache.ignite.internal.util.CompletableFutures; + +/** + * Tracker of read operations from metastorage or its storage. Used to track the completion of read operations before start local + * compaction of metastorage. + * + * <p>Expected usage:</p> + * <ul> + * <li>Before starting execution, the reading command invoke {@link #track} with its ID and the compaction revision that is currently + * set ({@link MetaStorageCompactionManager#setCompactionRevisionLocally}/{@link KeyValueStorage#setCompactionRevision}).</li> + * <li>After completion, the reading command will invoke {@link #untrack} with the same arguments as when calling {@link #track}, + * regardless of whether the operation was successful or not.</li> + * <li>{@link #collect} will be invoked only after a new compaction revision has been set + * ({@link MetaStorageCompactionManager#setCompactionRevisionLocally}/{@link KeyValueStorage#setCompactionRevision}) for a new + * compaction revision.</li> + * </ul> + */ +public class ReadOperationForCompactionTracker { + private final Map<ReadOperationKey, CompletableFuture<Void>> readOperationFutureByKey = new ConcurrentHashMap<>(); + + /** + * Starts tracking the completion of a read operation on the current compaction revision. + * + * <p>Method is expected not to be called more than once for the same arguments.</p> + * + * <p>Expected usage pattern:</p> + * <pre><code> + * Object readOperationId = ...; + * int compactionRevision = ...; + * + * tracker.track(readOperationId, compactionRevision); + * + * try { + * doReadOperation(...); + * } finally { + * tracker.untrack(readOperationId, compactionRevision); + * } + * </code></pre> + * + * @see #untrack(Object, long) + */ + public void track(Object readOperationId, long compactionRevision) { Review Comment: could we use UUID instead of Object? ########## modules/metastorage/src/test/java/org/apache/ignite/internal/metastorage/server/AbstractCompactionKeyValueStorageTest.java: ########## @@ -894,6 +898,50 @@ void testRangeAfterSetCompactionRevisionButBeforeStartCompaction() { } } + @Test + void testReadOperationsFutureWithoutReadOperations() { + assertTrue(storage.readOperationsFuture(0).isDone()); + assertTrue(storage.readOperationsFuture(1).isDone()); + } + + /** + * Tests {@link KeyValueStorage#readOperationsFuture} as expected in use. + * <ul> + * <li>Create read operations, we only need cursors since reading one entry or a batch is synchronized with + * {@link KeyValueStorage#setCompactionRevision}.</li> + * <li>Set a new compaction revision via {@link KeyValueStorage#setCompactionRevision}.</li> + * <li>Wait for the completion of read operations on the new compaction revision.</li> + * </ul> + * + * <p>The keys are chosen randomly. Keys with their revisions are added in {@link #setUp()}.</p> + */ + @Test + void testReadOperationsFuture() throws Exception { + Cursor<Entry> range0 = storage.range(FOO_KEY, FOO_KEY); + Cursor<Entry> range1 = storage.range(BAR_KEY, BAR_KEY, 5); + + try { + storage.setCompactionRevision(3); + + CompletableFuture<Void> readOperationsFuture = storage.readOperationsFuture(3); Review Comment: the same, let's check that the argument is used correctly ########## modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/server/ReadOperationForCompactionTracker.java: ########## @@ -0,0 +1,147 @@ +/* + * 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; + +import static java.util.stream.Collectors.collectingAndThen; +import static java.util.stream.Collectors.toList; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.ignite.internal.metastorage.MetaStorageCompactionManager; +import org.apache.ignite.internal.tostring.IgniteToStringInclude; +import org.apache.ignite.internal.tostring.S; +import org.apache.ignite.internal.util.CompletableFutures; + +/** + * Tracker of read operations from metastorage or its storage. Used to track the completion of read operations before start local + * compaction of metastorage. + * + * <p>Expected usage:</p> + * <ul> + * <li>Before starting execution, the reading command invoke {@link #track} with its ID and the compaction revision that is currently + * set ({@link MetaStorageCompactionManager#setCompactionRevisionLocally}/{@link KeyValueStorage#setCompactionRevision}).</li> + * <li>After completion, the reading command will invoke {@link #untrack} with the same arguments as when calling {@link #track}, + * regardless of whether the operation was successful or not.</li> + * <li>{@link #collect} will be invoked only after a new compaction revision has been set + * ({@link MetaStorageCompactionManager#setCompactionRevisionLocally}/{@link KeyValueStorage#setCompactionRevision}) for a new + * compaction revision.</li> + * </ul> + */ +public class ReadOperationForCompactionTracker { + private final Map<ReadOperationKey, CompletableFuture<Void>> readOperationFutureByKey = new ConcurrentHashMap<>(); + + /** + * Starts tracking the completion of a read operation on the current compaction revision. + * + * <p>Method is expected not to be called more than once for the same arguments.</p> + * + * <p>Expected usage pattern:</p> + * <pre><code> + * Object readOperationId = ...; + * int compactionRevision = ...; + * + * tracker.track(readOperationId, compactionRevision); + * + * try { + * doReadOperation(...); + * } finally { + * tracker.untrack(readOperationId, compactionRevision); + * } + * </code></pre> + * + * @see #untrack(Object, long) + */ + public void track(Object readOperationId, long compactionRevision) { + var key = new ReadOperationKey(readOperationId, compactionRevision); + + CompletableFuture<Void> previous = readOperationFutureByKey.putIfAbsent(key, new CompletableFuture<>()); + + assert previous == null : key; + } + + /** + * Stops tracking the read operation on the compaction revision on which tracking start. + * + * <p>Method is expected not to be called more than once for the same arguments, and {@link #track} was previously called for same + * arguments.</p> + * + * @see #track(Object, long) + */ + public void untrack(Object readOperationId, long compactionRevision) { + var key = new ReadOperationKey(readOperationId, compactionRevision); + + CompletableFuture<Void> removed = readOperationFutureByKey.remove(key); + + assert removed != null : key; + + removed.complete(null); + } + + /** + * Collects all read operations that were started before {@code compactionRevisionExcluded} and returns a future that will complete + * when all collected operations complete. Review Comment: ```suggestion * Returns a future that completes when all read operations that were started before {@code compactionRevisionExcluded} complete. ``` a little inconsistent naming - revisionExcluded and compactionRevisionExcluded in different places ########## modules/metastorage-api/src/main/java/org/apache/ignite/internal/metastorage/MetaStorageCompactionManager.java: ########## @@ -102,4 +103,16 @@ public interface MetaStorageCompactionManager extends IgniteComponent { * @see #saveCompactionRevisionLocally(long) */ long getCompactionRevisionLocally(); + + /** + * Returns a future that will complete when all read operations (from leader and locally) that were started before + * {@code revisionExcluded}. + * + * <p>Current method is expected to be invoked after {@link #setCompactionRevisionLocally} on the same revision.</p> Review Comment: ```suggestion * <p>Should be called after {@link #setCompactionRevisionLocally} on the same revision.</p> ``` ########## modules/metastorage-api/src/main/java/org/apache/ignite/internal/metastorage/MetaStorageCompactionManager.java: ########## @@ -102,4 +103,16 @@ public interface MetaStorageCompactionManager extends IgniteComponent { * @see #saveCompactionRevisionLocally(long) */ long getCompactionRevisionLocally(); + + /** + * Returns a future that will complete when all read operations (from leader and locally) that were started before + * {@code revisionExcluded}. Review Comment: ```suggestion * {@code revisionExcluded} will be completed. ``` ########## modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/server/AbstractKeyValueStorage.java: ########## @@ -273,6 +264,11 @@ public void watchExact(byte[] key, long rev, WatchListener listener) { watchProcessor.addWatch(new Watch(rev, listener, exactPredicate)); } + @Override + public CompletableFuture<Void> readOperationsFuture(long revisionExcluded) { Review Comment: ```suggestion public CompletableFuture<Void> readOperationsCompletedFuture(long revisionExcluded) { ``` not insisting -- 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-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org