Skip to content

feat(task): add index on job_job, let job query data range configurable #4440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev/4.3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--
-- Add constraint (status, executor_destroyed_time) to `job_job` table
--
alter table `job_job` add index `idx_job_job_status_destroy_time`(`status`, `executor_destroyed_time`);
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,7 @@ public class DefaultTaskFrameworkProperties implements TaskFrameworkProperties {

// keep supervisor endpoint alive for a period if there is no task running on it or release it
private int supervisorReleaseBackSeconds = 300;

// only query task create time in recent days, -1 means query all created jobs
private int queryJobInRecentDays = -1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,8 @@ public interface TaskFrameworkProperties {

int getSupervisorReleaseBackSeconds();

/**
* only query task create time in recent days, -1 means query all created jobs
*/
int getQueryJobInRecentDays();
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@
@SkipAuthorize("odc internal usage")
public class StdTaskFrameworkService implements TaskFrameworkService {

private static final int RECENT_DAY = 30;

@Autowired
private JobRepository jobRepository;
@Autowired
Expand Down Expand Up @@ -167,79 +165,90 @@ public Page<JobEntity> find(JobStatus status, int page, int size) {

@Override
public Page<JobEntity> find(List<JobStatus> status, int page, int size) {
Specification<JobEntity> condition = Specification.where(getRecentDaySpec(RECENT_DAY))
.and(SpecificationUtil.columnIn(JobEntityColumn.STATUS, status));
Specification<JobEntity> condition =
Specification.where(getRecentDaySpec(taskFrameworkProperties.getQueryJobInRecentDays()))
.and(SpecificationUtil.columnIn(JobEntityColumn.STATUS, status));
return page(condition, page, size);
}

@Override
public Page<JobEntity> findCancelingJob(int page, int size) {
Specification<JobEntity> condition = Specification.where(getRecentDaySpec(RECENT_DAY))
.and(SpecificationUtil.columnEqual(JobEntityColumn.STATUS, JobStatus.CANCELING))
.and(getExecutorSpec());
Specification<JobEntity> condition =
Specification.where(getRecentDaySpec(taskFrameworkProperties.getQueryJobInRecentDays()))
.and(SpecificationUtil.columnEqual(JobEntityColumn.STATUS, JobStatus.CANCELING))
.and(getExecutorSpec());
return page(condition, page, size);
}

@Override
public Page<JobEntity> findNeedStoppedJobs(int page, int size) {
Specification<JobEntity> condition = Specification.where(getRecentDaySpec(RECENT_DAY))
.and(SpecificationUtil.columnIn(JobEntityColumn.STATUS,
Lists.newArrayList(JobStatus.CANCELING, JobStatus.TIMEOUT)))
.and(getExecutorSpec());
Specification<JobEntity> condition =
Specification.where(getRecentDaySpec(taskFrameworkProperties.getQueryJobInRecentDays()))
.and(SpecificationUtil.columnIn(JobEntityColumn.STATUS,
Lists.newArrayList(JobStatus.CANCELING, JobStatus.TIMEOUT)))
.and(getExecutorSpec());
return page(condition, page, size);
}

@Override
public Page<JobEntity> findNeedPullResultJobs(int page, int size) {
Specification<JobEntity> condition = Specification.where(getRecentDaySpec(RECENT_DAY))
.and(SpecificationUtil.columnIn(JobEntityColumn.STATUS,
Lists.newArrayList(JobStatus.DO_CANCELING, JobStatus.RUNNING)))
.and(getExecutorSpec());
Specification<JobEntity> condition =
Specification.where(getRecentDaySpec(taskFrameworkProperties.getQueryJobInRecentDays()))
.and(SpecificationUtil.columnIn(JobEntityColumn.STATUS,
Lists.newArrayList(JobStatus.DO_CANCELING, JobStatus.RUNNING)))
.and(getExecutorSpec());
return page(condition, page, size);
}

@Override
public Page<JobEntity> findTerminalJob(int page, int size) {
Specification<JobEntity> condition = Specification.where(getRecentDaySpec(RECENT_DAY))
.and(SpecificationUtil.columnIn(JobEntityColumn.STATUS,
Lists.newArrayList(JobStatus.EXEC_TIMEOUT, JobStatus.CANCELED, JobStatus.DONE,
JobStatus.FAILED)))
.and(SpecificationUtil.columnIsNull(JobEntityColumn.EXECUTOR_DESTROYED_TIME))
.and(getExecutorSpec());
Specification<JobEntity> condition =
Specification.where(getRecentDaySpec(taskFrameworkProperties.getQueryJobInRecentDays()))
.and(SpecificationUtil.columnIn(JobEntityColumn.STATUS,
Lists.newArrayList(JobStatus.EXEC_TIMEOUT, JobStatus.CANCELED, JobStatus.DONE,
JobStatus.FAILED)))
.and(SpecificationUtil.columnIsNull(JobEntityColumn.EXECUTOR_DESTROYED_TIME))
.and(getExecutorSpec());
return page(condition, page, size);
}

@Override
public Page<ResourceEntity> findAbandonedResource(int page, int size) {
Specification<ResourceEntity> specification = SpecificationUtil.columnLate(ResourceEntity.CREATE_TIME,
JobDateUtils.getCurrentDateSubtractDays(RECENT_DAY));
Specification<ResourceEntity> condition = Specification.where(specification)
.and(SpecificationUtil.columnEqual(ResourceEntity.STATUS, ResourceState.ABANDONED))
.and(SpecificationUtil.columnEqual(ResourceEntity.TYPE,
AbstractK8sResourceOperatorBuilder.CLOUD_K8S_POD_TYPE));
int recentDay = taskFrameworkProperties.getQueryJobInRecentDays();
Specification<ResourceEntity> condition =
Specification.where(SpecificationUtil.columnEqual(ResourceEntity.STATUS, ResourceState.ABANDONED));
condition = condition.and(SpecificationUtil.columnEqual(ResourceEntity.TYPE,
AbstractK8sResourceOperatorBuilder.CLOUD_K8S_POD_TYPE));
if (recentDay > 0) {
condition = condition.and(SpecificationUtil.columnLate(ResourceEntity.CREATE_TIME,
JobDateUtils.getCurrentDateSubtractDays(recentDay)));
}
return resourceRepository.findAll(condition, PageRequest.of(page, size));
}

@Override
public Page<JobEntity> findHeartTimeTimeoutJobs(int timeoutSeconds, int page, int size) {
Specification<JobEntity> condition = Specification.where(getRecentDaySpec(RECENT_DAY))
.and(SpecificationUtil.columnEqual(JobEntityColumn.STATUS, JobStatus.RUNNING))
.and((root, query, cb) -> getHeartTimeoutPredicate(root, cb, timeoutSeconds));
Specification<JobEntity> condition =
Specification.where(getRecentDaySpec(taskFrameworkProperties.getQueryJobInRecentDays()))
.and(SpecificationUtil.columnEqual(JobEntityColumn.STATUS, JobStatus.RUNNING))
.and((root, query, cb) -> getHeartTimeoutPredicate(root, cb, timeoutSeconds));
return page(condition, page, size);
}

@Override
public Page<JobEntity> findIncompleteJobs(int page, int size) {
Specification<JobEntity> condition = Specification.where(getRecentDaySpec(RECENT_DAY))
.and(SpecificationUtil.columnIn(JobEntityColumn.STATUS,
Lists.newArrayList(JobStatus.PREPARING, JobStatus.RUNNING)));
Specification<JobEntity> condition =
Specification.where(getRecentDaySpec(taskFrameworkProperties.getQueryJobInRecentDays()))
.and(SpecificationUtil.columnIn(JobEntityColumn.STATUS,
Lists.newArrayList(JobStatus.PREPARING, JobStatus.RUNNING)));
return page(condition, page, size);
}

@Override
public Page<JobEntity> findRunningJobs(int page, int size) {
Specification<JobEntity> condition = Specification.where(getRecentDaySpec(RECENT_DAY))
.and(SpecificationUtil.columnEqual(JobEntityColumn.STATUS, JobStatus.RUNNING));
Specification<JobEntity> condition =
Specification.where(getRecentDaySpec(taskFrameworkProperties.getQueryJobInRecentDays()))
.and(SpecificationUtil.columnEqual(JobEntityColumn.STATUS, JobStatus.RUNNING));
return page(condition, page, size);
}

Expand All @@ -258,13 +267,22 @@ public long countRunningJobs(TaskRunMode runMode) {

Root<JobEntity> root = query.from(JobEntity.class);
query.select(cb.count(root));
query.where(
cb.greaterThan(root.get(JobEntityColumn.CREATE_TIME),
JobDateUtils.getCurrentDateSubtractDays(RECENT_DAY)),
cb.equal(root.get(JobEntityColumn.RUN_MODE), runMode),
root.get(JobEntityColumn.STATUS).in(JobStatus.PREPARING).not(),
cb.isNull(root.get(JobEntityColumn.EXECUTOR_DESTROYED_TIME)),
executorPredicate(root, cb));
int recentDay = taskFrameworkProperties.getQueryJobInRecentDays();
if (recentDay > 0) {
query.where(
cb.greaterThan(root.get(JobEntityColumn.CREATE_TIME),
JobDateUtils.getCurrentDateSubtractDays(recentDay)),
cb.equal(root.get(JobEntityColumn.RUN_MODE), runMode),
root.get(JobEntityColumn.STATUS).in(JobStatus.PREPARING).not(),
cb.isNull(root.get(JobEntityColumn.EXECUTOR_DESTROYED_TIME)),
executorPredicate(root, cb));
} else {
query.where(
cb.equal(root.get(JobEntityColumn.RUN_MODE), runMode),
root.get(JobEntityColumn.STATUS).in(JobStatus.PREPARING).not(),
cb.isNull(root.get(JobEntityColumn.EXECUTOR_DESTROYED_TIME)),
executorPredicate(root, cb));
}
return entityManager.createQuery(query).getSingleResult();
}

Expand All @@ -290,7 +308,15 @@ private Page<JobEntity> page(Specification<JobEntity> specification, int page, i
}

private Specification<JobEntity> getRecentDaySpec(int days) {
return SpecificationUtil.columnLate(JobEntityColumn.CREATE_TIME, JobDateUtils.getCurrentDateSubtractDays(days));
if (days > 0) {
return SpecificationUtil.columnLate(JobEntityColumn.CREATE_TIME,
JobDateUtils.getCurrentDateSubtractDays(days));
} else {
// return always true like true = true
return (root, query, cb) -> {
return cb.isTrue(cb.literal(true));
};
}
}

@Override
Expand Down