Search in sources :

Example 11 with Endpoint

use of io.mantisrx.common.network.Endpoint in project mantis by Netflix.

the class Reconciliator method deltas.

private Observable<EndpointChange> deltas() {
    final Map<String, Endpoint> sideEffectState = new HashMap<String, Endpoint>();
    final PublishSubject<Integer> stopReconciliator = PublishSubject.create();
    return Observable.merge(reconciledChanges.takeUntil(stopReconciliator).doOnCompleted(() -> {
        logger.info("onComplete triggered for reconciledChanges");
    }).doOnError(e -> logger.error("caught exception for reconciledChanges {}", e.getMessage(), e)), injector.deltas().doOnCompleted(new Action0() {

        @Override
        public void call() {
            // injector has completed recieving updates, complete reconciliator
            // observable
            logger.info("Stopping reconciliator, injector completed.");
            stopReconciliator.onNext(1);
            stopReconciliation();
        }
    }).doOnError(e -> logger.error("caught exception for injector deltas {}", e.getMessage(), e)).doOnNext(new Action1<EndpointChange>() {

        @Override
        public void call(EndpointChange newEndpointChange) {
            String id = Endpoint.uniqueHost(newEndpointChange.getEndpoint().getHost(), newEndpointChange.getEndpoint().getPort(), newEndpointChange.getEndpoint().getSlotId());
            if (sideEffectState.containsKey(id)) {
                if (newEndpointChange.getType() == Type.complete) {
                    // remove from expecected set
                    expectedSetSize.decrement();
                    sideEffectState.remove(id);
                    currentExpectedSet.onNext(new HashSet<Endpoint>(sideEffectState.values()));
                }
            } else {
                if (newEndpointChange.getType() == Type.add) {
                    expectedSetSize.increment();
                    sideEffectState.put(id, new Endpoint(newEndpointChange.getEndpoint().getHost(), newEndpointChange.getEndpoint().getPort(), newEndpointChange.getEndpoint().getSlotId()));
                    currentExpectedSet.onNext(new HashSet<Endpoint>(sideEffectState.values()));
                }
            }
        }
    })).doOnError(t -> logger.error("caught error processing reconciliator deltas {}", t.getMessage(), t)).doOnSubscribe(new Action0() {

        @Override
        public void call() {
            logger.info("Subscribed to deltas for {}, clearing active connection set", name);
            connectionSet.resetActiveConnections();
            startReconciliation();
        }
    }).doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            logger.info("Unsubscribed from deltas for {}", name);
        }
    });
}
Also used : Counter(io.mantisrx.common.metrics.Counter) EndpointChange(io.reactivex.mantis.remote.observable.EndpointChange) EndpointInjector(io.reactivex.mantis.remote.observable.EndpointInjector) Logger(org.slf4j.Logger) Endpoint(io.mantisrx.common.network.Endpoint) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Action1(rx.functions.Action1) Observable(rx.Observable) HashSet(java.util.HashSet) Func1(rx.functions.Func1) Action0(rx.functions.Action0) Func2(rx.functions.Func2) DynamicConnectionSet(io.reactivex.mantis.remote.observable.DynamicConnectionSet) Type(io.reactivex.mantis.remote.observable.EndpointChange.Type) Gauge(io.mantisrx.common.metrics.Gauge) Map(java.util.Map) Metrics(io.mantisrx.common.metrics.Metrics) Subscription(rx.Subscription) PublishSubject(rx.subjects.PublishSubject) Action0(rx.functions.Action0) Endpoint(io.mantisrx.common.network.Endpoint) HashMap(java.util.HashMap) EndpointChange(io.reactivex.mantis.remote.observable.EndpointChange) HashSet(java.util.HashSet)

Example 12 with Endpoint

use of io.mantisrx.common.network.Endpoint in project mantis by Netflix.

the class DynamicConnectionSetTest method testMergeInBadObservable.

