Flink 分布式缓存原理及使用

背景

在1.9.1版本中分布式缓存并未拷贝HDFS下的文件到TM,运行时抛出如下异常。



升级到1.10.1版本,能正常使用。借此,学习下Flink 分布式缓存相关知识。

定义

官网对 distributed cache 的定义:

    Flink offers a distributed cache, similar to Apache Hadoop, to make files locally accessible to parallel instances of user functions. This functionality can be used to share files that contain static external data such as dictionaries or machine-learned regression models.
        
   The cache works as follows. A program registers a file or directory of a local or remote filesystem such as HDFS or S3 under a specific name in its ExecutionEnvironment as a cached file. When the program is executed, Flink automatically copies the file or directory to the local filesystem of all workers. A user function can look up the file or directory under the specified name and access it from the worker’s local filesystem.

意思是通过Flink程序注册一个本地或者Hdfs文件,程序在运行时,Flink会自动将该文件拷贝到每个tm中,每个函数可以通过注册的名称获取该文件。

使用

官网给出的使用案例:

final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

// register a file from HDFS
env.registerCachedFile("hdfs:///path/to/your/file", "hdfsFile")

// register a local executable file (script, executable, ...)
env.registerCachedFile("file:///path/to/exec/file", "localExecFile", true)

// define your program and execute
...
DataStream<String> input = ...
DataStream<Integer> result = input.map(new MyMapper());
...
env.execute();

---------------------------------------------------------------
// extend a RichFunction to have access to the RuntimeContext
public final class MyMapper extends RichMapFunction<String, Integer> {

    @Override
    public void open(Configuration config) {

      // access cached file via RuntimeContext and DistributedCache
      File myFile = getRuntimeContext().getDistributedCache().getFile("hdfsFile");
      // read the file (or navigate the directory)
      ...
    }

    @Override
    public Integer map(String value) throws Exception {
      // use content of cached file
      ...
    }
}

实现流程

参考flink1.10.1版本的源码,了解实现流程。

  1. 将分布式文件地址及注册名称写入StreamExecutionEnvironment的cacheFile中。
protected final List<Tuple2<String, DistributedCache.DistributedCacheEntry>> cacheFile = new ArrayList<>();

public void registerCachedFile(String filePath, String name, boolean executable) {
    this.cacheFile.add(new Tuple2<>(name, new DistributedCache.DistributedCacheEntry(filePath, executable)));
}
  1. 在生成StreamGraph时将该cacheFile传递给StreamGraph的 userArtifacts。

StreamGraphGenerator-->StreamGraph

org.apache.flink.streaming.api.environment.StreamExecutionEnvironment#getStreamGraphGenerator   
private StreamGraphGenerator getStreamGraphGenerator() {
    if (transformations.size() <= 0) {
        throw new IllegalStateException("No operators defined in streaming topology. Cannot execute.");
    }
    return new StreamGraphGenerator(transformations, config, checkpointCfg)
        .setStateBackend(defaultStateBackend)
        .setChaining(isChainingEnabled)
        .setUserArtifacts(cacheFile)   // note:传递cacheFile
        .setTimeCharacteristic(timeCharacteristic)
        .setDefaultBufferTimeout(bufferTimeout);
}

org.apache.flink.streaming.api.graph.StreamGraphGenerator#generate
public StreamGraph generate() {
    streamGraph = new StreamGraph(executionConfig, checkpointConfig, savepointRestoreSettings);
    streamGraph.setStateBackend(stateBackend);
    streamGraph.setChaining(chaining);
    streamGraph.setScheduleMode(scheduleMode);
    streamGraph.setUserArtifacts(userArtifacts); // note:传递userArtifacts
    streamGraph.setTimeCharacteristic(timeCharacteristic);
    streamGraph.setJobName(jobName);
    streamGraph.setBlockingConnectionsBetweenChains(blockingConnectionsBetweenChains);

    alreadyTransformed = new HashMap<>();

    for (Transformation<?> transformation: transformations) {
        transform(transformation);
    }

    final StreamGraph builtStreamGraph = streamGraph;

    alreadyTransformed.clear();
    alreadyTransformed = null;
    streamGraph = null;

    return builtStreamGraph;
}
  1. 在生成JobGraph时将StreamGraph的userArtifacts 传递给JobGraph的userArtifacts。如果缓存文件为本地文件夹则会将该文件夹压缩为.zip格式存储在客户端的临时文件夹中,并使用新的存储路径。
