You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "ibessonov (via GitHub)" <gi...@apache.org> on 2023/08/08 08:56:43 UTC

[GitHub] [ignite-3] ibessonov commented on a diff in pull request #2390: IGNITE-19009 Introduce file transfer support in messaging

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


##########
modules/core/src/main/java/org/apache/ignite/internal/util/FilesUtils.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * 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.util;
+
+import java.io.IOException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
+
+/**
+ * Files utilities.
+ */
+public class FilesUtils {

Review Comment:
   I think we already have `org.apache.ignite.internal.util.IgniteUtils#deleteIfExists` and `org.apache.ignite.internal.util.IgniteUtils#deleteIfExistsThrowable`. What's the reason for adding a literal copy? Seems very confusing to me.
   Just fix the `deleteIfExistsThrowable` and that's it.



##########
modules/core/src/testFixtures/java/org/apache/ignite/internal/testframework/matchers/FileContentMatcher.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.matchers;
+
+import static org.hamcrest.CoreMatchers.is;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+/**
+ * {@link TypeSafeMatcher} for matching file content.
+ */
+public class FileContentMatcher extends TypeSafeMatcher<File> {
+
+    /** Matcher to forward the result of the completable future. */
+    private final Matcher<byte[]> matcher;
+
+    private FileContentMatcher(Matcher<byte[]> matcher) {
+        this.matcher = matcher;
+    }
+
+    /**
+     * Creates a matcher for matching file content.
+     *
+     * @param file File to match.
+     * @return Matcher for matching file content.
+     */
+    public static FileContentMatcher hasContent(File file) {
+        try {
+            return hasContent(is(Files.readAllBytes(file.toPath())));

Review Comment:
   `org.hamcrest.Matchers#equalTo` would fit better than `is`



##########
modules/core/src/testFixtures/java/org/apache/ignite/internal/testframework/matchers/FileContentMatcher.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.matchers;
+
+import static org.hamcrest.CoreMatchers.is;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+/**
+ * {@link TypeSafeMatcher} for matching file content.
+ */
+public class FileContentMatcher extends TypeSafeMatcher<File> {
+
+    /** Matcher to forward the result of the completable future. */
+    private final Matcher<byte[]> matcher;
+
+    private FileContentMatcher(Matcher<byte[]> matcher) {
+        this.matcher = matcher;
+    }
+
+    /**
+     * Creates a matcher for matching file content.
+     *
+     * @param file File to match.
+     * @return Matcher for matching file content.
+     */
+    public static FileContentMatcher hasContent(File file) {
+        try {
+            return hasContent(is(Files.readAllBytes(file.toPath())));
+        } catch (IOException e) {
+            throw new RuntimeException("Could not read file content", e);
+        }
+    }
+
+    /**
+     * Creates a matcher for matching file content.
+     *
+     * @param matcher Matcher for matching file content.
+     * @return Matcher for matching file content.
+     */
+    public static FileContentMatcher hasContent(Matcher<byte[]> matcher) {
+        return new FileContentMatcher(matcher);
+    }
+
+    @Override
+    protected boolean matchesSafely(File file) {
+        try {
+            return matcher.matches(Files.readAllBytes(file.toPath()));
+        } catch (IOException e) {
+            throw new RuntimeException("Could not read file content", e);
+        }
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText("matches content: ").appendDescriptionOf(matcher);
+    }
+
+    @Override
+    protected void describeMismatchSafely(File item, Description mismatchDescription) {
+        try {
+            mismatchDescription.appendText("was ").appendValue(Files.readAllBytes(item.toPath()));
+        } catch (IOException e) {
+            throw new RuntimeException("Could not read file content", e);
+        }
+    }
+}
+

Review Comment:
   Please remove this empty line at the end



##########
modules/file-transfer/src/integrationTest/java/org/apache/ignite/internal/network/file/ItFileTransferTest.java:
##########
@@ -0,0 +1,336 @@
+/*
+ * 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 org.apache.ignite.internal.network.file.FileAssertions.assertContentEquals;
+import static org.apache.ignite.internal.network.file.FileGenerator.randomFile;
+import static org.apache.ignite.internal.network.file.FileUtils.sortByNames;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrow;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrowWithCauseOrSuppressed;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willTimeoutIn;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.AccessDeniedException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import org.apache.ignite.internal.configuration.testframework.InjectConfiguration;
+import org.apache.ignite.internal.network.configuration.FileTransferConfiguration;
+import org.apache.ignite.internal.network.file.TestCluster.Node;
+import org.apache.ignite.internal.network.file.exception.FileTransferException;
+import org.apache.ignite.internal.network.file.messages.Metadata;
+import org.apache.ignite.internal.network.file.messages.MetadataImpl;
+import org.apache.ignite.internal.testframework.WorkDirectory;
+import org.apache.ignite.internal.testframework.WorkDirectoryExtension;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Integration tests for file transferring.
+ */
+@ExtendWith(ConfigurationExtension.class)
+@ExtendWith(WorkDirectoryExtension.class)
+public class ItFileTransferTest {
+
+    @WorkDirectory
+    private Path workDir;
+
+    @InjectConfiguration("mock.chunkSize=1024")
+    private FileTransferConfiguration configuration;
+
+    private TestCluster cluster;
+
+    @BeforeEach
+    void setUp(TestInfo testInfo) throws InterruptedException {
+        cluster = new TestCluster(2, configuration, workDir, testInfo);
+        cluster.startAwait();
+    }
+
+    @AfterEach
+    void tearDown() throws Exception {
+        cluster.shutdown();
+    }
+
+    @Test
+    void download() throws IOException {
+        Node targetNode = cluster.members.get(0);
+
+        String unit = "unit";
+        String version = "1.0.0";
+        Path unitPath = targetNode.workDir().resolve(unit).resolve(version);
+        Files.createDirectories(unitPath);
+
+        int chunkSize = configuration.value().chunkSize();
+        File file1 = randomFile(unitPath, 0);
+        File file2 = randomFile(unitPath, chunkSize + 1);
+        File file3 = randomFile(unitPath, chunkSize * 2);
+        File file4 = randomFile(unitPath, chunkSize * 2);
+
+        targetNode.fileTransferringService().addFileProvider(
+                Metadata.class,
+                req -> completedFuture(List.of(file1, file2, file3, file4))
+        );
+
+        Node sourceNode = cluster.members.get(1);
+        CompletableFuture<List<File>> downloadedFilesFuture = sourceNode.fileTransferringService().download(
+                targetNode.nodeName(),
+                MetadataImpl.builder().build()
+        );
+
+        assertThat(
+                downloadedFilesFuture.thenAccept(files -> {
+                    assertContentEquals(sortByNames(file1, file2, file3, file4), sortByNames(files));

Review Comment:
   Is there an easier way to do this? Such check should probably be inside of `willBe` matcher, not in the future itself. Maybe some "comtainsInAnyOrder" with another composit matcher (hasContent && name matching) would look better



##########
modules/file-transfer/build.gradle:
##########
@@ -0,0 +1,49 @@
+/*
+ * 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.
+ */
+
+apply from: "$rootDir/buildscripts/java-core.gradle"
+apply from: "$rootDir/buildscripts/publishing.gradle"
+apply from: "$rootDir/buildscripts/java-junit5.gradle"
+apply from: "$rootDir/buildscripts/java-integration-test.gradle"
+apply from: "$rootDir/buildscripts/java-test-fixtures.gradle"
+
+dependencies {
+    annotationProcessor project(':ignite-network-annotation-processor')
+
+    implementation project(':ignite-core')
+    implementation project(':ignite-network')
+    implementation project(':ignite-configuration-api')
+    implementation libs.jetbrains.annotations
+
+    testImplementation(testFixtures(project))
+    testImplementation(testFixtures(project(':ignite-core')))
+    testImplementation libs.hamcrest.core
+    testImplementation libs.mockito.core
+    testImplementation libs.mockito.junit
+
+    integrationTestImplementation(testFixtures(project))
+    integrationTestImplementation(testFixtures(project(':ignite-core')))
+    integrationTestImplementation(testFixtures(project(':ignite-network')))
+    integrationTestImplementation(testFixtures(project(':ignite-configuration')))
+    integrationTestImplementation libs.hamcrest.core
+    integrationTestImplementation libs.awaitility
+
+    testFixturesImplementation(testFixtures(project(':ignite-core')))
+    testFixturesImplementation libs.hamcrest.core
+}
+
+description = 'ignite-file-io'

Review Comment:
   What?



##########
modules/file-transfer/src/integrationTest/java/org/apache/ignite/internal/network/file/ItFileTransferTest.java:
##########
@@ -0,0 +1,336 @@
+/*
+ * 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 org.apache.ignite.internal.network.file.FileAssertions.assertContentEquals;
+import static org.apache.ignite.internal.network.file.FileGenerator.randomFile;
+import static org.apache.ignite.internal.network.file.FileUtils.sortByNames;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrow;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrowWithCauseOrSuppressed;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willTimeoutIn;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.AccessDeniedException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import org.apache.ignite.internal.configuration.testframework.InjectConfiguration;
+import org.apache.ignite.internal.network.configuration.FileTransferConfiguration;
+import org.apache.ignite.internal.network.file.TestCluster.Node;
+import org.apache.ignite.internal.network.file.exception.FileTransferException;
+import org.apache.ignite.internal.network.file.messages.Metadata;
+import org.apache.ignite.internal.network.file.messages.MetadataImpl;
+import org.apache.ignite.internal.testframework.WorkDirectory;
+import org.apache.ignite.internal.testframework.WorkDirectoryExtension;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Integration tests for file transferring.
+ */
+@ExtendWith(ConfigurationExtension.class)
+@ExtendWith(WorkDirectoryExtension.class)
+public class ItFileTransferTest {
+
+    @WorkDirectory
+    private Path workDir;
+
+    @InjectConfiguration("mock.chunkSize=1024")
+    private FileTransferConfiguration configuration;
+
+    private TestCluster cluster;
+
+    @BeforeEach
+    void setUp(TestInfo testInfo) throws InterruptedException {
+        cluster = new TestCluster(2, configuration, workDir, testInfo);
+        cluster.startAwait();
+    }
+
+    @AfterEach
+    void tearDown() throws Exception {
+        cluster.shutdown();
+    }
+
+    @Test
+    void download() throws IOException {
+        Node targetNode = cluster.members.get(0);
+
+        String unit = "unit";
+        String version = "1.0.0";
+        Path unitPath = targetNode.workDir().resolve(unit).resolve(version);
+        Files.createDirectories(unitPath);
+
+        int chunkSize = configuration.value().chunkSize();
+        File file1 = randomFile(unitPath, 0);
+        File file2 = randomFile(unitPath, chunkSize + 1);
+        File file3 = randomFile(unitPath, chunkSize * 2);
+        File file4 = randomFile(unitPath, chunkSize * 2);
+
+        targetNode.fileTransferringService().addFileProvider(
+                Metadata.class,
+                req -> completedFuture(List.of(file1, file2, file3, file4))
+        );
+
+        Node sourceNode = cluster.members.get(1);
+        CompletableFuture<List<File>> downloadedFilesFuture = sourceNode.fileTransferringService().download(
+                targetNode.nodeName(),
+                MetadataImpl.builder().build()
+        );
+
+        assertThat(
+                downloadedFilesFuture.thenAccept(files -> {
+                    assertContentEquals(sortByNames(file1, file2, file3, file4), sortByNames(files));
+                }),
+                willCompleteSuccessfully()
+        );
+    }
+
+    @Test
+    void downloadNonReadableFiles() throws IOException {
+        Node targetNode = cluster.members.get(0);
+
+        String unit = "unit";
+        String version = "1.0.0";
+        Path unitPath = targetNode.workDir().resolve(unit).resolve(version);
+        Files.createDirectories(unitPath);
+
+        int chunkSize = configuration.value().chunkSize();
+        File file1 = randomFile(unitPath, chunkSize - 1);
+        File file2 = randomFile(unitPath, chunkSize + 1);
+        File file3 = randomFile(unitPath, chunkSize * 2);
+
+        assertTrue(file2.setReadable(false));
+
+        targetNode.fileTransferringService().addFileProvider(
+                Metadata.class,
+                req -> completedFuture(List.of(file1, file2, file3))
+        );
+
+        Node sourceNode = cluster.members.get(1);
+        CompletableFuture<List<File>> download = sourceNode.fileTransferringService().download(
+                targetNode.nodeName(),
+                MetadataImpl.builder().build()
+        );
+        assertThat(download, willThrow(FileTransferException.class, "Permission denied"));
+    }
+
+    @Test
+    void downloadFilesWhenProviderThrowsException() {
+        Node targetNode = cluster.members.get(0);
+
+        targetNode.fileTransferringService().addFileProvider(
+                Metadata.class,
+                req -> failedFuture(new RuntimeException("Test exception"))
+        );
+
+        Node sourceNode = cluster.members.get(1);
+        CompletableFuture<List<File>> download = sourceNode.fileTransferringService().download(
+                targetNode.nodeName(),
+                MetadataImpl.builder().build()
+        );
+        assertThat(download, willThrowWithCauseOrSuppressed(FileTransferException.class));
+    }
+
+    @Test
+    void downloadFilesWhenDoNotHaveAccessToWrite() throws IOException {
+        Node targetNode = cluster.members.get(0);
+
+        String unit = "unit";
+        String version = "1.0.0";
+        Path unitPath = targetNode.workDir().resolve(unit).resolve(version);
+        Files.createDirectories(unitPath);
+
+        int chunkSize = configuration.value().chunkSize();
+        File file1 = randomFile(unitPath, 0);
+        File file2 = randomFile(unitPath, chunkSize + 1);
+        File file3 = randomFile(unitPath, chunkSize * 2);
+        File file4 = randomFile(unitPath, chunkSize * 2);
+
+        targetNode.fileTransferringService().addFileProvider(
+                Metadata.class,
+                req -> completedFuture(List.of(file1, file2, file3, file4))
+        );
+
+        Node sourceNode = cluster.members.get(1);
+        assertTrue(sourceNode.workDir().toFile().setWritable(false));
+
+        CompletableFuture<List<File>> downloadedFilesFuture = sourceNode.fileTransferringService().download(
+                targetNode.nodeName(),
+                MetadataImpl.builder().build()
+        );
+
+        assertThat(
+                downloadedFilesFuture.thenAccept(files -> {
+                    assertContentEquals(sortByNames(file1, file2, file3, file4), sortByNames(files));
+                }),
+                willThrowWithCauseOrSuppressed(AccessDeniedException.class)
+        );
+    }
+
+    @Test
+    void downloadEmptyFileList() {
+        Node targetNode = cluster.members.get(0);
+
+        targetNode.fileTransferringService().addFileProvider(
+                Metadata.class,
+                req -> completedFuture(List.of())
+        );
+
+        Node sourceNode = cluster.members.get(1);
+        CompletableFuture<List<File>> downloadedFiles = sourceNode.fileTransferringService().download(
+                targetNode.nodeName(),
+                MetadataImpl.builder().build()
+        );
+        assertThat(downloadedFiles, willThrow(FileTransferException.class, "No files to download"));
+    }
+
+    @Test
+    void upload() throws IOException {
+        Node sourceNode = cluster.members.get(0);
+
+        String unit = "unit";
+        String version = "1.0.0";
+        Path unitPath = sourceNode.workDir().resolve(unit).resolve(version);
+        Files.createDirectories(unitPath);
+
+        int chunkSize = configuration.value().chunkSize();
+        File file1 = randomFile(unitPath, 0);
+        File file2 = randomFile(unitPath, chunkSize - 1);
+        File file3 = randomFile(unitPath, chunkSize + 1);
+        File file4 = randomFile(unitPath, chunkSize * 2);
+
+        sourceNode.fileTransferringService().addFileProvider(
+                Metadata.class,
+                req -> completedFuture(List.of(file1, file2, file3, file4))
+        );
+
+        Node targetNode = cluster.members.get(1);
+
+        CompletableFuture<List<File>> uploadedFilesFuture = new CompletableFuture<>();
+        targetNode.fileTransferringService().addFileHandler(Metadata.class, ((metadata, uploadedFilesDir) -> {
+            uploadedFilesFuture.complete(uploadedFilesDir);
+            return completedFuture(null);
+        }));
+
+        sourceNode.fileTransferringService().upload(targetNode.nodeName(), MetadataImpl.builder().build());
+
+        assertThat(
+                uploadedFilesFuture.thenAccept(files -> {
+                    assertContentEquals(sortByNames(file1, file2, file3, file4), sortByNames(files));
+                }),
+                willCompleteSuccessfully()
+        );
+    }
+
+    @Test
+    void uploadEmptyFileList() {
+        Node sourceNode = cluster.members.get(0);
+
+        sourceNode.fileTransferringService().addFileProvider(
+                Metadata.class,
+                req -> completedFuture(List.of())
+        );
+
+        Node targetNode = cluster.members.get(1);
+
+        assertThat(
+                sourceNode.fileTransferringService().upload(targetNode.nodeName(), MetadataImpl.builder().build()),
+                willThrow(FileTransferException.class, "No files to upload")
+        );
+    }
+
+    @Test
+    void uploadNonReadableFiles() throws IOException {
+        Node sourceNode = cluster.members.get(0);
+
+        String unit = "unit";
+        String version = "1.0.0";
+        Path unitPath = sourceNode.workDir().resolve(unit).resolve(version);
+        Files.createDirectories(unitPath);
+
+        int chunkSize = configuration.value().chunkSize();
+        File file1 = randomFile(unitPath, chunkSize - 1);
+        File file2 = randomFile(unitPath, chunkSize + 1);
+        File file3 = randomFile(unitPath, chunkSize * 2);
+
+        assertTrue(file2.setReadable(false));
+
+        sourceNode.fileTransferringService().addFileProvider(
+                Metadata.class,
+                req -> completedFuture(List.of(file1, file2, file3))
+        );
+
+        Node targetNode = cluster.members.get(1);
+
+        CompletableFuture<List<File>> uploadedFilesFuture = new CompletableFuture<>();
+        targetNode.fileTransferringService().addFileHandler(Metadata.class, ((metadata, uploadedFilesDir) -> {
+            uploadedFilesFuture.complete(uploadedFilesDir);
+            return completedFuture(null);
+        }));
+
+        assertThat(
+                sourceNode.fileTransferringService().upload(targetNode.nodeName(), MetadataImpl.builder().build()),
+                willThrow(FileTransferException.class, "Failed to send files to node: ")
+        );
+
+        assertThat(
+                uploadedFilesFuture,
+                willTimeoutIn(1, TimeUnit.SECONDS)

Review Comment:
   Usually I don't like tests that would spend a second on a simple assertion. Is there a better way to check the same thing?



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/ChunkedFileWriter.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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 java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.file.Path;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+import org.apache.ignite.internal.network.file.messages.FileChunkMessage;
+
+class ChunkedFileWriter implements ManuallyCloseable {
+    private final RandomAccessFile raf;
+
+    private long fileSize;
+
+    private final Queue<FileChunkMessage> chunks = new PriorityQueue<>(FileChunkMessage.COMPARATOR);
+
+    private ChunkedFileWriter(RandomAccessFile raf, long fileSize) {
+        this.raf = raf;
+        this.fileSize = fileSize;
+    }
+
+    static ChunkedFileWriter open(Path path, long fileSize) throws FileNotFoundException {
+        return new ChunkedFileWriter(new RandomAccessFile(path.toFile(), "rw"), fileSize);
+    }
+
+    void write(FileChunkMessage chunk) throws IOException {
+        chunks.add(chunk);
+
+        while (!chunks.isEmpty() && chunks.peek().offset() == raf.getFilePointer()) {
+            raf.write(chunks.poll().data());
+        }
+
+        if (fileSize > -1 && raf.getFilePointer() > fileSize) {
+            throw new IOException("File size exceeded");

Review Comment:
   Should we have a more specific type of exception?



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileConsumer.java:
##########
@@ -0,0 +1,37 @@
+/*
+ * 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 java.io.File;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import org.apache.ignite.internal.network.file.messages.Metadata;
+
+/**
+ * Handler for the uploaded file.

Review Comment:
   This may be a stupid question, but - do we call this method on the sender or on the receiver? I don't understand it from the documentation.



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileReceiver.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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 java.nio.file.Path;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantLock;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+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.FileHeaderMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferErrorMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import org.apache.ignite.internal.thread.NamedThreadFactory;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.internal.util.RefCountedObjectPool;
+
+/**
+ * File receiver.
+ */
+class FileReceiver implements ManuallyCloseable {

Review Comment:
   What class is responsible for handling "node left" events and cancelling all operations?



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileReceiver.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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 java.nio.file.Path;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantLock;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+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.FileHeaderMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferErrorMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import org.apache.ignite.internal.thread.NamedThreadFactory;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.internal.util.RefCountedObjectPool;
+
+/**
+ * File receiver.
+ */
+class FileReceiver implements ManuallyCloseable {
+    private static final IgniteLogger LOG = Loggers.forClass(FileReceiver.class);
+
+    private final ExecutorService executorService;
+
+    private final Map<UUID, FileTransferMessagesHandler> transferIdToHandler = new ConcurrentHashMap<>();
+
+    private final Map<String, Set<UUID>> senderConsistentIdToTransferIds = new ConcurrentHashMap<>();
+
+    private final RefCountedObjectPool<String, ReentrantLock> senderConsistentIdToLock = new RefCountedObjectPool<>();
+
+    FileReceiver(String nodeName, int threadPoolSize) {
+        this.executorService = new ThreadPoolExecutor(
+                0,
+                threadPoolSize,
+                0L, TimeUnit.MILLISECONDS,
+                new LinkedBlockingQueue<Runnable>(),
+                NamedThreadFactory.create(nodeName, "file-receiver", LOG)
+        );
+    }
+
+    FileTransferMessagesHandler registerTransfer(String senderConsistentId, UUID transferId, Path handlerDir) {
+        ReentrantLock lock = senderConsistentIdToLock.acquire(senderConsistentId, ignored -> new ReentrantLock());
+        lock.lock();
+        try {
+            FileTransferMessagesHandler handler = new FileTransferMessagesHandler(handlerDir);
+            transferIdToHandler.put(transferId, handler);
+            senderConsistentIdToTransferIds.compute(senderConsistentId, (k, v) -> {
+                if (v == null) {
+                    v = new HashSet<>();
+                }
+                v.add(transferId);
+                return v;
+            });
+            handler.result().whenComplete((files, throwable) -> deregisterTransfer(senderConsistentId, transferId));
+            return handler;
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    private void deregisterTransfer(String senderConsistentId, UUID transferId) {
+        ReentrantLock lock = senderConsistentIdToLock.acquire(senderConsistentId, ignored -> new ReentrantLock());
+        lock.lock();
+        try {
+            transferIdToHandler.remove(transferId);
+            Set<UUID> uuids = senderConsistentIdToTransferIds.get(senderConsistentId);
+            uuids.remove(transferId);
+            if (uuids.isEmpty()) {
+                senderConsistentIdToTransferIds.remove(senderConsistentId);
+            }
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    void cancelTransfersFromSender(String senderConsistentId) {
+        ReentrantLock lock = senderConsistentIdToLock.acquire(senderConsistentId, ignored -> new ReentrantLock());
+        lock.lock();
+        try {
+            new HashSet<>(senderConsistentIdToTransferIds.get(senderConsistentId))

Review Comment:
   I'm sorry, but why do you create a new hash set here? Also, why not using a `for ( : )` instead of streams, what are the benefits?



##########
modules/file-transfer/src/integrationTest/java/org/apache/ignite/internal/network/file/ItFileTransferTest.java:
##########
@@ -0,0 +1,336 @@
+/*
+ * 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 org.apache.ignite.internal.network.file.FileAssertions.assertContentEquals;
+import static org.apache.ignite.internal.network.file.FileGenerator.randomFile;
+import static org.apache.ignite.internal.network.file.FileUtils.sortByNames;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrow;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrowWithCauseOrSuppressed;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willTimeoutIn;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.AccessDeniedException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import org.apache.ignite.internal.configuration.testframework.InjectConfiguration;
+import org.apache.ignite.internal.network.configuration.FileTransferConfiguration;
+import org.apache.ignite.internal.network.file.TestCluster.Node;
+import org.apache.ignite.internal.network.file.exception.FileTransferException;
+import org.apache.ignite.internal.network.file.messages.Metadata;
+import org.apache.ignite.internal.network.file.messages.MetadataImpl;
+import org.apache.ignite.internal.testframework.WorkDirectory;
+import org.apache.ignite.internal.testframework.WorkDirectoryExtension;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Integration tests for file transferring.
+ */
+@ExtendWith(ConfigurationExtension.class)
+@ExtendWith(WorkDirectoryExtension.class)
+public class ItFileTransferTest {
+
+    @WorkDirectory
+    private Path workDir;
+
+    @InjectConfiguration("mock.chunkSize=1024")
+    private FileTransferConfiguration configuration;
+
+    private TestCluster cluster;
+
+    @BeforeEach
+    void setUp(TestInfo testInfo) throws InterruptedException {
+        cluster = new TestCluster(2, configuration, workDir, testInfo);
+        cluster.startAwait();
+    }
+
+    @AfterEach
+    void tearDown() throws Exception {
+        cluster.shutdown();
+    }
+
+    @Test
+    void download() throws IOException {
+        Node targetNode = cluster.members.get(0);
+
+        String unit = "unit";
+        String version = "1.0.0";
+        Path unitPath = targetNode.workDir().resolve(unit).resolve(version);
+        Files.createDirectories(unitPath);
+
+        int chunkSize = configuration.value().chunkSize();
+        File file1 = randomFile(unitPath, 0);
+        File file2 = randomFile(unitPath, chunkSize + 1);
+        File file3 = randomFile(unitPath, chunkSize * 2);
+        File file4 = randomFile(unitPath, chunkSize * 2);
+
+        targetNode.fileTransferringService().addFileProvider(
+                Metadata.class,
+                req -> completedFuture(List.of(file1, file2, file3, file4))
+        );
+
+        Node sourceNode = cluster.members.get(1);
+        CompletableFuture<List<File>> downloadedFilesFuture = sourceNode.fileTransferringService().download(
+                targetNode.nodeName(),
+                MetadataImpl.builder().build()
+        );
+
+        assertThat(
+                downloadedFilesFuture.thenAccept(files -> {
+                    assertContentEquals(sortByNames(file1, file2, file3, file4), sortByNames(files));
+                }),
+                willCompleteSuccessfully()
+        );
+    }
+
+    @Test
+    void downloadNonReadableFiles() throws IOException {
+        Node targetNode = cluster.members.get(0);
+
+        String unit = "unit";
+        String version = "1.0.0";

Review Comment:
   Could these be constants? I see these values being the same in many tests



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileSender.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BiFunction;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.network.file.exception.FileTransferException;
+import org.apache.ignite.internal.thread.NamedThreadFactory;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.network.NetworkMessage;
+
+class FileSender implements ManuallyCloseable {
+    private static final IgniteLogger LOG = Loggers.forClass(FileSender.class);
+
+    private final int chunkSize;
+
+    private final RateLimiter rateLimiter;
+
+    private final BiFunction<String, NetworkMessage, CompletableFuture<Void>> send;
+
+    private final ExecutorService executorService;
+
+    FileSender(
+            String nodeName,
+            int chunkSize,
+            int threadPoolSize,
+            RateLimiter rateLimiter,
+            BiFunction<String, NetworkMessage, CompletableFuture<Void>> send) {
+        this.send = send;
+        this.chunkSize = chunkSize;
+        this.rateLimiter = rateLimiter;
+        this.executorService = new ThreadPoolExecutor(
+                0,
+                threadPoolSize,
+                0L, TimeUnit.MILLISECONDS,
+                new LinkedBlockingQueue<Runnable>(),
+                NamedThreadFactory.create(nodeName, "file-sender", LOG)
+        );
+    }
+
+    /**
+     * Adds files to the queue to be sent to the receiver.
+     */
+    CompletableFuture<Void> send(String receiverConsistentId, UUID id, List<File> files) {
+        return CompletableFuture.runAsync(() -> send0(receiverConsistentId, id, files), executorService);
+    }
+
+    private void send0(String receiverConsistentId, UUID id, List<File> files) {
+        AtomicReference<Throwable> error = new AtomicReference<>();
+        try (FileTransferMessagesStream stream = new FileTransferMessagesStream(id, files, chunkSize)) {
+            while (stream.hasNextMessage() && error.get() == null && !Thread.currentThread().isInterrupted()) {

Review Comment:
   Is this OK that we block the sending thread with the rate limiter? Doesn't look like a good solution to me



##########
modules/file-transfer/src/integrationTest/java/org/apache/ignite/internal/network/file/ItFileTransferTest.java:
##########
@@ -0,0 +1,336 @@
+/*
+ * 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 org.apache.ignite.internal.network.file.FileAssertions.assertContentEquals;
+import static org.apache.ignite.internal.network.file.FileGenerator.randomFile;
+import static org.apache.ignite.internal.network.file.FileUtils.sortByNames;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrow;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrowWithCauseOrSuppressed;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willTimeoutIn;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.AccessDeniedException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import org.apache.ignite.internal.configuration.testframework.ConfigurationExtension;
+import org.apache.ignite.internal.configuration.testframework.InjectConfiguration;
+import org.apache.ignite.internal.network.configuration.FileTransferConfiguration;
+import org.apache.ignite.internal.network.file.TestCluster.Node;
+import org.apache.ignite.internal.network.file.exception.FileTransferException;
+import org.apache.ignite.internal.network.file.messages.Metadata;
+import org.apache.ignite.internal.network.file.messages.MetadataImpl;
+import org.apache.ignite.internal.testframework.WorkDirectory;
+import org.apache.ignite.internal.testframework.WorkDirectoryExtension;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Integration tests for file transferring.
+ */
+@ExtendWith(ConfigurationExtension.class)
+@ExtendWith(WorkDirectoryExtension.class)
+public class ItFileTransferTest {
+
+    @WorkDirectory
+    private Path workDir;
+
+    @InjectConfiguration("mock.chunkSize=1024")
+    private FileTransferConfiguration configuration;
+
+    private TestCluster cluster;
+
+    @BeforeEach
+    void setUp(TestInfo testInfo) throws InterruptedException {
+        cluster = new TestCluster(2, configuration, workDir, testInfo);
+        cluster.startAwait();
+    }
+
+    @AfterEach
+    void tearDown() throws Exception {
+        cluster.shutdown();
+    }
+
+    @Test
+    void download() throws IOException {
+        Node targetNode = cluster.members.get(0);
+
+        String unit = "unit";
+        String version = "1.0.0";
+        Path unitPath = targetNode.workDir().resolve(unit).resolve(version);
+        Files.createDirectories(unitPath);
+
+        int chunkSize = configuration.value().chunkSize();
+        File file1 = randomFile(unitPath, 0);
+        File file2 = randomFile(unitPath, chunkSize + 1);
+        File file3 = randomFile(unitPath, chunkSize * 2);
+        File file4 = randomFile(unitPath, chunkSize * 2);
+
+        targetNode.fileTransferringService().addFileProvider(
+                Metadata.class,
+                req -> completedFuture(List.of(file1, file2, file3, file4))
+        );
+
+        Node sourceNode = cluster.members.get(1);
+        CompletableFuture<List<File>> downloadedFilesFuture = sourceNode.fileTransferringService().download(
+                targetNode.nodeName(),
+                MetadataImpl.builder().build()

Review Comment:
   That's not how you receive a builder, please use a factory. That would be a good habit.
   I'm actually surprised that `Impl` classes are not package-private. They should be



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileTransferMessagesStream.java:
##########
@@ -0,0 +1,226 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Queue;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+import org.apache.ignite.internal.network.file.messages.FileChunkMessage;
+import org.apache.ignite.internal.network.file.messages.FileHeaderMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferFactory;
+import org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import org.apache.ignite.network.NetworkMessage;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Stream of messages to send files.
+ */
+public class FileTransferMessagesStream implements Iterable<NetworkMessage>, AutoCloseable {
+    private final UUID transferId;
+
+    private final Queue<File> filesToSend;
+
+    private final int chunkSize;
+
+    @Nullable
+    private ChunkedFileReader currFile;
+
+    @Nullable
+    private FileTransferInfoMessage fileTransferInfoMessage;
+
+    private final AtomicBoolean closed = new AtomicBoolean(false);
+
+    private final FileTransferFactory factory = new FileTransferFactory();
+
+    /**
+     * Creates a new stream of messages to send files.
+     *
+     * @param transferId the id of the stream.
+     * @param filesToSend the files to send. Must not be empty.
+     * @param chunkSize the size of the chunks to send. Must be positive.
+     */
+    FileTransferMessagesStream(
+            UUID transferId,
+            List<File> filesToSend,
+            int chunkSize
+    ) {
+        if (chunkSize <= 0) {
+            throw new IllegalArgumentException("Chunk size must be positive.");
+        }
+
+        if (filesToSend.isEmpty()) {
+            throw new IllegalArgumentException("Files to send must not be empty.");
+        }
+
+        this.transferId = transferId;
+        this.filesToSend = new LinkedList<>(filesToSend);

Review Comment:
   Very interesting, could you comment the reason for this choice?



##########
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");

Review Comment:
   Don't we already have a channel type 1?



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

Review Comment:
   What about other 3 parameters?



##########
modules/file-transfer/README.md:
##########
@@ -0,0 +1,23 @@
+# Ignite File Transfer
+
+This module provides a way to transfer files between the host and the target.
+
+## Usage
+
+The module provides a `org.apache.ignite.internal.network.file.FileTransferService` class that can be used to transfer files between the
+host and the target.
+
+### Metadata 
+For all files, that age going to be transferred, the metadata should be provided.

Review Comment:
   ```suggestion
   For all files, that are going to be transferred, the metadata should be provided.
   ```



##########
modules/file-transfer/src/integrationTest/java/org/apache/ignite/internal/network/file/TestCluster.java:
##########
@@ -0,0 +1,195 @@
+/*
+ * 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.stream.Collectors.toUnmodifiableList;
+import static org.apache.ignite.utils.ClusterServiceTestUtils.clusterService;
+import static org.apache.ignite.utils.ClusterServiceTestUtils.findLocalAddresses;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Stream;
+import org.apache.ignite.internal.manager.IgniteComponent;
+import org.apache.ignite.internal.network.configuration.FileTransferConfiguration;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.network.ClusterNode;
+import org.apache.ignite.network.ClusterService;
+import org.apache.ignite.network.NetworkAddress;
+import org.apache.ignite.network.NodeFinder;
+import org.apache.ignite.network.StaticNodeFinder;
+import org.apache.ignite.network.TopologyEventHandler;
+import org.junit.jupiter.api.TestInfo;
+
+/**
+ * Test cluster.
+ */
+public class TestCluster {
+    /** Startup timeout in seconds. */
+    private static final int STARTUP_TIMEOUT = 3;
+
+    /** Members of the cluster. */
+    final List<Node> members;
+
+    /** Latch that is locked until all members are visible in the topology. */
+    private final CountDownLatch startupLatch;
+
+    /** Node finder. */
+    private final NodeFinder nodeFinder;
+
+    private final FileTransferConfiguration configuration;
+
+    /**
+     * Creates a test cluster with the given amount of members.
+     *
+     * @param numOfNodes Amount of cluster members.
+     * @param testInfo Test info.
+     */
+    TestCluster(int numOfNodes, FileTransferConfiguration configuration, Path workDir, TestInfo testInfo) {
+        this.startupLatch = new CountDownLatch(numOfNodes - 1);
+
+        int initialPort = 3344;
+
+        List<NetworkAddress> addresses = findLocalAddresses(initialPort, initialPort + numOfNodes);
+
+        this.nodeFinder = new StaticNodeFinder(addresses);
+        this.configuration = configuration;
+
+        var isInitial = new AtomicBoolean(true);
+
+        this.members = addresses.stream()
+                .map(addr -> startNode(workDir, testInfo, addr, isInitial.getAndSet(false)))
+                .collect(toUnmodifiableList());
+    }
+
+    /**
+     * Start cluster node.
+     *
+     * @param testInfo Test info.
+     * @param addr Node address.
+     * @param initial Whether this node is the first one.
+     * @return Started cluster node.
+     */
+    private Node startNode(
+            Path workDir, TestInfo testInfo, NetworkAddress addr, boolean initial
+    ) {
+        ClusterService clusterSvc = clusterService(testInfo, addr.port(), nodeFinder);
+
+        if (initial) {
+            clusterSvc.topologyService().addEventHandler(new TopologyEventHandler() {
+                /** {@inheritDoc} */
+                @Override
+                public void onAppeared(ClusterNode member) {
+                    startupLatch.countDown();
+                }
+            });
+        }
+
+        try {
+            Path nodeDir = Files.createDirectory(workDir.resolve("node-" + clusterSvc.nodeName()));
+            FileTransferServiceImpl fileTransferringService = new FileTransferServiceImpl(
+                    clusterSvc.nodeName(),
+                    clusterSvc.topologyService(),
+                    clusterSvc.messagingService(),
+                    configuration,
+                    nodeDir
+            );
+            return new Node(nodeDir, clusterSvc, fileTransferringService);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Starts and waits for the cluster to come up.
+     *
+     * @throws InterruptedException If failed.
+     * @throws AssertionError If the cluster was unable to start in 3 seconds.
+     */
+    void startAwait() throws InterruptedException {
+        members.forEach(Node::start);
+
+        if (!startupLatch.await(STARTUP_TIMEOUT, TimeUnit.SECONDS)) {
+            throw new AssertionError();
+        }
+    }
+
+    /**
+     * Stops the cluster.
+     */
+    void shutdown() throws Exception {
+        for (Node member : members) {
+            member.stop();
+        }

Review Comment:
   Can we use "closeAll" here, or something similar? There's a risk of having stale node in case of exception



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/ChunkedFileReader.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+
+class ChunkedFileReader implements ManuallyCloseable {
+    private final String fileName;
+    private final RandomAccessFile raf;
+    private final int chunkSize;
+
+    private ChunkedFileReader(String fileName, RandomAccessFile raf, int chunkSize) {
+        this.fileName = fileName;
+        this.raf = raf;
+        this.chunkSize = chunkSize;
+    }
+
+    static ChunkedFileReader open(File file, int chunkSize) throws IOException {
+        RandomAccessFile raf = new RandomAccessFile(file, "r");
+        return new ChunkedFileReader(file.getName(), raf, chunkSize);

Review Comment:
   I noticed that you often don't add empty lines. Why?



##########
modules/file-transfer/src/integrationTest/java/org/apache/ignite/internal/network/file/TestCluster.java:
##########
@@ -0,0 +1,195 @@
+/*
+ * 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.stream.Collectors.toUnmodifiableList;
+import static org.apache.ignite.utils.ClusterServiceTestUtils.clusterService;
+import static org.apache.ignite.utils.ClusterServiceTestUtils.findLocalAddresses;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Stream;
+import org.apache.ignite.internal.manager.IgniteComponent;
+import org.apache.ignite.internal.network.configuration.FileTransferConfiguration;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.network.ClusterNode;
+import org.apache.ignite.network.ClusterService;
+import org.apache.ignite.network.NetworkAddress;
+import org.apache.ignite.network.NodeFinder;
+import org.apache.ignite.network.StaticNodeFinder;
+import org.apache.ignite.network.TopologyEventHandler;
+import org.junit.jupiter.api.TestInfo;
+
+/**
+ * Test cluster.
+ */
+public class TestCluster {
+    /** Startup timeout in seconds. */
+    private static final int STARTUP_TIMEOUT = 3;
+
+    /** Members of the cluster. */
+    final List<Node> members;
+
+    /** Latch that is locked until all members are visible in the topology. */
+    private final CountDownLatch startupLatch;
+
+    /** Node finder. */
+    private final NodeFinder nodeFinder;
+
+    private final FileTransferConfiguration configuration;
+
+    /**
+     * Creates a test cluster with the given amount of members.
+     *
+     * @param numOfNodes Amount of cluster members.
+     * @param testInfo Test info.
+     */
+    TestCluster(int numOfNodes, FileTransferConfiguration configuration, Path workDir, TestInfo testInfo) {
+        this.startupLatch = new CountDownLatch(numOfNodes - 1);
+
+        int initialPort = 3344;
+
+        List<NetworkAddress> addresses = findLocalAddresses(initialPort, initialPort + numOfNodes);
+
+        this.nodeFinder = new StaticNodeFinder(addresses);
+        this.configuration = configuration;
+
+        var isInitial = new AtomicBoolean(true);
+
+        this.members = addresses.stream()
+                .map(addr -> startNode(workDir, testInfo, addr, isInitial.getAndSet(false)))
+                .collect(toUnmodifiableList());
+    }
+
+    /**
+     * Start cluster node.
+     *
+     * @param testInfo Test info.
+     * @param addr Node address.
+     * @param initial Whether this node is the first one.
+     * @return Started cluster node.
+     */
+    private Node startNode(
+            Path workDir, TestInfo testInfo, NetworkAddress addr, boolean initial
+    ) {
+        ClusterService clusterSvc = clusterService(testInfo, addr.port(), nodeFinder);
+
+        if (initial) {
+            clusterSvc.topologyService().addEventHandler(new TopologyEventHandler() {
+                /** {@inheritDoc} */
+                @Override
+                public void onAppeared(ClusterNode member) {
+                    startupLatch.countDown();
+                }
+            });
+        }
+
+        try {
+            Path nodeDir = Files.createDirectory(workDir.resolve("node-" + clusterSvc.nodeName()));
+            FileTransferServiceImpl fileTransferringService = new FileTransferServiceImpl(
+                    clusterSvc.nodeName(),
+                    clusterSvc.topologyService(),
+                    clusterSvc.messagingService(),
+                    configuration,
+                    nodeDir
+            );
+            return new Node(nodeDir, clusterSvc, fileTransferringService);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Starts and waits for the cluster to come up.
+     *
+     * @throws InterruptedException If failed.
+     * @throws AssertionError If the cluster was unable to start in 3 seconds.
+     */
+    void startAwait() throws InterruptedException {
+        members.forEach(Node::start);
+
+        if (!startupLatch.await(STARTUP_TIMEOUT, TimeUnit.SECONDS)) {
+            throw new AssertionError();

Review Comment:
   Please add a message



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/ChunkedFileReader.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+
+class ChunkedFileReader implements ManuallyCloseable {
+    private final String fileName;
+    private final RandomAccessFile raf;
+    private final int chunkSize;
+
+    private ChunkedFileReader(String fileName, RandomAccessFile raf, int chunkSize) {
+        this.fileName = fileName;
+        this.raf = raf;
+        this.chunkSize = chunkSize;
+    }
+
+    static ChunkedFileReader open(File file, int chunkSize) throws IOException {
+        RandomAccessFile raf = new RandomAccessFile(file, "r");
+        return new ChunkedFileReader(file.getName(), raf, chunkSize);
+    }
+
+    byte[] readNextChunk() throws IOException {
+        int toRead = (int) Math.min(chunkSize, length() - offset());
+        byte[] data = new byte[toRead];
+        raf.read(data);
+        return data;
+    }
+
+    public String fileName() {

Review Comment:
   So, some methods are public, others are not. Class itself is not public. Maybe there should be no public methods in this class at all?



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileTransferMessagesStream.java:
##########
@@ -0,0 +1,226 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Queue;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+import org.apache.ignite.internal.network.file.messages.FileChunkMessage;
+import org.apache.ignite.internal.network.file.messages.FileHeaderMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferFactory;
+import org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import org.apache.ignite.network.NetworkMessage;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Stream of messages to send files.
+ */
+public class FileTransferMessagesStream implements Iterable<NetworkMessage>, AutoCloseable {
+    private final UUID transferId;
+
+    private final Queue<File> filesToSend;
+
+    private final int chunkSize;
+
+    @Nullable
+    private ChunkedFileReader currFile;
+
+    @Nullable
+    private FileTransferInfoMessage fileTransferInfoMessage;
+
+    private final AtomicBoolean closed = new AtomicBoolean(false);
+
+    private final FileTransferFactory factory = new FileTransferFactory();
+
+    /**
+     * Creates a new stream of messages to send files.
+     *
+     * @param transferId the id of the stream.
+     * @param filesToSend the files to send. Must not be empty.
+     * @param chunkSize the size of the chunks to send. Must be positive.
+     */
+    FileTransferMessagesStream(
+            UUID transferId,
+            List<File> filesToSend,
+            int chunkSize
+    ) {
+        if (chunkSize <= 0) {
+            throw new IllegalArgumentException("Chunk size must be positive.");
+        }
+
+        if (filesToSend.isEmpty()) {
+            throw new IllegalArgumentException("Files to send must not be empty.");
+        }
+
+        this.transferId = transferId;
+        this.filesToSend = new LinkedList<>(filesToSend);
+        this.chunkSize = chunkSize;
+        this.fileTransferInfoMessage = fileTransferInfo();
+    }
+
+    /**
+     * Returns true if there are more messages to send.
+     *
+     * @return true if there are more messages to send.
+     */
+    boolean hasNextMessage() throws IOException {
+        // check that the stream is not closed.

Review Comment:
   ```suggestion
           // Check that the stream is not closed.
   ```



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileProvider.java:
##########
@@ -0,0 +1,36 @@
+/*
+ * 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 java.io.File;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import org.apache.ignite.internal.network.file.messages.Metadata;
+
+/**
+ * Files provider.
+ */
+public interface FileProvider<M extends Metadata> {
+    /**
+     * Returns files for the given metadata. The provider must return non empty list of files.

Review Comment:
   ```suggestion
        * Returns files for the given metadata. The provider must return non-empty list of files.
   ```



##########
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)
+                            .thenCompose(ignored -> {
+                                return handler.result()
+                                        .thenCompose(files -> getFileHandler(metadata).handleUpload(metadata, files));

Review Comment:
   Is `getFileHandler(metadata)` the same as `handler` here? It's hard to follow, which one is which :)



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/ChunkedFileReader.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+
+class ChunkedFileReader implements ManuallyCloseable {

Review Comment:
   Javadoc is missing, code without documentation always looks unfinished



##########
modules/file-transfer/src/integrationTest/java/org/apache/ignite/internal/network/file/TestCluster.java:
##########
@@ -0,0 +1,195 @@
+/*
+ * 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.stream.Collectors.toUnmodifiableList;
+import static org.apache.ignite.utils.ClusterServiceTestUtils.clusterService;
+import static org.apache.ignite.utils.ClusterServiceTestUtils.findLocalAddresses;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Stream;
+import org.apache.ignite.internal.manager.IgniteComponent;
+import org.apache.ignite.internal.network.configuration.FileTransferConfiguration;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.network.ClusterNode;
+import org.apache.ignite.network.ClusterService;
+import org.apache.ignite.network.NetworkAddress;
+import org.apache.ignite.network.NodeFinder;
+import org.apache.ignite.network.StaticNodeFinder;
+import org.apache.ignite.network.TopologyEventHandler;
+import org.junit.jupiter.api.TestInfo;
+
+/**
+ * Test cluster.
+ */
+public class TestCluster {
+    /** Startup timeout in seconds. */
+    private static final int STARTUP_TIMEOUT = 3;

Review Comment:
   How stable is this time?



##########
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)
+                            .thenCompose(ignored -> {
+                                return handler.result()
+                                        .thenCompose(files -> getFileHandler(metadata).handleUpload(metadata, files));
+                            })
+                            .whenComplete((v, e) -> {
+                                if (e != null) {
+                                    LOG.error(
+                                            "Failed to handle file upload. Transfer ID: {}. Metadata: {}. Exception: {}",
+                                            transferId,
+                                            metadata,
+                                            e
+                                    );
+                                }
+                                deleteDirectoryIfExists(directory);
+                            });
+                });
+    }
+
+    private void processDownloadRequest(FileDownloadRequest message, String senderConsistentId, Long correlationId) {
+        supplyAsync(() -> getFileProvider(message.metadata()))
+                .thenCompose(provider -> provider.files(message.metadata()))
+                .whenComplete((files, e) -> {
+                    if (e != null) {
+                        LOG.error("Failed to get files for download. Metadata: {}", message.metadata(), e);
+                        FileDownloadResponse response = factory.fileDownloadResponse()

Review Comment:
   I will ask you one more time here to use more empty lines in your code. I didn't write this comment every time. Code this dense is difficult to read and it doesn't look like the rest of Ignite code. Please fix it everywhere in your code.



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileReceiver.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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 java.nio.file.Path;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantLock;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+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.FileHeaderMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferErrorMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import org.apache.ignite.internal.thread.NamedThreadFactory;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.internal.util.RefCountedObjectPool;
+
+/**
+ * File receiver.
+ */
+class FileReceiver implements ManuallyCloseable {
+    private static final IgniteLogger LOG = Loggers.forClass(FileReceiver.class);
+
+    private final ExecutorService executorService;
+
+    private final Map<UUID, FileTransferMessagesHandler> transferIdToHandler = new ConcurrentHashMap<>();
+
+    private final Map<String, Set<UUID>> senderConsistentIdToTransferIds = new ConcurrentHashMap<>();
+
+    private final RefCountedObjectPool<String, ReentrantLock> senderConsistentIdToLock = new RefCountedObjectPool<>();
+
+    FileReceiver(String nodeName, int threadPoolSize) {
+        this.executorService = new ThreadPoolExecutor(

Review Comment:
   Is there a matching method in `Executors` class?



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileReceiver.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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 java.nio.file.Path;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantLock;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+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.FileHeaderMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferErrorMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import org.apache.ignite.internal.thread.NamedThreadFactory;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.internal.util.RefCountedObjectPool;
+
+/**
+ * File receiver.
+ */
+class FileReceiver implements ManuallyCloseable {
+    private static final IgniteLogger LOG = Loggers.forClass(FileReceiver.class);
+
+    private final ExecutorService executorService;
+
+    private final Map<UUID, FileTransferMessagesHandler> transferIdToHandler = new ConcurrentHashMap<>();
+
+    private final Map<String, Set<UUID>> senderConsistentIdToTransferIds = new ConcurrentHashMap<>();
+
+    private final RefCountedObjectPool<String, ReentrantLock> senderConsistentIdToLock = new RefCountedObjectPool<>();
+
+    FileReceiver(String nodeName, int threadPoolSize) {
+        this.executorService = new ThreadPoolExecutor(
+                0,
+                threadPoolSize,
+                0L, TimeUnit.MILLISECONDS,
+                new LinkedBlockingQueue<Runnable>(),
+                NamedThreadFactory.create(nodeName, "file-receiver", LOG)
+        );
+    }
+
+    FileTransferMessagesHandler registerTransfer(String senderConsistentId, UUID transferId, Path handlerDir) {
+        ReentrantLock lock = senderConsistentIdToLock.acquire(senderConsistentId, ignored -> new ReentrantLock());
+        lock.lock();
+        try {
+            FileTransferMessagesHandler handler = new FileTransferMessagesHandler(handlerDir);
+            transferIdToHandler.put(transferId, handler);
+            senderConsistentIdToTransferIds.compute(senderConsistentId, (k, v) -> {
+                if (v == null) {
+                    v = new HashSet<>();
+                }
+                v.add(transferId);
+                return v;
+            });
+            handler.result().whenComplete((files, throwable) -> deregisterTransfer(senderConsistentId, transferId));
+            return handler;
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    private void deregisterTransfer(String senderConsistentId, UUID transferId) {
+        ReentrantLock lock = senderConsistentIdToLock.acquire(senderConsistentId, ignored -> new ReentrantLock());
+        lock.lock();
+        try {
+            transferIdToHandler.remove(transferId);
+            Set<UUID> uuids = senderConsistentIdToTransferIds.get(senderConsistentId);
+            uuids.remove(transferId);
+            if (uuids.isEmpty()) {
+                senderConsistentIdToTransferIds.remove(senderConsistentId);
+            }

Review Comment:
   In previous method you use `compute`, but here you use get/remove. What's the reason for discrepancy?



##########
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)
+                            .thenCompose(ignored -> {
+                                return handler.result()
+                                        .thenCompose(files -> getFileHandler(metadata).handleUpload(metadata, files));
+                            })
+                            .whenComplete((v, e) -> {
+                                if (e != null) {
+                                    LOG.error(
+                                            "Failed to handle file upload. Transfer ID: {}. Metadata: {}. Exception: {}",
+                                            transferId,
+                                            metadata,
+                                            e
+                                    );
+                                }
+                                deleteDirectoryIfExists(directory);
+                            });
+                });
+    }
+
+    private void processDownloadRequest(FileDownloadRequest message, String senderConsistentId, Long correlationId) {
+        supplyAsync(() -> getFileProvider(message.metadata()))
+                .thenCompose(provider -> provider.files(message.metadata()))
+                .whenComplete((files, e) -> {
+                    if (e != null) {
+                        LOG.error("Failed to get files for download. Metadata: {}", message.metadata(), e);
+                        FileDownloadResponse response = factory.fileDownloadResponse()
+                                .error(unwrapFileTransferException(e).getMessage())
+                                .build();
+                        messagingService.respond(senderConsistentId, FILE_TRANSFERRING_CHANNEL, response, correlationId);
+                    } else if (files.isEmpty()) {
+                        LOG.warn("No files to download. Metadata: {}", message.metadata());
+                        FileDownloadResponse response = factory.fileDownloadResponse()
+                                .error("No files to download")
+                                .build();
+                        messagingService.respond(senderConsistentId, FILE_TRANSFERRING_CHANNEL, response, correlationId);
+                    } else {
+                        FileDownloadResponse response = factory.fileDownloadResponse().build();
+                        messagingService.respond(senderConsistentId, FILE_TRANSFERRING_CHANNEL, response, correlationId)
+                                .thenCompose(v -> sendFiles(senderConsistentId, message.transferId(), files));
+                    }
+                });
+    }
+
+    private CompletableFuture<Void> sendFiles(String recipientConsistentId, UUID transferId, List<File> files) {
+        return fileSender.send(recipientConsistentId, transferId, files)
+                .whenComplete((v, e) -> {
+                    if (e != null) {
+                        LOG.error("Failed to send files to node: {}, transfer id: {}. Exception: {}",
+                                recipientConsistentId,
+                                transferId,
+                                e
+                        );
+                        FileTransferErrorMessage message = factory.fileTransferErrorMessage()
+                                .transferId(transferId)
+                                .error(unwrapFileTransferException(e).getMessage())
+                                .build();
+                        messagingService.send(recipientConsistentId, FILE_TRANSFERRING_CHANNEL, message);
+                    }
+                });
+    }
+
+    private void processFileTransferInfo(FileTransferInfoMessage info) {
+        fileReceiver.receiveFileTransferInfo(info);
+    }
+
+    private void processFileHeader(FileHeaderMessage header) {
+        fileReceiver.receiveFileHeader(header);
+    }
+
+    private void processFileChunk(FileChunkMessage fileChunk) {
+        fileReceiver.receiveFileChunk(fileChunk);
+    }
+
+    @Override
+    public void stop() throws Exception {
+        IgniteUtils.closeAllManually(fileSender, fileReceiver);
+    }
+
+    @Override
+    public <M extends Metadata> void addFileProvider(
+            Class<M> metadata,
+            FileProvider<M> provider
+    ) {
+        metadataToProvider.compute(
+                getMessageType(metadata),
+                (k, v) -> {
+                    if (v != null) {
+                        throw new IllegalArgumentException("File provider for metadata " + metadata.getName() + " already exists");
+                    }
+                    return (FileProvider<Metadata>) provider;
+                }
+        );
+    }
+
+    @Override
+    public <M extends Metadata> void addFileHandler(
+            Class<M> metadata,
+            FileConsumer<M> handler
+    ) {
+        metadataToHandler.compute(
+                getMessageType(metadata),
+                (k, v) -> {
+                    if (v != null) {
+                        throw new IllegalArgumentException("File handler for metadata " + metadata.getName() + " already exists");
+                    }
+                    return (FileConsumer<Metadata>) handler;
+                }
+        );
+    }
+
+    @Override
+    public CompletableFuture<List<File>> download(String nodeConsistentId, Metadata metadata) {
+        return completedFuture(UUID.randomUUID())
+                .thenApply(transferId -> factory.fileDownloadRequest()
+                        .transferId(transferId)
+                        .metadata(metadata)
+                        .build())
+                .thenCompose(message -> {
+                    Path directory = createTransferDirectory(message.transferId());
+                    FileTransferMessagesHandler handler = fileReceiver.registerTransfer(nodeConsistentId, message.transferId(), directory);
+                    return messagingService.invoke(nodeConsistentId, FILE_TRANSFERRING_CHANNEL, message,
+                                    RESPONSE_TIMEOUT)
+                            .thenApply(FileDownloadResponse.class::cast)
+                            .thenCompose(response -> {
+                                if (response.error() != null) {
+                                    return failedFuture(new FileTransferException(response.error()));
+                                } else {
+                                    return handler.result();
+                                }
+                            });
+                });
+    }
+
+    @Override
+    public CompletableFuture<Void> upload(String nodeConsistentId, Metadata metadata) {
+        return supplyAsync(() -> getFileProvider(metadata))
+                .thenCompose(provider -> provider.files(metadata))
+                .thenCompose(files -> {
+                    FileUploadRequest message = factory.fileUploadRequest()
+                            .metadata(metadata)
+                            .build();
+
+                    if (files.isEmpty()) {
+                        return failedFuture(new FileTransferException("No files to upload"));
+                    } else {
+                        return messagingService.invoke(nodeConsistentId, FILE_TRANSFERRING_CHANNEL, message, RESPONSE_TIMEOUT)
+                                .thenApply(FileUploadResponse.class::cast)
+                                .thenCompose(response -> sendFiles(nodeConsistentId, response.transferId(), files));
+                    }
+                });
+    }
+
+    private static short getMessageType(Class<?> metadata) {
+        Transferable annotation = metadata.getAnnotation(Transferable.class);
+        if (annotation == null) {
+            throw new IllegalArgumentException("Class " + metadata.getName() + " is not annotated with @Transferable");
+        }
+        return annotation.value();
+    }
+
+    private <M extends Metadata> FileProvider<M> getFileProvider(M metadata) {
+        FileProvider<Metadata> provider = metadataToProvider.get(metadata.messageType());
+        if (provider == null) {
+            throw new FileProviderNotFoundException(metadata.getClass());
+        } else {
+            return (FileProvider<M>) provider;
+        }
+    }
+
+    private <M extends Metadata> FileConsumer<M> getFileHandler(M metadata) {
+        FileConsumer<Metadata> handler = metadataToHandler.get(metadata.messageType());
+        if (handler == null) {
+            throw new FileHandlerNotFoundException(metadata.getClass());
+        } else {
+            return (FileConsumer<M>) handler;
+        }
+    }
+
+    private Path createTransferDirectory(UUID transferId) {
+        try {
+            return Files.createDirectories(tempDirectory.resolve(transferId.toString()));

Review Comment:
   Why do we use temp directories? Can't we ask handler for the destination?



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileConsumer.java:
##########
@@ -0,0 +1,37 @@
+/*
+ * 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 java.io.File;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import org.apache.ignite.internal.network.file.messages.Metadata;
+
+/**
+ * Handler for the uploaded file.
+ */
+public interface FileConsumer<M extends Metadata> {
+    /**
+     * Handles the uploaded files.
+     *
+     * @param metadata The metadata of the uploaded file.
+     * @param uploadedFiles The temporary files that were uploaded. These files will be deleted after the method returns.

Review Comment:
   The "temporary files" thing must be clarified. Does the uploader just write the data into arbitrary temp directory?



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/exception/FileProviderNotFoundException.java:
##########
@@ -0,0 +1,36 @@
+/*
+ * 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.exception;
+
+import org.apache.ignite.internal.network.file.messages.Metadata;
+import org.apache.ignite.lang.ErrorGroups.Network;
+import org.apache.ignite.lang.IgniteException;
+
+/**
+ * Exception thrown when file provider is not found for a metadata message.
+ */
+public class FileProviderNotFoundException extends IgniteException {
+    /**
+     * Constructor.
+     *
+     * @param message Message.
+     */
+    public FileProviderNotFoundException(Class<? extends Metadata> message) {
+        super(Network.FILE_PROVIDER_NOT_FOUND_ERR, "File provider not found for message: " + message.getName());

Review Comment:
   How is this a public exception for the user? Missing handler is a bug in the product, not an API misuse. Please remove these error codes from the "api" module



##########
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)
+                            .thenCompose(ignored -> {
+                                return handler.result()
+                                        .thenCompose(files -> getFileHandler(metadata).handleUpload(metadata, files));
+                            })
+                            .whenComplete((v, e) -> {
+                                if (e != null) {
+                                    LOG.error(
+                                            "Failed to handle file upload. Transfer ID: {}. Metadata: {}. Exception: {}",
+                                            transferId,
+                                            metadata,
+                                            e
+                                    );
+                                }
+                                deleteDirectoryIfExists(directory);
+                            });
+                });
+    }
+
+    private void processDownloadRequest(FileDownloadRequest message, String senderConsistentId, Long correlationId) {
+        supplyAsync(() -> getFileProvider(message.metadata()))
+                .thenCompose(provider -> provider.files(message.metadata()))
+                .whenComplete((files, e) -> {
+                    if (e != null) {
+                        LOG.error("Failed to get files for download. Metadata: {}", message.metadata(), e);
+                        FileDownloadResponse response = factory.fileDownloadResponse()
+                                .error(unwrapFileTransferException(e).getMessage())
+                                .build();
+                        messagingService.respond(senderConsistentId, FILE_TRANSFERRING_CHANNEL, response, correlationId);
+                    } else if (files.isEmpty()) {
+                        LOG.warn("No files to download. Metadata: {}", message.metadata());
+                        FileDownloadResponse response = factory.fileDownloadResponse()
+                                .error("No files to download")
+                                .build();
+                        messagingService.respond(senderConsistentId, FILE_TRANSFERRING_CHANNEL, response, correlationId);
+                    } else {
+                        FileDownloadResponse response = factory.fileDownloadResponse().build();
+                        messagingService.respond(senderConsistentId, FILE_TRANSFERRING_CHANNEL, response, correlationId)
+                                .thenCompose(v -> sendFiles(senderConsistentId, message.transferId(), files));
+                    }
+                });
+    }
+
+    private CompletableFuture<Void> sendFiles(String recipientConsistentId, UUID transferId, List<File> files) {
+        return fileSender.send(recipientConsistentId, transferId, files)
+                .whenComplete((v, e) -> {
+                    if (e != null) {
+                        LOG.error("Failed to send files to node: {}, transfer id: {}. Exception: {}",
+                                recipientConsistentId,
+                                transferId,
+                                e
+                        );
+                        FileTransferErrorMessage message = factory.fileTransferErrorMessage()
+                                .transferId(transferId)
+                                .error(unwrapFileTransferException(e).getMessage())
+                                .build();
+                        messagingService.send(recipientConsistentId, FILE_TRANSFERRING_CHANNEL, message);
+                    }
+                });
+    }
+
+    private void processFileTransferInfo(FileTransferInfoMessage info) {

Review Comment:
   Interesting, why not just inline these single-liners?



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileReceiver.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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 java.nio.file.Path;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantLock;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+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.FileHeaderMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferErrorMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import org.apache.ignite.internal.thread.NamedThreadFactory;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.internal.util.RefCountedObjectPool;
+
+/**
+ * File receiver.
+ */
+class FileReceiver implements ManuallyCloseable {
+    private static final IgniteLogger LOG = Loggers.forClass(FileReceiver.class);
+
+    private final ExecutorService executorService;
+
+    private final Map<UUID, FileTransferMessagesHandler> transferIdToHandler = new ConcurrentHashMap<>();
+
+    private final Map<String, Set<UUID>> senderConsistentIdToTransferIds = new ConcurrentHashMap<>();
+
+    private final RefCountedObjectPool<String, ReentrantLock> senderConsistentIdToLock = new RefCountedObjectPool<>();
+
+    FileReceiver(String nodeName, int threadPoolSize) {
+        this.executorService = new ThreadPoolExecutor(
+                0,
+                threadPoolSize,
+                0L, TimeUnit.MILLISECONDS,
+                new LinkedBlockingQueue<Runnable>(),
+                NamedThreadFactory.create(nodeName, "file-receiver", LOG)
+        );
+    }
+
+    FileTransferMessagesHandler registerTransfer(String senderConsistentId, UUID transferId, Path handlerDir) {
+        ReentrantLock lock = senderConsistentIdToLock.acquire(senderConsistentId, ignored -> new ReentrantLock());
+        lock.lock();
+        try {
+            FileTransferMessagesHandler handler = new FileTransferMessagesHandler(handlerDir);
+            transferIdToHandler.put(transferId, handler);
+            senderConsistentIdToTransferIds.compute(senderConsistentId, (k, v) -> {
+                if (v == null) {
+                    v = new HashSet<>();
+                }
+                v.add(transferId);
+                return v;
+            });
+            handler.result().whenComplete((files, throwable) -> deregisterTransfer(senderConsistentId, transferId));
+            return handler;
+        } finally {
+            lock.unlock();
+        }
+    }

Review Comment:
   Where are empty lines? This is very hard to read, and it violates code style guidelines. Please format your code properly



##########
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)
+                            .thenCompose(ignored -> {
+                                return handler.result()
+                                        .thenCompose(files -> getFileHandler(metadata).handleUpload(metadata, files));
+                            })
+                            .whenComplete((v, e) -> {
+                                if (e != null) {
+                                    LOG.error(
+                                            "Failed to handle file upload. Transfer ID: {}. Metadata: {}. Exception: {}",
+                                            transferId,
+                                            metadata,
+                                            e
+                                    );
+                                }
+                                deleteDirectoryIfExists(directory);
+                            });
+                });
+    }
+
+    private void processDownloadRequest(FileDownloadRequest message, String senderConsistentId, Long correlationId) {
+        supplyAsync(() -> getFileProvider(message.metadata()))
+                .thenCompose(provider -> provider.files(message.metadata()))
+                .whenComplete((files, e) -> {
+                    if (e != null) {
+                        LOG.error("Failed to get files for download. Metadata: {}", message.metadata(), e);
+                        FileDownloadResponse response = factory.fileDownloadResponse()
+                                .error(unwrapFileTransferException(e).getMessage())
+                                .build();
+                        messagingService.respond(senderConsistentId, FILE_TRANSFERRING_CHANNEL, response, correlationId);
+                    } else if (files.isEmpty()) {
+                        LOG.warn("No files to download. Metadata: {}", message.metadata());
+                        FileDownloadResponse response = factory.fileDownloadResponse()
+                                .error("No files to download")
+                                .build();
+                        messagingService.respond(senderConsistentId, FILE_TRANSFERRING_CHANNEL, response, correlationId);
+                    } else {
+                        FileDownloadResponse response = factory.fileDownloadResponse().build();
+                        messagingService.respond(senderConsistentId, FILE_TRANSFERRING_CHANNEL, response, correlationId)
+                                .thenCompose(v -> sendFiles(senderConsistentId, message.transferId(), files));
+                    }
+                });
+    }
+
+    private CompletableFuture<Void> sendFiles(String recipientConsistentId, UUID transferId, List<File> files) {
+        return fileSender.send(recipientConsistentId, transferId, files)
+                .whenComplete((v, e) -> {
+                    if (e != null) {
+                        LOG.error("Failed to send files to node: {}, transfer id: {}. Exception: {}",
+                                recipientConsistentId,
+                                transferId,
+                                e
+                        );
+                        FileTransferErrorMessage message = factory.fileTransferErrorMessage()
+                                .transferId(transferId)
+                                .error(unwrapFileTransferException(e).getMessage())
+                                .build();
+                        messagingService.send(recipientConsistentId, FILE_TRANSFERRING_CHANNEL, message);
+                    }
+                });
+    }
+
+    private void processFileTransferInfo(FileTransferInfoMessage info) {
+        fileReceiver.receiveFileTransferInfo(info);
+    }
+
+    private void processFileHeader(FileHeaderMessage header) {
+        fileReceiver.receiveFileHeader(header);
+    }
+
+    private void processFileChunk(FileChunkMessage fileChunk) {
+        fileReceiver.receiveFileChunk(fileChunk);
+    }
+
+    @Override
+    public void stop() throws Exception {
+        IgniteUtils.closeAllManually(fileSender, fileReceiver);
+    }
+
+    @Override
+    public <M extends Metadata> void addFileProvider(
+            Class<M> metadata,
+            FileProvider<M> provider
+    ) {
+        metadataToProvider.compute(
+                getMessageType(metadata),
+                (k, v) -> {
+                    if (v != null) {
+                        throw new IllegalArgumentException("File provider for metadata " + metadata.getName() + " already exists");
+                    }
+                    return (FileProvider<Metadata>) provider;
+                }
+        );
+    }
+
+    @Override
+    public <M extends Metadata> void addFileHandler(
+            Class<M> metadata,
+            FileConsumer<M> handler
+    ) {
+        metadataToHandler.compute(
+                getMessageType(metadata),
+                (k, v) -> {
+                    if (v != null) {
+                        throw new IllegalArgumentException("File handler for metadata " + metadata.getName() + " already exists");
+                    }
+                    return (FileConsumer<Metadata>) handler;
+                }
+        );
+    }
+
+    @Override
+    public CompletableFuture<List<File>> download(String nodeConsistentId, Metadata metadata) {
+        return completedFuture(UUID.randomUUID())
+                .thenApply(transferId -> factory.fileDownloadRequest()
+                        .transferId(transferId)
+                        .metadata(metadata)
+                        .build())
+                .thenCompose(message -> {
+                    Path directory = createTransferDirectory(message.transferId());
+                    FileTransferMessagesHandler handler = fileReceiver.registerTransfer(nodeConsistentId, message.transferId(), directory);
+                    return messagingService.invoke(nodeConsistentId, FILE_TRANSFERRING_CHANNEL, message,
+                                    RESPONSE_TIMEOUT)
+                            .thenApply(FileDownloadResponse.class::cast)
+                            .thenCompose(response -> {
+                                if (response.error() != null) {
+                                    return failedFuture(new FileTransferException(response.error()));
+                                } else {
+                                    return handler.result();
+                                }
+                            });
+                });
+    }
+
+    @Override
+    public CompletableFuture<Void> upload(String nodeConsistentId, Metadata metadata) {
+        return supplyAsync(() -> getFileProvider(metadata))
+                .thenCompose(provider -> provider.files(metadata))
+                .thenCompose(files -> {
+                    FileUploadRequest message = factory.fileUploadRequest()
+                            .metadata(metadata)
+                            .build();
+
+                    if (files.isEmpty()) {
+                        return failedFuture(new FileTransferException("No files to upload"));
+                    } else {
+                        return messagingService.invoke(nodeConsistentId, FILE_TRANSFERRING_CHANNEL, message, RESPONSE_TIMEOUT)
+                                .thenApply(FileUploadResponse.class::cast)
+                                .thenCompose(response -> sendFiles(nodeConsistentId, response.transferId(), files));
+                    }
+                });
+    }
+
+    private static short getMessageType(Class<?> metadata) {

Review Comment:
   Maybe we should register handlers by the numeric message types, rather than classes? Cause it looks a bit hacky right now



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/RateLimiter.java:
##########
@@ -0,0 +1,46 @@
+/*
+ * 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;
+
+class RateLimiter {

Review Comment:
   Why don't you use a semaphore instead?



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileSender.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BiFunction;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.network.file.exception.FileTransferException;
+import org.apache.ignite.internal.thread.NamedThreadFactory;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.network.NetworkMessage;
+
+class FileSender implements ManuallyCloseable {
+    private static final IgniteLogger LOG = Loggers.forClass(FileSender.class);
+
+    private final int chunkSize;
+
+    private final RateLimiter rateLimiter;
+
+    private final BiFunction<String, NetworkMessage, CompletableFuture<Void>> send;
+
+    private final ExecutorService executorService;
+
+    FileSender(
+            String nodeName,
+            int chunkSize,
+            int threadPoolSize,
+            RateLimiter rateLimiter,
+            BiFunction<String, NetworkMessage, CompletableFuture<Void>> send) {

Review Comment:
   Why not just providing a messaging service here?



##########
modules/file-transfer/src/integrationTest/java/org/apache/ignite/internal/network/file/TestCluster.java:
##########
@@ -0,0 +1,195 @@
+/*
+ * 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.stream.Collectors.toUnmodifiableList;
+import static org.apache.ignite.utils.ClusterServiceTestUtils.clusterService;
+import static org.apache.ignite.utils.ClusterServiceTestUtils.findLocalAddresses;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Stream;
+import org.apache.ignite.internal.manager.IgniteComponent;
+import org.apache.ignite.internal.network.configuration.FileTransferConfiguration;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.network.ClusterNode;
+import org.apache.ignite.network.ClusterService;
+import org.apache.ignite.network.NetworkAddress;
+import org.apache.ignite.network.NodeFinder;
+import org.apache.ignite.network.StaticNodeFinder;
+import org.apache.ignite.network.TopologyEventHandler;
+import org.junit.jupiter.api.TestInfo;
+
+/**
+ * Test cluster.
+ */
+public class TestCluster {
+    /** Startup timeout in seconds. */
+    private static final int STARTUP_TIMEOUT = 3;
+
+    /** Members of the cluster. */
+    final List<Node> members;
+
+    /** Latch that is locked until all members are visible in the topology. */
+    private final CountDownLatch startupLatch;
+
+    /** Node finder. */
+    private final NodeFinder nodeFinder;
+
+    private final FileTransferConfiguration configuration;
+
+    /**
+     * Creates a test cluster with the given amount of members.
+     *
+     * @param numOfNodes Amount of cluster members.
+     * @param testInfo Test info.
+     */
+    TestCluster(int numOfNodes, FileTransferConfiguration configuration, Path workDir, TestInfo testInfo) {
+        this.startupLatch = new CountDownLatch(numOfNodes - 1);
+
+        int initialPort = 3344;
+
+        List<NetworkAddress> addresses = findLocalAddresses(initialPort, initialPort + numOfNodes);
+
+        this.nodeFinder = new StaticNodeFinder(addresses);
+        this.configuration = configuration;
+
+        var isInitial = new AtomicBoolean(true);
+
+        this.members = addresses.stream()
+                .map(addr -> startNode(workDir, testInfo, addr, isInitial.getAndSet(false)))
+                .collect(toUnmodifiableList());
+    }
+
+    /**
+     * Start cluster node.
+     *
+     * @param testInfo Test info.
+     * @param addr Node address.
+     * @param initial Whether this node is the first one.
+     * @return Started cluster node.
+     */
+    private Node startNode(
+            Path workDir, TestInfo testInfo, NetworkAddress addr, boolean initial
+    ) {
+        ClusterService clusterSvc = clusterService(testInfo, addr.port(), nodeFinder);
+
+        if (initial) {
+            clusterSvc.topologyService().addEventHandler(new TopologyEventHandler() {
+                /** {@inheritDoc} */

Review Comment:
   This comment it not required. I don't mind having it, just wanted to mention that



##########
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)
+                            .thenCompose(ignored -> {
+                                return handler.result()
+                                        .thenCompose(files -> getFileHandler(metadata).handleUpload(metadata, files));
+                            })
+                            .whenComplete((v, e) -> {
+                                if (e != null) {
+                                    LOG.error(
+                                            "Failed to handle file upload. Transfer ID: {}. Metadata: {}. Exception: {}",
+                                            transferId,
+                                            metadata,
+                                            e
+                                    );
+                                }
+                                deleteDirectoryIfExists(directory);
+                            });
+                });
+    }
+
+    private void processDownloadRequest(FileDownloadRequest message, String senderConsistentId, Long correlationId) {
+        supplyAsync(() -> getFileProvider(message.metadata()))
+                .thenCompose(provider -> provider.files(message.metadata()))
+                .whenComplete((files, e) -> {
+                    if (e != null) {
+                        LOG.error("Failed to get files for download. Metadata: {}", message.metadata(), e);
+                        FileDownloadResponse response = factory.fileDownloadResponse()
+                                .error(unwrapFileTransferException(e).getMessage())
+                                .build();
+                        messagingService.respond(senderConsistentId, FILE_TRANSFERRING_CHANNEL, response, correlationId);
+                    } else if (files.isEmpty()) {
+                        LOG.warn("No files to download. Metadata: {}", message.metadata());
+                        FileDownloadResponse response = factory.fileDownloadResponse()
+                                .error("No files to download")
+                                .build();
+                        messagingService.respond(senderConsistentId, FILE_TRANSFERRING_CHANNEL, response, correlationId);
+                    } else {
+                        FileDownloadResponse response = factory.fileDownloadResponse().build();
+                        messagingService.respond(senderConsistentId, FILE_TRANSFERRING_CHANNEL, response, correlationId)
+                                .thenCompose(v -> sendFiles(senderConsistentId, message.transferId(), files));
+                    }
+                });
+    }
+
+    private CompletableFuture<Void> sendFiles(String recipientConsistentId, UUID transferId, List<File> files) {
+        return fileSender.send(recipientConsistentId, transferId, files)
+                .whenComplete((v, e) -> {
+                    if (e != null) {
+                        LOG.error("Failed to send files to node: {}, transfer id: {}. Exception: {}",
+                                recipientConsistentId,
+                                transferId,
+                                e
+                        );
+                        FileTransferErrorMessage message = factory.fileTransferErrorMessage()
+                                .transferId(transferId)
+                                .error(unwrapFileTransferException(e).getMessage())
+                                .build();
+                        messagingService.send(recipientConsistentId, FILE_TRANSFERRING_CHANNEL, message);
+                    }
+                });
+    }
+
+    private void processFileTransferInfo(FileTransferInfoMessage info) {
+        fileReceiver.receiveFileTransferInfo(info);
+    }
+
+    private void processFileHeader(FileHeaderMessage header) {
+        fileReceiver.receiveFileHeader(header);
+    }
+
+    private void processFileChunk(FileChunkMessage fileChunk) {
+        fileReceiver.receiveFileChunk(fileChunk);
+    }
+
+    @Override
+    public void stop() throws Exception {
+        IgniteUtils.closeAllManually(fileSender, fileReceiver);
+    }
+
+    @Override
+    public <M extends Metadata> void addFileProvider(
+            Class<M> metadata,
+            FileProvider<M> provider
+    ) {
+        metadataToProvider.compute(
+                getMessageType(metadata),
+                (k, v) -> {
+                    if (v != null) {
+                        throw new IllegalArgumentException("File provider for metadata " + metadata.getName() + " already exists");
+                    }
+                    return (FileProvider<Metadata>) provider;
+                }
+        );
+    }
+
+    @Override
+    public <M extends Metadata> void addFileHandler(
+            Class<M> metadata,
+            FileConsumer<M> handler
+    ) {
+        metadataToHandler.compute(
+                getMessageType(metadata),
+                (k, v) -> {
+                    if (v != null) {
+                        throw new IllegalArgumentException("File handler for metadata " + metadata.getName() + " already exists");
+                    }
+                    return (FileConsumer<Metadata>) handler;
+                }
+        );
+    }
+
+    @Override
+    public CompletableFuture<List<File>> download(String nodeConsistentId, Metadata metadata) {
+        return completedFuture(UUID.randomUUID())
+                .thenApply(transferId -> factory.fileDownloadRequest()
+                        .transferId(transferId)
+                        .metadata(metadata)
+                        .build())
+                .thenCompose(message -> {
+                    Path directory = createTransferDirectory(message.transferId());
+                    FileTransferMessagesHandler handler = fileReceiver.registerTransfer(nodeConsistentId, message.transferId(), directory);
+                    return messagingService.invoke(nodeConsistentId, FILE_TRANSFERRING_CHANNEL, message,
+                                    RESPONSE_TIMEOUT)
+                            .thenApply(FileDownloadResponse.class::cast)
+                            .thenCompose(response -> {
+                                if (response.error() != null) {
+                                    return failedFuture(new FileTransferException(response.error()));
+                                } else {
+                                    return handler.result();
+                                }
+                            });
+                });
+    }
+
+    @Override
+    public CompletableFuture<Void> upload(String nodeConsistentId, Metadata metadata) {
+        return supplyAsync(() -> getFileProvider(metadata))
+                .thenCompose(provider -> provider.files(metadata))
+                .thenCompose(files -> {
+                    FileUploadRequest message = factory.fileUploadRequest()
+                            .metadata(metadata)
+                            .build();
+
+                    if (files.isEmpty()) {
+                        return failedFuture(new FileTransferException("No files to upload"));
+                    } else {
+                        return messagingService.invoke(nodeConsistentId, FILE_TRANSFERRING_CHANNEL, message, RESPONSE_TIMEOUT)
+                                .thenApply(FileUploadResponse.class::cast)
+                                .thenCompose(response -> sendFiles(nodeConsistentId, response.transferId(), files));
+                    }
+                });
+    }
+
+    private static short getMessageType(Class<?> metadata) {
+        Transferable annotation = metadata.getAnnotation(Transferable.class);
+        if (annotation == null) {
+            throw new IllegalArgumentException("Class " + metadata.getName() + " is not annotated with @Transferable");
+        }
+        return annotation.value();
+    }
+
+    private <M extends Metadata> FileProvider<M> getFileProvider(M metadata) {
+        FileProvider<Metadata> provider = metadataToProvider.get(metadata.messageType());
+        if (provider == null) {
+            throw new FileProviderNotFoundException(metadata.getClass());
+        } else {
+            return (FileProvider<M>) provider;
+        }
+    }
+
+    private <M extends Metadata> FileConsumer<M> getFileHandler(M metadata) {
+        FileConsumer<Metadata> handler = metadataToHandler.get(metadata.messageType());
+        if (handler == null) {
+            throw new FileHandlerNotFoundException(metadata.getClass());
+        } else {
+            return (FileConsumer<M>) handler;
+        }
+    }
+
+    private Path createTransferDirectory(UUID transferId) {
+        try {
+            return Files.createDirectories(tempDirectory.resolve(transferId.toString()));
+        } catch (IOException e) {
+            throw new FileTransferException("Failed to create transfer directory. Transfer id: {}. Exception: {}" + transferId, e);
+        }
+    }
+
+    private static void deleteDirectoryIfExists(Path directory) {
+        try {
+            FilesUtils.deleteDirectoryIfExists(directory);
+        } catch (IOException e) {
+            LOG.error("Failed to delete directory: {}. Exception: {}", directory, e);
+        }
+    }
+
+    private static Throwable unwrapFileTransferException(Throwable throwable) {
+        Throwable unwrapped = ExceptionUtils.unwrapCause(throwable);
+        if (unwrapped instanceof FileTransferException) {
+            return unwrapped.getCause() == null ? unwrapped : unwrapped.getCause();

Review Comment:
   Wow, why so complicated? I would love to have a comment here, or a simpler solution when it comes to exceptions



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileSender.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BiFunction;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.network.file.exception.FileTransferException;
+import org.apache.ignite.internal.thread.NamedThreadFactory;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.network.NetworkMessage;
+
+class FileSender implements ManuallyCloseable {
+    private static final IgniteLogger LOG = Loggers.forClass(FileSender.class);
+
+    private final int chunkSize;
+
+    private final RateLimiter rateLimiter;
+
+    private final BiFunction<String, NetworkMessage, CompletableFuture<Void>> send;
+
+    private final ExecutorService executorService;
+
+    FileSender(
+            String nodeName,
+            int chunkSize,
+            int threadPoolSize,
+            RateLimiter rateLimiter,
+            BiFunction<String, NetworkMessage, CompletableFuture<Void>> send) {
+        this.send = send;
+        this.chunkSize = chunkSize;
+        this.rateLimiter = rateLimiter;
+        this.executorService = new ThreadPoolExecutor(
+                0,
+                threadPoolSize,
+                0L, TimeUnit.MILLISECONDS,
+                new LinkedBlockingQueue<Runnable>(),
+                NamedThreadFactory.create(nodeName, "file-sender", LOG)
+        );
+    }
+
+    /**
+     * Adds files to the queue to be sent to the receiver.
+     */
+    CompletableFuture<Void> send(String receiverConsistentId, UUID id, List<File> files) {
+        return CompletableFuture.runAsync(() -> send0(receiverConsistentId, id, files), executorService);
+    }
+
+    private void send0(String receiverConsistentId, UUID id, List<File> files) {
+        AtomicReference<Throwable> error = new AtomicReference<>();
+        try (FileTransferMessagesStream stream = new FileTransferMessagesStream(id, files, chunkSize)) {
+            while (stream.hasNextMessage() && error.get() == null && !Thread.currentThread().isInterrupted()) {
+                if (rateLimiter.tryAcquire()) {
+                    CompletableFuture.completedFuture(stream.nextMessage())
+                            .thenCompose(message -> send.apply(receiverConsistentId, message))

Review Comment:
   This approach is very non-intuitive to me. Why did you chose it instead of good old `send.apply(receiverConsistentId, stream.nextMessage())`?



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/ChunkedFileWriter.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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 java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.file.Path;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+import org.apache.ignite.internal.network.file.messages.FileChunkMessage;
+
+class ChunkedFileWriter implements ManuallyCloseable {
+    private final RandomAccessFile raf;
+
+    private long fileSize;
+
+    private final Queue<FileChunkMessage> chunks = new PriorityQueue<>(FileChunkMessage.COMPARATOR);
+
+    private ChunkedFileWriter(RandomAccessFile raf, long fileSize) {
+        this.raf = raf;
+        this.fileSize = fileSize;
+    }
+
+    static ChunkedFileWriter open(Path path, long fileSize) throws FileNotFoundException {
+        return new ChunkedFileWriter(new RandomAccessFile(path.toFile(), "rw"), fileSize);
+    }
+
+    void write(FileChunkMessage chunk) throws IOException {
+        chunks.add(chunk);
+
+        while (!chunks.isEmpty() && chunks.peek().offset() == raf.getFilePointer()) {

Review Comment:
   Does this mean that this method will be called concurrently with different chunks? Not only the queue is not thread-safe, the algorithm itself is not thread-safe either



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileSender.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BiFunction;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.thread.NamedThreadFactory;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.network.NetworkMessage;
+
+class FileSender {
+    private static final IgniteLogger LOG = Loggers.forClass(FileSender.class);
+
+    private final int chunkSize;
+
+    private final RateLimiter rateLimiter;
+
+    private final BiFunction<String, NetworkMessage, CompletableFuture<Void>> send;
+
+    private final ExecutorService executorService;
+
+    FileSender(
+            String nodeName,
+            int chunkSize,
+            int threadPoolSize,
+            RateLimiter rateLimiter,
+            BiFunction<String, NetworkMessage, CompletableFuture<Void>> send) {
+        this.send = send;
+        this.chunkSize = chunkSize;
+        this.rateLimiter = rateLimiter;
+        this.executorService = new ThreadPoolExecutor(
+                0,
+                threadPoolSize,
+                0L, TimeUnit.MILLISECONDS,
+                new LinkedBlockingQueue<Runnable>(),
+                NamedThreadFactory.create(nodeName, "file-sender", LOG)
+        );
+    }
+
+    /**
+     * Adds files to the queue to be sent to the receiver.
+     */
+    CompletableFuture<Void> send(String receiverConsistentId, UUID id, List<File> files) {
+        return CompletableFuture.runAsync(() -> send0(receiverConsistentId, id, files), executorService);
+    }
+
+    private void send0(String receiverConsistentId, UUID id, List<File> files) {
+        AtomicReference<Throwable> error = new AtomicReference<>();
+        try (FileTransferMessagesStream stream = new FileTransferMessagesStream(id, files, chunkSize)) {
+            while (stream.hasNextMessage() && error.get() == null && !Thread.currentThread().isInterrupted()) {
+                if (rateLimiter.tryAcquire()) {
+                    CompletableFuture.completedFuture(stream.nextMessage())
+                            .thenCompose(message -> send.apply(receiverConsistentId, message))
+                            .whenComplete((res, e) -> {
+                                try {
+                                    if (e != null) {
+                                        LOG.error("Failed to send message to node: {}, transfer id: {}. Exception: {}",
+                                                receiverConsistentId,
+                                                id,
+                                                e
+                                        );
+                                        error.compareAndSet(null, e);
+                                    }
+                                } finally {
+                                    rateLimiter.release();
+                                }
+                            });
+                }
+            }
+
+            if (error.get() != null) {

Review Comment:
   I think the question was "this error will be set in the future, asynchronously, why do we check it right here?"



##########
modules/file-transfer/src/test/java/org/apache/ignite/internal/network/file/FileTransferMessagesHandlerTest.java:
##########
@@ -0,0 +1,145 @@
+/*
+ * 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 org.apache.ignite.internal.network.file.FileAssertions.assertContentEquals;
+import static org.apache.ignite.internal.network.file.FileGenerator.randomFile;
+import static org.apache.ignite.internal.network.file.FileUtils.sortByNames;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+import java.util.stream.Stream;
+import org.apache.ignite.internal.network.file.messages.FileChunkMessage;
+import org.apache.ignite.internal.network.file.messages.FileHeaderMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import org.apache.ignite.internal.testframework.WorkDirectory;
+import org.apache.ignite.internal.testframework.WorkDirectoryExtension;
+import org.apache.ignite.network.NetworkMessage;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@ExtendWith(WorkDirectoryExtension.class)
+class FileTransferMessagesHandlerTest {
+    private static final int CHUNK_SIZE = 1024; // 1 KB

Review Comment:
   We know it's 1 KB :)
   In every other place there's no such comment, why here?



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileTransferMessagesHandler.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * 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 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.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.ignite.internal.network.file.messages.FileChunkMessage;
+import org.apache.ignite.internal.network.file.messages.FileHeaderMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.lang.IgniteInternalException;
+
+class FileTransferMessagesHandler {
+    private final Path dir;
+    private final AtomicInteger filesCount = new AtomicInteger(-1);
+    private final AtomicInteger filesFinished = new AtomicInteger(0);
+    private final CompletableFuture<List<File>> result = new CompletableFuture<>();
+    private final Map<String, ChunkedFileWriter> fileNameToWriter = new ConcurrentHashMap<>();
+    private final Map<String, Lock> fileNameToLock = new ConcurrentHashMap<>();
+
+    FileTransferMessagesHandler(Path dir) {
+        this.dir = dir;
+    }
+
+    void handleFileTransferInfo(FileTransferInfoMessage info) {
+        if (result.isDone()) {
+            throw new IllegalStateException("Received file transfer info after result is already done.");
+        }
+
+        filesCount.set(info.filesCount());
+
+        try {
+            completeIfAllFilesFinished();
+        } catch (IOException e) {
+            handleFileTransferError(e);
+        }
+    }
+
+    void handleFileHeader(FileHeaderMessage header) {
+        if (result.isDone()) {
+            throw new IllegalStateException("Received file header after result is already done.");
+        }
+        doInLock(header.fileName(), () -> handleFileHeader0(header));
+    }
+
+    private void handleFileHeader0(FileHeaderMessage header) {
+        ChunkedFileWriter writer = fileNameToWriter.compute(header.fileName(), (k, v) -> {
+            if (v == null) {
+                return writer(header.fileName(), header.fileSize());
+            } else {
+                v.fileSize(header.fileSize());
+                return v;
+            }
+        });
+
+        try {
+            if (writer.isFinished()) {
+                writer.close();
+                filesFinished.incrementAndGet();
+                completeIfAllFilesFinished();
+            }
+        } catch (IOException e) {
+            handleFileTransferError(e);
+        }
+    }
+
+    void handleFileChunk(FileChunkMessage fileChunk) {
+        if (result.isDone()) {
+            throw new IllegalStateException("Received chunked file after result is already done.");
+        }
+        doInLock(fileChunk.fileName(), () -> handleFileChunk0(fileChunk));
+    }
+
+    private void handleFileChunk0(FileChunkMessage fileChunk) {
+        try {
+            ChunkedFileWriter writer = fileNameToWriter.computeIfAbsent(fileChunk.fileName(), this::writer);
+
+            writer.write(fileChunk);
+
+            if (writer.isFinished()) {
+                writer.close();
+                fileNameToWriter.remove(fileChunk.fileName());
+                filesFinished.incrementAndGet();
+
+                completeIfAllFilesFinished();
+            }
+        } catch (IOException e) {
+            handleFileTransferError(e);
+        }
+    }
+
+    void handleFileTransferError(Throwable error) {
+        if (result.isDone()) {
+            throw new IllegalStateException("Received file transfer error after result is already done.");
+        }
+        result.completeExceptionally(error);
+        closeAllWriters();
+    }
+
+    private void doInLock(String fileName, Runnable runnable) {
+        Lock lock = fileNameToLock.computeIfAbsent(fileName, k -> new ReentrantLock());
+        lock.lock();
+        try {
+            runnable.run();
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    private void completeIfAllFilesFinished() throws IOException {
+        if (filesFinished.get() == filesCount.get()) {
+            try (Stream<Path> stream = Files.list(dir)) {
+                List<File> files = stream.map(Path::toFile).collect(Collectors.toList());
+                result.complete(files);
+            }
+        }
+    }
+
+    CompletableFuture<List<File>> result() {
+        return result;
+    }
+
+    private void closeAllWriters() {
+        try {
+            IgniteUtils.closeAllManually(fileNameToWriter.values().stream());
+        } catch (Exception e) {
+            throw new IgniteInternalException(e);
+        }
+    }
+
+    private ChunkedFileWriter writer(String fileName) {
+        // set -1 as file size to indicate that file size is unknown and will be set later

Review Comment:
   ```suggestion
           // Set -1 as file size to indicate that file size is unknown and will be set later.
   ```



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileTransferMessagesHandler.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * 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 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.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.ignite.internal.network.file.messages.FileChunkMessage;
+import org.apache.ignite.internal.network.file.messages.FileHeaderMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.lang.IgniteInternalException;
+
+class FileTransferMessagesHandler {
+    private final Path dir;
+    private final AtomicInteger filesCount = new AtomicInteger(-1);
+    private final AtomicInteger filesFinished = new AtomicInteger(0);
+    private final CompletableFuture<List<File>> result = new CompletableFuture<>();
+    private final Map<String, ChunkedFileWriter> fileNameToWriter = new ConcurrentHashMap<>();
+    private final Map<String, Lock> fileNameToLock = new ConcurrentHashMap<>();
+
+    FileTransferMessagesHandler(Path dir) {
+        this.dir = dir;
+    }
+
+    void handleFileTransferInfo(FileTransferInfoMessage info) {
+        if (result.isDone()) {
+            throw new IllegalStateException("Received file transfer info after result is already done.");
+        }
+
+        filesCount.set(info.filesCount());
+
+        try {
+            completeIfAllFilesFinished();
+        } catch (IOException e) {
+            handleFileTransferError(e);
+        }
+    }
+
+    void handleFileHeader(FileHeaderMessage header) {
+        if (result.isDone()) {
+            throw new IllegalStateException("Received file header after result is already done.");
+        }
+        doInLock(header.fileName(), () -> handleFileHeader0(header));
+    }
+
+    private void handleFileHeader0(FileHeaderMessage header) {
+        ChunkedFileWriter writer = fileNameToWriter.compute(header.fileName(), (k, v) -> {
+            if (v == null) {
+                return writer(header.fileName(), header.fileSize());
+            } else {
+                v.fileSize(header.fileSize());
+                return v;
+            }
+        });
+
+        try {
+            if (writer.isFinished()) {
+                writer.close();
+                filesFinished.incrementAndGet();
+                completeIfAllFilesFinished();
+            }
+        } catch (IOException e) {
+            handleFileTransferError(e);
+        }
+    }
+
+    void handleFileChunk(FileChunkMessage fileChunk) {
+        if (result.isDone()) {
+            throw new IllegalStateException("Received chunked file after result is already done.");
+        }
+        doInLock(fileChunk.fileName(), () -> handleFileChunk0(fileChunk));
+    }
+
+    private void handleFileChunk0(FileChunkMessage fileChunk) {
+        try {
+            ChunkedFileWriter writer = fileNameToWriter.computeIfAbsent(fileChunk.fileName(), this::writer);
+
+            writer.write(fileChunk);
+
+            if (writer.isFinished()) {
+                writer.close();
+                fileNameToWriter.remove(fileChunk.fileName());
+                filesFinished.incrementAndGet();
+
+                completeIfAllFilesFinished();
+            }
+        } catch (IOException e) {
+            handleFileTransferError(e);
+        }
+    }
+
+    void handleFileTransferError(Throwable error) {
+        if (result.isDone()) {
+            throw new IllegalStateException("Received file transfer error after result is already done.");
+        }
+        result.completeExceptionally(error);
+        closeAllWriters();
+    }
+
+    private void doInLock(String fileName, Runnable runnable) {
+        Lock lock = fileNameToLock.computeIfAbsent(fileName, k -> new ReentrantLock());
+        lock.lock();
+        try {
+            runnable.run();
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    private void completeIfAllFilesFinished() throws IOException {
+        if (filesFinished.get() == filesCount.get()) {
+            try (Stream<Path> stream = Files.list(dir)) {
+                List<File> files = stream.map(Path::toFile).collect(Collectors.toList());
+                result.complete(files);
+            }
+        }
+    }
+
+    CompletableFuture<List<File>> result() {
+        return result;
+    }
+
+    private void closeAllWriters() {
+        try {
+            IgniteUtils.closeAllManually(fileNameToWriter.values().stream());
+        } catch (Exception e) {
+            throw new IgniteInternalException(e);
+        }
+    }
+
+    private ChunkedFileWriter writer(String fileName) {
+        // set -1 as file size to indicate that file size is unknown and will be set later
+        return writer(fileName, -1);
+    }
+
+    private ChunkedFileWriter writer(String fileName, long fileSize) {
+        try {
+            Path path = Files.createFile(dir.resolve(fileName));
+            return ChunkedFileWriter.open(path, fileSize);
+        } catch (IOException e) {
+            handleFileTransferError(e);
+            throw new IgniteInternalException(e);

Review Comment:
   Please don't do that, throw proper exception instead



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/messages/FileTransferMessageType.java:
##########
@@ -0,0 +1,42 @@
+/*
+ * 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.messages;
+
+import org.apache.ignite.network.annotations.MessageGroup;
+
+/**
+ * File transfer message types.
+ */
+@MessageGroup(groupType = 200, groupName = "FileTransfer")
+public final class FileTransferMessageType {
+    public static final short FILE_TRANSFER_INFO = 0;

Review Comment:
   Please add a link to the class in javadocs for every constant, like we do it in other classes.



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileTransferMessagesHandler.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * 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 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.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.ignite.internal.network.file.messages.FileChunkMessage;
+import org.apache.ignite.internal.network.file.messages.FileHeaderMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.lang.IgniteInternalException;
+
+class FileTransferMessagesHandler {

Review Comment:
   Again, a total lack of comments in package-private classes. Do you assume that the code is trivial and doesn't require any comment at all?



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileTransferMessagesStream.java:
##########
@@ -0,0 +1,226 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Queue;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+import org.apache.ignite.internal.network.file.messages.FileChunkMessage;
+import org.apache.ignite.internal.network.file.messages.FileHeaderMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferFactory;
+import org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import org.apache.ignite.network.NetworkMessage;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Stream of messages to send files.
+ */
+public class FileTransferMessagesStream implements Iterable<NetworkMessage>, AutoCloseable {
+    private final UUID transferId;
+
+    private final Queue<File> filesToSend;
+
+    private final int chunkSize;
+
+    @Nullable
+    private ChunkedFileReader currFile;
+
+    @Nullable
+    private FileTransferInfoMessage fileTransferInfoMessage;
+
+    private final AtomicBoolean closed = new AtomicBoolean(false);
+
+    private final FileTransferFactory factory = new FileTransferFactory();
+
+    /**
+     * Creates a new stream of messages to send files.
+     *
+     * @param transferId the id of the stream.
+     * @param filesToSend the files to send. Must not be empty.
+     * @param chunkSize the size of the chunks to send. Must be positive.
+     */
+    FileTransferMessagesStream(
+            UUID transferId,
+            List<File> filesToSend,
+            int chunkSize
+    ) {
+        if (chunkSize <= 0) {
+            throw new IllegalArgumentException("Chunk size must be positive.");
+        }
+
+        if (filesToSend.isEmpty()) {
+            throw new IllegalArgumentException("Files to send must not be empty.");
+        }
+
+        this.transferId = transferId;
+        this.filesToSend = new LinkedList<>(filesToSend);
+        this.chunkSize = chunkSize;
+        this.fileTransferInfoMessage = fileTransferInfo();
+    }
+
+    /**
+     * Returns true if there are more messages to send.
+     *
+     * @return true if there are more messages to send.
+     */
+    boolean hasNextMessage() throws IOException {
+        // check that the stream is not closed.

Review Comment:
   Please check all your comments



##########
modules/file-transfer/src/testFixtures/java/org/apache/ignite/internal/network/file/FileGenerator.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Random;
+
+/**
+ * File generator.
+ */
+public class FileGenerator {
+    /**
+     * Generates a random file with the given content size. The file be deleted on exit.
+     *
+     * @param dir Directory to create the file in.
+     * @param contentSize Size of the file content.
+     * @return Generated file.
+     */
+    public static File randomFile(Path dir, int contentSize) {
+        try {
+            Path tempFile = Files.createTempFile(dir, null, null);
+            byte[] bytes = new byte[contentSize];
+            new Random().nextBytes(bytes);
+            tempFile.toFile().deleteOnExit();

Review Comment:
   Once again, what's the reason?



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/messages/FileTransferMessageType.java:
##########
@@ -0,0 +1,42 @@
+/*
+ * 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.messages;
+
+import org.apache.ignite.network.annotations.MessageGroup;
+
+/**
+ * File transfer message types.
+ */
+@MessageGroup(groupType = 200, groupName = "FileTransfer")
+public final class FileTransferMessageType {
+    public static final short FILE_TRANSFER_INFO = 0;
+    public static final short FILE_HEADER = 1;
+    public static final short FILE_CHUNK = 2;
+    public static final short FILE_DOWNLOAD_REQUEST = 3;
+    public static final short FILE_DOWNLOAD_RESPONSE = 4;
+    public static final short FILE_UPLOAD_REQUEST = 5;
+    public static final short FILE_UPLOAD_RESPONSE = 6;
+    public static final short FILE_TRANSFER_ERROR = 7;
+    public static final short METADATA = 8;
+
+    /**
+     * File transferring metadata.
+     */
+    public static final class Metadata {

Review Comment:
   What's this class for? I assume you forgot to delete it



##########
modules/file-transfer/src/testFixtures/java/org/apache/ignite/internal/network/file/FileGenerator.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Random;
+
+/**
+ * File generator.
+ */
+public class FileGenerator {
+    /**
+     * Generates a random file with the given content size. The file be deleted on exit.
+     *
+     * @param dir Directory to create the file in.
+     * @param contentSize Size of the file content.
+     * @return Generated file.
+     */
+    public static File randomFile(Path dir, int contentSize) {

Review Comment:
   Why do you use both old and new file API in the same signature?



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

Review Comment:
   Should this be configurable? I guess so



##########
modules/file-transfer/src/test/java/org/apache/ignite/internal/network/file/FileReceiverTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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 org.apache.ignite.internal.network.file.FileAssertions.assertContentEquals;
+import static org.apache.ignite.internal.network.file.FileGenerator.randomFile;
+import static org.apache.ignite.internal.network.file.FileUtils.sortByNames;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrow;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willTimeoutIn;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+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.UUID;
+import java.util.concurrent.TimeUnit;
+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.FileHeaderMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import org.apache.ignite.internal.testframework.WorkDirectory;
+import org.apache.ignite.internal.testframework.WorkDirectoryExtension;
+import org.apache.ignite.network.NetworkMessage;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+@ExtendWith(WorkDirectoryExtension.class)
+class FileReceiverTest {
+    private static final int CHUNK_SIZE = 1024;
+
+    @WorkDirectory
+    private Path workDir;
+
+    @Test
+    void receiveSingleFile() throws IOException {
+        // when a single file is sent
+        List<File> filesToSend = List.of(randomFile(workDir, CHUNK_SIZE));
+        FileReceiver receiver = new FileReceiver("node1", 10);
+        UUID transferId = UUID.randomUUID();
+
+        Path path = Files.createDirectory(workDir.resolve(transferId.toString()));
+        path.toFile().deleteOnExit();

Review Comment:
   But why? Work dir should be deleted after tests. If it's not, we should fix the extension itself



##########
modules/file-transfer/src/test/java/org/apache/ignite/internal/network/file/FileReceiverTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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 org.apache.ignite.internal.network.file.FileAssertions.assertContentEquals;
+import static org.apache.ignite.internal.network.file.FileGenerator.randomFile;
+import static org.apache.ignite.internal.network.file.FileUtils.sortByNames;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrow;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willTimeoutIn;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+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.UUID;
+import java.util.concurrent.TimeUnit;
+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.FileHeaderMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import org.apache.ignite.internal.testframework.WorkDirectory;
+import org.apache.ignite.internal.testframework.WorkDirectoryExtension;
+import org.apache.ignite.network.NetworkMessage;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+@ExtendWith(WorkDirectoryExtension.class)
+class FileReceiverTest {
+    private static final int CHUNK_SIZE = 1024;
+
+    @WorkDirectory
+    private Path workDir;
+
+    @Test
+    void receiveSingleFile() throws IOException {
+        // when a single file is sent

Review Comment:
   You get the idea



##########
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()

Review Comment:
   It would be nice to name it "messageFactory", because "factory" itself is too broad of a term



##########
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())

Review Comment:
   Once again I see this pattern. What's the reason? Writing
   ```
   UUID transferId = UUID.randomUUID();
   ```
   looks absolutely fine. Why would you need to pollute your code with extra paddings? To avoid writing the type explicitly?



##########
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;
+    /**

Review Comment:
   ```suggestion
   
       /**
   ```



##########
modules/file-transfer/src/main/java/org/apache/ignite/internal/network/file/FileSender.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * 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 java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.BiFunction;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+import org.apache.ignite.internal.logger.IgniteLogger;
+import org.apache.ignite.internal.logger.Loggers;
+import org.apache.ignite.internal.network.file.exception.FileTransferException;
+import org.apache.ignite.internal.thread.NamedThreadFactory;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.network.NetworkMessage;
+
+class FileSender implements ManuallyCloseable {
+    private static final IgniteLogger LOG = Loggers.forClass(FileSender.class);
+
+    private final int chunkSize;
+
+    private final RateLimiter rateLimiter;
+
+    private final BiFunction<String, NetworkMessage, CompletableFuture<Void>> send;
+
+    private final ExecutorService executorService;
+
+    FileSender(
+            String nodeName,
+            int chunkSize,
+            int threadPoolSize,
+            RateLimiter rateLimiter,
+            BiFunction<String, NetworkMessage, CompletableFuture<Void>> send) {
+        this.send = send;
+        this.chunkSize = chunkSize;
+        this.rateLimiter = rateLimiter;
+        this.executorService = new ThreadPoolExecutor(
+                0,
+                threadPoolSize,
+                0L, TimeUnit.MILLISECONDS,
+                new LinkedBlockingQueue<Runnable>(),
+                NamedThreadFactory.create(nodeName, "file-sender", LOG)
+        );
+    }
+
+    /**
+     * Adds files to the queue to be sent to the receiver.
+     */
+    CompletableFuture<Void> send(String receiverConsistentId, UUID id, List<File> files) {
+        return CompletableFuture.runAsync(() -> send0(receiverConsistentId, id, files), executorService);
+    }
+
+    private void send0(String receiverConsistentId, UUID id, List<File> files) {
+        AtomicReference<Throwable> error = new AtomicReference<>();
+        try (FileTransferMessagesStream stream = new FileTransferMessagesStream(id, files, chunkSize)) {
+            while (stream.hasNextMessage() && error.get() == null && !Thread.currentThread().isInterrupted()) {

Review Comment:
   Is this a spin-loop that will just burn the CPU?



##########
modules/file-transfer/src/test/java/org/apache/ignite/internal/network/file/FileReceiverTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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 org.apache.ignite.internal.network.file.FileAssertions.assertContentEquals;
+import static org.apache.ignite.internal.network.file.FileGenerator.randomFile;
+import static org.apache.ignite.internal.network.file.FileUtils.sortByNames;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willThrow;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willTimeoutIn;
+import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+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.UUID;
+import java.util.concurrent.TimeUnit;
+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.FileHeaderMessage;
+import org.apache.ignite.internal.network.file.messages.FileTransferInfoMessage;
+import org.apache.ignite.internal.testframework.WorkDirectory;
+import org.apache.ignite.internal.testframework.WorkDirectoryExtension;
+import org.apache.ignite.network.NetworkMessage;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+@ExtendWith(WorkDirectoryExtension.class)
+class FileReceiverTest {
+    private static final int CHUNK_SIZE = 1024;
+
+    @WorkDirectory
+    private Path workDir;
+
+    @Test
+    void receiveSingleFile() throws IOException {
+        // when a single file is sent

Review Comment:
   ```suggestion
           // When a single file is sent.
   ```



##########
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:
   Threading model here is not clear to me. All "compose" are not "async", how do you guarantee that network thread won't be executing your disk 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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org