Search in sources :

Example 1 with JobSnapshotFactory

use of com.netflix.titus.runtime.connector.jobmanager.snapshot.JobSnapshotFactory in project titus-control-plane by Netflix.

the class GrpcJobReplicatorEventStream method newConnection.

@Override
protected Flux<ReplicatorEvent<JobSnapshot, JobManagerEvent<?>>> newConnection() {
    return Flux.<ReplicatorEvent<JobSnapshot, JobManagerEvent<?>>>create(sink -> {
        CacheUpdater cacheUpdater = new CacheUpdater(jobSnapshotFactory, keepAliveEnabled, titusRuntime);
        logger.info("Connecting to the job event stream (filteringCriteria={})...", filteringCriteria);
        ConnectableFlux<JobManagerEvent<?>> connectableStream = client.observeJobs(filteringCriteria).publish();
        Flux<JobManagerEvent<?>> augmentedStream;
        if (configuration.isConnectionTimeoutEnabled()) {
            augmentedStream = Flux.merge(connectableStream.take(1).timeout(Duration.ofMillis(configuration.getConnectionTimeoutMs())).ignoreElements().onErrorMap(TimeoutException.class, error -> new TimeoutException(String.format("No event received from stream in %sms", configuration.getConnectionTimeoutMs()))), connectableStream);
        } else {
            augmentedStream = connectableStream;
        }
        Disposable disposable = augmentedStream.subscribe(jobEvent -> {
            long started = titusRuntime.getClock().wallTime();
            try {
                cacheUpdater.onEvent(jobEvent).ifPresent(sink::next);
                eventProcessingLatencies.recordLevel(titusRuntime.getClock().wallTime() - started);
            } catch (Exception e) {
                // Throw error to force the cache reconnect.
                logger.warn("Unexpected error when handling the job change notification: {}", jobEvent, e);
                ExceptionExt.silent(() -> sink.error(e));
            }
        }, e -> ExceptionExt.silent(() -> sink.error(e)), () -> ExceptionExt.silent(sink::complete));
        sink.onDispose(disposable);
        connectableStream.connect();
    }).doOnSubscribe(subscription -> subscriptionCounter.incrementAndGet()).doFinally(signal -> subscriptionCounter.decrementAndGet());
}
Also used : Disposable(reactor.core.Disposable) JobManagerConstants(com.netflix.titus.api.jobmanager.service.JobManagerConstants) Disposable(reactor.core.Disposable) Task(com.netflix.titus.api.jobmanager.model.job.Task) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) AbstractReplicatorEventStream(com.netflix.titus.runtime.connector.common.replicator.AbstractReplicatorEventStream) Scheduler(reactor.core.scheduler.Scheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ReplicatorEvent(com.netflix.titus.runtime.connector.common.replicator.ReplicatorEvent) SpectatorExt(com.netflix.titus.common.util.spectator.SpectatorExt) JobSnapshot(com.netflix.titus.runtime.connector.jobmanager.snapshot.JobSnapshot) ConnectableFlux(reactor.core.publisher.ConnectableFlux) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) JobState(com.netflix.titus.api.jobmanager.model.job.JobState) ExceptionExt(com.netflix.titus.common.util.ExceptionExt) RemoteJobManagementClientWithKeepAlive(com.netflix.titus.runtime.connector.jobmanager.RemoteJobManagementClientWithKeepAlive) Job(com.netflix.titus.api.jobmanager.model.job.Job) Logger(org.slf4j.Logger) JobUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.JobUpdateEvent) JobSnapshotFactory(com.netflix.titus.runtime.connector.jobmanager.snapshot.JobSnapshotFactory) Set(java.util.Set) JobManagerEvent(com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent) TaskState(com.netflix.titus.api.jobmanager.model.job.TaskState) JobKeepAliveEvent(com.netflix.titus.api.jobmanager.model.job.event.JobKeepAliveEvent) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) List(java.util.List) ValueRangeCounter(com.netflix.titus.common.util.spectator.ValueRangeCounter) TaskUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent) JobConnectorConfiguration(com.netflix.titus.runtime.connector.jobmanager.JobConnectorConfiguration) DataReplicatorMetrics(com.netflix.titus.runtime.connector.common.replicator.DataReplicatorMetrics) Optional(java.util.Optional) JobManagementClient(com.netflix.titus.runtime.connector.jobmanager.JobManagementClient) VisibleForTesting(com.google.common.annotations.VisibleForTesting) PolledMeter(com.netflix.spectator.api.patterns.PolledMeter) TitusRuntime(com.netflix.titus.common.runtime.TitusRuntime) Collections(java.util.Collections) JobManagerEvent(com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent) ReplicatorEvent(com.netflix.titus.runtime.connector.common.replicator.ReplicatorEvent) TimeoutException(java.util.concurrent.TimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 PolledMeter (com.netflix.spectator.api.patterns.PolledMeter)1 Job (com.netflix.titus.api.jobmanager.model.job.Job)1 JobState (com.netflix.titus.api.jobmanager.model.job.JobState)1 Task (com.netflix.titus.api.jobmanager.model.job.Task)1 TaskState (com.netflix.titus.api.jobmanager.model.job.TaskState)1 JobKeepAliveEvent (com.netflix.titus.api.jobmanager.model.job.event.JobKeepAliveEvent)1 JobManagerEvent (com.netflix.titus.api.jobmanager.model.job.event.JobManagerEvent)1 JobUpdateEvent (com.netflix.titus.api.jobmanager.model.job.event.JobUpdateEvent)1 TaskUpdateEvent (com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent)1 JobManagerConstants (com.netflix.titus.api.jobmanager.service.JobManagerConstants)1 TitusRuntime (com.netflix.titus.common.runtime.TitusRuntime)1 ExceptionExt (com.netflix.titus.common.util.ExceptionExt)1 SpectatorExt (com.netflix.titus.common.util.spectator.SpectatorExt)1 ValueRangeCounter (com.netflix.titus.common.util.spectator.ValueRangeCounter)1 AbstractReplicatorEventStream (com.netflix.titus.runtime.connector.common.replicator.AbstractReplicatorEventStream)1 DataReplicatorMetrics (com.netflix.titus.runtime.connector.common.replicator.DataReplicatorMetrics)1 ReplicatorEvent (com.netflix.titus.runtime.connector.common.replicator.ReplicatorEvent)1 JobConnectorConfiguration (com.netflix.titus.runtime.connector.jobmanager.JobConnectorConfiguration)1 JobManagementClient (com.netflix.titus.runtime.connector.jobmanager.JobManagementClient)1