Search in sources :

Example 16 with MantisServerSentEvent

use of io.mantisrx.common.MantisServerSentEvent in project mantis by Netflix.

the class SampleClient method getMetricsData.

private static void getMetricsData(WorkerMetricsClient mantisClient, final String localJobId) {
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch finishLatch = new CountDownLatch(1);
    final MetricsClient metricsClient = mantisClient.getMetricsClientByJobId(localJobId, new SseWorkerConnectionFunction(true, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            logger.error("Sink connection error: " + throwable.getMessage());
            try {
                Thread.sleep(500);
            } catch (InterruptedException ie) {
                logger.error("Interrupted waiting for retrying connection");
            }
        }
    }), null);
    logger.info("Getting results observable for job {}", localJobId);
    Observable<MantisServerSentEvent> resultsObservable = Observable.merge(metricsClient.getResults());
    logger.info("Subscribing to it");
    final AtomicReference<Subscription> ref = new AtomicReference<>(null);
    final Thread t = new Thread() {

        @Override
        public void run() {
            try {
                startLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                sleep(300000);
            } catch (InterruptedException ie) {
            }
            logger.info("Closing client conx");
            try {
                ref.get().unsubscribe();
                finishLatch.countDown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    t.setDaemon(true);
    final Subscription s = resultsObservable.doOnCompleted(new Action0() {

        @Override
        public void call() {
            finishLatch.countDown();
        }
    }).subscribe(new Action1<MantisServerSentEvent>() {

        @Override
        public void call(MantisServerSentEvent event) {
            if (startLatch.getCount() > 0) {
                startLatch.countDown();
            }
            logger.info("{} Got SSE: {}", localJobId, event.getEventAsString());
        }
    });
    ref.set(s);
    t.start();
    logger.info("SUBSCRIBED to job metrics changes");
    try {
        finishLatch.await();
        logger.info("Sink observable completed");
    } catch (InterruptedException e) {
        logger.error("thread interrupted", e);
    }
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) MetricsClient(io.mantisrx.server.worker.client.MetricsClient) WorkerMetricsClient(io.mantisrx.server.worker.client.WorkerMetricsClient) MantisServerSentEvent(io.mantisrx.common.MantisServerSentEvent) SseWorkerConnectionFunction(io.mantisrx.server.worker.client.SseWorkerConnectionFunction) Subscription(rx.Subscription)

Example 17 with MantisServerSentEvent

use of io.mantisrx.common.MantisServerSentEvent 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

MantisServerSentEvent (io.mantisrx.common.MantisServerSentEvent)17 IOException (java.io.IOException)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 Action1 (rx.functions.Action1)7 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 Observable (rx.Observable)6 Action0 (rx.functions.Action0)6 Subscription (rx.Subscription)5 MantisSSEJob (io.mantisrx.client.MantisSSEJob)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 Properties (java.util.Properties)4 BufferedReader (java.io.BufferedReader)3 JobSla (io.mantisrx.runtime.JobSla)2 StringReader (java.io.StringReader)2 DefaultRegistry (com.netflix.spectator.api.DefaultRegistry)1 SinkConnectionsStatus (io.mantisrx.client.SinkConnectionsStatus)1 Counter (io.mantisrx.common.metrics.Counter)1 Metrics (io.mantisrx.common.metrics.Metrics)1