You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by xt...@apache.org on 2024/09/19 11:32:36 UTC

(flink) branch master updated: [FLINK-36112] Add Support for CreateFlag.NO_LOCAL_WRITE in FLINK on YARN's File Creation to Manage Disk Space and Network Load in Labeled YARN Nodes

This is an automated email from the ASF dual-hosted git repository.

xtsong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/master by this push:
     new 5559126733b [FLINK-36112] Add Support for CreateFlag.NO_LOCAL_WRITE in FLINK on YARN's File Creation to Manage Disk Space and Network Load in Labeled YARN Nodes
5559126733b is described below

commit 5559126733bbb404536db29ed872decf58937e6e
Author: 余良 <yu...@chinaunicom.cn>
AuthorDate: Wed Aug 21 09:06:42 2024 +0800

    [FLINK-36112] Add Support for CreateFlag.NO_LOCAL_WRITE in FLINK on YARN's File Creation to Manage Disk Space and Network Load in Labeled YARN Nodes
    
    This closes #25226
---
 .../apache/flink/connector/file/sink/FileSink.java | 14 ++++-
 .../java/org/apache/flink/core/fs/FileSystem.java  | 12 ++++
 .../java/org/apache/flink/core/fs/IFileSystem.java | 20 +++++++
 .../flink/runtime/fs/hdfs/HadoopFileSystem.java    | 18 ++++++
 .../hdfs/HadoopRecoverableFsDataOutputStream.java  | 26 ++++++++-
 .../runtime/fs/hdfs/HadoopRecoverableWriter.java   | 11 +++-
 .../fs/hdfs/HadoopRecoverableWriterTest.java       | 66 +++++++++++++++++++++-
 tools/maven/suppressions.xml                       |  2 +-
 8 files changed, 163 insertions(+), 6 deletions(-)

diff --git a/flink-connectors/flink-connector-files/src/main/java/org/apache/flink/connector/file/sink/FileSink.java b/flink-connectors/flink-connector-files/src/main/java/org/apache/flink/connector/file/sink/FileSink.java
index 2e5f43e50d4..bf7cfa6c42a 100644
--- a/flink-connectors/flink-connector-files/src/main/java/org/apache/flink/connector/file/sink/FileSink.java
+++ b/flink-connectors/flink-connector-files/src/main/java/org/apache/flink/connector/file/sink/FileSink.java
@@ -71,6 +71,8 @@ import java.io.IOException;
 import java.io.Serializable;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 
 import static org.apache.flink.util.Preconditions.checkNotNull;
 import static org.apache.flink.util.Preconditions.checkState;
@@ -526,6 +528,10 @@ public class FileSink<IN>
 
         private CheckpointRollingPolicy<IN, String> rollingPolicy;
 
+        private Map<String, String> writerConfig = new HashMap<>();
+
+        private static final String HDFS_NO_LOCAL_WRITE = "fs.hdfs.no-local-write";
+
         private OutputFileConfig outputFileConfig;
 
         private boolean isCompactDisabledExplicitly = false;
@@ -580,6 +586,11 @@ public class FileSink<IN>
             return self();
         }
 
+        public T disableLocalWriting() {
+            this.writerConfig.put(HDFS_NO_LOCAL_WRITE, String.valueOf(true));
+            return self();
+        }
+
         public T withOutputFileConfig(final OutputFileConfig outputFileConfig) {
             this.outputFileConfig = outputFileConfig;
             return self();
@@ -695,7 +706,8 @@ public class FileSink<IN>
 
         BucketWriter<IN, String> createBucketWriter() throws IOException {
             return new BulkBucketWriter<>(
-                    FileSystem.get(basePath.toUri()).createRecoverableWriter(), writerFactory);
+                    FileSystem.get(basePath.toUri()).createRecoverableWriter(writerConfig),
+                    writerFactory);
         }
     }
 
diff --git a/flink-core/src/main/java/org/apache/flink/core/fs/FileSystem.java b/flink-core/src/main/java/org/apache/flink/core/fs/FileSystem.java
index 3beab0e05df..a84e77f7881 100644
--- a/flink-core/src/main/java/org/apache/flink/core/fs/FileSystem.java
+++ b/flink-core/src/main/java/org/apache/flink/core/fs/FileSystem.java
@@ -26,6 +26,7 @@ package org.apache.flink.core.fs;
 
 import org.apache.flink.annotation.Internal;
 import org.apache.flink.annotation.Public;
