use of com.netflix.titus.runtime.endpoint.JobQueryCriteria 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;
}
use of com.netflix.titus.runtime.endpoint.JobQueryCriteria in project titus-control-plane by Netflix.
the class LocalCacheQueryProcessor method findJobs.
public JobQueryResult findJobs(JobQuery jobQuery) {
JobQueryCriteria<TaskStatus.TaskState, JobDescriptor.JobSpecCase> queryCriteria = GrpcJobQueryModelConverters.toJobQueryCriteria(jobQuery);
Page page = toPage(jobQuery.getPage());
List<Job> matchingJobs = findMatchingJob(queryCriteria);
PageResult<Job> pageResult = JobManagerCursors.newCoreJobPaginationEvaluator().takePage(page, matchingJobs);
Set<String> fields = newFieldsFilter(jobQuery.getFieldsList(), JOB_MINIMUM_FIELD_SET);
List<com.netflix.titus.grpc.protogen.Job> grpcJob = pageResult.getItems().stream().map(coreJob -> {
com.netflix.titus.grpc.protogen.Job job = GrpcJobManagementModelConverters.toGrpcJob(coreJob);
if (!fields.isEmpty()) {
job = ProtobufExt.copy(job, fields);
}
return job;
}).collect(Collectors.toList());
return JobQueryResult.newBuilder().setPagination(toGrpcPagination(pageResult.getPagination())).addAllItems(grpcJob).build();
}
use of com.netflix.titus.runtime.endpoint.JobQueryCriteria in project titus-control-plane by Netflix.
the class LocalCacheQueryProcessor method findTasks.
public TaskQueryResult findTasks(TaskQuery taskQuery) {
JobQueryCriteria<TaskStatus.TaskState, JobDescriptor.JobSpecCase> queryCriteria = GrpcJobQueryModelConverters.toJobQueryCriteria(taskQuery);
Page page = toPage(taskQuery.getPage());
List<com.netflix.titus.api.jobmanager.model.job.Task> matchingTasks = findMatchingTasks(queryCriteria);
PageResult<com.netflix.titus.api.jobmanager.model.job.Task> pageResult = JobManagerCursors.newCoreTaskPaginationEvaluator().takePage(page, matchingTasks);
Set<String> fields = newFieldsFilter(taskQuery.getFieldsList(), TASK_MINIMUM_FIELD_SET);
List<Task> grpcTasks = pageResult.getItems().stream().map(task -> {
Task grpcTask = GrpcJobManagementModelConverters.toGrpcTask(task, logStorageInfo);
if (!fields.isEmpty()) {
grpcTask = ProtobufExt.copy(grpcTask, fields);
}
return grpcTask;
}).collect(Collectors.toList());
return TaskQueryResult.newBuilder().setPagination(toGrpcPagination(pageResult.getPagination())).addAllItems(grpcTasks).build();
}
Aggregations