Search in sources :

Example 6 with EndpointChange

use of io.reactivex.mantis.remote.observable.EndpointChange 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 7 with EndpointChange

use of io.reactivex.mantis.remote.observable.EndpointChange in project mantis by Netflix.

the class MasterClientWrapper method getSinkLocations.

public Observable<EndpointChange> getSinkLocations(final String jobId, final int sinkStage, final int forPartition, final int totalPartitions) {
    final ConditionalRetry schedInfoRetry = new ConditionalRetry(masterConnectRetryCounter, "SchedInfoRetry", 10);
    Observable<List<Endpoint>> schedulingUpdates = getMasterClientApi().take(1).flatMap((MantisMasterClientApi mantisMasterClientApi) -> {
        return mantisMasterClientApi.schedulingChanges(jobId).doOnError((Throwable throwable) -> {
            logger.warn(throwable.getMessage());
        }).retryWhen(schedInfoRetry.getRetryLogic()).map((JobSchedulingInfo jobSchedulingInfo) -> {
            logger.info("Got scheduling info for {}", jobId);
            if (logger.isDebugEnabled()) {
                logger.debug("Worker Assignments {}", jobSchedulingInfo.getWorkerAssignments().get(sinkStage));
            }
            return jobSchedulingInfo.getWorkerAssignments().get(sinkStage);
        }).map((WorkerAssignments workerAssignments) -> {
            List<Endpoint> endpoints = new ArrayList<>();
            if (workerAssignments != null) {
                logger.info("job " + jobId + " Creating endpoints conx from " + workerAssignments.getHosts().size() + " worker assignments");
                for (WorkerHost host : workerAssignments.getHosts().values()) {
                    final int workerIndex = host.getWorkerIndex();
                    final int totalFromPartitions = workerAssignments.getNumWorkers();
                    numSinkWorkersSubject.onNext(new JobSinkNumWorkers(jobId, totalFromPartitions));
                    if (usePartition(workerIndex, totalFromPartitions, forPartition, totalPartitions)) {
                        // logger.info("Using partition " + workerIndex);
                        if (host.getState() == MantisJobState.Started) {
                            Endpoint ep = new Endpoint(getWrappedHost(host.getHost(), host.getWorkerNumber()), host.getPort().get(0), // completed callback
                            () -> logger.info("job " + jobId + " WorkerIndex " + workerIndex + " completed"), // error callback
                            t1 -> logger.info("job " + jobId + " WorkerIndex " + workerIndex + " failed"));
                            endpoints.add(ep);
                        }
                    }
                }
            } else {
                logger.info("job " + jobId + " Has no active workers!");
            }
            return endpoints;
        }).doOnError((Throwable throwable) -> {
            logger.error(throwable.getMessage(), throwable);
        });
    });
    return (new ToDeltaEndpointInjector(schedulingUpdates)).deltas();
}
Also used : EndpointChange(io.reactivex.mantis.remote.observable.EndpointChange) CuratorService(io.mantisrx.server.core.zookeeper.CuratorService) MantisJobState(io.mantisrx.runtime.MantisJobState) MasterDescription(io.mantisrx.server.core.master.MasterDescription) MetricsRegistry(io.mantisrx.common.metrics.MetricsRegistry) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Action1(rx.functions.Action1) ArrayList(java.util.ArrayList) Observable(rx.Observable) MasterMonitor(io.mantisrx.server.core.master.MasterMonitor) Func1(rx.functions.Func1) Map(java.util.Map) ConfigurationFactory(io.mantisrx.server.master.client.config.ConfigurationFactory) WorkerEndpoint(io.mantisrx.common.network.WorkerEndpoint) Metrics(io.mantisrx.common.metrics.Metrics) NamedJobInfo(io.mantisrx.server.core.NamedJobInfo) Counter(io.mantisrx.common.metrics.Counter) JobSchedulingInfo(io.mantisrx.server.core.JobSchedulingInfo) StaticPropertiesConfigurationFactory(io.mantisrx.server.master.client.config.StaticPropertiesConfigurationFactory) Properties(java.util.Properties) Logger(org.slf4j.Logger) Endpoint(io.mantisrx.common.network.Endpoint) WorkerAssignments(io.mantisrx.server.core.WorkerAssignments) Observer(rx.Observer) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ToDeltaEndpointInjector(io.reactivex.mantis.remote.observable.ToDeltaEndpointInjector) Action0(rx.functions.Action0) CoreConfiguration(io.mantisrx.server.core.CoreConfiguration) BehaviorSubject(rx.subjects.BehaviorSubject) PublishSubject(rx.subjects.PublishSubject) WorkerHost(io.mantisrx.server.core.WorkerHost) WorkerHost(io.mantisrx.server.core.WorkerHost) JobSchedulingInfo(io.mantisrx.server.core.JobSchedulingInfo) ToDeltaEndpointInjector(io.reactivex.mantis.remote.observable.ToDeltaEndpointInjector) WorkerEndpoint(io.mantisrx.common.network.WorkerEndpoint) Endpoint(io.mantisrx.common.network.Endpoint) WorkerAssignments(io.mantisrx.server.core.WorkerAssignments) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with EndpointChange

