AsyncJobStatusCurator extends AbstractHibernateCurator<AsyncJobStatus> { public int deleteJobs(AsyncJobStatusQueryBuilder queryBuilder) { EntityManager entityManager = this.getEntityManager(); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaDelete<AsyncJobStatus> query = criteriaBuilder.createCriteriaDelete(AsyncJobStatus.class); Root<AsyncJobStatus> job = query.from(AsyncJobStatus.class); List<Predicate> predicates = this.buildJobQueryPredicates(criteriaBuilder, job, queryBuilder); if (predicates.size() > 0) { Predicate[] predicateArray = new Predicate[predicates.size()]; query.where(predicates.toArray(predicateArray)); return entityManager.createQuery(query) .executeUpdate(); } return 0; } AsyncJobStatusCurator(); List<AsyncJobStatus> getJobsInState(Collection<JobState> states); List<AsyncJobStatus> getJobsInState(JobState... states); List<AsyncJobStatus> getNonTerminalJobs(); List<AsyncJobStatus> findJobs(AsyncJobStatusQueryBuilder queryBuilder); long getJobCount(AsyncJobStatusQueryBuilder queryBuilder); int deleteJobs(AsyncJobStatusQueryBuilder queryBuilder); int updateJobState(AsyncJobStatusQueryBuilder queryBuilder, JobState state); List<String> fetchJobIdsByArguments(String jobKey, Map<String, String> arguments); static final int MAX_JOB_ARGUMENTS_PER_QUERY; }
@Test public void testDeleteJobsByIdsWithExtraneous() { List<String> keys = Arrays.asList("job_key-1", "job_key-2", "job_key-3"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<AsyncJobStatus> expected = new Random().ints(Math.min(3, created.size()), 0, created.size()) .mapToObj(i -> created.get(i)) .collect(Collectors.toList()); List<String> expectedJobIds = expected.stream() .map(job -> job.getId()) .collect(Collectors.toList()); List<String> extraneousJobIds = Arrays.asList("extra_id-1", "extra_id-2", "extra_id-3"); List<String> jobIds = new LinkedList<>(); jobIds.addAll(expectedJobIds); jobIds.addAll(extraneousJobIds); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobIds(jobIds); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(expected.size(), deleted); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsBySingleKey() { List<String> keys = Arrays.asList("job_key-1", "job_key-2", "job_key-3"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> jobKeys = Arrays.asList("job_key-1"); List<AsyncJobStatus> expected = created.stream() .filter(job -> jobKeys.contains(job.getJobKey())) .collect(Collectors.toList()); assertTrue(expected.size() > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobKeys(jobKeys); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(deleted, expected.size()); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsByMultipleKeys() { List<String> keys = Arrays.asList("job_key-1", "job_key-2", "job_key-3"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> jobKeys = Arrays.asList("job_key-1", "job_key-3"); List<AsyncJobStatus> expected = created.stream() .filter(job -> jobKeys.contains(job.getJobKey())) .collect(Collectors.toList()); assertTrue(expected.size() > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobKeys(jobKeys); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(deleted, expected.size()); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsByMultipleKeysWithExtraneousKeys() { List<String> keys = Arrays.asList("job_key-1", "job_key-2", "job_key-3"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> expectedKeys = Arrays.asList("job_key-1", "job_key-3"); List<String> extraneousKeys = Arrays.asList("job_key-4", "job_key-6"); List<AsyncJobStatus> expected = created.stream() .filter(job -> expectedKeys.contains(job.getJobKey())) .collect(Collectors.toList()); assertTrue(expected.size() > 0); List<String> jobKeys = new LinkedList<>(); jobKeys.addAll(expectedKeys); jobKeys.addAll(extraneousKeys); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobKeys(jobKeys); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(deleted, expected.size()); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsBySingleJobState() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<JobState> jobStates = Arrays.asList(JobState.RUNNING); List<AsyncJobStatus> expected = created.stream() .filter(job -> jobStates.contains(job.getState())) .collect(Collectors.toList()); assertTrue(expected.size() > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobStates(jobStates); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(deleted, expected.size()); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsByMultipleJobStates() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<JobState> jobStates = Arrays.asList(JobState.QUEUED, JobState.FINISHED); List<AsyncJobStatus> expected = created.stream() .filter(job -> jobStates.contains(job.getState())) .collect(Collectors.toList()); assertTrue(expected.size() > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobStates(jobStates); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(deleted, expected.size()); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsByMultipleJobStatesWithExtraneousStates() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.QUEUED, JobState.RUNNING, JobState.FINISHED); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<JobState> expectedStates = Arrays.asList(JobState.QUEUED, JobState.FINISHED); List<JobState> extraneousStates = Arrays.asList(JobState.FAILED, JobState.ABORTED); List<AsyncJobStatus> expected = created.stream() .filter(job -> expectedStates.contains(job.getState())) .collect(Collectors.toList()); assertTrue(expected.size() > 0); List<JobState> jobStates = new LinkedList<>(); jobStates.addAll(expectedStates); jobStates.addAll(extraneousStates); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobStates(jobStates); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(deleted, expected.size()); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsBySingleOwnerId() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, owner1, owner2, owner3); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> ownerIds = Arrays.asList(owner1.getId()); List<AsyncJobStatus> expected = created.stream() .filter(j -> j.getContextOwner() != null && ownerIds.contains(j.getContextOwner().getId())) .collect(Collectors.toList()); assertTrue(expected.size() > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setOwnerIds(ownerIds); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(deleted, expected.size()); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsByMultipleOwnerIds() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, owner1, owner2, owner3); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> ownerIds = Arrays.asList(owner1.getId(), owner3.getId()); List<AsyncJobStatus> expected = created.stream() .filter(j -> j.getContextOwner() != null && ownerIds.contains(j.getContextOwner().getId())) .collect(Collectors.toList()); assertTrue(expected.size() > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setOwnerIds(ownerIds); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(deleted, expected.size()); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsByMultipleOwnerIdsWithExtraneousKeys() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, owner1, owner2, owner3); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> expectedKeys = Arrays.asList(owner1.getId(), owner3.getId()); List<String> extraneousKeys = Arrays.asList("extra_key-1", "extra_key-2"); List<AsyncJobStatus> expected = created.stream() .filter(j -> j.getContextOwner() != null && expectedKeys.contains(j.getContextOwner().getId())) .collect(Collectors.toList()); assertTrue(expected.size() > 0); List<String> ownerIds = new LinkedList<>(); ownerIds.addAll(expectedKeys); ownerIds.addAll(extraneousKeys); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setOwnerIds(ownerIds); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(deleted, expected.size()); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsByNullOwnerId() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, owner1, owner2, owner3); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> ownerIds = new LinkedList<>(); ownerIds.add(null); List<AsyncJobStatus> expected = created.stream() .filter(job -> job.getContextOwner() == null) .collect(Collectors.toList()); assertTrue(expected.size() > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setOwnerIds(ownerIds); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(deleted, expected.size()); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsByMixedOwnerIds() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, owner1, owner2, owner3); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> ownerIds = new LinkedList<>(); ownerIds.add(null); ownerIds.add(owner2.getId()); List<AsyncJobStatus> expected = created.stream() .filter(j -> j.getContextOwner() == null || ownerIds.contains(j.getContextOwner().getId())) .collect(Collectors.toList()); assertTrue(expected.size() > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setOwnerIds(ownerIds); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(deleted, expected.size()); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsByLastUpdateTimeBeforeDate() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); Date now = new Date(); List<AsyncJobStatus> expected = created.stream() .filter(job -> now.compareTo(job.getUpdated()) >= 0) .collect(Collectors.toList()); assertTrue(expected.size() > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setEndDate(now); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(deleted, expected.size()); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsByLastUpdateTimeAfterDate() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); Date now = new Date(); List<AsyncJobStatus> expected = created.stream() .filter(job -> now.compareTo(job.getUpdated()) <= 0) .collect(Collectors.toList()); assertTrue(expected.size() > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setStartDate(now); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(deleted, expected.size()); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsByLastUpdateTimeInRange() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); Date start = Util.addDaysToDt(-1); Date end = Util.addDaysToDt(1); List<AsyncJobStatus> expected = created.stream() .filter(job -> start.compareTo(job.getUpdated()) <= 0 && end.compareTo(job.getUpdated()) >= 0) .collect(Collectors.toList()); assertTrue(expected.size() > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setStartDate(start) .setEndDate(end); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(deleted, expected.size()); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsByMultipleParameters() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); List<String> keys = Arrays.asList("job_key-1", "job_key-2", "job_key-3"); List<Owner> owners = Arrays.asList(null, owner1, owner2); List<JobState> states = Arrays.asList(JobState.QUEUED, JobState.RUNNING, JobState.FAILED, JobState.FINISHED); List<String> principals = Arrays.asList("principal-1", "principal-2", "principal-3"); List<String> origins = Arrays.asList("origin-1", "origin-2", "origin-3"); List<String> executors = Arrays.asList("executor-1", "executor-2", "executor-3"); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, principals, origins, executors); List<String> jobKeys = Arrays.asList("job_key-1"); List<JobState> jobStates = Arrays.asList(JobState.QUEUED, JobState.RUNNING); List<String> ownerIds = Arrays.asList(owner2.getId()); List<String> jobPrincipals = Arrays.asList("principal-2"); List<String> jobOrigins = Arrays.asList("origin-3"); List<String> jobExecutors = Arrays.asList("executor-1"); Date start = Util.addDaysToDt(-1); Date end = Util.addDaysToDt(1); List<AsyncJobStatus> expected = created.stream() .filter(job -> jobKeys.contains(job.getJobKey())) .filter(job -> jobStates.contains(job.getState())) .filter(job -> job.getContextOwner() != null && ownerIds.contains(job.getContextOwner().getId())) .filter(job -> start.compareTo(job.getUpdated()) <= 0 && end.compareTo(job.getUpdated()) >= 0) .filter(job -> jobPrincipals.contains(job.getPrincipalName())) .filter(job -> jobOrigins.contains(job.getOrigin())) .filter(job -> jobExecutors.contains(job.getExecutor())) .collect(Collectors.toList()); assertTrue(expected.size() > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobKeys(jobKeys) .setJobStates(jobStates) .setOwnerIds(ownerIds) .setPrincipalNames(jobPrincipals) .setOrigins(jobOrigins) .setExecutors(jobExecutors) .setStartDate(start) .setEndDate(end); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(deleted, expected.size()); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size() - expected.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); assertFalse(expected.contains(job)); } }
@Test public void testDeleteJobsWithoutParameters() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder(); int deleted = this.asyncJobCurator.deleteJobs(queryBuilder); assertEquals(0, deleted); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); } }
@Test public void testDeleteJobsWithNullBuilder() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); int deleted = this.asyncJobCurator.deleteJobs(null); assertEquals(0, deleted); List<AsyncJobStatus> remaining = this.asyncJobCurator.listAll().list(); assertEquals(created.size(), remaining.size()); for (AsyncJobStatus job : remaining) { assertTrue(created.contains(job)); } }