Search in sources :

Example 1 with KeyValuePair

use of io.reactivex.mantis.network.push.KeyValuePair in project mantis by Netflix.

the class WorkerPublisherRemoteObservable method start.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void start(final StageConfig<T, R> stage, Observable<Observable<R>> toServe) {
    RemoteRxServer.Builder serverBuilder = new RemoteRxServer.Builder();
    if (stage instanceof KeyToKey || stage instanceof ScalarToKey || stage instanceof ScalarToGroup || stage instanceof GroupToGroup) {
        if (runNewW2WserverGroups(jobName)) {
            logger.info("Modern server setup for name: " + name + " type: Keyedstage");
            long expiryTimeInSecs = Long.MAX_VALUE;
            if (stage instanceof KeyToKey) {
                expiryTimeInSecs = ((KeyToKey) stage).getKeyExpireTimeSeconds();
            } else if (stage instanceof ScalarToKey) {
                expiryTimeInSecs = ((ScalarToKey) stage).getKeyExpireTimeSeconds();
            }
            Func1<R, byte[]> valueEncoder = new Func1<R, byte[]>() {

                @Override
                public byte[] call(R t1) {
                    return stage.getOutputCodec().encode(t1);
                }
            };
            Func1<String, byte[]> keyEncoder = new Func1<String, byte[]>() {

                @Override
                public byte[] call(String t1) {
                    return Codecs.string().encode(t1);
                }
            };
            ServerConfig<KeyValuePair<String, R>> config = new ServerConfig.Builder<KeyValuePair<String, R>>().name(name).port(serverPort).metricsRegistry(MetricsRegistry.getInstance()).numQueueConsumers(numConsumerThreads()).maxChunkSize(maxChunkSize()).maxChunkTimeMSec(maxChunkTimeMSec()).bufferCapacity(bufferCapacity()).useSpscQueue(useSpsc()).router(Routers.consistentHashingLegacyTcpProtocol(jobName, keyEncoder, valueEncoder)).build();
            // remove type
            Observable go = toServe;
            if (stage instanceof ScalarToGroup || stage instanceof GroupToGroup) {
                final LegacyTcpPushServer<KeyValuePair<String, R>> modernServer = PushServers.infiniteStreamLegacyTcpNestedMantisGroup(config, go, expiryTimeInSecs, keyEncoder, io.reactivex.mantis.network.push.HashFunctions.ketama());
                modernServer.start();
                // support legacy server interface
                this.server = new RemoteRxServer() {

                    @Override
                    public void start() {
                    }

                    @Override
                    public void startAndWait() {
                    }

                    @Override
                    public void shutdown() {
                        modernServer.shutdown();
                    }

                    @Override
                    public void blockUntilServerShutdown() {
                        modernServer.blockUntilShutdown();
                    }
                };
            } else {
                // ScalarToKey or KeyTKey
                final LegacyTcpPushServer<KeyValuePair<String, R>> modernServer = PushServers.infiniteStreamLegacyTcpNestedGroupedObservable(config, go, expiryTimeInSecs, keyEncoder, io.reactivex.mantis.network.push.HashFunctions.ketama());
                modernServer.start();
                // support legacy server interface
                this.server = new RemoteRxServer() {

                    @Override
                    public void start() {
                    }

                    @Override
                    public void startAndWait() {
                    }

                    @Override
                    public void shutdown() {
                        modernServer.shutdown();
                    }

                    @Override
                    public void blockUntilServerShutdown() {
                        modernServer.blockUntilShutdown();
                    }
                };
            }
        }
    } else if (stage instanceof ScalarToScalar || stage instanceof KeyToScalar || stage instanceof GroupToScalar) {
        if (runNewW2Wserver(jobName)) {
            logger.info("Modern server setup for name: " + name + " type: Scalarstage");
            Func1<R, byte[]> encoder = new Func1<R, byte[]>() {

                @Override
                public byte[] call(R t1) {
                    return stage.getOutputCodec().encode(t1);
                }
            };
            ServerConfig<R> config = new ServerConfig.Builder<R>().name(name).port(serverPort).metricsRegistry(MetricsRegistry.getInstance()).router(Routers.roundRobinLegacyTcpProtocol(name, encoder)).build();
            final LegacyTcpPushServer<R> modernServer = PushServers.infiniteStreamLegacyTcpNested(config, toServe);
            modernServer.start();
            // support legacy server interface
            this.server = new RemoteRxServer() {

                @Override
                public void start() {
                }

                @Override
                public void startAndWait() {
                }

                @Override
                public void shutdown() {
                    modernServer.shutdown();
                }

                @Override
                public void blockUntilServerShutdown() {
                    modernServer.blockUntilShutdown();
                }
            };
        } else {
            logger.info("Legacy server setup for name: " + name + " type: Scalarstage");
            RoundRobin slotting = new RoundRobin();
            serverBuilder.addObservable(new ServeNestedObservable.Builder().name(name).encoder(stage.getOutputCodec()).observable(toServe).slottingStrategy(slotting).build());
            MetricsRegistry.getInstance().registerAndGet(slotting.getMetrics());
            server = serverBuilder.port(serverPort).build();
            server.start();
        }
    } else {
        throw new RuntimeException("Unsupported stage type: " + stage);
    }
}
Also used : ScalarToGroup(io.mantisrx.runtime.ScalarToGroup) GroupToScalar(io.mantisrx.runtime.GroupToScalar) ServerConfig(io.reactivex.mantis.network.push.ServerConfig) ScalarToScalar(io.mantisrx.runtime.ScalarToScalar) Func1(rx.functions.Func1) KeyValuePair(io.reactivex.mantis.network.push.KeyValuePair) ScalarToKey(io.mantisrx.runtime.ScalarToKey) LegacyTcpPushServer(io.reactivex.mantis.network.push.LegacyTcpPushServer) RemoteRxServer(io.reactivex.mantis.remote.observable.RemoteRxServer) KeyToScalar(io.mantisrx.runtime.KeyToScalar) ServeNestedObservable(io.reactivex.mantis.remote.observable.ServeNestedObservable) Observable(rx.Observable) GroupToGroup(io.mantisrx.runtime.GroupToGroup) RoundRobin(io.reactivex.mantis.remote.observable.slotting.RoundRobin) ServeNestedObservable(io.reactivex.mantis.remote.observable.ServeNestedObservable) KeyToKey(io.mantisrx.runtime.KeyToKey)

Aggregations

GroupToGroup (io.mantisrx.runtime.GroupToGroup)1 GroupToScalar (io.mantisrx.runtime.GroupToScalar)1 KeyToKey (io.mantisrx.runtime.KeyToKey)1 KeyToScalar (io.mantisrx.runtime.KeyToScalar)1 ScalarToGroup (io.mantisrx.runtime.ScalarToGroup)1 ScalarToKey (io.mantisrx.runtime.ScalarToKey)1 ScalarToScalar (io.mantisrx.runtime.ScalarToScalar)1 KeyValuePair (io.reactivex.mantis.network.push.KeyValuePair)1 LegacyTcpPushServer (io.reactivex.mantis.network.push.LegacyTcpPushServer)1 ServerConfig (io.reactivex.mantis.network.push.ServerConfig)1 RemoteRxServer (io.reactivex.mantis.remote.observable.RemoteRxServer)1 ServeNestedObservable (io.reactivex.mantis.remote.observable.ServeNestedObservable)1 RoundRobin (io.reactivex.mantis.remote.observable.slotting.RoundRobin)1 Observable (rx.Observable)1 Func1 (rx.functions.Func1)1