org.apache.flink.streaming.api.graph.StreamingJobGraphGenerator#createJobGraph()
    
private JobGraph createJobGraph() {
    ...
    JobGraphGenerator.addUserArtifactEntries(streamGraph.getUserArtifacts(), jobGraph);
    ...
    return jobGraph;
}

public static void addUserArtifactEntries(Collection<Tuple2<String, DistributedCache.DistributedCacheEntry>> userArtifacts, JobGraph jobGraph) {
    if (userArtifacts != null && !userArtifacts.isEmpty()) {
        try {
            java.nio.file.Path tmpDir = Files.createTempDirectory("flink-distributed-cache-" + jobGraph.getJobID());
            for (Tuple2<String, DistributedCache.DistributedCacheEntry> originalEntry : userArtifacts) {
                Path filePath = new Path(originalEntry.f1.filePath);
                boolean isLocalDir = false;
                try {
                    FileSystem sourceFs = filePath.getFileSystem();
                    isLocalDir = !sourceFs.isDistributedFS() && sourceFs.getFileStatus(filePath).isDir();
                } catch (IOException ioe) {
                    LOG.warn("Could not determine whether {} denotes a local path.", filePath, ioe);
                }
                // zip local directories because we only support file uploads
                DistributedCache.DistributedCacheEntry entry;
                if (isLocalDir) {
                    // note: 压缩本地文件夹,返回zip文件路径
                    Path zip = FileUtils.compressDirectory(filePath, new Path(tmpDir.toString(), filePath.getName() + ".zip"));
                    entry = new DistributedCache.DistributedCacheEntry(zip.toString(), originalEntry.f1.isExecutable, true);
                } else {
                    entry = new DistributedCache.DistributedCacheEntry(filePath.toString(), originalEntry.f1.isExecutable, false);
                }
                jobGraph.addUserArtifact(originalEntry.f0, entry);
            }
        } catch (IOException ioe) {
            throw new FlinkRuntimeException("Could not compress distributed-cache artifacts.", ioe);
        }
    }
}

4. yarnPerjob 模式部署jobGraph时,如果是本地文件则上传本地zip,返回该文件所在的hdfs路径。如果缓存文件为hdfs已存在路径,则直接写入配置文件。

org.apache.flink.yarn.YarnClusterDescriptor#startAppMaster

// only for per job mode
if (jobGraph != null) {
    for (Map.Entry<String, DistributedCache.DistributedCacheEntry> entry : jobGraph.getUserArtifacts().entrySet()) {
        org.apache.flink.core.fs.Path path = new org.apache.flink.core.fs.Path(entry.getValue().filePath);
        // only upload local files
        // note: 上传本地文件,返回hdfs中的路径存储在jobGraph的userArtifacts
        if (!path.getFileSystem().isDistributedFS()) {
            Path localPath = new Path(path.getPath());
            Tuple2<Path, Long> remoteFileInfo =
                Utils.uploadLocalFileToRemote(fs, appId.toString(), localPath, homeDir, entry.getKey());
            jobGraph.setUserArtifactRemotePath(entry.getKey(), remoteFileInfo.f0.toString());
        }
    }
    // 将分布式缓存文件信息写入到Configuration中
    jobGraph.writeUserArtifactEntriesToConfiguration();
}


DistributedCache#writeFileInfoToConfig
public static void writeFileInfoToConfig(String name, DistributedCacheEntry e, Configuration conf) {
    int num = conf.getInteger(CACHE_FILE_NUM, 0) + 1;
    conf.setInteger(CACHE_FILE_NUM, num);
    conf.setString(CACHE_FILE_NAME + num, name);
    // note: DISTRIBUTED_CACHE_FILE_PATH_0
    conf.setString(CACHE_FILE_PATH + num, e.filePath);
    conf.setBoolean(CACHE_FILE_EXE + num, e.isExecutable || new File(e.filePath).canExecute());
    conf.setBoolean(CACHE_FILE_DIR + num, e.isZipped || new File(e.filePath).isDirectory());
    if (e.blobKey != null) {
        conf.setBytes(CACHE_FILE_BLOB_KEY + num, e.blobKey);
    }
}
  1. Task执行时,会先读取缓存文件中,并传递给RuntimeEnvironment,这样便可以根据注册名称获取文件。
    1. 从config文件中读取缓存文件路径。
    2. 创建临时文件,将缓存文件从hdfs异步拷贝到当前TM,并将拷贝后的本地路径存储在内存中。临时文件夹格式flink-dist-cache-uuid/jobId/。
