use of com.netflix.titus.grpc.protogen.Page in project titus-control-plane by Netflix.
the class GatewayJobServiceGateway method findTasks.
@Override
public Observable<TaskQueryResult> findTasks(TaskQuery taskQuery, CallMetadata callMetadata) {
Map<String, String> filteringCriteriaMap = taskQuery.getFilteringCriteriaMap();
Set<String> v3JobIds = new HashSet<>(StringExt.splitByComma(filteringCriteriaMap.getOrDefault("jobIds", "")));
boolean needsMigrationFilter = "true".equalsIgnoreCase(filteringCriteriaMap.getOrDefault("needsMigration", "false"));
// "needsMigration" query is served from the local job and relocation cache.
if (needsMigrationFilter) {
PageResult<Task> pageResult = needsMigrationQueryHandler.findTasks(GrpcJobQueryModelConverters.toJobQueryCriteria(taskQuery), toPage(taskQuery.getPage()));
TaskQueryResult taskQueryResult = TaskQueryResult.newBuilder().setPagination(toGrpcPagination(pageResult.getPagination())).addAllItems(taskDataInjector.injectIntoTasks(pageResult.getItems())).build();
return Observable.just(taskQueryResult);
}
Set<String> taskStates = Sets.newHashSet(StringExt.splitByComma(taskQuery.getFilteringCriteriaMap().getOrDefault("taskStates", "")));
Observable<TaskQueryResult> observable;
if (v3JobIds.isEmpty()) {
// Active task set only
observable = newActiveTaskQueryAction(taskQuery, callMetadata);
} else {
if (!taskStates.contains(TaskState.Finished.name())) {
// Active task set only
observable = newActiveTaskQueryAction(taskQuery, callMetadata);
} else {
Page page = taskQuery.getPage();
boolean nextPageByNumber = StringExt.isEmpty(page.getCursor()) && page.getPageNumber() > 0;
if (nextPageByNumber) {
// In this case we ask for active and archived tasks using a page number > 0. Because of that
// we have to fetch as much tasks from master as we can. Tasks that we do not fetch, will not be
// visible to the client.
TaskQuery largePageQuery = taskQuery.toBuilder().setPage(taskQuery.getPage().toBuilder().setPageNumber(0).setPageSize(gatewayConfiguration.getMaxTaskPageSize())).build();
observable = newActiveTaskQueryAction(largePageQuery, callMetadata);
} else {
observable = newActiveTaskQueryAction(taskQuery, callMetadata);
}
observable = observable.flatMap(result -> retrieveArchivedTasksForJobs(v3JobIds, taskQuery).map(archivedTasks -> combineTaskResults(taskQuery, result, archivedTasks)));
}
}
return observable.timeout(tunablesConfiguration.getRequestTimeoutMs(), TimeUnit.MILLISECONDS).map(queryResult -> taskDataInjector.injectIntoTaskQueryResult(queryResult));
}
use of com.netflix.titus.grpc.protogen.Page in project titus-control-plane by Netflix.
the class LocalCacheQueryProcessorTest method testFindTasks.
@Test
public void testFindTasks() {
List<Task> tasks1 = addToJobDataReplicator(newJobAndTasks("job1", 2)).getRight();
List<Task> tasks2 = addToJobDataReplicator(newJobAndTasks("job2", 4)).getRight();
List<Task> tasks3 = addToJobDataReplicator(newJobAndTasks("job3", 0)).getRight();
// Expect three pages
TaskQueryResult page1Result = processor.findTasks(TASK_QUERY_ALL_WITH_PAGE_SIZE_2);
assertThat(page1Result.getItemsList()).hasSize(2);
Page page2 = Page.newBuilder().setPageSize(2).setCursor(page1Result.getPagination().getCursor()).build();
TaskQueryResult page2Result = processor.findTasks(TaskQuery.newBuilder().setPage(page2).build());
assertThat(page2Result.getItemsList()).hasSize(2);
Page page3 = Page.newBuilder().setPageSize(2).setCursor(page2Result.getPagination().getCursor()).build();
TaskQueryResult page3Result = processor.findTasks(TaskQuery.newBuilder().setPage(page3).build());
assertThat(page3Result.getItemsList()).hasSize(2);
assertThat(page3Result.getPagination().getHasMore()).isFalse();
List<String> taskIds = CollectionsExt.merge(page1Result.getItemsList().stream().map(com.netflix.titus.grpc.protogen.Task::getId).collect(Collectors.toList()), page2Result.getItemsList().stream().map(com.netflix.titus.grpc.protogen.Task::getId).collect(Collectors.toList()), page3Result.getItemsList().stream().map(com.netflix.titus.grpc.protogen.Task::getId).collect(Collectors.toList()));
List<String> expectedTaskIds = CollectionsExt.merge(tasks1.stream().map(Task::getId).collect(Collectors.toList()), tasks2.stream().map(Task::getId).collect(Collectors.toList()), tasks3.stream().map(Task::getId).collect(Collectors.toList()));
assertThat(taskIds).containsAll(expectedTaskIds);
}
use of com.netflix.titus.grpc.protogen.Page in project titus-control-plane by Netflix.
the class JobCriteriaQueryTest method testPagination.
@Test(timeout = 30_000)
public void testPagination() {
// We have 3 batch and 3 service jobs.
Page firstPageOf5 = Page.newBuilder().setPageNumber(0).setPageSize(5).build();
Page secondPageOf5 = Page.newBuilder().setPageNumber(1).setPageSize(5).build();
// Jobs
JobQuery.Builder jobQueryBuilder = JobQuery.newBuilder().putFilteringCriteria("attributes", PRE_CREATED_JOBS_LABEL);
JobQueryResult jobQueryResult = client.findJobs(jobQueryBuilder.setPage(firstPageOf5).build());
assertThat(jobQueryResult.getItemsList()).hasSize(5);
checkPage(jobQueryResult.getPagination(), firstPageOf5, 2, 6, true);
JobQueryResult jobQueryResult2 = client.findJobs(jobQueryBuilder.setPage(secondPageOf5).build());
assertThat(jobQueryResult2.getItemsList()).hasSize(1);
checkPage(jobQueryResult2.getPagination(), secondPageOf5, 2, 6, false);
Set<String> foundJobIds = new HashSet<>();
jobQueryResult.getItemsList().forEach(j -> foundJobIds.add(j.getId()));
jobQueryResult2.getItemsList().forEach(j -> foundJobIds.add(j.getId()));
assertThat(foundJobIds).hasSize(6);
// Tasks
TaskQuery.Builder taskQueryBuilder = TaskQuery.newBuilder().putFilteringCriteria("attributes", PRE_CREATED_JOBS_LABEL);
TaskQueryResult taskQueryResult = client.findTasks(taskQueryBuilder.setPage(firstPageOf5).build());
assertThat(taskQueryResult.getItemsList()).hasSize(5);
checkPage(taskQueryResult.getPagination(), firstPageOf5, 2, 6, true);
TaskQueryResult taskQueryResult2 = client.findTasks(taskQueryBuilder.setPage(secondPageOf5).build());
assertThat(taskQueryResult2.getItemsList()).hasSize(1);
checkPage(taskQueryResult2.getPagination(), secondPageOf5, 2, 6, false);
Set<String> foundTasksIds = new HashSet<>();
taskQueryResult.getItemsList().forEach(j -> foundTasksIds.add(j.getId()));
taskQueryResult2.getItemsList().forEach(j -> foundTasksIds.add(j.getId()));
assertThat(foundTasksIds).hasSize(6);
}
use of com.netflix.titus.grpc.protogen.Page in project titus-control-plane by Netflix.
the class JobCursorArchiveQueryTest method testArchiveQuery.
@Test(timeout = TEST_TIMEOUT_MS)
public void testArchiveQuery() {
Evaluators.times(TASKS_PER_JOB, idx -> jobScenarioBuilder.getTaskByIndex(idx).killTask().completeKillInitiated().expectStateUpdateSkipOther(TaskStatus.TaskState.Finished));
Evaluators.times(TASKS_PER_JOB, idx -> jobScenarioBuilder.expectTaskInSlot(idx, 1));
int allTasksCount = TASKS_PER_JOB * 2;
Set<String> taskIds = new HashSet<>();
Page current = TWO_ITEM_PAGE;
for (int i = 0; i < TASKS_PER_JOB; i++) {
TaskQuery query = TaskQuery.newBuilder().setPage(current).putFilteringCriteria("jobIds", jobScenarioBuilder.getJobId()).putFilteringCriteria("taskStates", ALL_TASK_STATES).build();
TaskQueryResult result = client.findTasks(query);
assertThat(result.getPagination().getTotalItems()).isEqualTo(allTasksCount);
assertThat(result.getPagination().getCursor()).isNotEmpty();
taskIds.addAll(result.getItemsList().stream().map(Task::getId).collect(Collectors.toList()));
current = query.getPage().toBuilder().setCursor(result.getPagination().getCursor()).build();
}
assertThat(taskIds).hasSize(allTasksCount);
}
Aggregations