tkalkirill commented on code in PR #1626:
URL: https://github.com/apache/ignite-3/pull/1626#discussion_r1097334101


##########
modules/core/src/testFixtures/java/org/apache/ignite/internal/testframework/flow/FlowUtils.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * 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.testframework.flow;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.concurrent.Flow.Subscription;
+import java.util.concurrent.ForkJoinPool;
+import org.apache.ignite.internal.util.Cursor;
+
+/**
+ * Utility methods for extracting data from {@link Publisher}s.
+ */
+public class FlowUtils {
+    /**
+     * Subscribes to the given publisher, collecting all values into a list.
+     */
+    public static <T> CompletableFuture<List<T>> subscribeToList(Publisher<T> 
publisher) {
+        var resultFuture = new CompletableFuture<List<T>>();
+
+        publisher.subscribe(new Subscriber<>() {
+            private final List<T> items = new ArrayList<>();

Review Comment:
   Isn't a parallel collection needed here?



##########
modules/core/src/testFixtures/java/org/apache/ignite/internal/testframework/flow/FlowUtils.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * 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.testframework.flow;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.concurrent.Flow.Subscription;
+import java.util.concurrent.ForkJoinPool;
+import org.apache.ignite.internal.util.Cursor;
+
+/**
+ * Utility methods for extracting data from {@link Publisher}s.
+ */
+public class FlowUtils {
+    /**
+     * Subscribes to the given publisher, collecting all values into a list.
+     */
+    public static <T> CompletableFuture<List<T>> subscribeToList(Publisher<T> 
publisher) {
+        var resultFuture = new CompletableFuture<List<T>>();
+
+        publisher.subscribe(new Subscriber<>() {
+            private final List<T> items = new ArrayList<>();
+
+            @Override
+            public void onSubscribe(Subscription subscription) {
+                subscription.request(Long.MAX_VALUE);
+            }
+
+            @Override
+            public void onNext(T item) {
+                items.add(item);
+            }
+
+            @Override
+            public void onError(Throwable throwable) {
+                resultFuture.completeExceptionally(throwable);
+            }
+
+            @Override
+            public void onComplete() {
+                resultFuture.complete(items);
+            }
+        });
+
+        return resultFuture;
+    }
+
+    /**
+     * Subscribes to the given publisher, extracting a single value from it.
+     */
+    public static <T> CompletableFuture<T> subscribeToValue(Publisher<T> 
publisher) {
+        var resultFuture = new CompletableFuture<T>();
+
+        publisher.subscribe(new Subscriber<>() {
+            private Subscription subscription;
+
+            @Override
+            public void onSubscribe(Subscription subscription) {
+                this.subscription = subscription;
+
+                subscription.request(1);
+            }
+
+            @Override
+            public void onNext(T item) {
+                resultFuture.complete(item);
+
+                subscription.cancel();
+            }
+
+            @Override
+            public void onError(Throwable throwable) {
+                resultFuture.completeExceptionally(throwable);
+            }
+
+            @Override
+            public void onComplete() {
+                resultFuture.completeExceptionally(new 
NoSuchElementException());
+            }
+        });
+
+        return resultFuture;
+    }
+
+    /**
+     * Creates a Publisher that provides data form a given cursor.
+     */
+    public static <T> Publisher<T> fromCursor(Cursor<T> cursor) {
+        return subscriber -> subscriber.onSubscribe(new Subscription() {
+            private long demand;
+
+            private boolean isStarted;
+
+            private boolean isCancelled;
+
+            @Override
+            public void request(long n) {
+                demand += n;
+
+                if (!isStarted) {
+                    isStarted = true;
+
+                    start();
+                }
+            }
+
+            private void start() {
+                ForkJoinPool.commonPool().execute(() -> {

Review Comment:
   I think you need to use a separate pool.



##########
modules/core/src/testFixtures/java/org/apache/ignite/internal/testframework/flow/FlowUtils.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * 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.testframework.flow;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.concurrent.Flow.Subscription;
+import java.util.concurrent.ForkJoinPool;
+import org.apache.ignite.internal.util.Cursor;
+
+/**
+ * Utility methods for extracting data from {@link Publisher}s.
+ */
+public class FlowUtils {
+    /**
+     * Subscribes to the given publisher, collecting all values into a list.
+     */
+    public static <T> CompletableFuture<List<T>> subscribeToList(Publisher<T> 
publisher) {
+        var resultFuture = new CompletableFuture<List<T>>();
+
+        publisher.subscribe(new Subscriber<>() {
+            private final List<T> items = new ArrayList<>();
+
+            @Override
+            public void onSubscribe(Subscription subscription) {
+                subscription.request(Long.MAX_VALUE);
+            }
+
+            @Override
+            public void onNext(T item) {
+                items.add(item);
+            }
+
+            @Override
+            public void onError(Throwable throwable) {
+                resultFuture.completeExceptionally(throwable);
+            }
+
+            @Override
+            public void onComplete() {
+                resultFuture.complete(items);
+            }
+        });
+
+        return resultFuture;
+    }
+
+    /**
+     * Subscribes to the given publisher, extracting a single value from it.
+     */
+    public static <T> CompletableFuture<T> subscribeToValue(Publisher<T> 
publisher) {
+        var resultFuture = new CompletableFuture<T>();
+
+        publisher.subscribe(new Subscriber<>() {
+            private Subscription subscription;
+
+            @Override
+            public void onSubscribe(Subscription subscription) {
+                this.subscription = subscription;
+
+                subscription.request(1);
+            }
+
+            @Override
+            public void onNext(T item) {
+                resultFuture.complete(item);
+
+                subscription.cancel();
+            }
+
+            @Override
+            public void onError(Throwable throwable) {
+                resultFuture.completeExceptionally(throwable);
+            }
+
+            @Override
+            public void onComplete() {
+                resultFuture.completeExceptionally(new 
NoSuchElementException());
+            }
+        });
+
+        return resultFuture;
+    }
+
+    /**
+     * Creates a Publisher that provides data form a given cursor.
+     */
+    public static <T> Publisher<T> fromCursor(Cursor<T> cursor) {
+        return subscriber -> subscriber.onSubscribe(new Subscription() {
+            private long demand;
+
+            private boolean isStarted;
+
+            private boolean isCancelled;

Review Comment:
   Shouldn't these variables be thread-safe?



##########
modules/core/src/testFixtures/java/org/apache/ignite/internal/testframework/flow/FlowUtils.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * 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.testframework.flow;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.concurrent.Flow.Subscription;
+import java.util.concurrent.ForkJoinPool;
+import org.apache.ignite.internal.util.Cursor;
+
+/**
+ * Utility methods for extracting data from {@link Publisher}s.
+ */
+public class FlowUtils {

Review Comment:
   Let's rename to `TestFlowUtils` to make it clearer that this is from the 
test framework.



##########
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/impl/CursorSubscription.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.impl;
+
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.concurrent.Flow.Subscription;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.metastorage.Entry;
+import 
org.apache.ignite.internal.metastorage.command.MetaStorageCommandsFactory;
+import 
org.apache.ignite.internal.metastorage.command.cursor.CloseCursorCommand;
+import org.apache.ignite.internal.metastorage.command.cursor.NextBatchCommand;
+import org.apache.ignite.internal.metastorage.command.response.BatchResponse;
+import org.apache.ignite.internal.raft.service.RaftGroupService;
+import org.apache.ignite.lang.IgniteUuid;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Subscription used to notify a subscriber on Meta Storage cursor data.
+ *
+ * @implNote Subscription is always processed sequentially in one of the 
provided executor's thread, so no explicit synchronization is used.
+ *     It is expected that subscription methods (i.e. {@code request} and 
{@code cancel}) are called from the same thread that
+ *     was used to invoke {@code onNext}.
+ */
+class CursorSubscription implements Subscription {
+    private static final IgniteLogger LOG = 
Loggers.forClass(MetaStorageService.class);
+
+    /** Default batch size that is requested from the remote server. */
+    private static final int BATCH_SIZE = 1000;
+
+    private final RaftGroupService raftService;
+
+    private final MetaStorageCommandsFactory commandsFactory;
+
+    private final ExecutorService executorService;
+
+    private final IgniteUuid cursorId;
+
+    private final Subscriber<? super Entry> subscriber;
+
+    /** Flag indicating that either the whole data range has been exhausted or 
the subscription has been cancelled. */
+    private boolean isDone = false;
+
+    /** Cached response from the Meta Storage leader. {@code null} if no 
requests have been issued yet. */
+    @Nullable
+    private BatchResponse cachedResponse;
+
+    /** Index inside the cached response. */
+    private int responseIndex;
+
+    /** Amount of entries requested by the subscriber. */
+    private long demand;
+

Review Comment:
   Shouldn't variables be thread-safe?



##########
modules/metastorage-api/src/main/java/org/apache/ignite/internal/metastorage/MetaStorageManager.java:
##########
@@ -63,32 +63,40 @@ public interface MetaStorageManager extends IgniteComponent 
{
      * {@code revUpperBound == -1}.
      *
      * @param keyPrefix Prefix of the key to retrieve the entries. Couldn't be 
{@code null}.
-     * @return Cursor built upon entries corresponding to the given range and 
revision.
+     * @return Publisher that will provide entries corresponding to the given 
prefix.
      * @throws OperationTimeoutException If the operation is timed out.
      * @throws CompactedException If the desired revisions are removed from 
the storage due to a compaction.
      * @see ByteArray
      * @see Entry
      */
-    Cursor<Entry> prefix(ByteArray keyPrefix) throws NodeStoppingException;
+    Publisher<Entry> prefix(ByteArray keyPrefix);
 
     /**
      * Retrieves entries for the given key prefix in lexicographic order. 
Entries will be filtered out by upper bound of given revision
      * number.
      *
      * @param keyPrefix Prefix of the key to retrieve the entries. Couldn't be 
{@code null}.
      * @param revUpperBound The upper bound for entry revision. {@code -1} 
means latest revision.
-     * @return Cursor built upon entries corresponding to the given range and 
revision.
+     * @return Publisher that will provide entries corresponding to the given 
prefix and revision.
      * @throws OperationTimeoutException If the operation is timed out.
      * @throws CompactedException If the desired revisions are removed from 
the storage due to a compaction.
      * @see ByteArray
      * @see Entry
      */
-    Cursor<Entry> prefix(ByteArray keyPrefix, long revUpperBound) throws 
NodeStoppingException;
+    Publisher<Entry> prefix(ByteArray keyPrefix, long revUpperBound);
 
     /**
      * Retrieves entries for the given key range in lexicographic order.
+     *
+     * @param keyFrom Range lower bound (inclusive).
+     * @param keyTo Range upper bound (exclusive), {@code null} represents an 
unbound range.
+     * @return Publisher that will provide entries corresponding to the given 
range.
+     * @throws OperationTimeoutException If the operation is timed out.
+     * @throws CompactedException If the desired revisions are removed from 
the storage due to a compaction.
+     * @see ByteArray
+     * @see Entry
      */
-    Cursor<Entry> range(ByteArray keyFrom, @Nullable ByteArray keyTo) throws 
NodeStoppingException;
+    Publisher<Entry> range(ByteArray keyFrom, @Nullable ByteArray keyTo);

Review Comment:
   Same



##########
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/impl/CursorSubscription.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.impl;
+
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.concurrent.Flow.Subscription;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.metastorage.Entry;
+import 
org.apache.ignite.internal.metastorage.command.MetaStorageCommandsFactory;
+import 
org.apache.ignite.internal.metastorage.command.cursor.CloseCursorCommand;
+import org.apache.ignite.internal.metastorage.command.cursor.NextBatchCommand;
+import org.apache.ignite.internal.metastorage.command.response.BatchResponse;
+import org.apache.ignite.internal.raft.service.RaftGroupService;
+import org.apache.ignite.lang.IgniteUuid;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Subscription used to notify a subscriber on Meta Storage cursor data.
+ *
+ * @implNote Subscription is always processed sequentially in one of the 
provided executor's thread, so no explicit synchronization is used.
+ *     It is expected that subscription methods (i.e. {@code request} and 
{@code cancel}) are called from the same thread that
+ *     was used to invoke {@code onNext}.
+ */
+class CursorSubscription implements Subscription {
+    private static final IgniteLogger LOG = 
Loggers.forClass(MetaStorageService.class);
+
+    /** Default batch size that is requested from the remote server. */
+    private static final int BATCH_SIZE = 1000;
+
+    private final RaftGroupService raftService;
+
+    private final MetaStorageCommandsFactory commandsFactory;
+
+    private final ExecutorService executorService;
+
+    private final IgniteUuid cursorId;
+
+    private final Subscriber<? super Entry> subscriber;
+
+    /** Flag indicating that either the whole data range has been exhausted or 
the subscription has been cancelled. */
+    private boolean isDone = false;

Review Comment:
   same



##########
modules/metastorage/src/test/java/org/apache/ignite/internal/metastorage/impl/CursorPublisherTest.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.impl;
+
+import static java.util.Collections.nCopies;
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static java.util.concurrent.CompletableFuture.failedFuture;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.will;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willFailFast;
+import static 
org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willFailIn;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasSize;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.concurrent.Flow.Subscription;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.TimeUnit;
+import org.apache.ignite.internal.metastorage.Entry;
+import 
org.apache.ignite.internal.metastorage.command.MetaStorageCommandsFactory;
+import 
org.apache.ignite.internal.metastorage.command.cursor.CloseCursorCommand;
+import 
org.apache.ignite.internal.metastorage.command.cursor.CreateRangeCursorCommand;
+import org.apache.ignite.internal.metastorage.command.cursor.NextBatchCommand;
+import org.apache.ignite.internal.metastorage.command.response.BatchResponse;
+import org.apache.ignite.internal.raft.service.RaftGroupService;
+import org.apache.ignite.internal.testframework.flow.FlowUtils;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+/**
+ * Tests for {@link CursorPublisher} and {@link CursorSubscription}.
+ */
+@ExtendWith(MockitoExtension.class)
+public class CursorPublisherTest {
+    @Mock
+    private RaftGroupService raftService;
+
+    private CursorPublisher publisher;
+
+    @BeforeEach
+    void setUp() {
+        publisher = new CursorPublisher(
+                raftService,
+                new MetaStorageCommandsFactory(),
+                ForkJoinPool.commonPool(),

Review Comment:
   
https://avatars.mds.yandex.net/i?id=1cbe54508db85747fa40c700b3a1d6fab3115558-7086141-images-thumbs&n=13



##########
modules/schema/src/main/java/org/apache/ignite/internal/schema/SchemaManager.java:
##########
@@ -433,21 +434,46 @@ public void stop() throws Exception {
      * @return The latest schema version.
      */
     private int latestSchemaVersion(UUID tblId) {
-        try (Cursor<Entry> cur = 
metastorageMgr.prefix(schemaHistPrefix(tblId))) {
-            int lastVer = INITIAL_SCHEMA_VERSION;
+        var latestVersionFuture = new CompletableFuture<Integer>();
 
-            for (Entry ent : cur) {
-                String key = new String(ent.key(), StandardCharsets.UTF_8);
+        metastorageMgr.prefix(schemaHistPrefix(tblId)).subscribe(new 
Subscriber<>() {
+            private int lastVer = INITIAL_SCHEMA_VERSION;
+
+            @Override
+            public void onSubscribe(Subscription subscription) {
+                // Request unlimited demand.
+                subscription.request(Long.MAX_VALUE);
+            }
+
+            @Override
+            public void onNext(Entry item) {
+                String key = new String(item.key(), StandardCharsets.UTF_8);
                 int descVer = extractVerFromSchemaKey(key);
 
                 if (descVer > lastVer) {
                     lastVer = descVer;
                 }
             }
 
-            return lastVer;
-        } catch (NodeStoppingException e) {
-            throw new IgniteException(e.traceId(), e.code(), e.getMessage(), 
e);
+            @Override
+            public void onError(Throwable throwable) {
+                latestVersionFuture.completeExceptionally(throwable);
+            }
+
+            @Override
+            public void onComplete() {
+                latestVersionFuture.complete(lastVer);
+            }
+        });
+
+        try {
+            return latestVersionFuture.get(10, TimeUnit.SECONDS);

Review Comment:
   This is bad, you need to get rid of the synchronous code.



##########
modules/metastorage-api/src/main/java/org/apache/ignite/internal/metastorage/MetaStorageManager.java:
##########
@@ -63,32 +63,40 @@ public interface MetaStorageManager extends IgniteComponent 
{
      * {@code revUpperBound == -1}.
      *
      * @param keyPrefix Prefix of the key to retrieve the entries. Couldn't be 
{@code null}.
-     * @return Cursor built upon entries corresponding to the given range and 
revision.
+     * @return Publisher that will provide entries corresponding to the given 
prefix.
      * @throws OperationTimeoutException If the operation is timed out.
      * @throws CompactedException If the desired revisions are removed from 
the storage due to a compaction.
      * @see ByteArray
      * @see Entry
      */
-    Cursor<Entry> prefix(ByteArray keyPrefix) throws NodeStoppingException;
+    Publisher<Entry> prefix(ByteArray keyPrefix);

Review Comment:
   Why not throw an **NodeStoppingException** if the node has stopped?



##########
modules/metastorage-api/src/main/java/org/apache/ignite/internal/metastorage/MetaStorageManager.java:
##########
@@ -63,32 +63,40 @@ public interface MetaStorageManager extends IgniteComponent 
{
      * {@code revUpperBound == -1}.
      *
      * @param keyPrefix Prefix of the key to retrieve the entries. Couldn't be 
{@code null}.
-     * @return Cursor built upon entries corresponding to the given range and 
revision.
+     * @return Publisher that will provide entries corresponding to the given 
prefix.
      * @throws OperationTimeoutException If the operation is timed out.
      * @throws CompactedException If the desired revisions are removed from 
the storage due to a compaction.
      * @see ByteArray
      * @see Entry
      */
-    Cursor<Entry> prefix(ByteArray keyPrefix) throws NodeStoppingException;
+    Publisher<Entry> prefix(ByteArray keyPrefix);
 
     /**
      * Retrieves entries for the given key prefix in lexicographic order. 
Entries will be filtered out by upper bound of given revision
      * number.
      *
      * @param keyPrefix Prefix of the key to retrieve the entries. Couldn't be 
{@code null}.
      * @param revUpperBound The upper bound for entry revision. {@code -1} 
means latest revision.
-     * @return Cursor built upon entries corresponding to the given range and 
revision.
+     * @return Publisher that will provide entries corresponding to the given 
prefix and revision.
      * @throws OperationTimeoutException If the operation is timed out.
      * @throws CompactedException If the desired revisions are removed from 
the storage due to a compaction.
      * @see ByteArray
      * @see Entry
      */
-    Cursor<Entry> prefix(ByteArray keyPrefix, long revUpperBound) throws 
NodeStoppingException;
+    Publisher<Entry> prefix(ByteArray keyPrefix, long revUpperBound);

Review Comment:
   Same



##########
modules/core/src/testFixtures/java/org/apache/ignite/internal/testframework/flow/FlowUtils.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * 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.testframework.flow;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.concurrent.Flow.Subscription;
+import java.util.concurrent.ForkJoinPool;
+import org.apache.ignite.internal.util.Cursor;
+
+/**
+ * Utility methods for extracting data from {@link Publisher}s.
+ */
+public class FlowUtils {
+    /**
+     * Subscribes to the given publisher, collecting all values into a list.
+     */
+    public static <T> CompletableFuture<List<T>> subscribeToList(Publisher<T> 
publisher) {
+        var resultFuture = new CompletableFuture<List<T>>();
+
+        publisher.subscribe(new Subscriber<>() {
+            private final List<T> items = new ArrayList<>();
+
+            @Override
+            public void onSubscribe(Subscription subscription) {
+                subscription.request(Long.MAX_VALUE);
+            }
+
+            @Override
+            public void onNext(T item) {
+                items.add(item);
+            }
+
+            @Override
+            public void onError(Throwable throwable) {
+                resultFuture.completeExceptionally(throwable);
+            }
+
+            @Override
+            public void onComplete() {
+                resultFuture.complete(items);
+            }
+        });
+
+        return resultFuture;
+    }
+
+    /**
+     * Subscribes to the given publisher, extracting a single value from it.
+     */
+    public static <T> CompletableFuture<T> subscribeToValue(Publisher<T> 
publisher) {
+        var resultFuture = new CompletableFuture<T>();
+
+        publisher.subscribe(new Subscriber<>() {
+            private Subscription subscription;
+
+            @Override
+            public void onSubscribe(Subscription subscription) {
+                this.subscription = subscription;
+
+                subscription.request(1);
+            }
+
+            @Override
+            public void onNext(T item) {
+                resultFuture.complete(item);
+
+                subscription.cancel();
+            }
+
+            @Override
+            public void onError(Throwable throwable) {
+                resultFuture.completeExceptionally(throwable);
+            }
+
+            @Override
+            public void onComplete() {
+                resultFuture.completeExceptionally(new 
NoSuchElementException());

Review Comment:
   It should be described in the javaDoc.



##########
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/impl/MetaStorageServiceImpl.java:
##########
@@ -82,29 +88,27 @@ public class MetaStorageServiceImpl implements 
MetaStorageService {
     public MetaStorageServiceImpl(RaftGroupService metaStorageRaftGrpSvc, 
ClusterNode localNode) {
         this.metaStorageRaftGrpSvc = metaStorageRaftGrpSvc;
         this.localNode = localNode;
+        this.executorService = 
Executors.newCachedThreadPool(NamedThreadFactory.create(localNode.name(), 
"metastorage-publisher", LOG));

Review Comment:
   Could there be too many threads here?



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