-
Notifications
You must be signed in to change notification settings - Fork 498
[FLINK-33525] Move ImpulseSource to new Source API #950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
618d50a
de5fb01
0d2bd2d
eb8ef85
de713b7
fabd090
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,12 +18,16 @@ | |
|
|
||
| package autoscaling; | ||
|
|
||
| import org.apache.flink.api.common.eventtime.WatermarkStrategy; | ||
| import org.apache.flink.api.common.functions.RichFlatMapFunction; | ||
| import org.apache.flink.api.common.typeinfo.Types; | ||
| import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy; | ||
| import org.apache.flink.api.java.utils.ParameterTool; | ||
| import org.apache.flink.connector.datagen.source.DataGeneratorSource; | ||
| import org.apache.flink.connector.datagen.source.GeneratorFunction; | ||
| import org.apache.flink.streaming.api.datastream.DataStream; | ||
| import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; | ||
| import org.apache.flink.streaming.api.functions.sink.DiscardingSink; | ||
| import org.apache.flink.streaming.api.functions.source.SourceFunction; | ||
| import org.apache.flink.util.Collector; | ||
|
|
||
| import org.slf4j.Logger; | ||
|
|
@@ -74,8 +78,38 @@ public static void main(String[] args) throws Exception { | |
| for (String branch : maxLoadPerTask.split("\n")) { | ||
| String[] taskLoads = branch.split(";"); | ||
|
|
||
| /* | ||
| * Creates an unbounded stream that continuously emits the constant value 42L. | ||
| * Flink's DataGeneratorSource with RateLimiterStrategy is used to control the emission rate. | ||
| * | ||
| * Rate Calculation: | ||
| * - samplingIntervalMs / 10 gives maxSleepTimeMs, which represents the interval between emissions. | ||
| * - To determine the number of records emitted per second: | ||
| * 1000 / maxSleepTimeMs | ||
| * Since 1000 ms equals 1 second, this formula calculates the emission rate. | ||
| * | ||
| * Example: | ||
| * - If samplingIntervalMs = 1000 ms: | ||
| * maxSleepTimeMs = 100 ms | ||
| * - This results in: | ||
| * 1000 ms / 100 ms = 10 records per second. | ||
| * | ||
| * RateLimiterStrategy.perSecond((double) 1000 / ((double) samplingIntervalMs / 10)) | ||
| * ensures this rate is maintained efficiently without blocking execution. | ||
| */ | ||
| DataStream<Long> stream = | ||
| env.addSource(new ImpulseSource(samplingIntervalMs)).name("ImpulseSource"); | ||
| env.fromSource( | ||
| new DataGeneratorSource<>( | ||
| (GeneratorFunction<Long, Long>) | ||
| (index) -> 42L, // Emits constant value 42 | ||
| Long.MAX_VALUE, // Unbounded stream | ||
| RateLimiterStrategy.perSecond( | ||
| (double) 1000 | ||
|
||
| / ((double) samplingIntervalMs | ||
| / 10)), // Controls rate | ||
| Types.LONG), | ||
| WatermarkStrategy.noWatermarks(), | ||
| "ImpulseSource (Using DataGeneratorSource)"); | ||
|
|
||
| for (String load : taskLoads) { | ||
| double maxLoad = Double.parseDouble(load); | ||
|
|
@@ -97,31 +131,6 @@ public static void main(String[] args) throws Exception { | |
| + ")"); | ||
| } | ||
|
|
||
| private static class ImpulseSource implements SourceFunction<Long> { | ||
| private final int maxSleepTimeMs; | ||
| volatile boolean canceled; | ||
|
|
||
| public ImpulseSource(int samplingInterval) { | ||
| this.maxSleepTimeMs = samplingInterval / 10; | ||
| } | ||
|
|
||
| @Override | ||
| public void run(SourceContext<Long> sourceContext) throws Exception { | ||
| while (!canceled) { | ||
| synchronized (sourceContext.getCheckpointLock()) { | ||
| sourceContext.collect(42L); | ||
| } | ||
| // Provide an impulse to keep the load simulation active | ||
| Thread.sleep(maxSleepTimeMs); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void cancel() { | ||
| canceled = true; | ||
| } | ||
| } | ||
|
|
||
| private static class LoadSimulationFn extends RichFlatMapFunction<Long, Long> { | ||
|
|
||
| private final double maxLoad; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this section needs to be adjusted. Since we do not control the maxSleepTimeMs directly, but it is rather controlled internally by the Guava's token bucket algorithm, it should not be explicitly mentioned here. It is also not strictly guaranteed to be max - at the startup there can be a short burst. The value 10 seems to just be a hardcoded parameter meaning that we want at least 10 impulses per sampling interval. Basically it should rather explain: check how many sampling intervals are there within a second, make sure that 10 impulses are generated for each sampling interval (IMPULSES_PER_SAMPLING_INTERVAL).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the comment. Let me know if this makes sense.