-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUniformIncrementSourceFunction.java
65 lines (48 loc) · 2.15 KB
/
UniformIncrementSourceFunction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package ch.ethz.systems.strymon.ds2.flink.wordcount.sources;
import ch.ethz.systems.strymon.ds2.common.RandomSentenceGenerator;
import org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UniformIncrementSourceFunction extends RichParallelSourceFunction<String> {
private static final Logger logger = LoggerFactory.getLogger(UniformIncrementSourceFunction.class);
/** how many sentences to output per second for rate_1 **/
private final int sentenceRateStart;
/** how many sentences to output per second for rate_2 **/
private final int timeStep;
/** the time period for rate_1 in minutes **/
private final int incrementSize;
/** the length of each sentence (in chars) **/
private final int sentenceSize;
private final RandomSentenceGenerator generator;
private volatile boolean running = true;
public UniformIncrementSourceFunction(int rate, int step, int increment, int size) {
sentenceRateStart = rate;
timeStep = step;
incrementSize = increment;
generator = new RandomSentenceGenerator();
sentenceSize = size;
}
@Override
public void run(SourceContext<String> ctx) throws Exception {
long startTime = System.currentTimeMillis();
while (running) {
long emitStartTime = System.currentTimeMillis();
long runTime = ((System.currentTimeMillis() - startTime) / 1000);
int sentenceRate = (int) Math.round(sentenceRateStart + (Math.floor(runTime/timeStep) * incrementSize));
logger.info("sentenceRate is {}", sentenceRate);
for (int i = 0; i < sentenceRate; i++) {
ctx.collect(generator.nextSentence(sentenceSize));
}
// Sleep for the rest of timeslice if needed
long emitTime = System.currentTimeMillis() - emitStartTime;
if (emitTime < 1000) {
Thread.sleep(1000 - emitTime);
}
}
ctx.close();
}
@Override
public void cancel() {
running = false;
}
}