package com.cloudera.flume.handlers.debug;
import java.io.IOException;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cloudera.flume.conf.SourceFactory.SourceBuilder;
import com.cloudera.flume.core.Event;
import com.cloudera.flume.core.EventImpl;
import com.cloudera.flume.core.EventSource;
import com.cloudera.util.Clock;
static final Logger LOG = LoggerFactory.getLogger(SynthSourceRndSize.class);
final long total;
final long seed;
final Random rand;
long count = 0;
final int minBodySize;
final int maxBodySize;
this(total, lowLimit, upLimit, Clock.unixTime());
}
this.rand = new Random(seed);
this.seed = seed;
this.minBodySize = minsize;
this.maxBodySize = maxsize;
this.total = count;
}
@Override
public void close()
throws IOException {
LOG.info("closing SynthSourceRandSize");
}
@Override
public Event
next()
throws IOException {
if (count >= total && total != 0)
return null;
int size = this.minBodySize
+ this.rand.nextInt(this.maxBodySize - this.minBodySize);
count++;
byte[] data = new byte[size];
rand.nextBytes(data);
Event e = new EventImpl(data);
updateEventProcessingStats(e);
return e;
}
@Override
public void open()
throws IOException {
LOG.info("Resetting count and seed; openingSynthSourceRandSize");
count = 0;
rand.setSeed(seed);
}
public static SourceBuilder
builder() {
return new SourceBuilder() {
@Override
public EventSource
build(String... argv) {
int minSize = 0;
int maxSize = 10;
long total = 0;
if (argv.length != 3) {
throw new IllegalArgumentException(
"usage: synthrndsize(count=0, minsize=0, maxsize=10) // count=0 infinite");
}
total = Long.parseLong(argv[0]);
minSize = Integer.parseInt(argv[1]);
maxSize = Integer.parseInt(argv[2]);
return new SynthSourceRndSize(total, minSize, maxSize);
}
};
}
}