Search in sources :

Example 26 with Endpoint

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

the class ConsistentHashTest method twoNodeTest.

@Test
public void twoNodeTest() {
    Endpoint n1 = new Endpoint("host1", 7001);
    Endpoint n2 = new Endpoint("host2", 7001);
    List<Endpoint> nodes = new ArrayList<Endpoint>();
    nodes.add(n1);
    nodes.add(n2);
    ConsistentHash<Endpoint> ch = new ConsistentHash<Endpoint>(HashFunctions.ketama(), new EndpointConfiguration(), nodes);
    int hostHitCountNode1 = 0;
    int hostHitCountNode2 = 0;
    int nonHitCount = 0;
    int MSG_COUNT = 100000;
    for (int i = 0; i < MSG_COUNT; i++) {
        Endpoint sn = ch.get(("msg:" + i).getBytes());
        if (sn.getHost().equals("host1")) {
            hostHitCountNode1++;
        } else if (sn.getHost().equals("host2")) {
            hostHitCountNode2++;
        } else {
            nonHitCount++;
        }
    }
    double host1HitPercentage = (double) hostHitCountNode1 / (double) MSG_COUNT;
    System.out.println("host1 hit % " + host1HitPercentage);
    assertTrue(host1HitPercentage > 0.48 && host1HitPercentage < 0.52);
    assertTrue(nonHitCount == 0);
    assertEquals(MSG_COUNT, hostHitCountNode1 + hostHitCountNode2);
}
Also used : ConsistentHash(io.mantisrx.common.network.ConsistentHash) Endpoint(io.mantisrx.common.network.Endpoint) ArrayList(java.util.ArrayList) EndpointConfiguration(io.mantisrx.common.network.EndpointConfiguration) Endpoint(io.mantisrx.common.network.Endpoint) Test(org.junit.Test)

Example 27 with Endpoint

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

the class MetricsClientImplTest method testMetricConnections.

@Test
public void testMetricConnections() throws InterruptedException, UnsupportedEncodingException, JsonProcessingException {
    final String jobId = "test-job-1";
    final String testResUsageMetricData = generateMetricJson(MetricStringConstants.RESOURCE_USAGE_METRIC_GROUP);
    final String testDropDataMetricData = generateMetricJson(MetricStringConstants.DATA_DROP_METRIC_GROUP);
    final int metricsPort = TestSseServerFactory.newServerWithInitialData(testResUsageMetricData);
    final AtomicInteger i = new AtomicInteger(0);
    final Observable<EndpointChange> workerMetricLocationStream = Observable.interval(1, TimeUnit.SECONDS, Schedulers.io()).map(new Func1<Long, EndpointChange>() {

        @Override
        public EndpointChange call(Long aLong) {
            logger.info("emitting endpointChange");
            if (i.getAndIncrement() % 10 == 0) {
                return new EndpointChange(EndpointChange.Type.add, new Endpoint("localhost", 31002));
            } else {
                return new EndpointChange(EndpointChange.Type.add, new WorkerEndpoint("localhost", 31002, 1, metricsPort, 0, 1));
            }
        }
    });
    MetricsClientImpl<MantisServerSentEvent> metricsClient = new MetricsClientImpl<>(jobId, new SseWorkerConnectionFunction(true, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            logger.error("Metric connection error: " + throwable.getMessage());
            try {
                Thread.sleep(500);
            } catch (InterruptedException ie) {
                logger.error("Interrupted waiting for retrying connection");
            }
        }
    }, new SinkParameters.Builder().withParameter("name", MetricStringConstants.RESOURCE_USAGE_METRIC_GROUP).build()), new JobWorkerMetricsLocator() {

        @Override
        public Observable<EndpointChange> locateWorkerMetricsForJob(String jobId) {
            return workerMetricLocationStream;
        }
    }, Observable.just(1), new Observer<WorkerConnectionsStatus>() {

        @Override
        public void onCompleted() {
            logger.info("got onCompleted in WorkerConnStatus obs");
        }

        @Override
        public void onError(Throwable e) {
            logger.info("got onError in WorkerConnStatus obs");
        }

        @Override
        public void onNext(WorkerConnectionsStatus workerConnectionsStatus) {
            logger.info("got WorkerConnStatus {}", workerConnectionsStatus);
        }
    }, 60);
    final CountDownLatch latch = new CountDownLatch(1);
    final Observable<Observable<MantisServerSentEvent>> results = metricsClient.getResults();
    Observable.merge(results).doOnNext(new Action1<MantisServerSentEvent>() {

        @Override
        public void call(MantisServerSentEvent event) {
            logger.info("got event {}", event.getEventAsString());
            assertEquals(testResUsageMetricData, event.getEventAsString());
            latch.countDown();
        }
    }).doOnError(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            logger.error("got error {}", throwable.getMessage(), throwable);
        }
    }).doOnCompleted(new Action0() {

        @Override
        public void call() {
            logger.info("onComplete");
        }
    }).subscribe();
    latch.await(30, TimeUnit.SECONDS);
    TestSseServerFactory.stopAllRunning();
}
Also used : EndpointChange(io.reactivex.mantis.remote.observable.EndpointChange) WorkerEndpoint(io.mantisrx.common.network.WorkerEndpoint) Endpoint(io.mantisrx.common.network.Endpoint) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) Action0(rx.functions.Action0) Action1(rx.functions.Action1) WorkerEndpoint(io.mantisrx.common.network.WorkerEndpoint) CountDownLatch(java.util.concurrent.CountDownLatch) WorkerEndpoint(io.mantisrx.common.network.WorkerEndpoint) Endpoint(io.mantisrx.common.network.Endpoint) Observable(rx.Observable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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