@Test(expected = RuntimeException.class)
public void testMergeInBadObservable() throws InterruptedException {
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    final int server1Port = portSelector.acquirePort();
    final int server2Port = portSelector.acquirePort();
    Observable<Integer> badObservable = Observable.create(new OnSubscribe<Integer>() {

        @Override
        public void call(Subscriber<? super Integer> subscriber) {
            for (int i = 100; i < 200; i++) {
                subscriber.onNext(i);
                if (i == 150) {
                    subscriber.onError(new Exception("error"));
                }
            }
        }
    });
    // setup servers
    RemoteRxServer server1 = RemoteObservable.serve(server1Port, Observable.range(1, 50), Codecs.integer());
    RemoteRxServer server2 = RemoteObservable.serve(server2Port, badObservable, Codecs.integer());
    server1.start();
    server2.start();
    EndpointInjector staticEndpoints = new EndpointInjector() {

        @Override
        public Observable<EndpointChange> deltas() {
            return Observable.create(new OnSubscribe<EndpointChange>() {

                @Override
                public void call(Subscriber<? super EndpointChange> subscriber) {
                    subscriber.onNext(new EndpointChange(EndpointChange.Type.add, new Endpoint("localhost", server1Port, "1")));
                    subscriber.onNext(new EndpointChange(EndpointChange.Type.add, new Endpoint("localhost", server2Port, "2")));
                    subscriber.onCompleted();
                }
            });
        }
    };
    DynamicConnectionSet<Integer> cm = DynamicConnectionSet.create(new ConnectToObservable.Builder<Integer>().decoder(Codecs.integer()));
    cm.setEndpointInjector(staticEndpoints);
    int sum = MathObservable.sumInteger(Observable.merge(cm.observables())).toBlocking().last();
    Assert.assertEquals(5050, sum);
}
Also used : Endpoint(io.mantisrx.common.network.Endpoint) Endpoint(io.mantisrx.common.network.Endpoint) Test(org.junit.Test)

Example 13 with Endpoint

use of io.mantisrx.common.network.Endpoint in project mantis by Netflix.

the class DynamicConnectionSetTest method testMergeInWithDeltaEndpointService.

@Test
public void testMergeInWithDeltaEndpointService() {
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    final int server1Port = portSelector.acquirePort();
    final int server2Port = portSelector.acquirePort();
    // setup servers
    RemoteRxServer server1 = RemoteObservable.serve(server1Port, Observable.range(1, 50), Codecs.integer());
    RemoteRxServer server2 = RemoteObservable.serve(server2Port, Observable.range(51, 50), Codecs.integer());
    server1.start();
    server2.start();
    ReplaySubject<List<Endpoint>> subject = ReplaySubject.create();
    List<Endpoint> endpoints = new LinkedList<Endpoint>();
    endpoints.add(new Endpoint("localhost", server1Port));
    endpoints.add(new Endpoint("localhost", server2Port));
    subject.onNext(endpoints);
    subject.onCompleted();
    DynamicConnectionSet<Integer> cm = DynamicConnectionSet.create(new ConnectToObservable.Builder<Integer>().decoder(Codecs.integer()));
    cm.setEndpointInjector(new ToDeltaEndpointInjector(subject));
    int sum = MathObservable.sumInteger(Observable.merge(cm.observables())).toBlocking().last();
    Assert.assertEquals(5050, sum);
}
Also used : Endpoint(io.mantisrx.common.network.Endpoint) List(java.util.List) LinkedList(java.util.LinkedList) Endpoint(io.mantisrx.common.network.Endpoint) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 14 with Endpoint

use of io.mantisrx.common.network.Endpoint in project mantis by Netflix.

the class DynamicConnectionSetTest method testMergeInConnections.

