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


##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileTransferServiceImpl.java:
##########
@@ -0,0 +1,402 @@
+/*
+ * 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.completedFuture;
+import static java.util.concurrent.CompletableFuture.failedFuture;
+import static java.util.concurrent.CompletableFuture.supplyAsync;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import 
org.apache.ignite.internal.network.configuration.FileTransferConfiguration;
+import 
org.apache.ignite.internal.network.file.exception.FileHandlerNotFoundException;
+import 
org.apache.ignite.internal.network.file.exception.FileProviderNotFoundException;
+import org.apache.ignite.internal.network.file.exception.FileTransferException;
+import org.apache.ignite.internal.network.file.messages.FileChunkMessage;
+import org.apache.ignite.internal.network.file.messages.FileDownloadRequest;
+import org.apache.ignite.internal.network.file.messages.FileDownloadResponse;
+import org.apache.ignite.internal.network.file.messages.FileHeaderMessage;
+import 
org.apache.ignite.internal.network.file.messages.FileTransferErrorMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferFactory;
+import 
org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import 
org.apache.ignite.internal.network.file.messages.FileTransferMessageType;
+import org.apache.ignite.internal.network.file.messages.FileUploadRequest;
+import org.apache.ignite.internal.network.file.messages.FileUploadResponse;
+import org.apache.ignite.internal.network.file.messages.Metadata;
+import org.apache.ignite.internal.util.ExceptionUtils;
+import org.apache.ignite.internal.util.FilesUtils;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.network.ChannelType;
+import org.apache.ignite.network.ClusterNode;
+import org.apache.ignite.network.MessagingService;
+import org.apache.ignite.network.TopologyEventHandler;
+import org.apache.ignite.network.TopologyService;
+import org.apache.ignite.network.annotations.Transferable;
+import org.jetbrains.annotations.TestOnly;
+
+/**
+ * Implementation of {@link FileTransferService}.
+ */
+public class FileTransferServiceImpl implements FileTransferService {
+    private static final IgniteLogger LOG = 
Loggers.forClass(FileTransferServiceImpl.class);
+
+    private static final ChannelType FILE_TRANSFERRING_CHANNEL = 
ChannelType.register((short) 1, "FileTransferring");
+
+    private static final long RESPONSE_TIMEOUT = 10_000;
+
+    /**
+     * Topology service.
+     */
+    private final TopologyService topologyService;
+    /**
+     * Cluster service.
+     */
+    private final MessagingService messagingService;
+
+    /**
+     * Temporary directory for saving files.
+     */
+    private final Path tempDirectory;
+
+    /**
+     * File sender.
+     */
+    private final FileSender fileSender;
+
+    /**
+     * File receiver.
+     */
+    private final FileReceiver fileReceiver;
+
+    /**
+     * Map of file providers.
+     */
+    private final Map<Short, FileProvider<Metadata>> metadataToProvider = new 
ConcurrentHashMap<>();
+
+    /**
+     * Map of file handlers.
+     */
+    private final Map<Short, FileConsumer<Metadata>> metadataToHandler = new 
ConcurrentHashMap<>();
+
+    /**
+     * File transfer factory.
+     */
+    private final FileTransferFactory factory = new FileTransferFactory();
+
+    /**
+     * Constructor.
+     *
+     * @param messagingService Messaging service.
+     * @param tempDirectory Temporary directory.
+     */
+    public FileTransferServiceImpl(
+            String nodeName,
+            TopologyService topologyService,
+            MessagingService messagingService,
+            FileTransferConfiguration configuration,
+            Path tempDirectory
+    ) {
+        this(
+                topologyService,
+                messagingService,
+                tempDirectory,
+                new FileSender(
+                        nodeName,
+                        configuration.value().senderThreadPoolSize(),
+                        configuration.value().chunkSize(),
+                        new 
RateLimiter(configuration.value().maxConcurrentRequests()),
+                        (recipientConsistentId, message) -> 
messagingService.send(recipientConsistentId, FILE_TRANSFERRING_CHANNEL,
+                                message)
+                ),
+                new FileReceiver(nodeName, 
configuration.value().receiverThreadPoolSize())
+        );
+    }
+
+    @TestOnly
+    FileTransferServiceImpl(
+            TopologyService topologyService,
+            MessagingService messagingService,
+            Path tempDirectory,
+            FileSender fileSender,
+            FileReceiver fileReceiver
+    ) {
+        this.topologyService = topologyService;
+        this.messagingService = messagingService;
+        this.tempDirectory = tempDirectory;
+        this.fileSender = fileSender;
+        this.fileReceiver = fileReceiver;
+    }
+
+    @Override
+    public void start() {
+        topologyService.addEventHandler(new TopologyEventHandler() {
+            @Override
+            public void onDisappeared(ClusterNode member) {
+                fileReceiver.cancelTransfersFromSender(member.id());
+            }
+        });
+
+        messagingService.addMessageHandler(FileTransferMessageType.class,
+                (message, senderConsistentId, correlationId) -> {
+                    if (message instanceof FileDownloadRequest) {
+                        processDownloadRequest((FileDownloadRequest) message, 
senderConsistentId, correlationId);
+                    } else if (message instanceof FileUploadRequest) {
+                        processUploadRequest((FileUploadRequest) message, 
senderConsistentId, correlationId);
+                    } else if (message instanceof FileTransferErrorMessage) {
+                        
processFileTransferErrorMessage((FileTransferErrorMessage) message);
+                    } else if (message instanceof FileTransferInfoMessage) {
+                        processFileTransferInfo((FileTransferInfoMessage) 
message);
+                    } else if (message instanceof FileHeaderMessage) {
+                        processFileHeader((FileHeaderMessage) message);
+                    } else if (message instanceof FileChunkMessage) {
+                        processFileChunk((FileChunkMessage) message);
+                    } else {
+                        LOG.error("Unexpected message received: {}", message);
+                    }
+                });
+    }
+
+    private void processFileTransferErrorMessage(FileTransferErrorMessage 
message) {
+        fileReceiver.receiveFileTransferErrorMessage(message);
+    }
+
+    private void processUploadRequest(FileUploadRequest message, String 
senderConsistentId, long correlationId) {
+        completedFuture(UUID.randomUUID())
+                .thenCompose(transferId -> {
+                    Path directory = createTransferDirectory(transferId);
+                    FileTransferMessagesHandler handler = 
fileReceiver.registerTransfer(
+                            senderConsistentId,
+                            transferId,
+                            directory
+                    );
+                    FileUploadResponse response = factory.fileUploadResponse()
+                            .transferId(transferId)
+                            .build();
+                    Metadata metadata = message.metadata();
+                    return messagingService.respond(senderConsistentId, 
FILE_TRANSFERRING_CHANNEL, response, correlationId)

Review Comment:
   `org.apache.ignite.internal.network.file.FileSender` and 
`org.apache.ignite.internal.network.file.FileReceiver` have their own executors 
where they do all IO operations. 



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