Search in sources :

Example 1 with ConnectionHandler

use of mantis.io.reactivex.netty.channel.ConnectionHandler in project mantis by Netflix.

the class LegacyTcpPushServer method createServer.

@Override
public RxServer<?, ?> createServer() {
    RxServer<RemoteRxEvent, RemoteRxEvent> server = RxNetty.newTcpServerBuilder(port, new ConnectionHandler<RemoteRxEvent, RemoteRxEvent>() {

        @Override
        public Observable<Void> handle(final ObservableConnection<RemoteRxEvent, RemoteRxEvent> newConnection) {
            final InetSocketAddress socketAddress = (InetSocketAddress) newConnection.getChannel().remoteAddress();
            // extract groupId, id, predicate from incoming byte[]
            return newConnection.getInput().flatMap(new Func1<RemoteRxEvent, Observable<Void>>() {

                @Override
                public Observable<Void> call(RemoteRxEvent incomingRequest) {
                    if (incomingRequest.getType() == RemoteRxEvent.Type.subscribed) {
                        Map<String, String> params = incomingRequest.getSubscribeParameters();
                        // client state
                        String id = null;
                        String slotId = null;
                        String groupId = null;
                        // sample state
                        boolean enableSampling = false;
                        long samplingTimeMsec = 0;
                        // predicate state
                        Map<String, List<String>> predicateParams = null;
                        if (params != null && !params.isEmpty()) {
                            predicateParams = new HashMap<String, List<String>>();
                            for (Entry<String, String> entry : params.entrySet()) {
                                List<String> values = new LinkedList<>();
                                values.add(entry.getValue());
                                predicateParams.put(entry.getKey(), values);
                            }
                            if (params.containsKey("id")) {
                                id = params.get("id");
                            }
                            if (params.containsKey("slotId")) {
                                slotId = params.get("slotId");
                            }
                            if (params.containsKey("groupId")) {
                                groupId = params.get("groupId");
                            }
                            if (params.containsKey("sample")) {
                                samplingTimeMsec = Long.parseLong(params.get("sample")) * 1000;
                                if (samplingTimeMsec < 50) {
                                    throw new IllegalArgumentException("Sampling rate too low: " + samplingTimeMsec);
                                }
                                enableSampling = true;
                            }
                            if (params.containsKey("sampleMSec")) {
                                samplingTimeMsec = Long.parseLong(params.get("sampleMSec"));
                                if (samplingTimeMsec < 50) {
                                    throw new IllegalArgumentException("Sampling rate too low: " + samplingTimeMsec);
                                }
                                enableSampling = true;
                            }
                        }
                        Func1<T, Boolean> predicateFunction = null;
                        if (predicate != null) {
                            predicateFunction = predicate.call(predicateParams);
                        }
                        // support legacy metrics per connection
                        Metrics sseSinkMetrics = new Metrics.Builder().name("DropOperator_outgoing_subject_" + slotId).addCounter("onNext").addCounter("dropped").build();
                        sseSinkMetrics = metricsRegistry.registerAndGet(sseSinkMetrics);
                        Counter legacyMsgProcessedCounter = sseSinkMetrics.getCounter("onNext");
                        Counter legacyDroppedWrites = sseSinkMetrics.getCounter("dropped");
                        return manageConnection(newConnection, socketAddress.getHostString(), socketAddress.getPort(), groupId, slotId, id, null, false, null, enableSampling, samplingTimeMsec, predicateFunction, null, legacyMsgProcessedCounter, legacyDroppedWrites, null);
                    }
                    return null;
                }
            });
        }
    }).pipelineConfigurator(new PipelineConfiguratorComposite<RemoteRxEvent, RemoteRxEvent>(new PipelineConfigurator<RemoteRxEvent, RemoteRxEvent>() {

        @Override
        public void configureNewPipeline(ChannelPipeline pipeline) {
            // pipeline.addLast(new LoggingHandler(LogLevel.ERROR)); // uncomment to enable debug logging
            pipeline.addLast("idleStateHandler", new IdleStateHandler(10, 2, 0));
            pipeline.addLast("heartbeat", new HeartbeatHandler());
            pipeline.addLast("gzipInflater", new JdkZlibEncoder(ZlibWrapper.GZIP));
            pipeline.addLast("gzipDeflater", new JdkZlibDecoder(ZlibWrapper.GZIP));
            // 4 bytes to encode length
            pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
            // max frame = half MB
            pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(5242880, 0, 4, 0, 4));
        }
    }, new LegacyTcpPipelineConfigurator(name))).channelOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 5 * 1024 * 1024)).build();
    return server;
}
Also used : HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) Entry(java.util.Map.Entry) Metrics(io.mantisrx.common.metrics.Metrics) Counter(io.mantisrx.common.metrics.Counter) LinkedList(java.util.LinkedList) List(java.util.List) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) Func1(rx.functions.Func1) JdkZlibEncoder(io.netty.handler.codec.compression.JdkZlibEncoder) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ObservableConnection(mantis.io.reactivex.netty.channel.ObservableConnection) Observable(rx.Observable) ChannelPipeline(io.netty.channel.ChannelPipeline) PipelineConfigurator(mantis.io.reactivex.netty.pipeline.PipelineConfigurator) ConnectionHandler(mantis.io.reactivex.netty.channel.ConnectionHandler) JdkZlibDecoder(io.netty.handler.codec.compression.JdkZlibDecoder) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Counter (io.mantisrx.common.metrics.Counter)1 Metrics (io.mantisrx.common.metrics.Metrics)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 WriteBufferWaterMark (io.netty.channel.WriteBufferWaterMark)1 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)1 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)1 JdkZlibDecoder (io.netty.handler.codec.compression.JdkZlibDecoder)1 JdkZlibEncoder (io.netty.handler.codec.compression.JdkZlibEncoder)1 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)1 InetSocketAddress (java.net.InetSocketAddress)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 ConnectionHandler (mantis.io.reactivex.netty.channel.ConnectionHandler)1 ObservableConnection (mantis.io.reactivex.netty.channel.ObservableConnection)1 PipelineConfigurator (mantis.io.reactivex.netty.pipeline.PipelineConfigurator)1 Observable (rx.Observable)1 Func1 (rx.functions.Func1)1