SammyVimes commented on code in PR #1807:
URL: https://github.com/apache/ignite-3/pull/1807#discussion_r1143534856


##########
modules/core/src/main/java/org/apache/ignite/internal/causality/CompletionListener.java:
##########
@@ -0,0 +1,37 @@
+/*
+ * 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.causality;
+
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Listener that will be notified of every completion of a Versioned Value.
+ *
+ * @see AbstractVersionedValue#whenComplete(CompletionListener)
+ */
+@FunctionalInterface
+public interface CompletionListener<T> {
+    /**
+     * Method that will be called of every completion of a Versioned Value.

Review Comment:
   ```suggestion
        * Method that will be called on every completion of a Versioned Value.
   ```



##########
modules/table/src/main/java/org/apache/ignite/internal/table/distributed/TableManager.java:
##########
@@ -722,7 +662,7 @@ private void 
updateAssignmentInternal(ConfigurationNotificationEvent<byte[]> ass
         String localMemberName = 
clusterService.topologyService().localMember().name();
 
         // Create new raft nodes according to new assignments.
-        tablesByIdVv.update(causalityToken, (tablesById, e) -> {
+        return tablesByIdVv.update(causalityToken, (tablesById, e) -> {

Review Comment:
   Excuse me king, you dropped this 👑



##########
modules/core/src/main/java/org/apache/ignite/internal/causality/CompletableVersionedValue.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.causality;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Supplier;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Versioned Value flavor that allows to be completed explicitly either via a 
value, or an exception.
+ */
+public class CompletableVersionedValue<T> implements VersionedValue<T> {

Review Comment:
   This is just a shorthand for the BaseVersionedValue's APIs, right?



##########
modules/core/src/main/java/org/apache/ignite/internal/causality/IncrementalVersionedValue.java:
##########
@@ -0,0 +1,242 @@
+/*
+ * 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.causality;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static java.util.concurrent.CompletableFuture.failedFuture;
+
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
+import java.util.function.BiFunction;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Versioned Value flavor that accumulates updates posted by the {@link 
#update} method and "commits" them when {@link #complete} or
+ * {@link #completeExceptionally} are called or a storage revision is updated 
(see constructors for details).
+ */
+public class IncrementalVersionedValue<T> implements VersionedValue<T> {
+    private final BaseVersionedValue<T> versionedValue;
+
+    /** Update mutex. */
+    private final Object updateMutex = new Object();
+
+    /**
+     * Token that was used with the most recent {@link #update} call.
+     *
+     * <p>Multi-threaded access is guarded by {@link #updateMutex}.
+     */
+    private long expectedToken = -1;
+
+    /**
+     * Token that was used with the most recent {@link #completeInternal} call.
+     *
+     * <p>Multi-threaded access is guarded by {@link #updateMutex}.
+     */
+    private long lastCompleteToken = -1;
+
+    /**
+     * Future that will be completed after all updates over the value in 
context of current causality token will be performed.
+     *
+     * <p>Multi-threaded access is guarded by {@link #updateMutex}.
+     */
+    private CompletableFuture<T> updaterFuture;
+
+    /**
+     * Constructor.
+     *
+     * @param observableRevisionUpdater A closure intended to connect this 
VersionedValue with a revision updater, that this
+     *         VersionedValue should be able to listen to, for receiving 
storage revision updates. This closure is called once on a
+     *         construction of this VersionedValue and accepts a {@code 
Function<Long, CompletableFuture<?>>} that should be called on every
+     *         update of storage revision as a listener.
+     * @param maxHistorySize Size of the history of changes to store, 
including last applied token.
+     * @param defaultValueSupplier Supplier of the default value, that is used 
on {@link #update(long, BiFunction)} to evaluate the
+     *         default value if the value is not initialized yet. It is not 
guaranteed to execute only once.
+     */
+    public IncrementalVersionedValue(
+            @Nullable Consumer<Function<Long, CompletableFuture<?>>> 
observableRevisionUpdater,
+            int maxHistorySize,
+            @Nullable Supplier<T> defaultValueSupplier
+    ) {
+        this.versionedValue = new BaseVersionedValue<>(maxHistorySize, 
defaultValueSupplier);
+
+        this.updaterFuture = completedFuture(versionedValue.getDefault());
+
+        if (observableRevisionUpdater != null) {
+            observableRevisionUpdater.accept(this::completeInternal);
+        }
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param observableRevisionUpdater A closure intended to connect this 
VersionedValue with a revision updater, that this
+     *         VersionedValue should be able to listen to, for receiving 
storage revision updates. This closure is called once on a
+     *         construction of this VersionedValue and accepts a {@code 
Function<Long, CompletableFuture<?>>} that should be called on every
+     *         update of storage revision as a listener.
+     * @param defaultValueSupplier Supplier of the default value, that is used 
on {@link #update(long, BiFunction)} to evaluate the
+     *         default value if the value is not initialized yet. It is not 
guaranteed to execute only once.
+     */
+    public IncrementalVersionedValue(
+            @Nullable Consumer<Function<Long, CompletableFuture<?>>> 
observableRevisionUpdater,
+            @Nullable Supplier<T> defaultValueSupplier
+    ) {
+        this.versionedValue = new BaseVersionedValue<>(defaultValueSupplier);
+
+        this.updaterFuture = completedFuture(versionedValue.getDefault());
+
+        if (observableRevisionUpdater != null) {
+            observableRevisionUpdater.accept(this::completeInternal);
+        }
+    }
+
+    /**
+     * Constructor with default history size.
+     *
+     * @param observableRevisionUpdater A closure intended to connect this 
VersionedValue with a revision updater, that this
+     *         VersionedValue should be able to listen to, for receiving 
storage revision updates. This closure is called once on a
+     *         construction of this VersionedValue and accepts a {@code 
Function<Long, CompletableFuture<?>>} that should be called on every
+     *         update of storage revision as a listener.
+     */
+    public IncrementalVersionedValue(Consumer<Function<Long, 
CompletableFuture<?>>> observableRevisionUpdater) {
+        this(observableRevisionUpdater, null);
+    }
+
+    @Override
+    public CompletableFuture<T> get(long causalityToken) {
+        return versionedValue.get(causalityToken);
+    }
+
+    @Override
+    public @Nullable T latest() {
+        return versionedValue.latest();
+    }
+
+    @Override
+    public void whenComplete(CompletionListener<T> action) {
+        versionedValue.whenComplete(action);
+    }
+
+    @Override
+    public void removeWhenComplete(CompletionListener<T> action) {
+        versionedValue.removeWhenComplete(action);
+    }
+
+    /**
+     * Updates the value using the given updater. The updater receives the 
value on previous token, or default value (see constructor) if

Review Comment:
   ```suggestion
        * Updates the value using the given updater. The updater receives the 
value associated with the previous token, or default value (see constructor) if
   ```



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