BufferTimeSizeWeightedFlow: 基于时间、大小和权重窗口的流聚合器
package com.taobao.powermsg3.sdk.streams;
import com.taobao.powermsg3.sdk.localserver.scheduler.Scheduler;
import com.taobao.powermsg3.sdk.util.ConstantFunction;
import io.vertx.core.Future;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;
import static com.taobao.pubsub.util.Helper.require;
/**
* 按照时间、大小窗口聚合
*
* @author 虎鸣, hepin.p@alibaba-inc.com
*/
class BufferTimeSizeWeightedFlow<T> extends PmFlow<T, List<T>> {
private final String TIMER_KEY = UUID.randomUUID().toString();
private final ArrayList<T> EMPTY_BUFFER = new ArrayList<>(0);
private final boolean emitEmptyBuffer;
private final int bufferNumberLimit;
private final long weightLimit;
private final Function<? super T, Long> weightFunction;
private final Duration bufferTimeInMillis;
private long currentWeightAccumulate;
private final BufferAccumulator<T> bufferAccumulator;
private final Scheduler scheduler;
public BufferTimeSizeWeightedFlow(final int bufferNumberLimit,
final long bufferTimeInMillis,
final Scheduler scheduler) {
this(bufferNumberLimit, bufferTimeInMillis, false, scheduler);
}
public BufferTimeSizeWeightedFlow(final int bufferNumberLimit,
final long bufferTimeInMillis,
final boolean emitEmptyBuffer,
final Scheduler scheduler) {
this(bufferNumberLimit, Long.MAX_VALUE, ConstantFunction.ALWAYS_ZERO, bufferTimeInMillis, emitEmptyBuffer,
new ListBufferAccumulator<>(),
scheduler);
}
public BufferTimeSizeWeightedFlow(final int bufferNumberLimit,
final long weightLimit,
final Function<? super T, Long> weightFunction,
final long bufferTimeInMillis,
final boolean emitEmptyBuffer,
final BufferAccumulator<T> bufferAccumulator,
final Scheduler scheduler) {
super();
require(bufferNumberLimit > 0, 'bufferSizeLimit should > 0');
require(bufferTimeInMillis > 1, 'bufferTime should > 1ms');
require(weightLimit >= 0, 'weight limit must >= 0');
require(weightFunction != null, 'weight function should not be null.');
require(bufferAccumulator != null, 'bufferAccumulator should not be null.');
require(scheduler != null, 'scheduler should not be null.');
this.bufferNumberLimit = bufferNumberLimit;
this.weightLimit = weightLimit;
this.weightFunction = weightFunction;
this.bufferTimeInMillis = Duration.ofMillis(bufferTimeInMillis);
this.emitEmptyBuffer = emitEmptyBuffer;
this.bufferAccumulator = bufferAccumulator;
this.scheduler = scheduler;
//schedule
scheduleNextTickWithFixDelay();
}
@Override
protected void onTimer(final Object timerKey) {
super.onTimer(timerKey);
if (bufferAccumulator.isNotEmpty()) {
final List<T> result = bufferAccumulator.result();
select(result).write(Collections.unmodifiableList(result));
bufferAccumulator.allocate(bufferNumberLimit);
} else if (emitEmptyBuffer) {
select(EMPTY_BUFFER).write(Collections.unmodifiableList(EMPTY_BUFFER));
}
}
@Override
protected Future<Void> onPush(final T data) {
final long currentElemWeight = weightFunction.apply(data);
require(currentElemWeight >= 0, 'weight function should return >= 0');
final long maybeWeightExceed = (currentWeightAccumulate + currentElemWeight) - weightLimit;
if (maybeWeightExceed == 0) {
bufferAccumulator.offer(data);
final List<T> currentBuffer = bufferAccumulator.result();
//reschedule
scheduleNextTickWithFixDelay();
bufferAccumulator.allocate(bufferNumberLimit);
currentWeightAccumulate = 0;
return select(currentBuffer).write(Collections.unmodifiableList(currentBuffer));
} else if (maybeWeightExceed > 0) {
final List<T> currentBuffer = bufferAccumulator.result();
//reschedule
scheduleNextTickWithFixDelay();
bufferAccumulator.allocate(bufferNumberLimit);
currentWeightAccumulate = 0;
select(currentBuffer).write(Collections.unmodifiableList(currentBuffer));
} else {
currentWeightAccumulate += currentElemWeight;
}
bufferAccumulator.offer(data);
if (bufferAccumulator.size() == bufferNumberLimit) {
final List<T> currentBuffer = bufferAccumulator.result();
//reschedule
scheduleNextTickWithFixDelay();
bufferAccumulator.allocate(bufferNumberLimit);
currentWeightAccumulate = 0;
return select(currentBuffer).write(Collections.unmodifiableList(currentBuffer));
}
return context.succeededFuture();
}
protected void scheduleNextTickWithFixDelay() {
//取消之前的timer
scheduler.cancel(TIMER_KEY);
//重新调度一个timer
scheduler.scheduleWithFixDelay(TIMER_KEY, bufferTimeInMillis, bufferTimeInMillis,
() -> onTimer(TIMER_KEY),
context);
}
@Override
protected Future<Void> onUpstreamFinish() {
scheduler.cancel(TIMER_KEY);
if (bufferAccumulator.isNotEmpty()) {
final List<T> currentBuffer = bufferAccumulator.result();
return select(currentBuffer).write(Collections.unmodifiableList(currentBuffer));
}
return super.onUpstreamFinish();
}
}
代码分析:
您提供的代码看起来是线程安全的,主要原因在于:
scheduleWithFixDelay方法内部使用了 Vert.x 的Context来保证在正确的线程中执行定时任务。 Vert.x 的Context机制可以确保代码在同一个线程中执行,从而避免了多线程并发问题。- 在
onPush和onUpstreamFinish方法中,也没有使用任何共享变量,都是在方法内部使用局部变量,因此也不会有并发问题。 这些方法都是基于事件驱动的,每个事件都会在一个独立的线程中执行,不会共享数据,因此不存在并发问题。
结论:
总体来说,您提供的代码在多线程环境下是线程安全的。
其他建议:
- 为了更加明确地说明代码的线程安全性,您可以在代码注释中添加一些说明,例如:
/**
* 按照时间、大小窗口聚合
*
* @author 虎鸣, hepin.p@alibaba-inc.com
* @since 2023-03-21
* @threadSafe
*/
class BufferTimeSizeWeightedFlow<T> extends PmFlow<T, List<T>> {
- 您可以使用一些测试工具来对代码进行测试,以验证其线程安全性。
希望以上分析对您有所帮助。如果您还有其他问题,请随时提问。
原文地址: https://www.cveoy.top/t/topic/lIfh 著作权归作者所有。请勿转载和采集!