BoundedOutOfOrdernessTimestampExtractor 实现了
AssignerWithPeriodicWatermarks (注册周期生成Watermarks)
源码如下
第一次注册会生成当前最大时间戳currentMaxTimestamp =lastEmittedWatermark + 时间间隔(maxOutOfOrderness)
public abstract class BoundedOutOfOrdernessTimestampExtractor<T> implements AssignerWithPeriodicWatermarks<T> {
private static final long serialVersionUID = 1L;
/** The current maximum timestamp seen so far. */
//定义当前最大时间戳
private long currentMaxTimestamp;
/** The timestamp of the last emitted watermark. */
//最后提交的时间戳
private long lastEmittedWatermark = Long.MIN_VALUE;
/**
* The (fixed) interval between the maximum seen timestamp seen in the records
* and that of the watermark to be emitted.
*/
private final long maxOutOfOrderness;
public BoundedOutOfOrdernessTimestampExtractor(Time maxOutOfOrderness) {
if (maxOutOfOrderness.toMilliseconds() < 0) {
throw new RuntimeException("Tried to set the maximum allowed " +
"lateness to " + maxOutOfOrderness + ". This parameter cannot be negative.");
}
this.maxOutOfOrderness = maxOutOfOrderness.toMilliseconds();
this.currentMaxTimestamp = Long.MIN_VALUE + this.maxOutOfOrderness;
}
public long getMaxOutOfOrdernessInMillis() {
return maxOutOfOrderness;
}
/**
* Extracts the timestamp from the given element.
*
* @param element The element that the timestamp is extracted from.
* @return The new timestamp.
*/
public abstract long extractTimestamp(T element);
@Override
public final Watermark getCurrentWatermark() {
// this guarantees that the watermark never goes backwards.
//这个句代码保证了生成的水印是单调递增的
//当前最大的时间戳减去延时时间和上次最后提交的水印时间比较
//保留最大的时间(减去延时时间)作为水印
long potentialWM = currentMaxTimestamp - maxOutOfOrderness;
if (potentialWM >= lastEmittedWatermark) {
lastEmittedWatermark = potentialWM;
}
return new Watermark(lastEmittedWatermark);
}
//提取数据中时间作为timestamp
//如果timestamp 大于最大的currentMaxTimestamp 就把currentMaxTimestamp 置为 timestamp
//返回当前提取到的timestamp
@Override
public final long extractTimestamp(T element, long previousElementTimestamp) {
long timestamp = extractTimestamp(element);
if (timestamp > currentMaxTimestamp) {
currentMaxTimestamp = timestamp;
}
return timestamp;
}
}