@Test
public void testMergeInConnections() throws InterruptedException {
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    final int server1Port = portSelector.acquirePort();
    final int server2Port = portSelector.acquirePort();
    // setup servers
    RemoteRxServer server1 = RemoteObservable.serve(server1Port, Observable.range(1, 50), Codecs.integer());
    RemoteRxServer server2 = RemoteObservable.serve(server2Port, Observable.range(51, 50), Codecs.integer());
    server1.start();
    server2.start();
    EndpointInjector staticEndpoints = new EndpointInjector() {

        @Override
        public Observable<EndpointChange> deltas() {
            return Observable.create(new OnSubscribe<EndpointChange>() {

                @Override
                public void call(Subscriber<? super EndpointChange> subscriber) {
                    subscriber.onNext(new EndpointChange(EndpointChange.Type.add, new Endpoint("localhost", server1Port, "1")));
                    subscriber.onNext(new EndpointChange(EndpointChange.Type.add, new Endpoint("localhost", server2Port, "2")));
                    subscriber.onCompleted();
                }
            });
        }
    };
    DynamicConnectionSet<Integer> cm = DynamicConnectionSet.create(new ConnectToObservable.Builder<Integer>().decoder(Codecs.integer()));
    cm.setEndpointInjector(staticEndpoints);
    int sum = MathObservable.sumInteger(Observable.merge(cm.observables())).toBlocking().last();
    Assert.assertEquals(5050, sum);
}
Also used : Endpoint(io.mantisrx.common.network.Endpoint) Endpoint(io.mantisrx.common.network.Endpoint) Test(org.junit.Test)

Example 15 with Endpoint

use of io.mantisrx.common.network.Endpoint in project mantis by Netflix.

the class FixedConnectionSetTest method testMergeInBadObservable.

@Test(expected = RuntimeException.class)
public void testMergeInBadObservable() throws InterruptedException {
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    final int server1Port = portSelector.acquirePort();
    final int server2Port = portSelector.acquirePort();
    Observable<Integer> badObservable = Observable.create(new OnSubscribe<Integer>() {

        @Override
        public void call(Subscriber<? super Integer> subscriber) {
            for (int i = 100; i < 200; i++) {
                subscriber.onNext(i);
                if (i == 150) {
                    subscriber.onError(new Exception("error"));
                }
            }
        }
    });
    // setup servers
    RemoteRxServer server1 = RemoteObservable.serve(server1Port, Observable.range(1, 50), Codecs.integer());
    RemoteRxServer server2 = RemoteObservable.serve(server2Port, badObservable, Codecs.integer());
    server1.start();
    server2.start();
    EndpointInjector staticEndpoints = new EndpointInjector() {

        @Override
        public Observable<EndpointChange> deltas() {
            return Observable.create(new OnSubscribe<EndpointChange>() {

                @Override
                public void call(Subscriber<? super EndpointChange> subscriber) {
                    subscriber.onNext(new EndpointChange(EndpointChange.Type.add, new Endpoint("localhost", server1Port, "1")));
                    subscriber.onNext(new EndpointChange(EndpointChange.Type.add, new Endpoint("localhost", server2Port, "2")));
                    subscriber.onCompleted();
                }
            });
        }
    };
    FixedConnectionSet<Integer> cm = FixedConnectionSet.create(2, new ConnectToObservable.Builder<Integer>().decoder(Codecs.integer()), staticEndpoints);
    int sum = MathObservable.sumInteger(Observable.merge(cm.getObservables())).toBlocking().last();
    Assert.assertEquals(5050, sum);
}
Also used : Endpoint(io.mantisrx.common.network.Endpoint) Endpoint(io.mantisrx.common.network.Endpoint) Test(org.junit.Test)

Aggregations

Endpoint (io.mantisrx.common.network.Endpoint)27 Test (org.junit.Test)16 Action0 (rx.functions.Action0)8 EndpointChange (io.reactivex.mantis.remote.observable.EndpointChange)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 LinkedList (java.util.LinkedList)6 List (java.util.List)6 Map (java.util.Map)6 Observable (rx.Observable)6 Set (java.util.Set)5 Action1 (rx.functions.Action1)5 Func1 (rx.functions.Func1)5 Context (io.mantisrx.runtime.Context)4 StageConfig (io.mantisrx.runtime.StageConfig)4 EndpointInjector (io.reactivex.mantis.remote.observable.EndpointInjector)4 RemoteRxServer (io.reactivex.mantis.remote.observable.RemoteRxServer)4 HashMap (java.util.HashMap)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4