Search in sources :

Example 1 with Pagination

use of com.netflix.titus.api.model.Pagination in project titus-control-plane by Netflix.

the class DefaultJobManagementServiceGrpc method findTasks.

@Override
public void findTasks(TaskQuery taskQuery, StreamObserver<TaskQueryResult> responseObserver) {
    if (!checkPageIsValid(taskQuery.getPage(), responseObserver)) {
        return;
    }
    try {
        // We need to find all tasks to get the total number of them.
        List<com.netflix.titus.api.jobmanager.model.job.Task> allFilteredTasks = new ArrayList<>();
        for (Pair<com.netflix.titus.api.jobmanager.model.job.Job<?>, com.netflix.titus.api.jobmanager.model.job.Task> jobTaskPair : jobOperations.findTasks(new V3TaskQueryCriteriaEvaluator(toJobQueryCriteria(taskQuery), titusRuntime), 0, Integer.MAX_VALUE / 2)) {
            com.netflix.titus.api.jobmanager.model.job.Task right = jobTaskPair.getRight();
            allFilteredTasks.add(right);
        }
        Pair<List<com.netflix.titus.api.jobmanager.model.job.Task>, Pagination> queryResult = PaginationUtil.takePageWithCursorAndKeyExtractor(toPage(taskQuery.getPage()), allFilteredTasks, JobComparators::createTaskKeyOf, JobManagerCursors::coreTaskIndexOf, JobManagerCursors::newTaskCoreCursorFrom);
        List<Task> grpcTasks = new ArrayList<>();
        for (com.netflix.titus.api.jobmanager.model.job.Task task : queryResult.getLeft()) {
            Task toGrpcTask = grpcObjectsCache.getTask(task);
            grpcTasks.add(toGrpcTask);
        }
        TaskQueryResult grpcQueryResult;
        if (taskQuery.getFieldsList().isEmpty()) {
            grpcQueryResult = toTaskQueryResult(grpcTasks, queryResult.getRight());
        } else {
            Set<String> fields = new HashSet<>(taskQuery.getFieldsList());
            fields.addAll(TASK_MINIMUM_FIELD_SET);
            List<Task> filtered = new ArrayList<>();
            for (Task t : grpcTasks) {
                filtered.add(ProtobufExt.copy(t, fields));
            }
            grpcQueryResult = toTaskQueryResult(filtered, queryResult.getRight());
        }
        responseObserver.onNext(grpcQueryResult);
        responseObserver.onCompleted();
    } catch (Exception e) {
        safeOnError(logger, e, responseObserver);
    }
}
Also used : Task(com.netflix.titus.grpc.protogen.Task) V3TaskQueryCriteriaEvaluator(com.netflix.titus.runtime.endpoint.v3.grpc.query.V3TaskQueryCriteriaEvaluator) ArrayList(java.util.ArrayList) JobManagerCursors(com.netflix.titus.runtime.jobmanager.JobManagerCursors) Pagination(com.netflix.titus.api.model.Pagination) GrpcJobQueryModelConverters.toGrpcPagination(com.netflix.titus.runtime.endpoint.v3.grpc.GrpcJobQueryModelConverters.toGrpcPagination) JobComparators(com.netflix.titus.runtime.jobmanager.JobComparators) ArrayList(java.util.ArrayList) List(java.util.List) Job(com.netflix.titus.grpc.protogen.Job) HashSet(java.util.HashSet) StatusRuntimeException(io.grpc.StatusRuntimeException) TitusServiceException(com.netflix.titus.api.service.TitusServiceException) JobManagerException(com.netflix.titus.api.jobmanager.service.JobManagerException) TaskQueryResult(com.netflix.titus.grpc.protogen.TaskQueryResult)

Example 2 with Pagination

use of com.netflix.titus.api.model.Pagination in project titus-control-plane by Netflix.

the class DefaultLoadBalancerServiceGrpc method getAllLoadBalancers.

