Search in sources :

Example 1 with ObserveJobsQuery

use of com.netflix.titus.grpc.protogen.ObserveJobsQuery in project titus-control-plane by Netflix.

the class ObserveJobsSubscription method tryInitialize.

private boolean tryInitialize() {
    ObserveJobsQuery query = getLastObserveJobsQueryEvent();
    if (query == null) {
        return false;
    }
    Stopwatch start = Stopwatch.createStarted();
    String trxId = UUID.randomUUID().toString();
    CallMetadata callMetadata = context.getCallMetadataResolver().resolve().orElse(CallMetadataConstants.UNDEFINED_CALL_METADATA);
    metrics.observeJobsStarted(trxId, callMetadata);
    JobQueryCriteria<TaskStatus.TaskState, JobDescriptor.JobSpecCase> criteria = toJobQueryCriteria(query);
    V3JobQueryCriteriaEvaluator jobsPredicate = new V3JobQueryCriteriaEvaluator(criteria, titusRuntime);
    V3TaskQueryCriteriaEvaluator tasksPredicate = new V3TaskQueryCriteriaEvaluator(criteria, titusRuntime);
    Observable<JobChangeNotification> eventStream = context.getJobOperations().observeJobs(jobsPredicate, tasksPredicate, true).filter(event -> withArchived || !event.isArchived()).observeOn(context.getObserveJobsScheduler()).subscribeOn(context.getObserveJobsScheduler(), false).map(event -> GrpcJobManagementModelConverters.toGrpcJobChangeNotification(event, context.getGrpcObjectsCache(), titusRuntime.getClock().wallTime())).compose(ObservableExt.head(() -> {
        List<JobChangeNotification> snapshot = createJobsSnapshot(jobsPredicate, tasksPredicate);
        snapshot.add(SNAPSHOT_END_MARKER);
        return snapshot;
    })).doOnError(e -> logger.error("Unexpected error in jobs event stream", e));
    AtomicBoolean closingProcessed = new AtomicBoolean();
    this.jobServiceSubscription = eventStream.doOnUnsubscribe(() -> {
        if (!closingProcessed.getAndSet(true)) {
            metrics.observeJobsUnsubscribed(trxId, start.elapsed(TimeUnit.MILLISECONDS));
        }
    }).subscribe(event -> {
        metrics.observeJobsEventEmitted(trxId);
        jobServiceEvents.add(event);
        drain();
    }, e -> {
        if (!closingProcessed.getAndSet(true)) {
            metrics.observeJobsError(trxId, start.elapsed(TimeUnit.MILLISECONDS), e);
        }
        jobServiceCompleted = true;
        jobServiceError = new StatusRuntimeException(Status.INTERNAL.withDescription("All jobs monitoring stream terminated with an error").withCause(e));
        drain();
    }, () -> {
        if (!closingProcessed.getAndSet(true)) {
            metrics.observeJobsCompleted(trxId, start.elapsed(TimeUnit.MILLISECONDS));
        }
        jobServiceCompleted = true;
        drain();
    });
    this.grpcStreamInitiated = true;
    return true;
}
Also used : KeepAliveResponse(com.netflix.titus.grpc.protogen.KeepAliveResponse) Stopwatch(com.google.common.base.Stopwatch) ObserveJobsQuery(com.netflix.titus.grpc.protogen.ObserveJobsQuery) Task(com.netflix.titus.api.jobmanager.model.job.Task) CallMetadataConstants(com.netflix.titus.api.model.callmetadata.CallMetadataConstants) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) Observable(rx.Observable) StreamObserver(io.grpc.stub.StreamObserver) Pair(com.netflix.titus.common.util.tuple.Pair) TaskStatus(com.netflix.titus.grpc.protogen.TaskStatus) ExceptionExt(com.netflix.titus.common.util.ExceptionExt) Status(io.grpc.Status) CallMetadata(com.netflix.titus.api.model.callmetadata.CallMetadata) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) JobDescriptor(com.netflix.titus.grpc.protogen.JobDescriptor) V3TaskQueryCriteriaEvaluator(com.netflix.titus.runtime.endpoint.v3.grpc.query.V3TaskQueryCriteriaEvaluator) Job(com.netflix.titus.api.jobmanager.model.job.Job) KeepAliveRequest(com.netflix.titus.grpc.protogen.KeepAliveRequest) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) SNAPSHOT_END_MARKER(com.netflix.titus.master.jobmanager.endpoint.v3.grpc.ObserveJobsContext.SNAPSHOT_END_MARKER) BlockingQueue(java.util.concurrent.BlockingQueue) UUID(java.util.UUID) GrpcJobManagementModelConverters(com.netflix.titus.runtime.endpoint.v3.grpc.GrpcJobManagementModelConverters) TimeUnit(java.util.concurrent.TimeUnit) StatusRuntimeException(io.grpc.StatusRuntimeException) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) JobQueryCriteria(com.netflix.titus.runtime.endpoint.JobQueryCriteria) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) V3JobQueryCriteriaEvaluator(com.netflix.titus.runtime.endpoint.v3.grpc.query.V3JobQueryCriteriaEvaluator) GrpcJobQueryModelConverters.toJobQueryCriteria(com.netflix.titus.runtime.endpoint.v3.grpc.GrpcJobQueryModelConverters.toJobQueryCriteria) ObserveJobsWithKeepAliveRequest(com.netflix.titus.grpc.protogen.ObserveJobsWithKeepAliveRequest) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TitusRuntime(com.netflix.titus.common.runtime.TitusRuntime) ObservableExt(com.netflix.titus.common.util.rx.ObservableExt) Subscription(rx.Subscription) JobChangeNotification(com.netflix.titus.grpc.protogen.JobChangeNotification) V3TaskQueryCriteriaEvaluator(com.netflix.titus.runtime.endpoint.v3.grpc.query.V3TaskQueryCriteriaEvaluator) CallMetadata(com.netflix.titus.api.model.callmetadata.CallMetadata) Stopwatch(com.google.common.base.Stopwatch) V3JobQueryCriteriaEvaluator(com.netflix.titus.runtime.endpoint.v3.grpc.query.V3JobQueryCriteriaEvaluator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JobChangeNotification(com.netflix.titus.grpc.protogen.JobChangeNotification) StatusRuntimeException(io.grpc.StatusRuntimeException) ObserveJobsQuery(com.netflix.titus.grpc.protogen.ObserveJobsQuery)

Example 2 with ObserveJobsQuery

use of com.netflix.titus.grpc.protogen.ObserveJobsQuery in project titus-control-plane by Netflix.

the class JobObserveTest method observeSnapshotWithFilter.

@Test(timeout = LONG_TEST_TIMEOUT_MS)
public void observeSnapshotWithFilter() throws InterruptedException {
    startAll(batchJobDescriptors().getValue().toBuilder().withApplicationName("myApp").build(), batchJobDescriptors().getValue().toBuilder().withApplicationName("otherApp").build());
    String myAppJobId = jobsScenarioBuilder.takeJobId(0);
    // start the stream after tasks are already running
    ObserveJobsQuery query = ObserveJobsQuery.newBuilder().putFilteringCriteria("applicationName", "myApp").build();
    TestStreamObserver<JobChangeNotification> subscriber = subscribe(query);
    assertNextIsJobEvent(subscriber, job -> assertThat(job.getJobDescriptor().getApplicationName()).isEqualTo("myApp"));
    assertNextIsTaskEvent(subscriber, task -> assertThat(task.getJobId()).isEqualTo(myAppJobId));
    assertNextIsSnapshot(subscriber);
}
Also used : JobChangeNotification(com.netflix.titus.grpc.protogen.JobChangeNotification) ObserveJobsQuery(com.netflix.titus.grpc.protogen.ObserveJobsQuery) BaseIntegrationTest(com.netflix.titus.master.integration.BaseIntegrationTest) IntegrationTest(com.netflix.titus.testkit.junit.category.IntegrationTest) Test(org.junit.Test)

Example 3 with ObserveJobsQuery

use of com.netflix.titus.grpc.protogen.ObserveJobsQuery in project titus-control-plane by Netflix.

the class ObserveJobsCommand method executeWithFiltering.

private void executeWithFiltering(CommandContext context, Set<String> jobFields, Set<String> taskFields, boolean printEvents, boolean snapshotOnly) {
    JobManagementServiceBlockingStub stub = context.getJobManagementGrpcBlockingStub();
    Stopwatch stopwatch = Stopwatch.createStarted();
    ObserveJobsQuery query = ObserveJobsQuery.newBuilder().addAllJobFields(jobFields).addAllTaskFields(taskFields).build();
    Iterator<JobChangeNotification> eventIt = stub.observeJobs(query);
    while (eventIt.hasNext()) {
        JobChangeNotification next = eventIt.next();
        if (next.getNotificationCase() == JobChangeNotification.NotificationCase.SNAPSHOTEND) {
            logger.info("Emitted: snapshot marker in {}ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));
            if (snapshotOnly) {
                return;
            }
        } else if (next.getNotificationCase() == JobChangeNotification.NotificationCase.JOBUPDATE) {
            com.netflix.titus.grpc.protogen.Job job = next.getJobUpdate().getJob();
            if (printEvents) {
                logger.info("Emitted job update: jobId={}({}), jobState={}, version={}", job.getId(), next.getJobUpdate().getArchived() ? "archived" : job.getStatus().getState(), job.getStatus(), job.getVersion());
            }
        } else if (next.getNotificationCase() == JobChangeNotification.NotificationCase.TASKUPDATE) {
            com.netflix.titus.grpc.protogen.Task task = next.getTaskUpdate().getTask();
            if (printEvents) {
                logger.info("Emitted task update: jobId={}({}), taskId={}, taskState={}, version={}", task.getJobId(), next.getTaskUpdate().getArchived() ? "archived" : task.getStatus().getState(), task.getId(), task.getStatus(), task.getVersion());
            }
        } else {
            logger.info("Unrecognized event type: {}", next);
        }
    }
}
Also used : JobChangeNotification(com.netflix.titus.grpc.protogen.JobChangeNotification) JobManagementServiceBlockingStub(com.netflix.titus.grpc.protogen.JobManagementServiceGrpc.JobManagementServiceBlockingStub) Stopwatch(com.google.common.base.Stopwatch) Job(com.netflix.titus.api.jobmanager.model.job.Job) ObserveJobsQuery(com.netflix.titus.grpc.protogen.ObserveJobsQuery)

Example 4 with ObserveJobsQuery

use of com.netflix.titus.grpc.protogen.ObserveJobsQuery in project titus-control-plane by Netflix.

the class ObserveJobsSubscription method getLastObserveJobsQueryEvent.

private ObserveJobsQuery getLastObserveJobsQueryEvent() {
    ObserveJobsQuery jobsQuery = null;
    Pair<Long, ObserveJobsWithKeepAliveRequest> eventPair;
    while ((eventPair = grpcClientEvents.poll()) != null) {
        ObserveJobsWithKeepAliveRequest event = eventPair.getRight();
        if (event.getKindCase() == ObserveJobsWithKeepAliveRequest.KindCase.QUERY) {
            jobsQuery = event.getQuery();
        }
    }
    return jobsQuery;
}
Also used : ObserveJobsWithKeepAliveRequest(com.netflix.titus.grpc.protogen.ObserveJobsWithKeepAliveRequest) AtomicLong(java.util.concurrent.atomic.AtomicLong) ObserveJobsQuery(com.netflix.titus.grpc.protogen.ObserveJobsQuery)

Example 5 with ObserveJobsQuery

use of com.netflix.titus.grpc.protogen.ObserveJobsQuery in project titus-control-plane by Netflix.

the class LocalCacheQueryProcessorTest method testObserveJobsWithFieldsFiltering.

@Test
public void testObserveJobsWithFieldsFiltering() throws InterruptedException {
    Pair<Job<?>, List<Task>> jobAndTasks = addToJobDataReplicator(newJobAndTasks("job1", 1));
    Job<?> job1 = jobAndTasks.getLeft();
    Task task1 = jobAndTasks.getRight().get(0);
    ExtTestSubscriber<JobChangeNotification> subscriber = new ExtTestSubscriber<>();
    ObserveJobsQuery query = ObserveJobsQuery.newBuilder().addJobFields("status").addTaskFields("taskContext").build();
    processor.observeJobs(query).subscribe(subscriber);
    // Snapshot
    emitEvent(Pair.of(jobDataReplicator.getCurrent(), JobManagerEvent.snapshotMarker()));
    expectJobUpdateEvent(subscriber, j -> {
        assertThat(j.getId()).isEqualTo(job1.getId());
        assertThat(j.getStatus().getReasonMessage()).isEqualTo("<not_given>");
        assertThat(j.getJobDescriptor()).isEqualTo(JobDescriptor.getDefaultInstance());
    });
    expectTaskUpdateEvent(subscriber, t -> {
        assertThat(t.getId()).isEqualTo(task1.getId());
        assertThat(t.getStatus()).isEqualTo(TaskStatus.getDefaultInstance());
        assertThat(t.getTaskContextMap()).containsAllEntriesOf(task1.getTaskContext());
    });
    expectSnapshot(subscriber);
    // Updates after snapshot marker
    Pair<Job<?>, List<Task>> jobAndTasks2 = addToJobDataReplicator(newJobAndTasks("job2", 1));
    Job<?> job2 = jobAndTasks2.getLeft();
    Task task2 = jobAndTasks2.getRight().get(0);
    JobUpdateEvent jobUpdateEvent = JobUpdateEvent.newJob(job2, JUNIT_CALL_METADATA);
    TaskUpdateEvent taskUpdateEvent = TaskUpdateEvent.newTask(job2, task2, JUNIT_CALL_METADATA);
    emitEvent(Pair.of(jobDataReplicator.getCurrent(), jobUpdateEvent));
    emitEvent(Pair.of(jobDataReplicator.getCurrent(), taskUpdateEvent));
    expectJobUpdateEvent(subscriber, j -> {
        assertThat(j.getId()).isEqualTo(job2.getId());
        assertThat(j.getStatus().getReasonMessage()).isEqualTo("<not_given>");
        assertThat(j.getJobDescriptor()).isEqualTo(JobDescriptor.getDefaultInstance());
    });
    expectTaskUpdateEvent(subscriber, t -> {
        assertThat(t.getId()).isEqualTo(task2.getId());
        assertThat(t.getStatus()).isEqualTo(TaskStatus.getDefaultInstance());
        assertThat(t.getTaskContextMap()).containsAllEntriesOf(task2.getTaskContext());
    });
}
Also used : Task(com.netflix.titus.api.jobmanager.model.job.Task) ExtTestSubscriber(com.netflix.titus.testkit.rx.ExtTestSubscriber) JobChangeNotification(com.netflix.titus.grpc.protogen.JobChangeNotification) List(java.util.List) Job(com.netflix.titus.api.jobmanager.model.job.Job) ObserveJobsQuery(com.netflix.titus.grpc.protogen.ObserveJobsQuery) TaskUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent) JobUpdateEvent(com.netflix.titus.api.jobmanager.model.job.event.JobUpdateEvent) Test(org.junit.Test)

Aggregations

ObserveJobsQuery (com.netflix.titus.grpc.protogen.ObserveJobsQuery)5 JobChangeNotification (com.netflix.titus.grpc.protogen.JobChangeNotification)4 Job (com.netflix.titus.api.jobmanager.model.job.Job)3 Stopwatch (com.google.common.base.Stopwatch)2 Task (com.netflix.titus.api.jobmanager.model.job.Task)2 ObserveJobsWithKeepAliveRequest (com.netflix.titus.grpc.protogen.ObserveJobsWithKeepAliveRequest)2 List (java.util.List)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 JobUpdateEvent (com.netflix.titus.api.jobmanager.model.job.event.JobUpdateEvent)1 TaskUpdateEvent (com.netflix.titus.api.jobmanager.model.job.event.TaskUpdateEvent)1 CallMetadata (com.netflix.titus.api.model.callmetadata.CallMetadata)1 CallMetadataConstants (com.netflix.titus.api.model.callmetadata.CallMetadataConstants)1 TitusRuntime (com.netflix.titus.common.runtime.TitusRuntime)1 ExceptionExt (com.netflix.titus.common.util.ExceptionExt)1 ObservableExt (com.netflix.titus.common.util.rx.ObservableExt)1 Pair (com.netflix.titus.common.util.tuple.Pair)1 JobDescriptor (com.netflix.titus.grpc.protogen.JobDescriptor)1 JobManagementServiceBlockingStub (com.netflix.titus.grpc.protogen.JobManagementServiceGrpc.JobManagementServiceBlockingStub)1