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);
}
});
}
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);
}
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);
}
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);
}
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);
}
Aggregations