Search in sources :

Example 1 with PortRequest

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")));
}
Also used : Action0(rx.functions.Action0) MonitorOperator(io.mantisrx.common.metrics.rx.MonitorOperator) Subscriber(rx.Subscriber) PortRequest(io.mantisrx.runtime.PortRequest) Observable(rx.Observable)

Example 2 with PortRequest

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();
}
Also used : Context(io.mantisrx.runtime.Context) StringParameter(io.mantisrx.runtime.parameter.type.StringParameter) Action1(rx.functions.Action1) Metadata(io.mantisrx.runtime.Metadata) Index(io.mantisrx.runtime.source.Index) Observable(rx.Observable) ScalarToScalar(io.mantisrx.runtime.ScalarToScalar) PortRequest(io.mantisrx.runtime.PortRequest) ScalarComputation(io.mantisrx.runtime.computation.ScalarComputation) IntParameter(io.mantisrx.runtime.parameter.type.IntParameter)

Example 3 with PortRequest

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);
}
Also used : Predicate(io.mantisrx.runtime.sink.predicate.Predicate) List(java.util.List) Func2(rx.functions.Func2) Sink(io.mantisrx.runtime.sink.Sink) Map(java.util.Map) PortRequest(io.mantisrx.runtime.PortRequest) EventFilter(io.mantisrx.connector.publish.core.EventFilter) ServerSentEventsSink(io.mantisrx.runtime.sink.ServerSentEventsSink) Context(io.mantisrx.runtime.Context) Observable(rx.Observable) ServerSentEventsSink(io.mantisrx.runtime.sink.ServerSentEventsSink) EventFilter(io.mantisrx.connector.publish.core.EventFilter) Predicate(io.mantisrx.runtime.sink.predicate.Predicate)

Example 4 with PortRequest

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();
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Logger(org.slf4j.Logger) Metadata(io.mantisrx.runtime.Metadata) LoggerFactory(org.slf4j.LoggerFactory) PortRequest(io.mantisrx.runtime.PortRequest) Parameters(io.mantisrx.runtime.parameter.Parameters) Context(io.mantisrx.runtime.Context) AtomicReference(java.util.concurrent.atomic.AtomicReference) ParameterDefinition(io.mantisrx.runtime.parameter.ParameterDefinition) Validators(io.mantisrx.runtime.parameter.validator.Validators) SelfDocumentingSink(io.mantisrx.runtime.sink.SelfDocumentingSink) ArrayList(java.util.ArrayList) Observable(rx.Observable) List(java.util.List) Func1(rx.functions.Func1) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) Registry(com.netflix.spectator.api.Registry) Map(java.util.Map) Schedulers(rx.schedulers.Schedulers) StringParameter(io.mantisrx.runtime.parameter.type.StringParameter) Parameters(io.mantisrx.runtime.parameter.Parameters)

Aggregations

PortRequest (io.mantisrx.runtime.PortRequest)4 Observable (rx.Observable)4 Context (io.mantisrx.runtime.Context)3 Metadata (io.mantisrx.runtime.Metadata)2 StringParameter (io.mantisrx.runtime.parameter.type.StringParameter)2 List (java.util.List)2 Map (java.util.Map)2 Registry (com.netflix.spectator.api.Registry)1 MonitorOperator (io.mantisrx.common.metrics.rx.MonitorOperator)1 EventFilter (io.mantisrx.connector.publish.core.EventFilter)1 ScalarToScalar (io.mantisrx.runtime.ScalarToScalar)1 ScalarComputation (io.mantisrx.runtime.computation.ScalarComputation)1 ParameterDefinition (io.mantisrx.runtime.parameter.ParameterDefinition)1 Parameters (io.mantisrx.runtime.parameter.Parameters)1 IntParameter (io.mantisrx.runtime.parameter.type.IntParameter)1 Validators (io.mantisrx.runtime.parameter.validator.Validators)1 SelfDocumentingSink (io.mantisrx.runtime.sink.SelfDocumentingSink)1 ServerSentEventsSink (io.mantisrx.runtime.sink.ServerSentEventsSink)1 Sink (io.mantisrx.runtime.sink.Sink)1 Predicate (io.mantisrx.runtime.sink.predicate.Predicate)1