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


##########
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/impl/CursorPublisher.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.function.Function;
+import org.apache.ignite.internal.metastorage.Entry;
+import 
org.apache.ignite.internal.metastorage.command.MetaStorageCommandsFactory;
+import org.apache.ignite.internal.raft.WriteCommand;
+import org.apache.ignite.internal.raft.service.RaftGroupService;
+import org.apache.ignite.lang.IgniteUuid;
+import org.apache.ignite.lang.IgniteUuidGenerator;
+
+/**
+ * Publisher that streams data from a remote Meta Storage cursor.
+ *
+ * @see CursorSubscription
+ */
+class CursorPublisher implements Publisher<Entry> {
+    private static final IgniteUuidGenerator UUID_GENERATOR = new 
IgniteUuidGenerator(UUID.randomUUID(), 0);
+
+    private final RaftGroupService raftService;
+
+    private final MetaStorageCommandsFactory commandsFactory;
+
+    private final ExecutorService executorService;
+
+    private final Function<IgniteUuid, WriteCommand> createCursorSupplier;
+
+    /**
+     * Creates a new publisher instance.
+     *
+     * @param raftService Meta Storage Raft service.
+     * @param commandsFactory Meta Storage commands factory.
+     * @param executorService Executor that will be used to call the 
subscriber's methods.
+     * @param createCursorSupplier Factory that creates a remote Meta Storage 
cursor (provided with a cursor ID).
+     */
+    CursorPublisher(
+            RaftGroupService raftService,
+            MetaStorageCommandsFactory commandsFactory,
+            ExecutorService executorService,
+            Function<IgniteUuid, WriteCommand> createCursorSupplier
+    ) {
+        this.raftService = raftService;
+        this.commandsFactory = commandsFactory;
+        this.executorService = executorService;
+        this.createCursorSupplier = createCursorSupplier;
+    }
+
+    @Override
+    public void subscribe(Subscriber<? super Entry> subscriber) {
+        IgniteUuid cursorId = UUID_GENERATOR.randomUuid();

Review Comment:
   It's covered with outer busyLock, correct? 



##########
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/impl/CursorPublisher.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.function.Function;
+import org.apache.ignite.internal.metastorage.Entry;
+import 
org.apache.ignite.internal.metastorage.command.MetaStorageCommandsFactory;
+import org.apache.ignite.internal.raft.WriteCommand;
+import org.apache.ignite.internal.raft.service.RaftGroupService;
+import org.apache.ignite.lang.IgniteUuid;
+import org.apache.ignite.lang.IgniteUuidGenerator;
+
+/**
+ * Publisher that streams data from a remote Meta Storage cursor.
+ *
+ * @see CursorSubscription
+ */
+class CursorPublisher implements Publisher<Entry> {
+    private static final IgniteUuidGenerator UUID_GENERATOR = new 
IgniteUuidGenerator(UUID.randomUUID(), 0);
+
+    private final RaftGroupService raftService;
+
+    private final MetaStorageCommandsFactory commandsFactory;
+
+    private final ExecutorService executorService;
+
+    private final Function<IgniteUuid, WriteCommand> createCursorSupplier;
+
+    /**
+     * Creates a new publisher instance.
+     *
+     * @param raftService Meta Storage Raft service.
+     * @param commandsFactory Meta Storage commands factory.
+     * @param executorService Executor that will be used to call the 
subscriber's methods.
+     * @param createCursorSupplier Factory that creates a remote Meta Storage 
cursor (provided with a cursor ID).
+     */
+    CursorPublisher(
+            RaftGroupService raftService,
+            MetaStorageCommandsFactory commandsFactory,
+            ExecutorService executorService,
+            Function<IgniteUuid, WriteCommand> createCursorSupplier
+    ) {
+        this.raftService = raftService;
+        this.commandsFactory = commandsFactory;
+        this.executorService = executorService;
+        this.createCursorSupplier = createCursorSupplier;
+    }
+
+    @Override
+    public void subscribe(Subscriber<? super Entry> subscriber) {
+        IgniteUuid cursorId = UUID_GENERATOR.randomUuid();
+
+        WriteCommand createCursorCommand = 
createCursorSupplier.apply(cursorId);
+
+        raftService.run(createCursorCommand)

Review Comment:
   Did you consider the solution that will eliminate additional rtt on cursor 
creation? E.g. it's possible return BatchResponse even for cursor creation and 
postpone cursor creation until explicit request within onSubscribe.



##########
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;
+
+    CursorSubscription(
+            RaftGroupService raftService,
+            MetaStorageCommandsFactory commandsFactory,
+            ExecutorService executorService,
+            IgniteUuid cursorId,
+            Subscriber<? super Entry> subscriber
+    ) {
+        this.raftService = raftService;
+        this.commandsFactory = commandsFactory;
+        this.executorService = executorService;
+        this.cursorId = cursorId;
+        this.subscriber = subscriber;
+    }
+
+    @Override
+    public void request(long n) {
+        if (n <= 0) {
+            onError(new IllegalArgumentException("Requested amount must be 
greater than zero, got: " + n));
+
+            return;
+        }
+
+        if (isDone) {
+            return;
+        }
+
+        // Increase the number of requested items.
+        demand += n;
+
+        if (demand <= 0) {
+            onError(new IllegalArgumentException("Long overflow"));
+
+            return;
+        }
+
+        // Start the processing if it has not been started yet (async 
operation).
+        if (cachedResponse == null) {
+            requestNextBatch();
+        }
+    }
+
+    /**
+     * Does the actual request processing. Must always be called on {@link 
CursorSubscription#executorService}.
+     */
+    private void processRequest() {
+        if (isDone) {
+            return;
+        }
+
+        try {
+            assert cachedResponse != null;
+
+            List<Entry> entries = cachedResponse.entries();
+
+            while (demand > 0 && !isDone) {
+                if (responseIndex < entries.size()) {
+                    subscriber.onNext(entries.get(responseIndex));
+
+                    responseIndex++;
+                    demand--;
+                } else {
+                    if (cachedResponse.hasNextBatch()) {
+                        requestNextBatch();
+                    } else {
+                        isDone = true;
+
+                        subscriber.onComplete();
+                    }
+
+                    return;
+                }
+            }
+        } catch (Exception e) {
+            onError(e);
+        }
+    }
+
+    private void requestNextBatch() {
+        NextBatchCommand command = commandsFactory.nextBatchCommand()
+                .cursorId(cursorId)
+                .batchSize(BATCH_SIZE)
+                .build();
+
+        raftService.<BatchResponse>run(command)
+                .whenCompleteAsync((resp, e) -> {
+                    if (e == null) {
+                        cachedResponse = resp;

Review Comment:
   Not sure that I get it. Do you expect that whenCompleteAsync will be 
executed in the same thread as CursorSubscription.request(n)?



##########
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/impl/CursorPublisher.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.function.Function;
+import org.apache.ignite.internal.metastorage.Entry;
+import 
org.apache.ignite.internal.metastorage.command.MetaStorageCommandsFactory;
+import org.apache.ignite.internal.raft.WriteCommand;
+import org.apache.ignite.internal.raft.service.RaftGroupService;
+import org.apache.ignite.lang.IgniteUuid;
+import org.apache.ignite.lang.IgniteUuidGenerator;
+
+/**
+ * Publisher that streams data from a remote Meta Storage cursor.
+ *
+ * @see CursorSubscription
+ */
+class CursorPublisher implements Publisher<Entry> {
+    private static final IgniteUuidGenerator UUID_GENERATOR = new 
IgniteUuidGenerator(UUID.randomUUID(), 0);
+
+    private final RaftGroupService raftService;
+
+    private final MetaStorageCommandsFactory commandsFactory;
+
+    private final ExecutorService executorService;
+
+    private final Function<IgniteUuid, WriteCommand> createCursorSupplier;
+
+    /**
+     * Creates a new publisher instance.
+     *
+     * @param raftService Meta Storage Raft service.
+     * @param commandsFactory Meta Storage commands factory.
+     * @param executorService Executor that will be used to call the 
subscriber's methods.
+     * @param createCursorSupplier Factory that creates a remote Meta Storage 
cursor (provided with a cursor ID).
+     */
+    CursorPublisher(
+            RaftGroupService raftService,
+            MetaStorageCommandsFactory commandsFactory,
+            ExecutorService executorService,
+            Function<IgniteUuid, WriteCommand> createCursorSupplier
+    ) {
+        this.raftService = raftService;
+        this.commandsFactory = commandsFactory;
+        this.executorService = executorService;
+        this.createCursorSupplier = createCursorSupplier;
+    }
+
+    @Override
+    public void subscribe(Subscriber<? super Entry> subscriber) {
+        IgniteUuid cursorId = UUID_GENERATOR.randomUuid();

Review Comment:
   So, you do support multiple subscriptions, right?



##########
modules/metastorage/src/main/java/org/apache/ignite/internal/metastorage/impl/CursorPublisher.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Flow.Publisher;
+import java.util.concurrent.Flow.Subscriber;
+import java.util.function.Function;
+import org.apache.ignite.internal.metastorage.Entry;
+import 
org.apache.ignite.internal.metastorage.command.MetaStorageCommandsFactory;
+import org.apache.ignite.internal.raft.WriteCommand;
+import org.apache.ignite.internal.raft.service.RaftGroupService;
+import org.apache.ignite.lang.IgniteUuid;
+import org.apache.ignite.lang.IgniteUuidGenerator;
+
+/**
+ * Publisher that streams data from a remote Meta Storage cursor.
+ *
+ * @see CursorSubscription
+ */
+class CursorPublisher implements Publisher<Entry> {
+    private static final IgniteUuidGenerator UUID_GENERATOR = new 
IgniteUuidGenerator(UUID.randomUUID(), 0);
+
+    private final RaftGroupService raftService;
+
+    private final MetaStorageCommandsFactory commandsFactory;
+
+    private final ExecutorService executorService;
+
+    private final Function<IgniteUuid, WriteCommand> createCursorSupplier;
+
+    /**
+     * Creates a new publisher instance.
+     *
+     * @param raftService Meta Storage Raft service.
+     * @param commandsFactory Meta Storage commands factory.
+     * @param executorService Executor that will be used to call the 
subscriber's methods.
+     * @param createCursorSupplier Factory that creates a remote Meta Storage 
cursor (provided with a cursor ID).
+     */
+    CursorPublisher(
+            RaftGroupService raftService,
+            MetaStorageCommandsFactory commandsFactory,
+            ExecutorService executorService,
+            Function<IgniteUuid, WriteCommand> createCursorSupplier
+    ) {
+        this.raftService = raftService;
+        this.commandsFactory = commandsFactory;
+        this.executorService = executorService;
+        this.createCursorSupplier = createCursorSupplier;
+    }
+
+    @Override
+    public void subscribe(Subscriber<? super Entry> subscriber) {
+        IgniteUuid cursorId = UUID_GENERATOR.randomUuid();
+
+        WriteCommand createCursorCommand = 
createCursorSupplier.apply(cursorId);
+
+        raftService.run(createCursorCommand)
+                .whenCompleteAsync((v, e) -> {
+                    if (e == null) {

Review Comment:
   Should we also cover such code with busyLock?



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