本文主要研究一下flink的BlobStoreService

BlobView

flink-release-1.7.2/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobView.java

public interface BlobView {

	/**
* Copies a blob to a local file.
*
* @param jobId ID of the job this blob belongs to (or <tt>null</tt> if job-unrelated)
* @param blobKey The blob ID
* @param localFile The local file to copy to
*
* @return whether the file was copied (<tt>true</tt>) or not (<tt>false</tt>)
* @throws IOException If the copy fails
*/
boolean get(JobID jobId, BlobKey blobKey, File localFile) throws IOException;
}
复制代码
  • BlobView定义了get方法,将指定的blob拷贝到localFile

BlobStore

flink-release-1.7.2/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobStore.java

public interface BlobStore extends BlobView {

	/**
* Copies the local file to the blob store.
*
* @param localFile The file to copy
* @param jobId ID of the job this blob belongs to (or <tt>null</tt> if job-unrelated)
* @param blobKey The ID for the file in the blob store
*
* @return whether the file was copied (<tt>true</tt>) or not (<tt>false</tt>)
* @throws IOException If the copy fails
*/
boolean put(File localFile, JobID jobId, BlobKey blobKey) throws IOException; /**
* Tries to delete a blob from storage.
*
* <p>NOTE: This also tries to delete any created directories if empty.</p>
*
* @param jobId ID of the job this blob belongs to (or <tt>null</tt> if job-unrelated)
* @param blobKey The blob ID
*
* @return <tt>true</tt> if the given blob is successfully deleted or non-existing;
* <tt>false</tt> otherwise
*/
boolean delete(JobID jobId, BlobKey blobKey); /**
* Tries to delete all blobs for the given job from storage.
*
* <p>NOTE: This also tries to delete any created directories if empty.</p>
*
* @param jobId The JobID part of all blobs to delete
*
* @return <tt>true</tt> if the job directory is successfully deleted or non-existing;
* <tt>false</tt> otherwise
*/
boolean deleteAll(JobID jobId);
}
复制代码
  • BlobStore继承了BlobView,它定义了put、delete、deleteAll方法

BlobStoreService

flink-release-1.7.2/flink-runtime/src/main/java/org/apache/flink/runtime/blob/BlobStoreService.java

public interface BlobStoreService extends BlobStore, Closeable {

	/**
* Closes and cleans up the store. This entails the deletion of all blobs.
*/
void closeAndCleanupAllData();
}
复制代码
  • BlobStoreService继承了BlobStore及Closeable接口,它定义了closeAndCleanupAllData方法;它有两个实现类,分别是VoidBlobStore、FileSystemBlobStore

VoidBlobStore

flink-release-1.7.2/flink-runtime/src/main/java/org/apache/flink/runtime/blob/VoidBlobStore.java

public class VoidBlobStore implements BlobStoreService {

	@Override
public boolean put(File localFile, JobID jobId, BlobKey blobKey) throws IOException {
return false;
} @Override
public boolean get(JobID jobId, BlobKey blobKey, File localFile) throws IOException {
return false;
} @Override
public boolean delete(JobID jobId, BlobKey blobKey) {
return true;
} @Override
public boolean deleteAll(JobID jobId) {
return true;
} @Override
public void closeAndCleanupAllData() {} @Override
public void close() throws IOException {}
}
复制代码
  • VoidBlobStore实现了BlobStoreService接口,它执行空操作

FileSystemBlobStore

flink-release-1.7.2/flink-runtime/src/main/java/org/apache/flink/runtime/blob/FileSystemBlobStore.java

public class FileSystemBlobStore implements BlobStoreService {

	private static final Logger LOG = LoggerFactory.getLogger(FileSystemBlobStore.class);

