Flaugh24 commented on code in PR #2390:
URL: https://github.com/apache/ignite-3/pull/2390#discussion_r1291064723


##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileSender.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.network.file;
+
+import static java.util.concurrent.CompletableFuture.allOf;
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static java.util.concurrent.CompletableFuture.failedFuture;
+import static java.util.concurrent.CompletableFuture.supplyAsync;
+import static 
org.apache.ignite.internal.network.file.Channel.FILE_TRANSFER_CHANNEL;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Queue;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Collectors;
+import org.apache.ignite.internal.network.file.exception.FileTransferException;
+import org.apache.ignite.network.MessagingService;
+
+/**
+ * A class that sends files to a node. It uses a rate limiter to limit the 
bandwidth used. It also uses a thread pool to send the files in
+ * parallel.
+ */
+class FileSender {
+    private final int chunkSize;
+
+    private final Semaphore rateLimiter;
+
+    private final MessagingService messagingService;
+
+    private final ExecutorService executorService;
+
+    private final Queue<FileTransfer> requests = new LinkedList<>();
+
+    FileSender(
+            int chunkSize,
+            Semaphore rateLimiter,
+            MessagingService messagingService,
+            ExecutorService executorService
+    ) {
+        this.chunkSize = chunkSize;
+        this.rateLimiter = rateLimiter;
+        this.messagingService = messagingService;
+        this.executorService = executorService;
+    }
+
+    /**
+     * Creates {@link FileTransfer} objects for each file and places them in 
the queue. The files will be sent when the rate limiter is
+     * available.
+     *
+     * @param targetNodeConsistentId The consistent ID of the node to send the 
files to.
+     * @param transferId The ID of the transfer.
+     * @param paths The paths of the files to send.
+     * @return A future that will be completed when the transfer is complete.
+     */
+    CompletableFuture<Void> send(String targetNodeConsistentId, UUID 
transferId, List<Path> paths) {
+        // It doesn't make sense to continue file transfer if there is a 
failure in one of the files.
+        AtomicBoolean shouldBeCancelled = new AtomicBoolean(false);
+
+        List<FileTransfer> transfers = paths.stream()
+                .map(path -> new FileTransfer(targetNodeConsistentId, 
transferId, path, shouldBeCancelled))
+                .collect(Collectors.toList());
+
+        CompletableFuture[] results = transfers.stream().map(it -> it.result)
+                .map(it -> it.whenComplete((v, e) -> {
+                    if (e != null) {
+                        shouldBeCancelled.set(true);
+                    }
+                }))
+                .toArray(CompletableFuture[]::new);
+
+        transfers.forEach(this::processTransfer);
+
+        return allOf(results);
+    }
+
+    /**
+     * Processes the given transfer. If the rate limiter is not available, the 
transfer will be processed later.
+     *
+     * @param transfer The transfer to process.
+     */
+    private void processTransfer(FileTransfer transfer) {
+        synchronized (rateLimiter) {

Review Comment:
   I suppose there is a chance that one thread which has a permit can check the 
queue and see it's empty. At the same time, the other thread can't get a permit 
from the limiter and places a transfer in the queue. As a result, there will be 
a transfer in the queue, and no one will process it until the Sender gets the 
next transfer.



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