@Override
public void getAllLoadBalancers(GetAllLoadBalancersRequest request, StreamObserver<GetAllLoadBalancersResult> responseObserver) {
    logger.debug("Received get all load balancer request {}", request);
    Page grpcPage = request.getPage();
    if (!checkPageIsValid(grpcPage, responseObserver)) {
        return;
    }
    try {
        Pair<List<JobLoadBalancer>, Pagination> pageResult = loadBalancerService.getAllLoadBalancers(toPage(grpcPage));
        responseObserver.onNext(toGetAllLoadBalancersResult(pageResult.getLeft(), pageResult.getRight()));
        responseObserver.onCompleted();
    } catch (Exception e) {
        safeOnError(logger, e, responseObserver);
    }
}
Also used : Pagination(com.netflix.titus.api.model.Pagination) Page(com.netflix.titus.grpc.protogen.Page) GrpcJobQueryModelConverters.toPage(com.netflix.titus.runtime.endpoint.v3.grpc.GrpcJobQueryModelConverters.toPage) List(java.util.List)

Example 3 with Pagination

use of com.netflix.titus.api.model.Pagination in project titus-control-plane by Netflix.

the class DefaultLoadBalancerService method getAllLoadBalancers.

@Override
public Pair<List<JobLoadBalancer>, Pagination> getAllLoadBalancers(Page page) {
    if (StringExt.isNotEmpty(page.getCursor())) {
        final List<JobLoadBalancer> allLoadBalancers = loadBalancerStore.getAssociations().stream().map(JobLoadBalancerState::getJobLoadBalancer).sorted(LoadBalancerCursors.loadBalancerComparator()).collect(Collectors.toList());
        return PaginationUtil.takePageWithCursor(Page.newBuilder().withPageSize(page.getPageSize()).withCursor(page.getCursor()).build(), allLoadBalancers, LoadBalancerCursors.loadBalancerComparator(), LoadBalancerCursors::loadBalancerIndexOf, LoadBalancerCursors::newCursorFrom);
    }
    // no cursor provided
    int offset = page.getPageSize() * page.getPageNumber();
    // Grab an extra item so we can tell if there's more to read after offset+limit.
    int limit = page.getPageSize() + 1;
    List<JobLoadBalancer> jobLoadBalancerPageList = loadBalancerStore.getAssociationsPage(offset, limit);
    boolean hasMore = jobLoadBalancerPageList.size() > page.getPageSize();
    jobLoadBalancerPageList = hasMore ? jobLoadBalancerPageList.subList(0, page.getPageSize()) : jobLoadBalancerPageList;
    final String cursor = jobLoadBalancerPageList.isEmpty() ? "" : LoadBalancerCursors.newCursorFrom(jobLoadBalancerPageList.get(jobLoadBalancerPageList.size() - 1));
    return Pair.of(jobLoadBalancerPageList, new Pagination(page, hasMore, 1, jobLoadBalancerPageList.size(), cursor, 0));
}
Also used : Pagination(com.netflix.titus.api.model.Pagination) JobLoadBalancerState(com.netflix.titus.api.loadbalancer.model.JobLoadBalancerState) LoadBalancerCursors(com.netflix.titus.runtime.loadbalancer.LoadBalancerCursors) JobLoadBalancer(com.netflix.titus.api.loadbalancer.model.JobLoadBalancer)

Example 4 with Pagination

use of com.netflix.titus.api.model.Pagination in project titus-control-plane by Netflix.

the class DefaultJobManagementServiceGrpc method findJobs.

