ibessonov commented on code in PR #2390: URL: https://github.com/apache/ignite-3/pull/2390#discussion_r1291026316
########## 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); Review Comment: This code could be much simpler if "processTransfer" returned the "result" future. That way you wouldn't need additional "transfers" collection. _I think_ It's actually pretty difficult to understand the entire thing) ########## 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() + .peek(this::processTransferAsync) + .map(it -> it.result) + .map(it -> it.whenComplete((v, e) -> { + if (e != null) { + shouldBeCancelled.set(true); + } + })) + .toArray(CompletableFuture[]::new); + + 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 processTransferAsync(FileTransfer transfer) { + synchronized (rateLimiter) { + if (rateLimiter.tryAcquire()) { + processTransferWithNextAsync(transfer); + } else { + requests.add(transfer); + } + } + } + + /** + * Process the given transfer and then process the next transfer if the rate limiter is available and there is a next transfer. + * + * @return A future that will be completed when the request is processed. + */ + private CompletableFuture<Void> processTransferWithNextAsync(FileTransfer transfer) { + return sendTransfer(transfer) + .thenComposeAsync(v -> { + synchronized (rateLimiter) { + FileTransfer nextTransfer = requests.poll(); + + // If there is a next transfer, process it. + // Otherwise, release the rate limiter. + if (nextTransfer != null) { + return processTransferWithNextAsync(nextTransfer); + } else { + rateLimiter.release(); + return completedFuture(null); + } + } + }, executorService); + } + + /** + * Sends the file to the node with the given consistent id. + * + * @param transfer The transfer to send. + * @return A future that will be completed when the file is sent. The future will be completed successfully always, even if the file is + * not sent. + */ + private CompletableFuture<Void> sendTransfer(FileTransfer transfer) { + return sendFile(transfer.receiverConsistentId, transfer.transferId, transfer.path, transfer.shouldBeCancelled) + .handle((v, e) -> { + if (e == null) { + transfer.result.complete(null); + } else { + transfer.result.completeExceptionally(e); + } + return null; + }); + } + + /** + * Sends the file to the node with the given consistent id. + * + * @param receiverConsistentId The consistent id of the node to send the file to. + * @param id The id of the file transfer. + * @param path The path of the file to send. + * @return A future that will be completed when the file is sent. + */ + private CompletableFuture<Void> sendFile(String receiverConsistentId, UUID id, Path path, AtomicBoolean shouldBeCancelled) { + if (path.toFile().length() == 0) { + return completedFuture(null); + } else { + return supplyAsync(() -> { + try { + return FileChunkMessagesStream.fromPath(chunkSize, id, path); + } catch (IOException e) { + throw new FileTransferException("Failed to create a file transfer stream", e); + } + }, executorService) + .thenCompose(stream -> { Review Comment: What's the deal with "thenCompose"? You could put this code right into "supplyAsync"? Please explain, don't just close the conversation. ########## 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: Please document, how and why you use this synchronization. If it protects the "requests" queue, then why do you use rate limiter as a monitor? Not very intuitive to me ########## 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; Review Comment: So, what exactly are we limiting? The number of files that we send concurrently? Can we send a single file concurrently (sending N chunks in parallel)? ########## 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() + .peek(this::processTransferAsync) + .map(it -> it.result) + .map(it -> it.whenComplete((v, e) -> { + if (e != null) { + shouldBeCancelled.set(true); + } + })) + .toArray(CompletableFuture[]::new); + + 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 processTransferAsync(FileTransfer transfer) { + synchronized (rateLimiter) { + if (rateLimiter.tryAcquire()) { + processTransferWithNextAsync(transfer); + } else { + requests.add(transfer); + } + } + } + + /** + * Process the given transfer and then process the next transfer if the rate limiter is available and there is a next transfer. + * + * @return A future that will be completed when the request is processed. + */ + private CompletableFuture<Void> processTransferWithNextAsync(FileTransfer transfer) { + return sendTransfer(transfer) + .thenComposeAsync(v -> { + synchronized (rateLimiter) { + FileTransfer nextTransfer = requests.poll(); + + // If there is a next transfer, process it. + // Otherwise, release the rate limiter. + if (nextTransfer != null) { + return processTransferWithNextAsync(nextTransfer); + } else { + rateLimiter.release(); + return completedFuture(null); + } + } + }, executorService); + } + + /** + * Sends the file to the node with the given consistent id. + * + * @param transfer The transfer to send. + * @return A future that will be completed when the file is sent. The future will be completed successfully always, even if the file is + * not sent. + */ + private CompletableFuture<Void> sendTransfer(FileTransfer transfer) { + return sendFile(transfer.receiverConsistentId, transfer.transferId, transfer.path, transfer.shouldBeCancelled) + .handle((v, e) -> { + if (e == null) { + transfer.result.complete(null); + } else { + transfer.result.completeExceptionally(e); + } + return null; + }); + } + + /** + * Sends the file to the node with the given consistent id. + * + * @param receiverConsistentId The consistent id of the node to send the file to. + * @param id The id of the file transfer. + * @param path The path of the file to send. + * @return A future that will be completed when the file is sent. + */ + private CompletableFuture<Void> sendFile(String receiverConsistentId, UUID id, Path path, AtomicBoolean shouldBeCancelled) { + if (path.toFile().length() == 0) { + return completedFuture(null); Review Comment: Why don't we send empty files? -- 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]
