rpuch commented on code in PR #4839: URL: https://github.com/apache/ignite-3/pull/4839#discussion_r1871428408
########## modules/metastorage/src/test/java/org/apache/ignite/internal/metastorage/server/UpdateRevisionOperation.java: ########## @@ -0,0 +1,143 @@ +/* + * 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.toList; +import static org.apache.ignite.internal.hlc.HybridTimestamp.hybridTimestamp; +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.metastorage.server.AbstractKeyValueStorageTest.key; +import static org.apache.ignite.internal.metastorage.server.BasicOperationsKeyValueStorageTest.createCommandId; +import static org.apache.ignite.internal.metastorage.server.KeyValueUpdateContext.kvContext; +import static org.apache.ignite.internal.util.ArrayUtils.INT_EMPTY_ARRAY; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; +import java.util.function.Consumer; +import java.util.stream.IntStream; +import org.apache.ignite.internal.lang.ByteArray; +import org.apache.ignite.internal.metastorage.Entry; +import org.apache.ignite.internal.metastorage.dsl.Operation; +import org.apache.ignite.internal.metastorage.dsl.Operations; +import org.apache.ignite.internal.metastorage.server.ExistenceCondition.Type; + +/** + * Enumeration for testing storage revision change notification. It is expected that there is already a {@link Entry} with key {@code 0} in Review Comment: ```suggestion * Enumeration for testing storage revision change notification. It is expected that there is already an {@link Entry} with key {@code 0} in ``` ########## modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/server/persistence/RocksDbKeyValueStorage.java: ########## @@ -978,12 +979,14 @@ public void startWatches(long startRevision, WatchEventHandlingCallback callback } if (currentRevision != 0) { - Set<NotifyWatchProcessorEvent> fromStorage = collectNotifyWatchProcessorEventsFromStorage(startRevision, currentRevision); + Set<UpdateEntriesEvent> updateEntriesEvents = collectUpdateEntriesEventsFromStorage(startRevision, currentRevision); + Set<UpdateOnlyRevisionEvent> updateOnlyRevisionEvents = collectUpdateRevisionEventsFromStorage(startRevision, currentRevision); rwLock.writeLock().lock(); try { - notifyWatchProcessorEventsBeforeStartingWatches.addAll(fromStorage); + notifyWatchProcessorEventsBeforeStartingWatches.addAll(updateEntriesEvents); + notifyWatchProcessorEventsBeforeStartingWatches.addAll(updateOnlyRevisionEvents); Review Comment: Let's add a comment explaining that this addition will only add events for revisions which did not have entries as revisions which had entries are already 'occupied' ########## modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/server/WatchProcessor.java: ########## @@ -427,4 +427,22 @@ void updateCompactionRevision(long compactionRevision, HybridTimestamp time) { } }); } + + /** + * Updates the metastorage revision in the WatchEvent queue. It should be used for those cases when the revision has been updated but + * no {@link Entry}s have been updated. + * + * @param newRevision New metastorage revision. + * @param time Metastorage revision update timestamp. + */ + void updateOnlyRevision(long newRevision, HybridTimestamp time) { + notificationFuture = notificationFuture + .thenComposeAsync(unused -> notifyUpdateRevisionListeners(newRevision), watchExecutor) + .thenRunAsync(() -> invokeOnRevisionCallback(newRevision, time), watchExecutor) + .whenComplete((ignored, e) -> { + if (e != null) { + failureManager.process(new FailureContext(CRITICAL_ERROR, e)); Review Comment: ```suggestion notifyFailureHandlerOnFirstFailureInNotificationChain(e); ``` Otherwise it will start spamming the logs on each revision update following a failed one ########## modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/server/persistence/RocksDbKeyValueStorage.java: ########## @@ -1144,39 +1147,24 @@ private void addAllToBatch( * Adds modified entries to the watch event queue. */ private void queueWatchEvent() { - if (updatedEntries.isEmpty()) { Review Comment: Why is this block removed? ########## modules/metastorage/src/test/java/org/apache/ignite/internal/metastorage/server/UpdateRevisionOperation.java: ########## @@ -0,0 +1,143 @@ +/* + * 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.toList; +import static org.apache.ignite.internal.hlc.HybridTimestamp.hybridTimestamp; +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.metastorage.server.AbstractKeyValueStorageTest.key; +import static org.apache.ignite.internal.metastorage.server.BasicOperationsKeyValueStorageTest.createCommandId; +import static org.apache.ignite.internal.metastorage.server.KeyValueUpdateContext.kvContext; +import static org.apache.ignite.internal.util.ArrayUtils.INT_EMPTY_ARRAY; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; +import java.util.function.Consumer; +import java.util.stream.IntStream; +import org.apache.ignite.internal.lang.ByteArray; +import org.apache.ignite.internal.metastorage.Entry; +import org.apache.ignite.internal.metastorage.dsl.Operation; +import org.apache.ignite.internal.metastorage.dsl.Operations; +import org.apache.ignite.internal.metastorage.server.ExistenceCondition.Type; + +/** + * Enumeration for testing storage revision change notification. It is expected that there is already a {@link Entry} with key {@code 0} in + * the storage. + */ +enum UpdateRevisionOperation { + // Simple operations. + PUT_NEW(storage -> put(storage, 1)), + PUT_ALL_NEW(storage -> putAll(storage, 1)), + PUT_EXISTS(storage -> put(storage, 0)), + PUT_ALL_EXISTS(storage -> putAll(storage, 0)), + REMOVE_EXISTS(storage -> remove(storage, 0)), + REMOVE_ALL_EXISTS(storage -> removeAll(storage, 0)), + REMOVE_NOT_EXISTS(storage -> remove(storage, 1)), + REMOVE_ALL_NOT_EXISTS(storage -> removeAll(storage, 1)), + PUT_ALL_EMPTY(storage -> putAll(storage, INT_EMPTY_ARRAY)), + REMOVE_ALL_EMPTY(storage -> removeAll(storage, INT_EMPTY_ARRAY)), + // Invoke operations. + INVOKE_PUT_NEW(storage -> invokeSuccessCondition(storage, putOperation(1))), + INVOKE_IF_PUT_NEW(storage -> invokeIfSuccessCondition(storage, putOperation(1))), + INVOKE_PUT_EXISTS(storage -> invokeSuccessCondition(storage, putOperation(0))), + INVOKE_IF_PUT_EXISTS(storage -> invokeIfSuccessCondition(storage, putOperation(0))), + INVOKE_REMOVE_EXISTS(storage -> invokeSuccessCondition(storage, removeOperation(0))), + INVOKE_IF_REMOVE_EXISTS(storage -> invokeIfSuccessCondition(storage, removeOperation(0))), + INVOKE_REMOVE_NOT_EXISTS(storage -> invokeSuccessCondition(storage, removeOperation(1))), + INVOKE_IF_REMOVE_NOT_EXISTS(storage -> invokeIfSuccessCondition(storage, removeOperation(1))), + INVOKE_NOOP(storage -> invokeSuccessCondition(storage, noop())), + INVOKE_IF_NOOP(storage -> invokeIfSuccessCondition(storage, noop())); + + private final Consumer<KeyValueStorage> function; + + UpdateRevisionOperation(Consumer<KeyValueStorage> function) { + this.function = function; + } + + void execute(KeyValueStorage storage) { + Entry entry = storage.get(key(0)); + + assertTrue(!entry.empty() && !entry.tombstone()); Review Comment: How about splitting this into 2 `assertFalse()` calls? It would become easier to understand which condition failed ########## modules/metastorage/src/test/java/org/apache/ignite/internal/metastorage/server/UpdateRevisionOperation.java: ########## @@ -0,0 +1,143 @@ +/* + * 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.toList; +import static org.apache.ignite.internal.hlc.HybridTimestamp.hybridTimestamp; +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.metastorage.server.AbstractKeyValueStorageTest.key; +import static org.apache.ignite.internal.metastorage.server.BasicOperationsKeyValueStorageTest.createCommandId; +import static org.apache.ignite.internal.metastorage.server.KeyValueUpdateContext.kvContext; +import static org.apache.ignite.internal.util.ArrayUtils.INT_EMPTY_ARRAY; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; +import java.util.function.Consumer; +import java.util.stream.IntStream; +import org.apache.ignite.internal.lang.ByteArray; +import org.apache.ignite.internal.metastorage.Entry; +import org.apache.ignite.internal.metastorage.dsl.Operation; +import org.apache.ignite.internal.metastorage.dsl.Operations; +import org.apache.ignite.internal.metastorage.server.ExistenceCondition.Type; + +/** + * Enumeration for testing storage revision change notification. It is expected that there is already a {@link Entry} with key {@code 0} in + * the storage. + */ +enum UpdateRevisionOperation { + // Simple operations. + PUT_NEW(storage -> put(storage, 1)), + PUT_ALL_NEW(storage -> putAll(storage, 1)), + PUT_EXISTS(storage -> put(storage, 0)), Review Comment: ```suggestion PUT_EXISTING(storage -> put(storage, 0)), ``` ########## modules/metastorage/src/test/java/org/apache/ignite/internal/metastorage/server/BasicOperationsKeyValueStorageTest.java: ########## @@ -2195,6 +2199,48 @@ void testClear() { assertThrows(CompactedException.class, () -> storage.revisionByTimestamp(hybridTimestamp(2))); } + @ParameterizedTest + @EnumSource(UpdateRevisionOperation.class) + void testNotifyUpdateRevisionForOperationAfterStartWatches(UpdateRevisionOperation updateRevisionOperation) { + var revisionUpdateListener = new TestRevisionUpdateListener(); + storage.registerRevisionUpdateListener(revisionUpdateListener); + + var watchEventHandlingCallback = new TestWatchEventHandlingCallback(); + storage.startWatches(1, watchEventHandlingCallback); + + storage.put(key(0), keyValue(0, 1), kvContext(hybridTimestamp(10))); + + long revision = storage.revision(); + long newRevision = revision + 1; + + updateRevisionOperation.execute(storage); + + assertThat(storage.revision(), equalTo(newRevision)); + assertThat(revisionUpdateListener.get(newRevision), willSucceedFast()); Review Comment: `willSucceedFast()` seems to be dangerous to use as an unfortunate GC pause might fail a test. `willCompleteSuccessfully()` looks safer ########## modules/metastorage/src/test/java/org/apache/ignite/internal/metastorage/server/UpdateRevisionOperation.java: ########## @@ -0,0 +1,143 @@ +/* + * 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.toList; +import static org.apache.ignite.internal.hlc.HybridTimestamp.hybridTimestamp; +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.metastorage.server.AbstractKeyValueStorageTest.key; +import static org.apache.ignite.internal.metastorage.server.BasicOperationsKeyValueStorageTest.createCommandId; +import static org.apache.ignite.internal.metastorage.server.KeyValueUpdateContext.kvContext; +import static org.apache.ignite.internal.util.ArrayUtils.INT_EMPTY_ARRAY; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; +import java.util.function.Consumer; +import java.util.stream.IntStream; +import org.apache.ignite.internal.lang.ByteArray; +import org.apache.ignite.internal.metastorage.Entry; +import org.apache.ignite.internal.metastorage.dsl.Operation; +import org.apache.ignite.internal.metastorage.dsl.Operations; +import org.apache.ignite.internal.metastorage.server.ExistenceCondition.Type; + +/** + * Enumeration for testing storage revision change notification. It is expected that there is already a {@link Entry} with key {@code 0} in + * the storage. + */ +enum UpdateRevisionOperation { + // Simple operations. + PUT_NEW(storage -> put(storage, 1)), + PUT_ALL_NEW(storage -> putAll(storage, 1)), + PUT_EXISTS(storage -> put(storage, 0)), + PUT_ALL_EXISTS(storage -> putAll(storage, 0)), + REMOVE_EXISTS(storage -> remove(storage, 0)), + REMOVE_ALL_EXISTS(storage -> removeAll(storage, 0)), + REMOVE_NOT_EXISTS(storage -> remove(storage, 1)), + REMOVE_ALL_NOT_EXISTS(storage -> removeAll(storage, 1)), + PUT_ALL_EMPTY(storage -> putAll(storage, INT_EMPTY_ARRAY)), + REMOVE_ALL_EMPTY(storage -> removeAll(storage, INT_EMPTY_ARRAY)), + // Invoke operations. + INVOKE_PUT_NEW(storage -> invokeSuccessCondition(storage, putOperation(1))), + INVOKE_IF_PUT_NEW(storage -> invokeIfSuccessCondition(storage, putOperation(1))), + INVOKE_PUT_EXISTS(storage -> invokeSuccessCondition(storage, putOperation(0))), + INVOKE_IF_PUT_EXISTS(storage -> invokeIfSuccessCondition(storage, putOperation(0))), + INVOKE_REMOVE_EXISTS(storage -> invokeSuccessCondition(storage, removeOperation(0))), + INVOKE_IF_REMOVE_EXISTS(storage -> invokeIfSuccessCondition(storage, removeOperation(0))), + INVOKE_REMOVE_NOT_EXISTS(storage -> invokeSuccessCondition(storage, removeOperation(1))), + INVOKE_IF_REMOVE_NOT_EXISTS(storage -> invokeIfSuccessCondition(storage, removeOperation(1))), + INVOKE_NOOP(storage -> invokeSuccessCondition(storage, noop())), + INVOKE_IF_NOOP(storage -> invokeIfSuccessCondition(storage, noop())); + + private final Consumer<KeyValueStorage> function; + + UpdateRevisionOperation(Consumer<KeyValueStorage> function) { + this.function = function; + } + + void execute(KeyValueStorage storage) { + Entry entry = storage.get(key(0)); + + assertTrue(!entry.empty() && !entry.tombstone()); + + function.accept(storage); + } + + private static void put(KeyValueStorage storage, int key) { + storage.put(key(key), value(1), randomKvContext()); + } + + private static void putAll(KeyValueStorage storage, int... keys) { + List<byte[]> keyList = IntStream.of(keys).mapToObj(AbstractKeyValueStorageTest::key).collect(toList()); + List<byte[]> valueList = IntStream.of(keys).mapToObj(UpdateRevisionOperation::value).collect(toList()); + + storage.putAll(keyList, valueList, randomKvContext()); + } + + private static void remove(KeyValueStorage storage, int key) { + storage.remove(key(key), randomKvContext()); + } + + private static void removeAll(KeyValueStorage storage, int... keys) { + List<byte[]> keyList = IntStream.of(keys).mapToObj(AbstractKeyValueStorageTest::key).collect(toList()); Review Comment: How about extracting a method? -- 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