/** * @return true, if for all engines there were less jobs acquired than requested */ public boolean areAllEnginesIdle() { for (AcquiredJobs acquiredJobs : acquiredJobsByEngine.values()) { int jobsAcquired = acquiredJobs.getJobIdBatches().size() + acquiredJobs.getNumberOfJobsFailedToLock(); if (jobsAcquired >= acquiredJobs.getNumberOfJobsAttemptedToAcquire()) { return false; } } return true; }
/** * @return true, if for all engines there were less jobs acquired than requested */ public boolean areAllEnginesIdle() { for (AcquiredJobs acquiredJobs : acquiredJobsByEngine.values()) { int jobsAcquired = acquiredJobs.getJobIdBatches().size() + acquiredJobs.getNumberOfJobsFailedToLock(); if (jobsAcquired >= acquiredJobs.getNumberOfJobsAttemptedToAcquire()) { return false; } } return true; }
public void acquiredJobs(String processEngine, AcquiredJobs acquiredJobs) { logDebug( "022", "Acquired {} jobs for process engine '{}': {}", acquiredJobs.size(), processEngine, acquiredJobs.getJobIdBatches()); }
public void acquiredJobs(String processEngine, AcquiredJobs acquiredJobs) { logDebug( "022", "Acquired {} jobs for process engine '{}': {}", acquiredJobs.size(), processEngine, acquiredJobs.getJobIdBatches()); }
/** * @return true, if all acquired jobs (spanning all engines) were rejected for execution */ protected boolean allSubmittedJobsRejected(JobAcquisitionContext context) { for (Map.Entry<String, AcquiredJobs> acquiredJobsForEngine : context.getAcquiredJobsByEngine().entrySet()) { String engineName = acquiredJobsForEngine.getKey(); List<List<String>> acquiredJobBatches = acquiredJobsForEngine.getValue().getJobIdBatches(); List<List<String>> resubmittedJobBatches = context.getAdditionalJobsByEngine().get(engineName); List<List<String>> rejectedJobBatches = context.getRejectedJobsByEngine().get(engineName); int numJobsSubmittedForExecution = acquiredJobBatches.size(); if (resubmittedJobBatches != null) { numJobsSubmittedForExecution += resubmittedJobBatches.size(); } int numJobsRejected = 0; if (rejectedJobBatches != null) { numJobsRejected += rejectedJobBatches.size(); } // if not all jobs scheduled for execution have been rejected if (numJobsRejected == 0 || numJobsSubmittedForExecution > numJobsRejected) { return false; } } return true; }
/** * @return true, if all acquired jobs (spanning all engines) were rejected for execution */ protected boolean allSubmittedJobsRejected(JobAcquisitionContext context) { for (Map.Entry<String, AcquiredJobs> acquiredJobsForEngine : context.getAcquiredJobsByEngine().entrySet()) { String engineName = acquiredJobsForEngine.getKey(); List<List<String>> acquiredJobBatches = acquiredJobsForEngine.getValue().getJobIdBatches(); List<List<String>> resubmittedJobBatches = context.getAdditionalJobsByEngine().get(engineName); List<List<String>> rejectedJobBatches = context.getRejectedJobsByEngine().get(engineName); int numJobsSubmittedForExecution = acquiredJobBatches.size(); if (resubmittedJobBatches != null) { numJobsSubmittedForExecution += resubmittedJobBatches.size(); } int numJobsRejected = 0; if (rejectedJobBatches != null) { numJobsRejected += rejectedJobBatches.size(); } // if not all jobs scheduled for execution have been rejected if (numJobsRejected == 0 || numJobsSubmittedForExecution > numJobsRejected) { return false; } } return true; }
protected void checkThatAcquiredJobsInDifferentBatches() { AcquiredJobs acquiredJobs = acquireJobsCmd.execute(commandContext); List<List<String>> jobIdBatches = acquiredJobs.getJobIdBatches(); assertThat(jobIdBatches.size(), is(2)); assertThat(jobIdBatches.get(0).size(), is(1)); assertThat(jobIdBatches.get(1).size(), is(1)); }
public void testAcquiredJobs() { List<String> firstBatch = new ArrayList<String>(Arrays.asList("a", "b", "c")); List<String> secondBatch = new ArrayList<String>(Arrays.asList("d", "e", "f")); List<String> thirdBatch = new ArrayList<String>(Arrays.asList("g")); AcquiredJobs acquiredJobs = new AcquiredJobs(0); acquiredJobs.addJobIdBatch(firstBatch); acquiredJobs.addJobIdBatch(secondBatch); acquiredJobs.addJobIdBatch(thirdBatch); assertEquals(firstBatch, acquiredJobs.getJobIdBatches().get(0)); assertEquals(secondBatch, acquiredJobs.getJobIdBatches().get(1)); assertEquals(thirdBatch, acquiredJobs.getJobIdBatches().get(2)); acquiredJobs.removeJobId("a"); assertEquals(Arrays.asList("b", "c"), acquiredJobs.getJobIdBatches().get(0)); assertEquals(secondBatch, acquiredJobs.getJobIdBatches().get(1)); assertEquals(thirdBatch, acquiredJobs.getJobIdBatches().get(2)); assertEquals(3, acquiredJobs.getJobIdBatches().size()); acquiredJobs.removeJobId("g"); assertEquals(2, acquiredJobs.getJobIdBatches().size()); } }
@Test public void exclusiveJobsSameInstance() { // given: two exclusive jobs for the same process instance JobEntity job1 = createExclusiveJob(JOB_ID_1, PROCESS_INSTANCE_ID_1); JobEntity job2 = createExclusiveJob(JOB_ID_2, PROCESS_INSTANCE_ID_1); // when the job executor acquire new jobs when(jobManager.findNextJobsToExecute(any(Page.class))).thenReturn(Arrays.asList(job1, job2)); // then the job executor should acquire job1 and job 2 in one batch AcquiredJobs acquiredJobs = acquireJobsCmd.execute(commandContext); List<List<String>> jobIdBatches = acquiredJobs.getJobIdBatches(); assertThat(jobIdBatches.size(), is(1)); assertThat(jobIdBatches.get(0).size(), is(2)); assertThat(jobIdBatches.get(0), hasItems(JOB_ID_1, JOB_ID_2)); }
protected void executeJobs(JobAcquisitionContext context, ProcessEngineImpl currentProcessEngine, AcquiredJobs acquiredJobs) { // submit those jobs that were acquired in previous cycles but could not be scheduled for execution List<List<String>> additionalJobs = context.getAdditionalJobsByEngine().get(currentProcessEngine.getName()); if (additionalJobs != null) { for (List<String> jobBatch : additionalJobs) { LOG.executeJobs(currentProcessEngine.getName(), jobBatch); jobExecutor.executeJobs(jobBatch, currentProcessEngine); } } // submit those jobs that were acquired in the current cycle for (List<String> jobIds : acquiredJobs.getJobIdBatches()) { LOG.executeJobs(currentProcessEngine.getName(), jobIds); jobExecutor.executeJobs(jobIds, currentProcessEngine); } }
protected void executeJobs(JobAcquisitionContext context, ProcessEngineImpl currentProcessEngine, AcquiredJobs acquiredJobs) { // submit those jobs that were acquired in previous cycles but could not be scheduled for execution List<List<String>> additionalJobs = context.getAdditionalJobsByEngine().get(currentProcessEngine.getName()); if (additionalJobs != null) { for (List<String> jobBatch : additionalJobs) { LOG.executeJobs(currentProcessEngine.getName(), jobBatch); jobExecutor.executeJobs(jobBatch, currentProcessEngine); } } // submit those jobs that were acquired in the current cycle for (List<String> jobIds : acquiredJobs.getJobIdBatches()) { LOG.executeJobs(currentProcessEngine.getName(), jobIds); jobExecutor.executeJobs(jobIds, currentProcessEngine); } }
public void testJobCommandsWithTimer() { // clock gets automatically reset in LogTestCase.runTest ClockUtil.setCurrentTime(new Date(SOME_TIME)); CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutorTxRequired(); JobExecutor jobExecutor = processEngineConfiguration.getJobExecutor(); String jobId = commandExecutor.execute(new Command<String>() { public String execute(CommandContext commandContext) { TimerEntity timer = createTweetTimer("i'm coding a test", new Date(SOME_TIME + (10 * SECOND))); commandContext.getJobManager().schedule(timer); return timer.getId(); } }); AcquiredJobs acquiredJobs = commandExecutor.execute(new AcquireJobsCmd(jobExecutor)); List<List<String>> jobIdsList = acquiredJobs.getJobIdBatches(); assertEquals(0, jobIdsList.size()); List<String> expectedJobIds = new ArrayList<String>(); ClockUtil.setCurrentTime(new Date(SOME_TIME + (20 * SECOND))); acquiredJobs = commandExecutor.execute(new AcquireJobsCmd(jobExecutor, jobExecutor.getMaxJobsPerAcquisition())); jobIdsList = acquiredJobs.getJobIdBatches(); assertEquals(1, jobIdsList.size()); List<String> jobIds = jobIdsList.get(0); expectedJobIds.add(jobId); assertEquals(expectedJobIds, new ArrayList<String>(jobIds)); assertEquals(0, tweetHandler.getMessages().size()); ExecuteJobHelper.executeJob(jobId, commandExecutor); assertEquals("i'm coding a test", tweetHandler.getMessages().get(0)); assertEquals(1, tweetHandler.getMessages().size()); clearDatabase(); }
public void testJobCommandsWithMessage() { CommandExecutor commandExecutor = processEngineConfiguration.getCommandExecutorTxRequired(); JobExecutor jobExecutor = processEngineConfiguration.getJobExecutor(); String jobId = commandExecutor.execute(new Command<String>() { public String execute(CommandContext commandContext) { MessageEntity message = createTweetMessage("i'm coding a test"); commandContext.getJobManager().send(message); return message.getId(); } }); AcquiredJobs acquiredJobs = commandExecutor.execute(new AcquireJobsCmd(jobExecutor)); List<List<String>> jobIdsList = acquiredJobs.getJobIdBatches(); assertEquals(1, jobIdsList.size()); List<String> jobIds = jobIdsList.get(0); List<String> expectedJobIds = new ArrayList<String>(); expectedJobIds.add(jobId); assertEquals(expectedJobIds, new ArrayList<String>(jobIds)); assertEquals(0, tweetHandler.getMessages().size()); ExecuteJobHelper.executeJob(jobId, commandExecutor); assertEquals("i'm coding a test", tweetHandler.getMessages().get(0)); assertEquals(1, tweetHandler.getMessages().size()); clearDatabase(); }
@Test public void testWaitTimeOnFullRejection() { // given a job acquisition strategy and a job acquisition context // with acquired jobs all of which have been rejected for execution JobAcquisitionContext context = new JobAcquisitionContext(); AcquiredJobs acquiredJobs = buildAcquiredJobs(NUM_JOBS_TO_ACQUIRE, NUM_JOBS_TO_ACQUIRE, 0); context.submitAcquiredJobs(ENGINE_NAME, acquiredJobs); for (int i = 0; i < NUM_JOBS_TO_ACQUIRE; i++) { context.submitRejectedBatch(ENGINE_NAME, acquiredJobs.getJobIdBatches().get(i)); } // when reconfiguring the strategy strategy.reconfigure(context); // then there is a slight wait time to avoid constant spinning while // no execution resources are available Assert.assertEquals(BackoffJobAcquisitionStrategy.DEFAULT_EXECUTION_SATURATION_WAIT_TIME, strategy.getWaitTime()); }
@Test public void testAcquireLessJobsOnRejection() { // given a job acquisition strategy and a job acquisition context // with acquired jobs, some of which have been rejected for execution JobAcquisitionContext context = new JobAcquisitionContext(); AcquiredJobs acquiredJobs = buildAcquiredJobs(NUM_JOBS_TO_ACQUIRE, NUM_JOBS_TO_ACQUIRE, 0); context.submitAcquiredJobs(ENGINE_NAME, acquiredJobs); // when half of the jobs are rejected int numJobsRejected = 5; for (int i = 0; i < numJobsRejected; i++) { context.submitRejectedBatch(ENGINE_NAME, acquiredJobs.getJobIdBatches().get(i)); } // then the strategy only attempts to acquire the number of jobs that were successfully submitted strategy.reconfigure(context); Assert.assertEquals(NUM_JOBS_TO_ACQUIRE - numJobsRejected, strategy.getNumJobsToAcquire(ENGINE_NAME)); // without a timeout Assert.assertEquals(0, strategy.getWaitTime()); }
/** * @return true, if for all engines there were less jobs acquired than requested */ public boolean areAllEnginesIdle() { for (AcquiredJobs acquiredJobs : acquiredJobsByEngine.values()) { int jobsAcquired = acquiredJobs.getJobIdBatches().size() + acquiredJobs.getNumberOfJobsFailedToLock(); if (jobsAcquired >= acquiredJobs.getNumberOfJobsAttemptedToAcquire()) { return false; } } return true; }
public void acquiredJobs(String processEngine, AcquiredJobs acquiredJobs) { logDebug( "022", "Acquired {} jobs for process engine '{}': {}", acquiredJobs.size(), processEngine, acquiredJobs.getJobIdBatches()); }
protected void checkThatAcquiredJobsInDifferentBatches() { AcquiredJobs acquiredJobs = acquireJobsCmd.execute(commandContext); List<List<String>> jobIdBatches = acquiredJobs.getJobIdBatches(); assertThat(jobIdBatches.size(), is(2)); assertThat(jobIdBatches.get(0).size(), is(1)); assertThat(jobIdBatches.get(1).size(), is(1)); }
protected void executeJobs(JobAcquisitionContext context, ProcessEngineImpl currentProcessEngine, AcquiredJobs acquiredJobs) { // submit those jobs that were acquired in previous cycles but could not be scheduled for execution List<List<String>> additionalJobs = context.getAdditionalJobsByEngine().get(currentProcessEngine.getName()); if (additionalJobs != null) { for (List<String> jobBatch : additionalJobs) { LOG.executeJobs(currentProcessEngine.getName(), jobBatch); jobExecutor.executeJobs(jobBatch, currentProcessEngine); } } // submit those jobs that were acquired in the current cycle for (List<String> jobIds : acquiredJobs.getJobIdBatches()) { LOG.executeJobs(currentProcessEngine.getName(), jobIds); jobExecutor.executeJobs(jobIds, currentProcessEngine); } }
@Test public void exclusiveJobsSameInstance() { // given: two exclusive jobs for the same process instance JobEntity job1 = createExclusiveJob(JOB_ID_1, PROCESS_INSTANCE_ID_1); JobEntity job2 = createExclusiveJob(JOB_ID_2, PROCESS_INSTANCE_ID_1); // when the job executor acquire new jobs when(jobManager.findNextJobsToExecute(any(Page.class))).thenReturn(Arrays.asList(job1, job2)); // then the job executor should acquire job1 and job 2 in one batch AcquiredJobs acquiredJobs = acquireJobsCmd.execute(commandContext); List<List<String>> jobIdBatches = acquiredJobs.getJobIdBatches(); assertThat(jobIdBatches.size(), is(1)); assertThat(jobIdBatches.get(0).size(), is(2)); assertThat(jobIdBatches.get(0), hasItems(JOB_ID_1, JOB_ID_2)); }