org.apache.flink.runtime.taskmanager.Task#doRun
private void doRun() {
    .......
    // all resource acquisitions and registrations from here on
    // need to be undone in the end
    Map<String, Future<Path>> distributedCacheEntries = new HashMap<>();

    // next, kick off the background copying of files for the distributed cache
    try {
        for (Map.Entry<String, DistributedCache.DistributedCacheEntry> entry :
                DistributedCache.readFileInfoFromConfig(jobConfiguration)) {
            LOG.info("Obtaining local cache file for '{}'.", entry.getKey());
            
            Future<Path> cp = fileCache.createTmpFile(entry.getKey(), entry.getValue(), jobId, executionId);
            distributedCacheEntries.put(entry.getKey(), cp);
        }
    }
    catch (Exception e) {
        throw new Exception(
            String.format("Exception while adding files to distributed cache of task %s (%s).", taskNameWithSubtask, executionId), e);
    }

    Environment env = new RuntimeEnvironment(
        jobId,
        vertexId,
        executionId,
        executionConfig,
        taskInfo,
        jobConfiguration,
        taskConfiguration,
        userCodeClassLoader,
        memoryManager,
        ioManager,
        broadcastVariableManager,
        taskStateManager,
        aggregateManager,
        accumulatorRegistry,
        kvStateRegistry,
        inputSplitProvider,
        distributedCacheEntries,  // note: 
        consumableNotifyingPartitionWriters,
        inputGates,
        taskEventDispatcher,
        checkpointResponder,
        taskManagerConfig,
        metrics,
        this);
}


public Future<Path> createTmpFile(String name, DistributedCacheEntry entry, JobID jobID, ExecutionAttemptID executionId) throws Exception {
    synchronized (lock) {
        Map<String, Future<Path>> jobEntries = entries.computeIfAbsent(jobID, k -> new HashMap<>());

        // register reference holder
        final Set<ExecutionAttemptID> refHolders = jobRefHolders.computeIfAbsent(jobID, id -> new HashSet<>());
        refHolders.add(executionId);

        Future<Path> fileEntry = jobEntries.get(name);
        if (fileEntry != null) {
            // file is already in the cache. return a future that
            // immediately returns the file
            return fileEntry;
        } else {
            // need to copy the file
            
            // create the target path
            File tempDirToUse = new File(storageDirectories[nextDirectory++], jobID.toString());
            if (nextDirectory >= storageDirectories.length) {
                nextDirectory = 0;
            }

            // kick off the copying
            Callable<Path> cp;
            if (entry.blobKey != null) {
                cp = new CopyFromBlobProcess(entry, jobID, blobService, new Path(tempDirToUse.getAbsolutePath()));
            } else {
                ## note: 从hdfs异步拷贝到TM内部文件夹
                cp = new CopyFromDFSProcess(entry, new Path(tempDirToUse.getAbsolutePath()));
            }
            FutureTask<Path> copyTask = new FutureTask<>(cp);
            executorService.submit(copyTask);

            // store our entry
            jobEntries.put(name, copyTask);

            return copyTask;
        }
    }
}


  1. 算子在open函数中,读取缓存文件。
org.apache.flink.api.common.cache.DistributedCache#getFile

public File getFile(String name) {
    // note: Map<String, Future<Path>> distributedCacheEntries 
    Future<Path> future = cacheCopyTasks.get(name);

    try {
        final Path path = future.get();
        URI tmp = path.makeQualified(path.getFileSystem()).toUri();
        return new File(tmp);
    }
    catch (ExecutionException e) {
        throw new RuntimeException("An error occurred while copying the file.", e.getCause());
    }
    catch (Exception e) {
        throw new RuntimeException("Error while getting the file registered under '" + name +
                "' from the distributed cache", e);
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,869评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,716评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,223评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,047评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,089评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,839评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,516评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,410评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,920评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,052评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,179评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,868评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,522评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,070评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,186评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,487评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,162评论 2 356

推荐阅读更多精彩内容