@Override
public void findJobs(JobQuery jobQuery, StreamObserver<JobQueryResult> responseObserver) {
    if (!checkPageIsValid(jobQuery.getPage(), responseObserver)) {
        return;
    }
    try {
        // We need to find all jobs to get the total number of them.
        List<com.netflix.titus.api.jobmanager.model.job.Job<?>> allFilteredJobs = jobOperations.findJobs(new V3JobQueryCriteriaEvaluator(toJobQueryCriteria(jobQuery), titusRuntime), 0, Integer.MAX_VALUE / 2);
        Pair<List<com.netflix.titus.api.jobmanager.model.job.Job<?>>, Pagination> queryResult = PaginationUtil.takePageWithCursorAndKeyExtractor(toPage(jobQuery.getPage()), allFilteredJobs, JobComparators::createJobKeyOf, JobManagerCursors::coreJobIndexOf, JobManagerCursors::newJobCoreCursorFrom);
        List<Job> grpcJobs = new ArrayList<>();
        for (com.netflix.titus.api.jobmanager.model.job.Job<?> job : queryResult.getLeft()) {
            Job toGrpcJob = grpcObjectsCache.getJob(job);
            grpcJobs.add(toGrpcJob);
        }
        JobQueryResult grpcQueryResult;
        if (jobQuery.getFieldsList().isEmpty()) {
            grpcQueryResult = toJobQueryResult(grpcJobs, queryResult.getRight());
        } else {
            Set<String> fields = new HashSet<>(jobQuery.getFieldsList());
            fields.addAll(JOB_MINIMUM_FIELD_SET);
            List<Job> list = new ArrayList<>();
            for (Job j : grpcJobs) {
                list.add(ProtobufExt.copy(j, fields));
            }
            grpcQueryResult = toJobQueryResult(list, queryResult.getRight());
        }
        responseObserver.onNext(grpcQueryResult);
        responseObserver.onCompleted();
    } catch (Exception e) {
        safeOnError(logger, e, responseObserver);
    }
}
Also used : ArrayList(java.util.ArrayList) JobManagerCursors(com.netflix.titus.runtime.jobmanager.JobManagerCursors) V3JobQueryCriteriaEvaluator(com.netflix.titus.runtime.endpoint.v3.grpc.query.V3JobQueryCriteriaEvaluator) JobQueryResult(com.netflix.titus.grpc.protogen.JobQueryResult) StatusRuntimeException(io.grpc.StatusRuntimeException) TitusServiceException(com.netflix.titus.api.service.TitusServiceException) JobManagerException(com.netflix.titus.api.jobmanager.service.JobManagerException) Pagination(com.netflix.titus.api.model.Pagination) GrpcJobQueryModelConverters.toGrpcPagination(com.netflix.titus.runtime.endpoint.v3.grpc.GrpcJobQueryModelConverters.toGrpcPagination) JobComparators(com.netflix.titus.runtime.jobmanager.JobComparators) ArrayList(java.util.ArrayList) List(java.util.List) Job(com.netflix.titus.grpc.protogen.Job) HashSet(java.util.HashSet)

Example 5 with Pagination

use of com.netflix.titus.api.model.Pagination in project titus-control-plane by Netflix.

the class CellWithFixedJobsService method findJobs.