+import org.apache.flink.annotation.PublicEvolving;
 import org.apache.flink.configuration.Configuration;
 import org.apache.flink.configuration.CoreOptions;
 import org.apache.flink.configuration.IllegalConfigurationException;
@@ -57,6 +58,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.ServiceLoader;
 import java.util.Set;
 import java.util.concurrent.locks.ReentrantLock;
@@ -665,6 +667,16 @@ public abstract class FileSystem implements IFileSystem {
         return IFileSystem.super.createRecoverableWriter();
     }
 
+    @PublicEvolving
+    @Override
+    public RecoverableWriter createRecoverableWriter(Map<String, String> conf) throws IOException {
+        if (conf == null || conf.isEmpty()) {
+            return createRecoverableWriter();
+        } else {
+            return IFileSystem.super.createRecoverableWriter(conf);
+        }
+    }
+
     @Override
     public abstract FileStatus[] listStatus(Path f) throws IOException;
 
diff --git a/flink-core/src/main/java/org/apache/flink/core/fs/IFileSystem.java b/flink-core/src/main/java/org/apache/flink/core/fs/IFileSystem.java
index dcbd79aa3f9..285ec045541 100644
--- a/flink-core/src/main/java/org/apache/flink/core/fs/IFileSystem.java
+++ b/flink-core/src/main/java/org/apache/flink/core/fs/IFileSystem.java
@@ -30,6 +30,7 @@ import org.apache.flink.core.fs.local.LocalFileSystem;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.net.URI;
+import java.util.Map;
 
 /**
  * Interface of all file systems used by Flink. This interface may be extended to implement
@@ -237,6 +238,25 @@ public interface IFileSystem {
                 "This file system does not support recoverable writers.");
     }
 
+    /**
+     * Creates a new {@link RecoverableWriter}. A recoverable writer creates streams that can
+     * persist and recover their intermediate state. Persisting and recovering intermediate state is
+     * a core building block for writing to files that span multiple checkpoints.
+     *
+     * <p>The returned object can act as a shared factory to open and recover multiple streams.
+     *
+     * <p>This method is optional on file systems and various file system implementations may not
+     * support this method, throwing an {@code UnsupportedOperationException}.
+     *
+     * @param conf Map contains a flag to indicate whether the writer should not write to local
+     *     storage. and can provide more information to instantiate the writer.
+     * @return A RecoverableWriter for this file system.
+     * @throws IOException Thrown, if the recoverable writer cannot be instantiated.
+     */
+    default RecoverableWriter createRecoverableWriter(Map<String, String> conf) throws IOException {
+        return createRecoverableWriter();
+    }
+
     /**
      * List the statuses of the files/directories in the given path if the path is a directory.
      *
diff --git a/flink-filesystems/flink-hadoop-fs/src/main/java/org/apache/flink/runtime/fs/hdfs/HadoopFileSystem.java b/flink-filesystems/flink-hadoop-fs/src/main/java/org/apache/flink/runtime/fs/hdfs/HadoopFileSystem.java
index fd2d016b423..6bf0adcf89c 100644
--- a/flink-filesystems/flink-hadoop-fs/src/main/java/org/apache/flink/runtime/fs/hdfs/HadoopFileSystem.java
+++ b/flink-filesystems/flink-hadoop-fs/src/main/java/org/apache/flink/runtime/fs/hdfs/HadoopFileSystem.java
@@ -28,6 +28,7 @@ import org.apache.flink.core.fs.RecoverableWriter;
 import java.io.IOException;
 import java.net.URI;
 import java.util.Locale;
+import java.util.Map;
 
 import static org.apache.flink.util.Preconditions.checkNotNull;
 
@@ -36,6 +37,8 @@ import static org.apache.flink.util.Preconditions.checkNotNull;
  */
 public class HadoopFileSystem extends FileSystem {
 
+    private static final String HDFS_NO_LOCAL_WRITE = "fs.hdfs.no-local-write";
+
     /** The wrapped Hadoop File System. */
     private final org.apache.hadoop.fs.FileSystem fs;
 
@@ -215,6 +218,21 @@ public class HadoopFileSystem extends FileSystem {
         return new HadoopRecoverableWriter(fs);
     }
 
+    @Override
+    public RecoverableWriter createRecoverableWriter(Map<String, String> conf) throws IOException {
+        // This writer is only supported on a subset of file systems, and on
+        // specific versions. We check these schemes and versions eagerly for better error
+        // messages in the constructor of the writer.
+        if (conf == null || conf.isEmpty()) {
+            return createRecoverableWriter();
+        } else if (conf.containsKey(HDFS_NO_LOCAL_WRITE)) {
+            return new HadoopRecoverableWriter(
+                    fs, Boolean.parseBoolean(conf.get(HDFS_NO_LOCAL_WRITE)));
+        } else {
+            return new HadoopRecoverableWriter(fs);
+        }
+    }
+
     // ------------------------------------------------------------------------
     //  Utilities
     // ------------------------------------------------------------------------
diff --git a/flink-filesystems/flink-hadoop-fs/src/main/java/org/apache/flink/runtime/fs/hdfs/HadoopRecoverableFsDataOutputStream.java b/flink-filesystems/flink-hadoop-fs/src/main/java/org/apache/flink/runtime/fs/hdfs/HadoopRecoverableFsDataOutputStream.java
index 8f695ea777e..01cf11f6e07 100644
--- a/flink-filesystems/flink-hadoop-fs/src/main/java/org/apache/flink/runtime/fs/hdfs/HadoopRecoverableFsDataOutputStream.java
+++ b/flink-filesystems/flink-hadoop-fs/src/main/java/org/apache/flink/runtime/fs/hdfs/HadoopRecoverableFsDataOutputStream.java
@@ -29,10 +29,12 @@ import org.apache.flink.util.FlinkRuntimeException;
 import org.apache.flink.util.IOUtils;
 import org.apache.flink.util.Preconditions;
 
+import org.apache.hadoop.fs.CreateFlag;
 import org.apache.hadoop.fs.FSDataOutputStream;
 import org.apache.hadoop.fs.FileStatus;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.permission.FsPermission;
 import org.apache.hadoop.fs.viewfs.ViewFileSystem;
 import org.apache.hadoop.hdfs.DistributedFileSystem;
 import org.apache.hadoop.util.VersionInfo;
@@ -43,8 +45,11 @@ import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.time.Duration;
+import java.util.EnumSet;
 
 import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.IO_FILE_BUFFER_SIZE_DEFAULT;
+import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.IO_FILE_BUFFER_SIZE_KEY;
 
 /**
  * An implementation of the {@link RecoverableFsDataOutputStream} for Hadoop's file system
@@ -57,7 +62,8 @@ class HadoopRecoverableFsDataOutputStream extends BaseHadoopFsRecoverableFsDataO
 
     private static Method truncateHandle;
 
-    HadoopRecoverableFsDataOutputStream(FileSystem fs, Path targetFile, Path tempFile)
+    HadoopRecoverableFsDataOutputStream(
+            FileSystem fs, Path targetFile, Path tempFile, boolean noLocalWrite)
             throws IOException {
 
         ensureTruncateInitialized();
@@ -65,7 +71,23 @@ class HadoopRecoverableFsDataOutputStream extends BaseHadoopFsRecoverableFsDataO
         this.fs = checkNotNull(fs);
         this.targetFile = checkNotNull(targetFile);
         this.tempFile = checkNotNull(tempFile);
-        this.out = fs.create(tempFile);
+        if (noLocalWrite) {
+            this.out =
+                    fs.create(
+                            tempFile,
+                            FsPermission.getFileDefault(),
+                            EnumSet.of(
+                                    CreateFlag.CREATE,
+                                    CreateFlag.OVERWRITE,
+                                    CreateFlag.NO_LOCAL_WRITE),
+                            fs.getConf()
+                                    .getInt(IO_FILE_BUFFER_SIZE_KEY, IO_FILE_BUFFER_SIZE_DEFAULT),
+                            fs.getDefaultReplication(),
+                            fs.getDefaultBlockSize(),
+                            null);
+        } else {
+            this.out = fs.create(tempFile);
+        }
     }
 
     @VisibleForTesting
diff --git a/flink-filesystems/flink-hadoop-fs/src/main/java/org/apache/flink/runtime/fs/hdfs/HadoopRecoverableWriter.java b/flink-filesystems/flink-hadoop-fs/src/main/java/org/apache/flink/runtime/fs/hdfs/HadoopRecoverableWriter.java
index a57f32933a6..45527bc62f4 100644
--- a/flink-filesystems/flink-hadoop-fs/src/main/java/org/apache/flink/runtime/fs/hdfs/HadoopRecoverableWriter.java
+++ b/flink-filesystems/flink-hadoop-fs/src/main/java/org/apache/flink/runtime/fs/hdfs/HadoopRecoverableWriter.java
@@ -46,6 +46,8 @@ public class HadoopRecoverableWriter implements RecoverableWriter {
     /** The Hadoop file system on which the writer operates. */
     protected final org.apache.hadoop.fs.FileSystem fs;
 
+    private final boolean noLocalWrite;
+
     /**
      * Creates a new Recoverable writer.
      *
@@ -53,6 +55,13 @@ public class HadoopRecoverableWriter implements RecoverableWriter {
      */
     public HadoopRecoverableWriter(org.apache.hadoop.fs.FileSystem fs) {
         this.fs = checkNotNull(fs);
+        this.noLocalWrite = false;
+        checkSupportedFSSchemes(fs);
+    }
+
+    public HadoopRecoverableWriter(org.apache.hadoop.fs.FileSystem fs, boolean noLocalWrite) {
+        this.fs = checkNotNull(fs);
+        this.noLocalWrite = noLocalWrite;
 
         checkSupportedFSSchemes(fs);
     }
@@ -86,7 +95,7 @@ public class HadoopRecoverableWriter implements RecoverableWriter {
     protected RecoverableFsDataOutputStream getRecoverableFsDataOutputStream(
             org.apache.hadoop.fs.Path targetFile, org.apache.hadoop.fs.Path tempFile)
             throws IOException {
-        return new HadoopRecoverableFsDataOutputStream(fs, targetFile, tempFile);
+        return new HadoopRecoverableFsDataOutputStream(fs, targetFile, tempFile, noLocalWrite);
     }
 
     @Override
diff --git a/flink-filesystems/flink-hadoop-fs/src/test/java/org/apache/flink/runtime/fs/hdfs/HadoopRecoverableWriterTest.java b/flink-filesystems/flink-hadoop-fs/src/test/java/org/apache/flink/runtime/fs/hdfs/HadoopRecoverableWriterTest.java
index e5a4ccafb9a..3a045992163 100644
--- a/flink-filesystems/flink-hadoop-fs/src/test/java/org/apache/flink/runtime/fs/hdfs/HadoopRecoverableWriterTest.java
+++ b/flink-filesystems/flink-hadoop-fs/src/test/java/org/apache/flink/runtime/fs/hdfs/HadoopRecoverableWriterTest.java
@@ -21,19 +21,35 @@ package org.apache.flink.runtime.fs.hdfs;
 import org.apache.flink.core.fs.AbstractRecoverableWriterTest;
 import org.apache.flink.core.fs.FileSystem;
 import org.apache.flink.core.fs.Path;
+import org.apache.flink.core.fs.RecoverableFsDataOutputStream;
+import org.apache.flink.core.fs.RecoverableWriter;
+import org.apache.flink.mock.Whitebox;
 import org.apache.flink.runtime.util.HadoopUtils;
 import org.apache.flink.testutils.junit.utils.TempDirUtils;
 import org.apache.flink.util.OperatingSystem;
 
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hdfs.MiniDFSCluster;
+import org.apache.hadoop.hdfs.protocol.BlockListAsLongs;
+import org.apache.hadoop.hdfs.protocol.HdfsConstants;
+import org.apache.hadoop.hdfs.server.blockmanagement.BlockManager;
+import org.apache.hadoop.hdfs.server.blockmanagement.DatanodeDescriptor;
+import org.apache.hadoop.hdfs.server.blockmanagement.DatanodeManager;
+import org.apache.hadoop.hdfs.server.protocol.DatanodeStorage;
 import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
 
 import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
 
 import static org.assertj.core.api.Assumptions.assumeThat;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
 
 /** Tests for the {@link HadoopRecoverableWriter}. */
 class HadoopRecoverableWriterTest extends AbstractRecoverableWriterTest {
@@ -63,8 +79,11 @@ class HadoopRecoverableWriterTest extends AbstractRecoverableWriterTest {
 
         final Configuration hdConf = new Configuration();
         hdConf.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, baseDir.getAbsolutePath());
+        hdConf.set("dfs.replication", "2");
+        hdConf.set("dfs.blocksize", String.valueOf(512));
+        hdConf.set("dfs.namenode.fs-limits.min-block-size", String.valueOf(512));
 
-        final MiniDFSCluster.Builder builder = new MiniDFSCluster.Builder(hdConf);
+        final MiniDFSCluster.Builder builder = new MiniDFSCluster.Builder(hdConf).numDataNodes(3);
         hdfsCluster = builder.build();
 
         final org.apache.hadoop.fs.FileSystem hdfs = hdfsCluster.getFileSystem();
@@ -83,6 +102,51 @@ class HadoopRecoverableWriterTest extends AbstractRecoverableWriterTest {
         }
     }
 
+    private RecoverableWriter getNoLocalWriteFileSystemWriter() throws Exception {
+        Map<String, String> conf = new HashMap<>();
+        conf.put("fs.hdfs.no-local-write", "true");
+        return fileSystem.createRecoverableWriter(conf);
+    }
+
+    @Test
+    void testNoLocalWriteFlag() throws Exception {
+        createHDFS();
+        final HadoopRecoverableWriter writer =
+                (HadoopRecoverableWriter) getNoLocalWriteFileSystemWriter();
+        BlockManager bm = hdfsCluster.getNameNode().getNamesystem().getBlockManager();
+        DatanodeManager dm = bm.getDatanodeManager();
+        try (RecoverableFsDataOutputStream os = writer.open(new Path("/tests/test-no-local"))) {
+            // Inject a DatanodeManager that returns one DataNode as local node for
+            // the client.
+            DatanodeManager spyDm = spy(dm);
+            DatanodeDescriptor dn1 =
+                    dm.getDatanodeListForReport(HdfsConstants.DatanodeReportType.LIVE).get(0);
+            doReturn(dn1).when(spyDm).getDatanodeByHost("127.0.0.1");
+            Whitebox.setInternalState(bm, "datanodeManager", spyDm);
+            byte[] buf = new byte[512 * 16];
+            new Random().nextBytes(buf);
+            os.write(buf);
+        } finally {
+            Whitebox.setInternalState(bm, "datanodeManager", dm);
+        }
+        hdfsCluster.triggerBlockReports();
+        final String bpid = hdfsCluster.getNamesystem().getBlockPoolId();
+        // Total number of DataNodes is 3.
+        Assertions.assertEquals(3, hdfsCluster.getAllBlockReports(bpid).size());
+        int numDataNodesWithData = 0;
+        for (Map<DatanodeStorage, BlockListAsLongs> dnBlocks :
+                hdfsCluster.getAllBlockReports(bpid)) {
+            for (BlockListAsLongs blocks : dnBlocks.values()) {
+                if (blocks.getNumberOfBlocks() > 0) {
+                    numDataNodesWithData++;
+                    break;
+                }
+            }
+        }
+        // Verify that only one DN has no data.
+        Assertions.assertEquals(1, 3 - numDataNodesWithData);
+    }
+
     @Override
     public Path getBasePath() {
         return basePath;
diff --git a/tools/maven/suppressions.xml b/tools/maven/suppressions.xml
index 13329e465d9..b0ceca15d29 100644
--- a/tools/maven/suppressions.xml
+++ b/tools/maven/suppressions.xml
@@ -43,7 +43,7 @@ under the License.
 
 		<!-- Legacy mockito usages -->
 		<!-- Updated this suppress files section due to FLINK-36063, which migrated many classes from flink-streaming-java to flink-runtime. This allows runtime access to stream graph. -->
-		<suppress files="BufferDataOverWindowOperatorTest.java|CEPOperatorTest.java|CepRuntimeContextTest.java|CliFrontendListTest.java|CliFrontendPackageProgramTest.java|CliFrontendSavepointTest.java|DemultiplexingRecordDeserializerTest.java|DropwizardMeterWrapperTest.java|DynamicEventTimeSessionWindowsTest.java|DynamicProcessingTimeSessionWindowsTest.java|EmbeddedRocksDBStateBackendTest.java|EventTimeSessionWindowsTest.java|FlinkCalciteCatalogReaderTest.java|FlinkMeterWrapperTest.java|Hadoop [...]
+		<suppress files="BufferDataOverWindowOperatorTest.java|CEPOperatorTest.java|CepRuntimeContextTest.java|CliFrontendListTest.java|CliFrontendPackageProgramTest.java|CliFrontendSavepointTest.java|DemultiplexingRecordDeserializerTest.java|DropwizardMeterWrapperTest.java|DynamicEventTimeSessionWindowsTest.java|DynamicProcessingTimeSessionWindowsTest.java|EmbeddedRocksDBStateBackendTest.java|EventTimeSessionWindowsTest.java|FlinkCalciteCatalogReaderTest.java|FlinkMeterWrapperTest.java|Hadoop [...]
 				  checks="IllegalImport"/>
 
 		<suppress files="org[\\/]apache[\\/]flink[\\/]formats[\\/]avro[\\/]generated[\\/].*.java" checks="[a-zA-Z0-9]*"/>