/** * String representation of the SchedulerJobInstanceHandle. * * @return the handleID as a string */ @Override public String toString() { return getHandleIdString(); }
public static SchedulerJobInstanceHandle generateSchedulerJobInstanceHandle() { return new SchedulerJobInstanceHandle(UUID.randomUUID()); } public static <T extends Enum<T>> T checkAndGetOperation(final String operation, Class<T> enumType,
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; }
private LensConf getLensConf(XJob job, SchedulerJobInstanceHandle instanceHandle, DateTime scheduledTime) { List<MapType> jobConf = job.getExecution().getQuery().getConf(); LensConf queryConf = new LensConf(); for (MapType element : jobConf) { queryConf.addProperty(element.getKey(), element.getValue()); } queryConf.addProperty(JOB_INSTANCE_ID_KEY, instanceHandle.getHandleId()); // Current time is used for resolving date. queryConf.addProperty(LensConfConstants.QUERY_CURRENT_TIME_IN_MILLIS, scheduledTime.getMillis()); return queryConf; }
private SchedulerJobInstanceInfo parseSchedulerInstance(Object[] instanceInfo) throws SQLException { SchedulerJobInstanceHandle id = SchedulerJobInstanceHandle.fromString((String) instanceInfo[0]); SchedulerJobHandle jobId = SchedulerJobHandle.fromString((String) instanceInfo[1]); long createdOn = (Long) instanceInfo[2]; // Get the Runs sorted by run id to make sure the last on the list is the latest run. String fetchSQL = "SELECT * FROM " + JOB_INSTANCE_RUN_TABLE + " WHERE " + COLUMN_ID + "=? ORDER BY " + COLUMN_RUN_ID; List<Object[]> instanceRuns = runner.query(fetchSQL, multipleRowsHandler, (String) instanceInfo[0]); List<SchedulerJobInstanceRun> runList = processInstanceRun(instanceRuns); return new SchedulerJobInstanceInfo(id, jobId, createdOn, runList); }
/** * String representation of the SchedulerJobInstanceHandle. * * @return the handleID as a string */ @Override public String toString() { return getHandleIdString(); }
/** * From string. * * @param handle the handle * @return the <code>SchedulerJobInstance</code>'s handle */ public static SchedulerJobInstanceHandle fromString(@NonNull String handle) { return new SchedulerJobInstanceHandle(UUID.fromString(handle)); }
.getSchedulerJobInstanceInfo(SchedulerJobInstanceHandle.fromString(instanceHandle)); List<SchedulerJobInstanceRun> runList = info.getInstanceRunList(); if (runList.size() == 0) {
/** * Gets the SchedulerJobInstanceInfo corresponding instance handle id. * * @param id : Job instance id * @return ShedulerJobInstanceInfo */ public SchedulerJobInstanceInfo getSchedulerJobInstanceInfo(SchedulerJobInstanceHandle id) { try { return store.getJobInstanceInfo(id.getHandleIdString()); } catch (SQLException e) { log.error("Error while getting the job instance info for {}", id.getHandleIdString(), e); return null; } }
/** * From string. * * @param handle the handle * @return the <code>SchedulerJobInstance</code>'s handle */ public static SchedulerJobInstanceHandle fromString(@NonNull String handle) { return new SchedulerJobInstanceHandle(UUID.fromString(handle)); }
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 testStoreInstance() throws Exception { long currentTime = System.currentTimeMillis(); SchedulerJobInstanceHandle instanceHandle = new SchedulerJobInstanceHandle(UUID.randomUUID()); SchedulerJobInstanceInfo firstInstance = new SchedulerJobInstanceInfo(instanceHandle, jobHandle, currentTime, new ArrayList<SchedulerJobInstanceRun>()); instanceHandle = new SchedulerJobInstanceHandle(UUID.randomUUID()); SchedulerJobInstanceInfo secondInstance = new SchedulerJobInstanceInfo(instanceHandle, jobHandle, currentTime, new ArrayList<SchedulerJobInstanceRun>());
/** * 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; } }
/** * Inserts the job instance info object into job instance table * * @param instanceInfo * @return number of rows inserted. * @throws SQLException */ public int insertIntoJobInstanceTable(SchedulerJobInstanceInfo instanceInfo) throws SQLException { String insertSQL = "INSERT INTO " + JOB_INSTANCE_TABLE + " VALUES(?,?,?)"; return runner .update(insertSQL, instanceInfo.getId().getHandleIdString(), instanceInfo.getJobId().getHandleIdString(), instanceInfo.getScheduleTime()); }
@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); }
public static QueryEnded mockQueryEnded(SchedulerJobInstanceHandle instanceHandle, QueryStatus.Status status) { QueryContext mockContext = PowerMockito.mock(QueryContext.class); PowerMockito.when(mockContext.getResultSetPath()).thenReturn("/tmp/query1/result"); Configuration conf = new Configuration(); // set the instance handle conf.set("job_instance_key", instanceHandle.getHandleIdString()); PowerMockito.when(mockContext.getConf()).thenReturn(conf); // Get the queryHandle. PowerMockito.when(mockContext.getQueryHandle()).thenReturn(new QueryHandle(UUID.randomUUID())); QueryEnded queryEnded = PowerMockito.mock(QueryEnded.class); PowerMockito.when(queryEnded.getQueryContext()).thenReturn(mockContext); PowerMockito.when(queryEnded.getCurrentValue()).thenReturn(status); return queryEnded; } }
if (runList.size() == 0) { throw new LensException(LensSchedulerErrorCode.JOB_INSTANCE_IS_NOT_YET_RUN.getLensErrorInfo(), null, instanceHandle.getHandleIdString(), instanceInfo.getJobId().getHandleIdString()); } 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());
log.error("Not able to recover instance {} ", run.getHandle().getHandleIdString(), e); } finally { try {
/** * {@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()); } }
/** * 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()); }