	/** The file system in which blobs are stored. */
private final FileSystem fileSystem; /** The base path of the blob store. */
private final String basePath; public FileSystemBlobStore(FileSystem fileSystem, String storagePath) throws IOException {
this.fileSystem = checkNotNull(fileSystem);
this.basePath = checkNotNull(storagePath) + "/blob"; LOG.info("Creating highly available BLOB storage directory at {}", basePath); fileSystem.mkdirs(new Path(basePath));
LOG.debug("Created highly available BLOB storage directory at {}", basePath);
} // - Put ------------------------------------------------------------------ @Override
public boolean put(File localFile, JobID jobId, BlobKey blobKey) throws IOException {
return put(localFile, BlobUtils.getStorageLocationPath(basePath, jobId, blobKey));
} private boolean put(File fromFile, String toBlobPath) throws IOException {
try (OutputStream os = fileSystem.create(new Path(toBlobPath), FileSystem.WriteMode.OVERWRITE)) {
LOG.debug("Copying from {} to {}.", fromFile, toBlobPath);
Files.copy(fromFile, os);
}
return true;
} // - Get ------------------------------------------------------------------ @Override
public boolean get(JobID jobId, BlobKey blobKey, File localFile) throws IOException {
return get(BlobUtils.getStorageLocationPath(basePath, jobId, blobKey), localFile, blobKey);
} private boolean get(String fromBlobPath, File toFile, BlobKey blobKey) throws IOException {
checkNotNull(fromBlobPath, "Blob path");
checkNotNull(toFile, "File");
checkNotNull(blobKey, "Blob key"); if (!toFile.exists() && !toFile.createNewFile()) {
throw new IOException("Failed to create target file to copy to");
} final Path fromPath = new Path(fromBlobPath);
MessageDigest md = BlobUtils.createMessageDigest(); final int buffSize = 4096; // like IOUtils#BLOCKSIZE, for chunked file copying boolean success = false;
try (InputStream is = fileSystem.open(fromPath);
FileOutputStream fos = new FileOutputStream(toFile)) {
LOG.debug("Copying from {} to {}.", fromBlobPath, toFile); // not using IOUtils.copyBytes(is, fos) here to be able to create a hash on-the-fly
final byte[] buf = new byte[buffSize];
int bytesRead = is.read(buf);
while (bytesRead >= 0) {
fos.write(buf, 0, bytesRead);
md.update(buf, 0, bytesRead); bytesRead = is.read(buf);
} // verify that file contents are correct
final byte[] computedKey = md.digest();
if (!Arrays.equals(computedKey, blobKey.getHash())) {
throw new IOException("Detected data corruption during transfer");
} success = true;
} finally {
// if the copy fails, we need to remove the target file because
// outside code relies on a correct file as long as it exists
if (!success) {
try {
toFile.delete();
} catch (Throwable ignored) {}
}
} return true; // success is always true here
} // - Delete --------------------------------------------------------------- @Override
public boolean delete(JobID jobId, BlobKey blobKey) {
return delete(BlobUtils.getStorageLocationPath(basePath, jobId, blobKey));
} @Override
public boolean deleteAll(JobID jobId) {
return delete(BlobUtils.getStorageLocationPath(basePath, jobId));
} private boolean delete(String blobPath) {
try {
LOG.debug("Deleting {}.", blobPath); Path path = new Path(blobPath); boolean result = fileSystem.delete(path, true); // send a call to delete the directory containing the file. This will
// fail (and be ignored) when some files still exist.
try {
fileSystem.delete(path.getParent(), false);
fileSystem.delete(new Path(basePath), false);
} catch (IOException ignored) {}
return result;
}
catch (Exception e) {
LOG.warn("Failed to delete blob at " + blobPath);
return false;
}
} @Override
public void closeAndCleanupAllData() {
try {
LOG.debug("Cleaning up {}.", basePath); fileSystem.delete(new Path(basePath), true);
}
catch (Exception e) {
LOG.error("Failed to clean up recovery directory.", e);
}
} @Override
public void close() throws IOException {
// nothing to do for the FileSystemBlobStore
}
}
复制代码
  • FileSystemBlobStore实现了BlobStoreService,它的构造器要求传入fileSystem及storagePath;put方法通过fileSystem.create来创建目标OutputStream,然后通过Files.copy把localFile拷贝到toBlobPath;get方法通过fileSystem.open打开要读取的blob,然后写入到localFile;delete及deleteAll方法通过BlobUtils.getStorageLocationPath获取blobPath,然后调用fileSystem.delete来删除;closeAndCleanupAllData方法直接调用fileSystem.delete来递归删除整个storagePath

小结

  • BlobView定义了get方法,将指定的blob拷贝到localFile;BlobStore继承了BlobView,它定义了put、delete、deleteAll方法
  • BlobStoreService继承了BlobStore及Closeable接口,它定义了closeAndCleanupAllData方法;它有两个实现类,分别是VoidBlobStore、FileSystemBlobStore
  • VoidBlobStore实现了BlobStoreService接口,它执行空操作;FileSystemBlobStore实现了BlobStoreService,它的构造器要求传入fileSystem及storagePath;put方法通过fileSystem.create来创建目标OutputStream,然后通过Files.copy把localFile拷贝到toBlobPath;get方法通过fileSystem.open打开要读取的blob,然后写入到localFile;delete及deleteAll方法通过BlobUtils.getStorageLocationPath获取blobPath,然后调用fileSystem.delete来删除;closeAndCleanupAllData方法直接调用fileSystem.delete来递归删除整个storagePath

doc

