use of io.mantisrx.runtime.PortRequest in project mantis by Netflix.
the class SinkPublisher method start.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void start(StageConfig<T, R> stage, Observable<Observable<R>> observablesToPublish) {
final Sink<R> sink = sinkHolder.getSinkAction();
int sinkPort = -1;
if (sinkHolder.isPortRequested()) {
sinkPort = portSelector.acquirePort();
}
// apply transform
Observable<R> merged = Observable.merge(observablesToPublish);
final Observable wrappedO = merged.lift(new MonitorOperator("worker_sink"));
Observable o = Observable.create(new Observable.OnSubscribe<Object>() {
@Override
public void call(Subscriber subscriber) {
logger.info("Got sink subscription, onSubscribe=" + onSubscribeAction);
wrappedO.doOnCompleted(observableOnCompleteCallback).doOnError(observableOnErrorCallback).doOnTerminate(observableTerminatedCallback).subscribe(subscriber);
if (onSubscribeAction != null) {
onSubscribeAction.call();
}
}
}).doOnUnsubscribe(new Action0() {
@Override
public void call() {
logger.info("Sink subscriptions clean up, action=" + onUnsubscribeAction);
if (onUnsubscribeAction != null)
onUnsubscribeAction.call();
}
}).share();
if (context.getWorkerInfo().getDurationType() == MantisJobDurationType.Perpetual) {
// eager subscribe, don't allow unsubscribe back
o.subscribe();
}
sink.init(context);
sink.call(context, new PortRequest(sinkPort), o);
// o.lift(new DoOnRequestOperator("beforeShare")).share().lift(new DropOperator<>("sink_share")));
}
use of io.mantisrx.runtime.PortRequest in project mantis by Netflix.
the class TestJobParameterized method getJobInstance.
@Override
public Job<Integer> getJobInstance() {
return MantisJob.<Integer>source(new Source<Integer>() {
@Override
public Observable<Observable<Integer>> call(Context context, Index index) {
Integer start = (Integer) context.getParameters().get("start-range");
Integer end = (Integer) context.getParameters().get("end-range");
return Observable.just(Observable.range(start, end));
}
}).stage(new ScalarComputation<Integer, Integer>() {
@Override
public Observable<Integer> call(Context context, Observable<Integer> t1) {
final Integer scale = (Integer) context.getParameters().get("scale-by");
return t1.map(new Func1<Integer, Integer>() {
@Override
public Integer call(Integer t1) {
return t1 * scale;
}
});
}
}, new ScalarToScalar.Config<Integer, Integer>().codec(Codecs.integer())).sink(new Sink<Integer>() {
@Override
public void init(Context context) {
System.out.println("sink init called");
}
@Override
public void call(Context context, PortRequest p, Observable<Integer> o) {
final String message = (String) context.getParameters().get("sink-message");
o.toBlocking().forEach(new Action1<Integer>() {
@Override
public void call(Integer t1) {
System.out.println(message + t1);
itemsWritten.add(t1);
}
});
}
}).metadata(new Metadata.Builder().name("test job").description("showcase parameters").build()).parameterDefinition(new IntParameter().name("start-range").validator(Validators.range(0, 100)).build()).parameterDefinition(new IntParameter().name("end-range").validator(Validators.range(100, 1000)).build()).parameterDefinition(new IntParameter().name("scale-by").description("scale each value from the range").validator(Validators.range(1, 10)).build()).parameterDefinition(new StringParameter().name("sink-message").defaultValue("hello: ").description("concat with results").validator(Validators.notNullOrEmpty()).build()).create();
}
use of io.mantisrx.runtime.PortRequest in project mantis by Netflix.
the class SourceSink method call.
@Override
public void call(Context context, PortRequest portRequest, Observable<String> observable) {
observable = observable.filter(t1 -> !t1.isEmpty());
ServerSentEventsSink<String> sink = new ServerSentEventsSink.Builder<String>().withEncoder(data -> data).withPredicate(new Predicate<>("description", new EventFilter(clientId))).withRequestPreprocessor(preProcessor).withRequestPostprocessor(postProcessor).build();
observable.subscribe();
sink.call(context, portRequest, observable);
}
use of io.mantisrx.runtime.PortRequest in project mantis by Netflix.
the class KafkaSink method call.
@Override
public void call(Context context, PortRequest ignore, Observable<T> dataO) {
if (kafkaProducerAtomicRef.get() == null) {
MantisKafkaProducerConfig mantisKafkaProducerConfig = new MantisKafkaProducerConfig(context);
Map<String, Object> producerProperties = mantisKafkaProducerConfig.getProducerProperties();
KafkaProducer<byte[], byte[]> kafkaProducer = new KafkaProducer<>(producerProperties);
kafkaProducerAtomicRef.compareAndSet(null, kafkaProducer);
logger.info("Kafka Producer initialized");
}
KafkaProducer<byte[], byte[]> kafkaProducer = kafkaProducerAtomicRef.get();
Parameters parameters = context.getParameters();
String topic = (String) parameters.get(KafkaSinkJobParameters.TOPIC);
dataO.map(encoder::call).flatMap((dataBytes) -> Observable.from(kafkaProducer.send(new ProducerRecord<>(topic, dataBytes))).subscribeOn(Schedulers.io())).subscribe();
}
Aggregations