简介
阅读Flink源码的时候,经常看到如addSource、map、flatmap等方法里面都会调用clean()方法,阅读其注释给人一种抽象、触摸不到的感觉。
public <OUT> DataStreamSource<OUT> addSource(SourceFunction<OUT> function, String sourceName, TypeInformation<OUT> typeInfo) {
...
clean(function);
...
}
Returns a “closure-cleaned” version of the given function. Cleans only if closure cleaning is not disabled in the {@link org.apache.flink.api.common.ExecutionConfig}
带着clean()方法的价值以及实现原理等问题,我们开始对其源码的学习之旅。
clean()方法原理及作用
Flink任务常使用内部类来完成业务逻辑开发,在编译代码的时候,默认内部类会持有一个外部对象的引用。如果外部对象没有实现序列化接口,序列化内部类对象就会失败。clean()方法就是将内部类指向外部类的引用设置为null,确保序列化过程的成功。在clean()
方法中首先调用ClosureCleaner.clean()
方法,然后再调用ClosureCleaner.ensureSerializable(f);
public <F> F clean(F f) {
if (getConfig().isClosureCleanerEnabled()) {
ClosureCleaner.clean(f, getConfig().getClosureCleanerLevel(), true);
}
ClosureCleaner.ensureSerializable(f);
return f;
}
接下来,我们通过一个例子来验证上述的知识点。
public class Outer {
public class Inner implements Serializable {
}
public static void main(String[] args) {
Outer outer = new Outer();
Inner inner = outer.new Inner();
// ClosureCleaner.clean(inner, ExecutionConfig.ClosureCleanerLevel.TOP_LEVEL, false);
ClosureCleaner.ensureSerializable(inner);
}
}
直接执行上述代码,抛出异常:
Exception in thread "main" org.apache.flink.api.common.InvalidProgramException: Object flink.sourcecode.Outer$Inner@5c0369c4 is not serializable
at org.apache.flink.api.java.ClosureCleaner.ensureSerializable(ClosureCleaner.java:180)
at flink.sourcecode.Outer.main(Outer.java:20)
Caused by: java.io.NotSerializableException: flink.sourcecode.Outer
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at org.apache.flink.util.InstantiationUtil.serializeObject(InstantiationUtil.java:586)
at org.apache.flink.api.java.ClosureCleaner.ensureSerializable(ClosureCleaner.java:178)
... 1 more
很显然,在调用ClosureCleaner.ensureSerializable(inner);
序列化inner
对象出现了问题。是不是很意外,为什么Inner
类明明实现了Serializable
接口,为什么还会序列化失败呢?很显然,inner
对象中包含了某些导致序列化失败。通过debug方式我们可以知道inner
对象实际上包含了this$0
变量,并且该值指向外部引用对象Outer@487
。但是,Outer
类并没有实现Serializable
,显然,序列化失败的原因找到了,也正如异常栈所说的java.io.NotSerializableException: flink.sourcecode.Outer
。
当我们加上ClosureCleaner.clean(inner, ExecutionConfig.ClosureCleanerLevel.TOP_LEVEL, false);
语句后,再次执行,顺利执行。
显然,clean()
方法将inner
对象所持有的外部对象的引用Outer
置为null
,确保了inner
对象序列化成功。
clean()源码分析
Flink中常见的clean()
方法实际上调用的是StreamExecutionEnvironment#clean(F f)
方法。在该方法中,首先会根据getConfig().isClosureCleanerEnabled()
判断是否需要将f
可能持有的外部对象的引用置为null
,进而完成当前对象f
的序列化操作。
public <F> F clean(F f) {
if (getConfig().isClosureCleanerEnabled()) {
ClosureCleaner.clean(f, getConfig().getClosureCleanerLevel(), true);
}
ClosureCleaner.ensureSerializable(f);
return f;
}
判断当前是否需要进行闭环清除的操作,closureCleanerLevel
默认取值为RECURSIVE
。
public boolean isClosureCleanerEnabled() {
return !(closureCleanerLevel == ClosureCleanerLevel.NONE);
}
紧接着,进入ClosureCleaner.clean()
方法,详解代码中注释以方便解说。
public static void clean(Object func, ExecutionConfig.ClosureCleanerLevel level, boolean checkSerializable) {
// 实际上调用重写的方法
clean(func, level, checkSerializable, Collections.newSetFromMap(new IdentityHashMap<>()));
}
private static void clean(Object func, ExecutionConfig.ClosureCleanerLevel level, boolean checkSerializable, Set<Object> visited) {
if (func == null) {
return;
}
// visited是一个Set集合,用于留在后面递归遍历每个字段时使用。
if (!visited.add(func)) {
return;
}
final Class<?> cls = func.getClass();
// 如果当前对象是基本数据类型或者其包装类,直接返回,如Integer、Double等。
if (ClassUtils.isPrimitiveOrWrapper(cls)) {
return;
}
// 如果当前对象是用户自定义的序列化类(实现Externalizable接口,或者重写writeObject、writeReplace方法),直接返回
if (usesCustomSerialization(cls)) {
return;
}
// 目前来看,主要用于日志使用,这里不做解释
boolean closureAccessed = false;
// 遍历cls所包含的字段信息
for (Field f: cls.getDeclaredFields()) {
// 如果当前内部类对象持有外部类的引用this$0、this$1...时
if (f.getName().startsWith("this$")) {
// cleanThis0(func, cls, f.getName())操作中,主要涉及到jvm层面的知识,这里不做展开说明,读者可以自行阅读,逻辑比较简单
// cleanThis0(func, cls, f.getName())操作主要是将func对象持有的外部对象的引用置为null
closureAccessed |= cleanThis0(func, cls, f.getName());
} else {
// 如果当前func对象中包含其他变量,进入该代码块
Object fieldObject;
try {
f.setAccessible(true);
fieldObject = f.get(func);
} catch (IllegalAccessException e) {
throw new RuntimeException(String.format("Can not access to the %s field in Class %s", f.getName(), func.getClass()));
}
// 满足条件,则会递归调用该clean()方法,并以当前变量为func值进行递归执行,对inner中包含的或者间接包含的所有变量可能包含的持有外部类的引用置为null
// needsRecursion()方法用以判断当前变量能否进行递归迭代遍历,判断条件:不为null、非静态、非瞬时。
if (level == ExecutionConfig.ClosureCleanerLevel.RECURSIVE && needsRecursion(f, fieldObject)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Dig to clean the {}", fieldObject.getClass().getName());
}
clean(fieldObject, ExecutionConfig.ClosureCleanerLevel.RECURSIVE, true, visited);
}
}
}
// checkSerializable变量,默认为true,表示是否需要对进行ClosureCleaner#clean()操作后的func对象进行序列化操作,
// 以提前发现问题,并以异常信息的方式打印出详细的信息。
if (checkSerializable) {
try {
InstantiationUtil.serializeObject(func);
}
catch (Exception e) {
...
}
}
}
最后,序列化进行闭环清除(ClosureCleaner#clean()
)后的对象。
public static void ensureSerializable(Object obj) {
try {
InstantiationUtil.serializeObject(obj);
} catch (Exception e) {
throw new InvalidProgramException("Object " + obj + " is not serializable", e);
}
}
ClosureCleanerLevel 是一个枚举类,有三个值:NONE,TOP_LEVEL,RECURSIVE。NONE表示不对当前对象进行闭环清除操作。TOP_LEVEL、RECURSIVE两个都会进行闭环清除操作,但是两者之间的作用范围又各不相同。TOP_LEVEL只会对当前对象进行闭环清除操作,不会对当前对象包含的其他变量进行闭环清除操作。RECURSIVE不光对当前对象进行闭环清除操作,也会对当前对象包含的其他变量及这些变量可能包含的变量也做闭环清除操作,更完全、更完整、也更详细。举个简单例子,如果A为外部类,B为其内部类并实现了Serializable,B里面包含了变量D d,而D是C的内部类并实现了Serializable接口,但是C并没有。这个时候,如果使用TOP_LEVEL级别,则仍然会序列化失败,因为并没有将D对象持有的外部类C对象的引用置为null。
public enum ClosureCleanerLevel {
/**
* Disable the closure cleaner completely.
*/
NONE,
/**
* Clean only the top-level class without recursing into fields.
*/
TOP_LEVEL,
/**
* Clean all the fields recursively.
*/
RECURSIVE
}
测试TOP_LEVEL
与RECURSIVE
异同代码如下:
public class A {
public class B implements Serializable {
private C.D d = new C().new D();
}
public static void main(String[] args) {
A a = new A();
B b = a.new B();
ClosureCleaner.clean(b, ExecutionConfig.ClosureCleanerLevel.TOP_LEVEL, false);
ClosureCleaner.ensureSerializable(b);
}
}
public class C {
class D implements Serializable {
}
}
特别说明
用户如果拿Flink任务来跟进这方面的源码时,务必使用第二个代码。原因是因为第一个代码中的内部类并不会持有外部类Test对象的应用。至于原因,这里不做解释,可以参考该篇文章。
public class Test {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.generateSequence(1, 10)
.map(new MapFunction<Long, Long>() {
public Long map(Long value) {
return value * 2;
}
})
.print();
env.execute();
}
}
public class Test {
public void runProgram() throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.generateSequence(1, 10)
.map(new MapFunction<Long, Long>() {
public Long map(Long value) {
return value * 2;
}
})
.print();
env.execute();
}
public static void main(String[] args) throws Exception {
new Test().runProgram();
}
}