@Override
public void findJobs(JobQuery request, StreamObserver<JobQueryResult> responseObserver) {
    Pair<List<Job>, Pagination> page = PaginationUtil.takePageWithCursor(toPage(request.getPage()), getJobsList(), JobManagerCursors.jobCursorOrderComparator(), JobManagerCursors::jobIndexOf, JobManagerCursors::newCursorFrom);
    Set<String> fieldsFilter = new HashSet<>(request.getFieldsList());
    if (!fieldsFilter.isEmpty()) {
        fieldsFilter.add("id");
        page = page.mapLeft(jobs -> jobs.stream().map(job -> ProtobufExt.copy(job, fieldsFilter)).collect(Collectors.toList()));
    }
    JobQueryResult result = JobQueryResult.newBuilder().addAllItems(page.getLeft()).setPagination(toGrpcPagination(page.getRight())).build();
    responseObserver.onNext(result);
    responseObserver.onCompleted();
}
Also used : NOT_FOUND(io.grpc.Status.NOT_FOUND) ObserveJobsQuery(com.netflix.titus.grpc.protogen.ObserveJobsQuery) Job(com.netflix.titus.grpc.protogen.Job) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Observable(rx.Observable) HashSet(java.util.HashSet) Empty(com.google.protobuf.Empty) JobQuery(com.netflix.titus.grpc.protogen.JobQuery) JobQueryResult(com.netflix.titus.grpc.protogen.JobQueryResult) GrpcUtil(com.netflix.titus.runtime.endpoint.common.grpc.GrpcUtil) StreamObserver(io.grpc.stub.StreamObserver) Pair(com.netflix.titus.common.util.tuple.Pair) Map(java.util.Map) SnapshotEnd(com.netflix.titus.grpc.protogen.JobChangeNotification.SnapshotEnd) PaginationUtil(com.netflix.titus.api.model.PaginationUtil) Set(java.util.Set) JobManagementServiceGrpc(com.netflix.titus.grpc.protogen.JobManagementServiceGrpc) Collectors(java.util.stream.Collectors) ProtobufExt(com.netflix.titus.common.util.ProtobufExt) GrpcJobQueryModelConverters.toPage(com.netflix.titus.runtime.endpoint.v3.grpc.GrpcJobQueryModelConverters.toPage) List(java.util.List) GrpcJobQueryModelConverters.toGrpcPagination(com.netflix.titus.runtime.endpoint.v3.grpc.GrpcJobQueryModelConverters.toGrpcPagination) JobManagerCursors(com.netflix.titus.runtime.jobmanager.JobManagerCursors) Pagination(com.netflix.titus.api.model.Pagination) Collections(java.util.Collections) Subscription(rx.Subscription) JobChangeNotification(com.netflix.titus.grpc.protogen.JobChangeNotification) JobId(com.netflix.titus.grpc.protogen.JobId) GrpcJobQueryModelConverters.toGrpcPagination(com.netflix.titus.runtime.endpoint.v3.grpc.GrpcJobQueryModelConverters.toGrpcPagination) Pagination(com.netflix.titus.api.model.Pagination) ArrayList(java.util.ArrayList) List(java.util.List) JobManagerCursors(com.netflix.titus.runtime.jobmanager.JobManagerCursors) JobQueryResult(com.netflix.titus.grpc.protogen.JobQueryResult) HashSet(java.util.HashSet)

Aggregations

Pagination (com.netflix.titus.api.model.Pagination)6 List (java.util.List)5 GrpcJobQueryModelConverters.toGrpcPagination (com.netflix.titus.runtime.endpoint.v3.grpc.GrpcJobQueryModelConverters.toGrpcPagination)4 JobManagerCursors (com.netflix.titus.runtime.jobmanager.JobManagerCursors)4 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Job (com.netflix.titus.grpc.protogen.Job)3 GrpcJobQueryModelConverters.toPage (com.netflix.titus.runtime.endpoint.v3.grpc.GrpcJobQueryModelConverters.toPage)3 Empty (com.google.protobuf.Empty)2 JobManagerException (com.netflix.titus.api.jobmanager.service.JobManagerException)2 PaginationUtil (com.netflix.titus.api.model.PaginationUtil)2 TitusServiceException (com.netflix.titus.api.service.TitusServiceException)2 ProtobufExt (com.netflix.titus.common.util.ProtobufExt)2 Pair (com.netflix.titus.common.util.tuple.Pair)2 JobManagementServiceGrpc (com.netflix.titus.grpc.protogen.JobManagementServiceGrpc)2 JobQueryResult (com.netflix.titus.grpc.protogen.JobQueryResult)2 Task (com.netflix.titus.grpc.protogen.Task)2 TaskQueryResult (com.netflix.titus.grpc.protogen.TaskQueryResult)2 JobComparators (com.netflix.titus.runtime.jobmanager.JobComparators)2 NOT_FOUND (io.grpc.Status.NOT_FOUND)2