use of io.reactivex.mantis.remote.observable.EndpointChange in project mantis by Netflix.

the class MasterClientWrapperTest method getSchedulingInfoTest.

// @Test
public void getSchedulingInfoTest() {
    String jobname = "GroupByIP";
    CountDownLatch cdLatch = new CountDownLatch(3);
    Observable<String> jobidO = clientWrapper.getNamedJobsIds(jobname).take(1).cache().subscribeOn(Schedulers.io());
    Observable<MantisMasterClientApi> mmciO = clientWrapper.getMasterClientApi().take(1).cache().subscribeOn(Schedulers.io());
    Observable<EndpointChange> epO = jobidO.map((jId) -> clientWrapper.getSinkLocations(jId, sinkStageNumber, 0, 0)).flatMap(e -> e).take(3).doOnNext((ep) -> System.out.println("Ep change: " + ep)).doOnNext((ep) -> cdLatch.countDown());
    Observable<Boolean> deleteWorkerO = jobidO.zipWith(mmciO, (String jId, MantisMasterClientApi mmci) -> {
        System.out.println("Job id is " + jId);
        return mmci.schedulingChanges(jId).map(jsi -> {
            Map<Integer, WorkerAssignments> workerAssignments = jsi.getWorkerAssignments();
            System.out.println("WorkerAssignments -> " + workerAssignments);
            WorkerAssignments workerAssignmentsForSink = workerAssignments.get(sinkStageNumber);
            System.out.println("WorkerAssignmentsForSink -> " + workerAssignmentsForSink);
            Map<Integer, WorkerHost> hostsForSink = workerAssignmentsForSink.getHosts();
            System.out.println("Host map -> " + hostsForSink);
            assertTrue(!hostsForSink.isEmpty());
            Iterator<Entry<Integer, WorkerHost>> it = hostsForSink.entrySet().iterator();
            while (it.hasNext()) {
                Entry<Integer, WorkerHost> e = it.next();
                return e.getValue().getWorkerNumber();
            }
            return -1;
        }).take(1).map((Integer workerNo) -> {
            System.out.println("Worker no is -> " + workerNo);
            return mmci.resubmitJobWorker(jId, "tester", workerNo, "testing");
        }).flatMap(b -> b);
    }).flatMap(b -> b).doOnNext((result) -> {
        assertTrue(result);
        cdLatch.countDown();
    });
    epO.subscribeOn(Schedulers.io()).subscribe((ep) -> System.out.println(ep), (t) -> t.printStackTrace(), () -> System.out.println("ep change completed"));
    deleteWorkerO.toBlocking().subscribe((n) -> System.out.println(n), (t) -> t.printStackTrace(), () -> System.out.println("worker deletion completed"));
    try {
        cdLatch.await(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        fail();
    }
}
Also used : EndpointChange(io.reactivex.mantis.remote.observable.EndpointChange) Properties(java.util.Properties) Iterator(java.util.Iterator) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) WorkerAssignments(io.mantisrx.server.core.WorkerAssignments) Observable(rx.Observable) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Func1(rx.functions.Func1) Map(java.util.Map) Entry(java.util.Map.Entry) Schedulers(rx.schedulers.Schedulers) Assert.fail(org.junit.Assert.fail) WorkerHost(io.mantisrx.server.core.WorkerHost) WorkerHost(io.mantisrx.server.core.WorkerHost) EndpointChange(io.reactivex.mantis.remote.observable.EndpointChange) CountDownLatch(java.util.concurrent.CountDownLatch) Entry(java.util.Map.Entry) WorkerAssignments(io.mantisrx.server.core.WorkerAssignments) Iterator(java.util.Iterator) Map(java.util.Map)

Example 9 with EndpointChange

use of io.reactivex.mantis.remote.observable.EndpointChange 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

EndpointChange (io.reactivex.mantis.remote.observable.EndpointChange)9 Endpoint (io.mantisrx.common.network.Endpoint)7 Observable (rx.Observable)6 Func1 (rx.functions.Func1)6 Test (org.junit.Test)5 Action0 (rx.functions.Action0)5 EndpointInjector (io.reactivex.mantis.remote.observable.EndpointInjector)4 Action1 (rx.functions.Action1)4 Context (io.mantisrx.runtime.Context)3 StageConfig (io.mantisrx.runtime.StageConfig)3 PortSelectorWithinRange (io.reactivex.mantis.remote.observable.PortSelectorWithinRange)3 RemoteRxServer (io.reactivex.mantis.remote.observable.RemoteRxServer)3 Map (java.util.Map)3 Properties (java.util.Properties)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Counter (io.mantisrx.common.metrics.Counter)2 Metrics (io.mantisrx.common.metrics.Metrics)2 WorkerEndpoint (io.mantisrx.common.network.WorkerEndpoint)2 WorkerAssignments (io.mantisrx.server.core.WorkerAssignments)2 WorkerHost (io.mantisrx.server.core.WorkerHost)2