You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "rpuch (via GitHub)" <gi...@apache.org> on 2023/06/28 08:02:38 UTC

[GitHub] [ignite-3] rpuch commented on a diff in pull request #2261: IGNITE-19851 Add MetaStorage revision update listeners and use instead of configuration revision update listeners

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


##########
modules/configuration/src/main/java/org/apache/ignite/internal/configuration/notifications/ConfigurationStorageRevisionListener.java:
##########
@@ -24,6 +24,7 @@
  *
  * <p>Storage revision - monotonously increasing counter, linked to the specific storage for current configuration values.
  */
+// TODO: IGNITE-19801 Get rid of

Review Comment:
   ```suggestion
   // TODO: IGNITE-19801 Get rid of this interface
   ```



##########
modules/metastorage-api/src/main/java/org/apache/ignite/internal/metastorage/MetaStorageManager.java:
##########
@@ -222,4 +222,13 @@ public interface MetaStorageManager extends IgniteComponent {
      * The value of the future is the revision which must be used for state recovery by other components.
      */
     CompletableFuture<Long> recoveryFinishedFuture();
+
+    /** Registers a Meta Storage revision update listener. */
+    void registerRevisionUpdateListener(RevisionUpdateListener listener);
+
+    /** Unregisters a Meta Storage revision update listener. */
+    void unregisterRevisionUpdateListener(RevisionUpdateListener listener);
+
+    /** Explicitly notifies revision update listeners. */
+    CompletableFuture<Void> notifyRevisionUpdateListenerOnStart(long newRevision);

Review Comment:
   Is this method needed on the 'public' interface? Would it be enough to have it on the main implementation?



##########
modules/metastorage-api/src/main/java/org/apache/ignite/internal/metastorage/WatchListener.java:
##########
@@ -37,6 +37,7 @@ public interface WatchListener {
      * @param revision Meta Storage revision.
      * @return Future that will be completed when the event is processed.
      */
+    // TODO: IGNITE-19801 Get rid of

Review Comment:
   ```suggestion
       // TODO: IGNITE-19801 Get rid of this method
   ```



##########
modules/metastorage-api/src/main/java/org/apache/ignite/internal/metastorage/RevisionUpdateListener.java:
##########
@@ -0,0 +1,34 @@
+/*
+ * 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;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * The listener which receives and handles the Meta Storage revision update.

Review Comment:
   Are there any guarantees that no listener will be notified about revision N+1 before all listeners have finished processing a notification about revision N? I think it makes sense to document it somewhere, maybe here.



##########
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/server/WatchProcessor.java:
##########
@@ -128,75 +131,94 @@ public void notifyWatches(List<Entry> updatedEntries, HybridTimestamp time) {
                     // Revision must be the same for all entries.
                     long newRevision = updatedEntries.get(0).revision();
 
-                    // Notify all watches in parallel, then aggregate the entries that they have processed.
-                    CompletableFuture<List<EntryEvent>>[] notificationFutures = watches.stream()
-                            .map(watch -> notifyWatch(watch, updatedEntries, newRevision, time))
-                            .toArray(CompletableFuture[]::new);
+                    // Collect all the events for each watch.
+                    CompletableFuture<List<WatchAndEvents>> watchAndEventsFuture = collectWatchAndEvents(updatedEntries, newRevision);
 
-                    return allOf(notificationFutures)
-                            .thenComposeAsync(ignored -> invokeOnRevisionCallback(notificationFutures, newRevision, time), watchExecutor);
+                    return watchAndEventsFuture
+                            .thenComposeAsync(watchAndEvents -> allOf(
+                                    notifyWatches(watchAndEvents, newRevision, time),
+                                    notifyUpdateRevisionListeners(newRevision)

Review Comment:
   So both types of notifications are run in parallel. Is this ok? I would expect the revision update listeners to be notified after all watches have been notified.



##########
modules/runner/src/main/java/org/apache/ignite/internal/app/IgniteImpl.java:
##########
@@ -754,7 +754,30 @@ public CompletableFuture<Ignite> start(Path configPath) {
                                 fut -> new ConfigurationCatchUpListener(cfgStorage, fut, LOG)
                         );
 
-                        return notifyConfigurationListeners()
+                        // Temporary workaround.
+                        // In order to avoid making a public getter for configuration revision, I read it from the startup notification.
+                        // It should be removed once we start using up-to-date meta-storage revision for node startup.

Review Comment:
   Let's add a TODO



##########
modules/configuration/src/testFixtures/java/org/apache/ignite/internal/configuration/testframework/InjectRevisionListenerHolder.java:
##########
@@ -30,5 +30,6 @@
  */
 @Retention(RetentionPolicy.RUNTIME)
 @Target({ElementType.FIELD, ElementType.PARAMETER})
+// TODO: IGNITE-19853 Get rid of

Review Comment:
   ```suggestion
   // TODO: IGNITE-19853 Get rid of this annotation
   ```



##########
modules/metastorage-api/src/main/java/org/apache/ignite/internal/metastorage/RevisionUpdateListener.java:
##########
@@ -0,0 +1,34 @@
+/*
+ * 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;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * The listener which receives and handles the Meta Storage revision update.
+ */
+@FunctionalInterface
+public interface RevisionUpdateListener {
+    /**
+     * Callback that will be invoked if a Meta Storage revision update has been received.

Review Comment:
   ```suggestion
        * Callback that will be invoked when a Meta Storage revision update has been received.
   ```



##########
modules/metastorage/src/integrationTest/java/org/apache/ignite/internal/metastorage/impl/ItMetaStorageManagerImplTest.java:
##########
@@ -284,4 +286,24 @@ void testMetaStorageStopBeforeRaftServiceStarted() throws Exception {
 
         assertThat(metaStorageManager.metaStorageServiceFuture(), willThrowFast(CancellationException.class));
     }
+
+    @Test
+    void testUpdateRevisionListener() {
+        // I'm using the future because mock+verify doesn't work.

Review Comment:
   I'm curious, what does not work here?



##########
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/server/WatchProcessor.java:
##########
@@ -128,75 +131,94 @@ public void notifyWatches(List<Entry> updatedEntries, HybridTimestamp time) {
                     // Revision must be the same for all entries.
                     long newRevision = updatedEntries.get(0).revision();
 
-                    // Notify all watches in parallel, then aggregate the entries that they have processed.
-                    CompletableFuture<List<EntryEvent>>[] notificationFutures = watches.stream()
-                            .map(watch -> notifyWatch(watch, updatedEntries, newRevision, time))
-                            .toArray(CompletableFuture[]::new);
+                    // Collect all the events for each watch.
+                    CompletableFuture<List<WatchAndEvents>> watchAndEventsFuture = collectWatchAndEvents(updatedEntries, newRevision);

Review Comment:
   ```suggestion
                       CompletableFuture<List<WatchAndEvents>> watchesAndEventsFuture = collectWatchAndEvents(updatedEntries, newRevision);
   ```



##########
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/server/WatchProcessor.java:
##########
@@ -128,75 +131,94 @@ public void notifyWatches(List<Entry> updatedEntries, HybridTimestamp time) {
                     // Revision must be the same for all entries.
                     long newRevision = updatedEntries.get(0).revision();
 
-                    // Notify all watches in parallel, then aggregate the entries that they have processed.
-                    CompletableFuture<List<EntryEvent>>[] notificationFutures = watches.stream()
-                            .map(watch -> notifyWatch(watch, updatedEntries, newRevision, time))
-                            .toArray(CompletableFuture[]::new);
+                    // Collect all the events for each watch.
+                    CompletableFuture<List<WatchAndEvents>> watchAndEventsFuture = collectWatchAndEvents(updatedEntries, newRevision);
 
-                    return allOf(notificationFutures)
-                            .thenComposeAsync(ignored -> invokeOnRevisionCallback(notificationFutures, newRevision, time), watchExecutor);
+                    return watchAndEventsFuture
+                            .thenComposeAsync(watchAndEvents -> allOf(
+                                    notifyWatches(watchAndEvents, newRevision, time),
+                                    notifyUpdateRevisionListeners(newRevision)
+                            ), watchExecutor)
+                            .thenComposeAsync(ignored -> invokeOnRevisionCallback(watchAndEventsFuture, newRevision, time), watchExecutor);
                 }, watchExecutor);
     }
 
-    private CompletableFuture<List<EntryEvent>> notifyWatch(Watch watch, List<Entry> updatedEntries, long revision, HybridTimestamp time) {
-        CompletableFuture<List<EntryEvent>> eventFuture = supplyAsync(() -> {
-            List<EntryEvent> entryEvents = List.of();
 
-            for (Entry newEntry : updatedEntries) {
-                byte[] newKey = newEntry.key();
+    private static CompletableFuture<Void> notifyWatches(List<WatchAndEvents> watchAndEventsList, long revision, HybridTimestamp time) {
+        if (watchAndEventsList.isEmpty()) {
+            return completedFuture(null);
+        }
 
-                assert newEntry.revision() == revision;
+        CompletableFuture<?>[] notifyWatchFutures = new CompletableFuture[watchAndEventsList.size()];
 
-                if (watch.matches(newKey, revision)) {
-                    Entry oldEntry = entryReader.get(newKey, revision - 1);
+        int i = 0;
 
-                    if (entryEvents.isEmpty()) {
-                        entryEvents = new ArrayList<>();
-                    }
+        for (WatchAndEvents watchAndEvents : watchAndEventsList) {
+            CompletableFuture<Void> notifyWatchFuture;
 
-                    entryEvents.add(new EntryEvent(oldEntry, newEntry));
-                }
+            try {
+                notifyWatchFuture = watchAndEvents.events.isEmpty()
+                        ? watchAndEvents.watch.onRevisionUpdated(revision)
+                        : watchAndEvents.watch.onUpdate(new WatchEvent(watchAndEvents.events, revision, time));
+            } catch (Throwable throwable) {
+                watchAndEvents.watch.onError(throwable);
+
+                notifyWatchFuture = failedFuture(throwable);
             }
 
-            return entryEvents;
-        }, watchExecutor);
+            notifyWatchFutures[i++] = notifyWatchFuture;
+        }
 
-        return eventFuture
-                .thenCompose(entryEvents -> {
-                    CompletableFuture<Void> eventNotificationFuture = entryEvents.isEmpty()
-                            ? watch.onRevisionUpdated(revision)
-                            : watch.onUpdate(new WatchEvent(entryEvents, revision, time));
-
-                    return eventNotificationFuture.thenApply(v -> entryEvents);
-                })
-                .whenComplete((v, e) -> {
-                    if (e != null) {
-                        if (e instanceof CompletionException) {
-                            e = e.getCause();
-                        }
+        return allOf(notifyWatchFutures);
+    }
 
-                        // TODO: IGNITE-14693 Implement Meta storage exception handling logic.
-                        LOG.error("Error occurred when processing a watch event", e);
+    private CompletableFuture<List<WatchAndEvents>> collectWatchAndEvents(List<Entry> updatedEntries, long revision) {

Review Comment:
   ```suggestion
       private CompletableFuture<List<WatchAndEvents>> collectWatchesAndEvents(List<Entry> updatedEntries, long revision) {
   ```



##########
modules/runner/src/main/java/org/apache/ignite/internal/app/IgniteImpl.java:
##########
@@ -754,7 +754,30 @@ public CompletableFuture<Ignite> start(Path configPath) {
                                 fut -> new ConfigurationCatchUpListener(cfgStorage, fut, LOG)
                         );
 
-                        return notifyConfigurationListeners()
+                        // Temporary workaround.

Review Comment:
   How about trying to extract this (maybe the whole 'recovery' block) to a method? It's ugly to have such long methods as this `start()`, it's really difficult to navigate. The workaround could (if possible) be extracted too, to improve readability.



##########
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/server/WatchProcessor.java:
##########
@@ -128,75 +131,94 @@ public void notifyWatches(List<Entry> updatedEntries, HybridTimestamp time) {
                     // Revision must be the same for all entries.
                     long newRevision = updatedEntries.get(0).revision();
 
-                    // Notify all watches in parallel, then aggregate the entries that they have processed.
-                    CompletableFuture<List<EntryEvent>>[] notificationFutures = watches.stream()
-                            .map(watch -> notifyWatch(watch, updatedEntries, newRevision, time))
-                            .toArray(CompletableFuture[]::new);
+                    // Collect all the events for each watch.
+                    CompletableFuture<List<WatchAndEvents>> watchAndEventsFuture = collectWatchAndEvents(updatedEntries, newRevision);
 
-                    return allOf(notificationFutures)
-                            .thenComposeAsync(ignored -> invokeOnRevisionCallback(notificationFutures, newRevision, time), watchExecutor);
+                    return watchAndEventsFuture
+                            .thenComposeAsync(watchAndEvents -> allOf(
+                                    notifyWatches(watchAndEvents, newRevision, time),
+                                    notifyUpdateRevisionListeners(newRevision)
+                            ), watchExecutor)
+                            .thenComposeAsync(ignored -> invokeOnRevisionCallback(watchAndEventsFuture, newRevision, time), watchExecutor);
                 }, watchExecutor);
     }
 
-    private CompletableFuture<List<EntryEvent>> notifyWatch(Watch watch, List<Entry> updatedEntries, long revision, HybridTimestamp time) {
-        CompletableFuture<List<EntryEvent>> eventFuture = supplyAsync(() -> {
-            List<EntryEvent> entryEvents = List.of();
 
-            for (Entry newEntry : updatedEntries) {
-                byte[] newKey = newEntry.key();
+    private static CompletableFuture<Void> notifyWatches(List<WatchAndEvents> watchAndEventsList, long revision, HybridTimestamp time) {
+        if (watchAndEventsList.isEmpty()) {
+            return completedFuture(null);
+        }
 
-                assert newEntry.revision() == revision;
+        CompletableFuture<?>[] notifyWatchFutures = new CompletableFuture[watchAndEventsList.size()];
 
-                if (watch.matches(newKey, revision)) {
-                    Entry oldEntry = entryReader.get(newKey, revision - 1);
+        int i = 0;
 
-                    if (entryEvents.isEmpty()) {
-                        entryEvents = new ArrayList<>();
-                    }
+        for (WatchAndEvents watchAndEvents : watchAndEventsList) {
+            CompletableFuture<Void> notifyWatchFuture;
 
-                    entryEvents.add(new EntryEvent(oldEntry, newEntry));
-                }
+            try {
+                notifyWatchFuture = watchAndEvents.events.isEmpty()
+                        ? watchAndEvents.watch.onRevisionUpdated(revision)
+                        : watchAndEvents.watch.onUpdate(new WatchEvent(watchAndEvents.events, revision, time));

Review Comment:
   Is it ok that we invoke the listener if there are events, and 'revision updated' callback when there are no events? If this the contract?



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