public int insertIntoJobInstanceRunTable(SchedulerJobInstanceRun instanceRun) throws SQLException { String insetSQL = "INSERT INTO " + JOB_INSTANCE_RUN_TABLE + " VALUES(?,?,?,?,?,?,?,?)"; return runner.update(insetSQL, instanceRun.getHandle().getHandleIdString(), instanceRun.getRunId(), instanceRun.getSessionHandle() == null ? "" : instanceRun.getSessionHandle().toString(), instanceRun.getStartTime(), instanceRun.getEndTime(), instanceRun.getResultPath(), instanceRun.getQueryHandle() == null ? "" : instanceRun.getQueryHandle().getHandleIdString(), instanceRun.getInstanceState().name()); }
private List<SchedulerJobInstanceRun> processInstanceRun(List<Object[]> instanceRuns) throws SQLException { List<SchedulerJobInstanceRun> runList = new ArrayList<>(); for (Object[] run : instanceRuns) { // run[0] will contain the instanceID SchedulerJobInstanceHandle id = SchedulerJobInstanceHandle.fromString((String) run[0]); int runId = (Integer) run[1]; LensSessionHandle sessionHandle = LensSessionHandle.valueOf((String) run[2]); long starttime = (Long) run[3]; long endtime = (Long) run[4]; String resultPath = (String) run[5]; String queryHandleString = (String) run[6]; QueryHandle queryHandle = null; if (!queryHandleString.isEmpty()) { queryHandle = QueryHandle.fromString((String) run[6]); } SchedulerJobInstanceState instanceStatus = SchedulerJobInstanceState.valueOf((String) run[7]); SchedulerJobInstanceRun instanceRun = new SchedulerJobInstanceRun(id, runId, sessionHandle, starttime, endtime, resultPath, queryHandle, instanceStatus); runList.add(instanceRun); } return runList; }
@Override public void run() { while (!Thread.currentThread().isInterrupted()) { // Get all the instance runs which are waiting. List<SchedulerJobInstanceRun> instanceRuns = schedulerDAO.getInstanceRuns(SchedulerJobInstanceState.WAITING); for (SchedulerJobInstanceRun run : instanceRuns) { SchedulerJobInstanceInfo instanceInfo = schedulerDAO.getSchedulerJobInstanceInfo(run.getHandle()); try { run.setInstanceState(run.getInstanceState().nextTransition(ON_PREPARE)); run.setEndTime(System.currentTimeMillis()); schedulerDAO.updateJobInstanceRun(run); eventService.notifyEvent( new SchedulerAlarmEvent(instanceInfo.getJobId(), new DateTime(instanceInfo.getScheduleTime()), SchedulerAlarmEvent.EventType.SCHEDULE, instanceInfo.getId())); } catch (LensException e) { log.error("Not able to notify schedule event for job {} and instanceId {}", instanceInfo.getJobId(), instanceInfo.getId()); } catch (InvalidStateTransitionException e) { log.error("Wrong state transition", e); } } } try { Thread.sleep(delay); } catch (InterruptedException e) { log.warn("Thread WaitingInstanceHandler was inerrupted", e); } } }
private boolean updateInstanceRun(SchedulerJobInstanceRun latestRun, SchedulerJobInstanceState state) throws LensException { latestRun.setEndTime(System.currentTimeMillis()); latestRun.setInstanceState(state); return schedulerDAO.updateJobInstanceRun(latestRun) == 1; }
/** * Updates the status of a job instance. * * @param instanceRun * @return number of rows updated. * @throws SQLException */ public int updateJobInstanceRun(SchedulerJobInstanceRun instanceRun) throws SQLException { String updateSQL = "UPDATE " + JOB_INSTANCE_RUN_TABLE + " SET " + COLUMN_END_TIME + "=?, " + COLUMN_RESULT_PATH + "=?, " + COLUMN_QUERY_HANDLE + "=?, " + COLUMN_STATUS + "=?" + " WHERE " + COLUMN_ID + "=? AND " + COLUMN_RUN_ID + "=?"; return runner.update(updateSQL, instanceRun.getEndTime(), instanceRun.getResultPath(), instanceRun.getQueryHandle() == null ? "" : instanceRun.getQueryHandle().getHandleIdString(), instanceRun.getInstanceState().name(), instanceRun.getHandle().getHandleIdString(), instanceRun.getRunId()); }
? null : instance.getInstanceRunList().get(instance.getInstanceRunList().size() - 1); if (run == null || run.getInstanceState() != SchedulerJobInstanceState.LAUNCHING) { run = new SchedulerJobInstanceRun(instanceHandle, instance.getInstanceRunList().size() + 1, null, currentTime, currentTime, "N/A", null, SchedulerJobInstanceState.LAUNCHING); instance.getInstanceRunList().add(run); run.setSessionHandle(sessionHandle); if (e.getErrorCode() == LensCubeErrorCode.NO_CANDIDATE_FACT_AVAILABLE.getLensErrorInfo().getErrorCode()) { run.setInstanceState(run.getInstanceState().nextTransition(SchedulerJobInstanceEvent.ON_CONDITIONS_NOT_MET)); run.setEndTime(System.currentTimeMillis()); schedulerDAO.updateJobInstanceRun(run); return; run.getRunId(), handle); run.setQueryHandle(handle); run.setInstanceState(run.getInstanceState().nextTransition(SchedulerJobInstanceEvent.ON_RUN)); run.setEndTime(System.currentTimeMillis()); .getMillis(), e); try { run.setInstanceState(run.getInstanceState().nextTransition(SchedulerJobInstanceEvent.ON_FAILURE)); run.setEndTime(System.currentTimeMillis()); schedulerDAO.updateJobInstanceRun(run); } catch (InvalidStateTransitionException e1) {
SchedulerJobInstanceState state = latestRun.getInstanceState(); try { switch (event.getCurrentValue()) { break; latestRun.setEndTime(System.currentTimeMillis()); latestRun.setInstanceState(state); latestRun.setResultPath(queryContext.getResultSetPath()); if (schedulerDAO.updateJobInstanceRun(latestRun) == 1) { log.info("Updated instance run {} for instance {} for job {} to {}", latestRun.getRunId(), info.getId(), info.getJobId(), state); } else { log.error("Failed to update instance run {} for instance {} for job {} to {}", latestRun.getRunId(), info.getId(), info.getJobId(), state);
SchedulerJobInstanceState state = latestRun.getInstanceState(); try { state = state.nextTransition(ON_KILL); } catch (InvalidStateTransitionException e) { throw new LensException(LensSchedulerErrorCode.INVALID_EVENT_FOR_JOB_INSTANCE.getLensErrorInfo(), e, ON_KILL.name(), latestRun.getInstanceState().name(), instanceInfo.getId().getHandleIdString(), instanceInfo.getJobId().getHandleIdString()); QueryHandle handle = latestRun.getQueryHandle(); if (handle == null || handle.getHandleIdString().isEmpty()) { log.info("Killing instance {} for job {} ", instanceInfo.getId(), instanceInfo.getJobId());
SchedulerJobInstanceRun run = new SchedulerJobInstanceRun(instanceHandle, instance.getInstanceRunList().size() + 1, null, currentTime, 0, "N/A", null, SchedulerJobInstanceState.WAITING); instance.getInstanceRunList().add(run); Assert.assertEquals(previousInstanceInfo.getInstanceRunList().get(0).getResultPath(), "/tmp/path"); Assert.assertEquals(previousInstanceInfo.getInstanceRunList().get(0).getInstanceState(), SchedulerJobInstanceState.SUCCEEDED); scheduler.expireJob(sessionHandle, jobHandle);
Assert.assertEquals(info.getInstanceRunList().get(0).getInstanceState(), SchedulerJobInstanceState.FAILED); Assert.assertEquals(info.getInstanceRunList().get(1).getResultPath(), "/tmp/query1/result"); Assert.assertEquals(info.getInstanceRunList().get(1).getInstanceState(), SchedulerJobInstanceState.SUCCEEDED); scheduler.expireJob(sessionHandle, jobHandle); Assert.assertEquals(scheduler.getSchedulerDAO().getJobState(jobHandle), SchedulerJobState.EXPIRED);
QueryHandle queryHandle = run.getQueryHandle(); LensQuery query = null; try { SchedulerJobInstanceState state = run.getInstanceState(); switch (status) { case NEW: state = SchedulerJobInstanceState.KILLED; run.setResultPath(query.getResultSetPath()); updateInstanceRun(run, state); return true;
LensSessionHandle sessionHandle = null; try { SchedulerJobInstanceInfo instanceInfo = schedulerDAO.getSchedulerJobInstanceInfo(run.getHandle()); if (instanceInfo == null) { log.error("Instance info was not found for {}", run.getHandle()); continue; switch (run.getInstanceState()) { case WAITING: case LAUNCHING: log.error("Not able to recover instance {} ", run.getHandle().getHandleIdString(), e); } finally { try {
public int storeJobInstanceRun(SchedulerJobInstanceRun instanceRun) { try { return store.insertIntoJobInstanceRunTable(instanceRun); } catch (SQLException e) { log.error("Error while storing job instance run for {} with run id {} ", instanceRun.getHandle().getHandleIdString(), instanceRun.getRunId(), e); return 0; } }
@Test(priority = 2) public void testKillRunningInstance() throws Exception { long currentTime = System.currentTimeMillis(); XJob job = getTestJob("0/5 * * * * ?", queryString, currentTime, currentTime + 180000); LensSessionHandle sessionHandle = scheduler.openSessionAsUser(user); SchedulerJobHandle jobHandle = scheduler.submitAndScheduleJob(sessionHandle, job); // Let it run Thread.sleep(6000); List<SchedulerJobInstanceInfo> instanceHandleList = scheduler.getSchedulerDAO().getJobInstances(jobHandle); Assert.assertTrue(scheduler.killInstance(sessionHandle, instanceHandleList.get(0).getId())); Thread.sleep(2000); SchedulerJobInstanceInfo info = scheduler.getSchedulerDAO() .getSchedulerJobInstanceInfo(instanceHandleList.get(0).getId()); Assert.assertEquals(info.getInstanceRunList().size(), 1); Assert.assertEquals(info.getInstanceRunList().get(0).getInstanceState(), SchedulerJobInstanceState.RUNNING); // Query End event eventService.notifyEvent(mockQueryEnded(instanceHandleList.get(0).getId(), QueryStatus.Status.CANCELED)); Thread.sleep(2000); info = scheduler.getSchedulerDAO().getSchedulerJobInstanceInfo(instanceHandleList.get(0).getId()); Assert.assertEquals(info.getInstanceRunList().get(0).getInstanceState(), SchedulerJobInstanceState.KILLED); scheduler.expireJob(sessionHandle, jobHandle); Assert.assertEquals(scheduler.getSchedulerDAO().getJobState(jobHandle), SchedulerJobState.EXPIRED); scheduler.closeSession(sessionHandle); }
@Test(priority = 3) public void testUpdateJobInstance() { SchedulerJobInstanceHandle handle = instances.keySet().iterator().next(); SchedulerJobInstanceInfo info = instances.get(handle); SchedulerJobInstanceRun run = info.getInstanceRunList().get(0); run.setInstanceState(SchedulerJobInstanceState.LAUNCHING); schedulerDAO.updateJobInstanceRun(run); // Get the instance Assert.assertEquals(schedulerDAO.getSchedulerJobInstanceInfo(handle), info); }
@Test public void updateInstance() throws Exception { String startDate = Util.modifyDate(Util.getCurrentDate(format), format, Calendar.SECOND, 10); String endDate = Util.modifyDate(startDate, format, Calendar.MINUTE, 3); XJob xJob = scheduleHelper.getXJob("job-update-action", QueryInventory.JDBC_DIM_QUERY, null, startDate, endDate, "0/20 * * * * ?"); String jobHandle = scheduleHelper.submitNScheduleJob(xJob, sessionHandleString); Thread.sleep(20000); List<SchedulerJobInstanceInfo> instanceList = scheduleHelper.getAllInstancesOfJob(jobHandle, "10", sessionHandleString); Thread.sleep(10000); LensAPIResult res = scheduleHelper.updateInstance(instanceList.get(0).getId().getHandleIdString(), "RERUN", sessionHandleString); Assert.assertTrue(res.getData().equals(true)); SchedulerJobInstanceInfo instanceInfo = scheduleHelper.getInstanceDetails(instanceList.get(0).getId() .getHandleIdString(), sessionHandleString).getData(); List<SchedulerJobInstanceRun> runList = instanceInfo.getInstanceRunList(); Assert.assertEquals(runList.size(), 2); Assert.assertEquals(runList.get(1).getRunId(), 2); }
@Test(enabled = false) public void testQueryNotFoundCaseOnRestart() throws Exception { String startDate = Util.modifyDate(Util.getCurrentDate(format), format, Calendar.SECOND, 2); String endDate = Util.modifyDate(startDate, format, Calendar.SECOND, 15); XJob xJob = scheduleHelper.getXJob("job-restart", QueryInventory.getSleepQuery("5"), null, startDate, endDate, "0/10 * * * * ?"); String jobHandle = scheduleHelper.submitNScheduleJob(xJob, sessionHandleString); Thread.sleep(10000); List<SchedulerJobInstanceInfo> instanceList = scheduleHelper.getAllInstancesOfJob(jobHandle, "10", sessionHandleString); lens.stop(); Util.runRemoteCommand("hadoop dfs -rmr /tmp/lensserver/query.*"); lens.start(); Thread.sleep(15000); // wait till instance gets killed and new instance is spawned String firstInstanceHandle = instanceList.get(0).getId().getHandleIdString(); SchedulerJobInstanceInfo instanceInfo = scheduleHelper.getInstanceDetails(firstInstanceHandle, sessionHandleString) .getData(); Assert.assertEquals(instanceInfo.getInstanceRunList().get(0).getInstanceState(), SchedulerJobInstanceState.KILLED); qHelper.waitForCompletion(instanceInfo.getInstanceRunList().get(1).getQueryHandle()); Thread.sleep(3000); instanceInfo = scheduleHelper.getInstanceDetails(firstInstanceHandle, sessionHandleString).getData(); Assert.assertEquals(instanceInfo.getInstanceRunList().get(1).getInstanceState(), SchedulerJobInstanceState.SUCCEEDED); }
@Test(priority = 1) public void testScheduler() throws Exception { LensServices.get().init(LensServerConf.getHiveConf()); LensServices.get().start(); scheduler = LensServices.get().getService(SchedulerService.NAME); eventService = LensServices.get().getService(EventServiceImpl.NAME); setupQueryService(scheduler); LensSessionHandle sessionHandle = scheduler.openSessionAsUser(user); long currentTime = System.currentTimeMillis(); XJob job = getTestJob("0/5 * * * * ?", queryString, currentTime, currentTime + 15000); SchedulerJobHandle jobHandle = scheduler.submitAndScheduleJob(sessionHandle, job); Assert.assertNotNull(jobHandle); Assert.assertEquals(scheduler.getSchedulerDAO().getJobState(jobHandle), SchedulerJobState.SCHEDULED); // Wait for job to finish Thread.sleep(30000); List<SchedulerJobInstanceInfo> instanceHandleList = scheduler.getSchedulerDAO().getJobInstances(jobHandle); Assert.assertEquals(instanceHandleList.size() >= 3, true); Assert.assertEquals(scheduler.getSchedulerDAO().getJobState(jobHandle), SchedulerJobState.EXPIRED); // SuccessFul query eventService.notifyEvent(mockQueryEnded(instanceHandleList.get(0).getId(), QueryStatus.Status.SUCCESSFUL)); // Wait, for event to get processed Thread.sleep(2000); // Check the instance value SchedulerJobInstanceInfo info = scheduler.getSchedulerDAO() .getSchedulerJobInstanceInfo(instanceHandleList.get(0).getId()); Assert.assertEquals(info.getInstanceRunList().size(), 1); Assert.assertEquals(info.getInstanceRunList().get(0).getResultPath(), "/tmp/query1/result"); Assert.assertEquals(info.getInstanceRunList().get(0).getInstanceState(), SchedulerJobInstanceState.SUCCEEDED); scheduler.closeSession(sessionHandle); }
/** * Updates the instance status * * @param instanceRun : instance Run object * @return number of rows updated. */ public int updateJobInstanceRun(SchedulerJobInstanceRun instanceRun) { try { return store.updateJobInstanceRun(instanceRun); } catch (SQLException e) { log.error("Error while updating the job instance status for {} and run id {}", instanceRun.getHandle().getHandleIdString(), instanceRun.getRunId(), e); return 0; } }
/** * {@inheritDoc} */ @Override public void rerunInstance(LensSessionHandle sessionHandle, SchedulerJobInstanceHandle instanceHandle) throws LensException { SchedulerJobInstanceInfo instanceInfo = schedulerDAO.getSchedulerJobInstanceInfo(instanceHandle); doesSessionBelongToUser(sessionHandle, schedulerDAO.getUser(instanceInfo.getJobId())); SchedulerJobState currentState = schedulerDAO.getJobState(instanceInfo.getJobId()); if (currentState != SchedulerJobState.SCHEDULED) { throw new LensException(LensSchedulerErrorCode.JOB_IS_NOT_SCHEDULED.getLensErrorInfo(), null, instanceInfo.getJobId().getHandleIdString(), currentState); } // Get the latest run. List<SchedulerJobInstanceRun> runList = instanceInfo.getInstanceRunList(); if (runList.size() == 0) { throw new LensException(LensSchedulerErrorCode.JOB_INSTANCE_IS_NOT_YET_RUN.getLensErrorInfo(), null, instanceHandle.getHandleIdString(), instanceInfo.getJobId().getHandleIdString()); } SchedulerJobInstanceRun latestRun = runList.get(runList.size() - 1); try { latestRun.getInstanceState().nextTransition(ON_RERUN); notifyRerun(instanceInfo); log.info("Rerunning the instance with {} for job {} ", instanceHandle, instanceInfo.getJobId()); } catch (InvalidStateTransitionException e) { throw new LensException(LensSchedulerErrorCode.INVALID_EVENT_FOR_JOB_INSTANCE.getLensErrorInfo(), e, ON_RERUN.name(), latestRun.getInstanceState().name(), instanceInfo.getId().getHandleIdString(), instanceInfo.getJobId().getHandleIdString()); } }