聊聊flink的BlobStoreService的更多相关文章

  1. 聊聊flink的NetworkEnvironmentConfiguration

    本文主要研究一下flink的NetworkEnvironmentConfiguration NetworkEnvironmentConfiguration flink-1.7.2/flink-runt ...

  2. 聊聊flink的CsvTableSource

    序 本文主要研究一下flink的CsvTableSource TableSource flink-table_2.11-1.7.1-sources.jar!/org/apache/flink/tabl ...

  3. 聊聊flink Table的groupBy操作

    本文主要研究一下flink Table的groupBy操作 Table.groupBy flink-table_2.11-1.7.0-sources.jar!/org/apache/flink/tab ...

  4. 聊聊flink的AsyncWaitOperator

    序本文主要研究一下flink的AsyncWaitOperator AsyncWaitOperatorflink-streaming-java_2.11-1.7.0-sources.jar!/org/a ...

  5. 聊聊flink的Async I/O

    // This example implements the asynchronous request and callback with Futures that have the // inter ...

  6. 聊聊flink的log.file配置

    本文主要研究一下flink的log.file配置 log4j.properties flink-release-1.6.2/flink-dist/src/main/flink-bin/conf/log ...

  7. [case49]聊聊flink的checkpoint配置

    序 本文主要研究下flink的checkpoint配置 实例 StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecut ...

  8. [源码分析] 从源码入手看 Flink Watermark 之传播过程

    [源码分析] 从源码入手看 Flink Watermark 之传播过程 0x00 摘要 本文将通过源码分析,带领大家熟悉Flink Watermark 之传播过程,顺便也可以对Flink整体逻辑有一个 ...

  9. Flink与Spark Streaming在与kafka结合的区别!

    本文主要是想聊聊flink与kafka结合.当然,单纯的介绍flink与kafka的结合呢,比较单调,也没有可对比性,所以的准备顺便帮大家简单回顾一下Spark Streaming与kafka的结合. ...

随机推荐

  1. Linux学习66 运维安全-通信加密和解密技术入门

    一.Linux Service and Security 1.OpenSSL(ssl/tls)协议 2.OpenSSH(ssh)协议 3.bind(dns) 4.web(http):httpd(apa ...

  2. Maven多模块项目+MVC框架+AJAX技术+layui分页对数据库增删改查实例

    昨天刚入门Maven多模块项目,所以简单写了一个小测试,就是对数据库单表的增删改查,例子比较综合,写得哪里不妥还望大神赐教,感谢! 首先看一下项目结构: 可以看到,一个项目MavenEmployee里 ...

  3. Linux基础:Day04

    进程管理1.操作系统基础 调用:kernel通过给应用程序提供system call方式来提供硬件资源:  注意:这个应用程序也包括库文件:  库文件是运行在ring 0上的一段程序代码,不对客户直接 ...

  4. Vulnhub DC-3靶机渗透

    修改错误配置 打开了ova文件会发现,怎么也找不到DC-3的ip地址,估计是网卡出了问题. 那么就先配置下网卡. 进入上面这个页面之前按e. 将这里的ro 替换为 rw signie init=/bi ...

  5. 这个案例写出来,还怕跟面试官扯不明白 OAuth2 登录流程?

    昨天和小伙伴们介绍了 OAuth2 的基本概念,在讲解 Spring Cloud Security OAuth2 之前,我还是先来通过实际代码来和小伙伴们把 OAuth2 中的各个授权模式走一遍,今天 ...

  6. es实现mysql的like查询

    es版本6.8 因为阿里云的dts同步最高支持es版本就是6.8 构建索引 PUT /z_test/ { "mappings": { "doc": { &quo ...

  7. mysql 主键和默认 设为索引的规则

    一.mysql 表中如果是单主键的话,那这个主键也会被 系统默认建为 索引 二.mysql 表中如果是复合主键的话,那系统会遵循左对齐原则,即如复合主键 a 和 b字段和c字段..., 默认建的主键索 ...

  8. 一个hql 关键字member(非mysql)引起的 vo 数据 保存数据库错误

    2015-03-19 14:16:29,285 ERROR [Thread-3] (DAOHelper.java:312) - updateByEntityPK:com.agileeagle.dao. ...

  9. AJ学IOS 之微博项目实战(7)程序启动新特性用UICollectionViewController实现

    AJ分享,必须精品 一:效果 这里实现了大多数app都会有的软件新特性的功能,用的是UICollectionViewController实现的 二:思路 这里用了UICollectionViewCon ...

  10. L17 AlexNet VGG NiN GoogLeNet

    深度卷积神经网络(AlexNet) LeNet: 在大的真实数据集上的表现并不尽如⼈意. 1.神经网络计算复杂. 2.还没有⼤量深⼊研究参数初始化和⾮凸优化算法等诸多领域. 机器学习的特征提取:手工定 ...