Skip to content

Commit

Permalink
Distinguish CreateTime and StartTime in StepExecution
Browse files Browse the repository at this point in the history
- Add field createTime for StepExecution.
- Add column create_time for batch_step_execution and make start_time default null.
- Make getLastStepExecution order by createTime instead of startTime.
- Add migration scripts for 5.0

Resolves #4049
  • Loading branch information
lcmarvin authored and fmbenhassine committed Apr 27, 2022
1 parent a19eefa commit 464985b
Show file tree
Hide file tree
Showing 26 changed files with 96 additions and 41 deletions.
Expand Up @@ -61,7 +61,9 @@ public class StepExecution extends Entity {

private volatile long writeSkipCount = 0;

private volatile Date startTime = new Date(System.currentTimeMillis());
private volatile Date startTime = null;

private volatile Date createTime = new Date(System.currentTimeMillis());

private volatile Date endTime = null;

Expand Down Expand Up @@ -248,6 +250,24 @@ public void setRollbackCount(long rollbackCount) {
this.rollbackCount = rollbackCount;
}

/**
* Gets the time this execution was created
*
* @return the time when this execution was created.
*/
public Date getCreateTime() {
return createTime;
}

/**
* Sets the time this execution was created
*
* @param createTime creation time of this execution.
*/
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

/**
* Gets the time this execution started
*
Expand Down
Expand Up @@ -72,7 +72,7 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
private static final String SAVE_STEP_EXECUTION = "INSERT into %PREFIX%STEP_EXECUTION(STEP_EXECUTION_ID, VERSION, " +
"STEP_NAME, JOB_EXECUTION_ID, START_TIME, END_TIME, STATUS, COMMIT_COUNT, READ_COUNT, FILTER_COUNT, " +
"WRITE_COUNT, EXIT_CODE, EXIT_MESSAGE, READ_SKIP_COUNT, WRITE_SKIP_COUNT, PROCESS_SKIP_COUNT, " +
"ROLLBACK_COUNT, LAST_UPDATED) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
"ROLLBACK_COUNT, LAST_UPDATED, CREATE_TIME) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

private static final String UPDATE_STEP_EXECUTION = "UPDATE %PREFIX%STEP_EXECUTION set START_TIME = ?, END_TIME = ?, "
+ "STATUS = ?, COMMIT_COUNT = ?, READ_COUNT = ?, FILTER_COUNT = ?, WRITE_COUNT = ?, EXIT_CODE = ?, "
Expand All @@ -82,8 +82,8 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement

private static final String GET_RAW_STEP_EXECUTIONS = "SELECT STEP_EXECUTION_ID, STEP_NAME, START_TIME, END_TIME, " +
"STATUS, COMMIT_COUNT, READ_COUNT, FILTER_COUNT, WRITE_COUNT, EXIT_CODE, EXIT_MESSAGE, READ_SKIP_COUNT, " +
"WRITE_SKIP_COUNT, PROCESS_SKIP_COUNT, ROLLBACK_COUNT, LAST_UPDATED, VERSION from %PREFIX%STEP_EXECUTION " +
"where JOB_EXECUTION_ID = ?";
"WRITE_SKIP_COUNT, PROCESS_SKIP_COUNT, ROLLBACK_COUNT, LAST_UPDATED, VERSION, CREATE_TIME from " +
"%PREFIX%STEP_EXECUTION where JOB_EXECUTION_ID = ?";

private static final String GET_STEP_EXECUTIONS = GET_RAW_STEP_EXECUTIONS + " order by STEP_EXECUTION_ID";

Expand All @@ -92,14 +92,14 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
private static final String GET_LAST_STEP_EXECUTION = "SELECT " +
" SE.STEP_EXECUTION_ID, SE.STEP_NAME, SE.START_TIME, SE.END_TIME, SE.STATUS, SE.COMMIT_COUNT, " +
"SE.READ_COUNT, SE.FILTER_COUNT, SE.WRITE_COUNT, SE.EXIT_CODE, SE.EXIT_MESSAGE, SE.READ_SKIP_COUNT, " +
"SE.WRITE_SKIP_COUNT, SE.PROCESS_SKIP_COUNT, SE.ROLLBACK_COUNT, SE.LAST_UPDATED, SE.VERSION," +
"SE.WRITE_SKIP_COUNT, SE.PROCESS_SKIP_COUNT, SE.ROLLBACK_COUNT, SE.LAST_UPDATED, SE.VERSION, SE.CREATE_TIME," +
" JE.JOB_EXECUTION_ID, JE.START_TIME, JE.END_TIME, JE.STATUS, JE.EXIT_CODE, JE.EXIT_MESSAGE, " +
"JE.CREATE_TIME, JE.LAST_UPDATED, JE.VERSION" +
" from %PREFIX%JOB_EXECUTION JE join %PREFIX%STEP_EXECUTION SE" +
" on SE.JOB_EXECUTION_ID = JE.JOB_EXECUTION_ID " +
"where JE.JOB_INSTANCE_ID = ?" +
" and SE.STEP_NAME = ?" +
" order by SE.START_TIME desc, SE.STEP_EXECUTION_ID desc";
" order by SE.CREATE_TIME desc, SE.STEP_EXECUTION_ID desc";

private static final String CURRENT_VERSION_STEP_EXECUTION = "SELECT VERSION FROM %PREFIX%STEP_EXECUTION WHERE " +
"STEP_EXECUTION_ID=?";
Expand Down Expand Up @@ -221,26 +221,26 @@ private List<Object[]> buildStepExecutionParameters(StepExecution stepExecution)
stepExecution.getReadCount(), stepExecution.getFilterCount(), stepExecution.getWriteCount(),
stepExecution.getExitStatus().getExitCode(), exitDescription, stepExecution.getReadSkipCount(),
stepExecution.getWriteSkipCount(), stepExecution.getProcessSkipCount(),
stepExecution.getRollbackCount(), stepExecution.getLastUpdated() };
stepExecution.getRollbackCount(), stepExecution.getLastUpdated(), stepExecution.getCreateTime() };
Integer[] parameterTypes = new Integer[] { Types.BIGINT, Types.INTEGER, Types.VARCHAR, Types.BIGINT,
Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.BIGINT, Types.BIGINT, Types.BIGINT,
Types.BIGINT, Types.VARCHAR, Types.VARCHAR, Types.BIGINT, Types.BIGINT, Types.BIGINT,
Types.BIGINT, Types.TIMESTAMP };
Types.BIGINT, Types.TIMESTAMP, Types.TIMESTAMP };
parameters.add(0, Arrays.copyOf(parameterValues,parameterValues.length));
parameters.add(1, Arrays.copyOf(parameterTypes,parameterTypes.length));
return parameters;
}

/**
* Validate StepExecution. At a minimum, JobId, StartTime, and Status cannot
* Validate StepExecution. At a minimum, JobId, CreateTime, and Status cannot
* be null. EndTime can be null for an unfinished job.
*
* @throws IllegalArgumentException
*/
private void validateStepExecution(StepExecution stepExecution) {
Assert.notNull(stepExecution, "stepExecution is required");
Assert.notNull(stepExecution.getStepName(), "StepExecution step name cannot be null.");
Assert.notNull(stepExecution.getStartTime(), "StepExecution start time cannot be null.");
Assert.notNull(stepExecution.getCreateTime(), "StepExecution create time cannot be null.");
Assert.notNull(stepExecution.getStatus(), "StepExecution status cannot be null.");
}

Expand Down Expand Up @@ -327,15 +327,15 @@ public StepExecution getLastStepExecution(JobInstance jobInstance, String stepNa
List<StepExecution> executions = getJdbcTemplate().query(
getQuery(GET_LAST_STEP_EXECUTION),
(rs, rowNum) -> {
Long jobExecutionId = rs.getLong(18);
Long jobExecutionId = rs.getLong(19);
JobExecution jobExecution = new JobExecution(jobExecutionId);
jobExecution.setStartTime(rs.getTimestamp(19));
jobExecution.setEndTime(rs.getTimestamp(20));
jobExecution.setStatus(BatchStatus.valueOf(rs.getString(21)));
jobExecution.setExitStatus(new ExitStatus(rs.getString(22), rs.getString(23)));
jobExecution.setCreateTime(rs.getTimestamp(24));
jobExecution.setLastUpdated(rs.getTimestamp(25));
jobExecution.setVersion(rs.getInt(26));
jobExecution.setStartTime(rs.getTimestamp(20));
jobExecution.setEndTime(rs.getTimestamp(21));
jobExecution.setStatus(BatchStatus.valueOf(rs.getString(22)));
jobExecution.setExitStatus(new ExitStatus(rs.getString(23), rs.getString(24)));
jobExecution.setCreateTime(rs.getTimestamp(25));
jobExecution.setLastUpdated(rs.getTimestamp(26));
jobExecution.setVersion(rs.getInt(27));
return new StepExecutionRowMapper(jobExecution).mapRow(rs, rowNum);
},
jobInstance.getInstanceId(), stepName);
Expand Down Expand Up @@ -382,6 +382,7 @@ public StepExecution mapRow(ResultSet rs, int rowNum) throws SQLException {
stepExecution.setRollbackCount(rs.getInt(15));
stepExecution.setLastUpdated(rs.getTimestamp(16));
stepExecution.setVersion(rs.getInt(17));
stepExecution.setCreateTime(rs.getTimestamp(18));
return stepExecution;
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2020 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -68,7 +68,7 @@ public interface StepExecutionDao {

/**
* Retrieve the last {@link StepExecution} for a given {@link JobInstance}
* ordered by starting time and then id.
* ordered by creation time and then id.
*
* @param jobInstance the parent {@link JobInstance}
* @param stepName the name of the step
Expand Down
@@ -0,0 +1,2 @@
ALTER TABLE BATCH_STEP_EXECUTION ADD CREATE_TIME TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:00';
ALTER TABLE BATCH_STEP_EXECUTION ALTER COLUMN START_TIME DROP NOT NULL;
@@ -0,0 +1,2 @@
ALTER TABLE BATCH_STEP_EXECUTION ADD CREATE_TIME TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:00';
ALTER TABLE BATCH_STEP_EXECUTION ALTER COLUMN START_TIME DROP NOT NULL;
@@ -0,0 +1,2 @@
ALTER TABLE BATCH_STEP_EXECUTION ADD CREATE_TIME TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:00';
ALTER TABLE BATCH_STEP_EXECUTION ALTER COLUMN START_TIME DROP NOT NULL;
@@ -0,0 +1,2 @@
ALTER TABLE BATCH_STEP_EXECUTION ADD CREATE_TIME TIMESTAMP DEFAULT '1970-01-01 00:00:00' NOT NULL;
ALTER TABLE BATCH_STEP_EXECUTION ALTER COLUMN START_TIME DROP NOT NULL;
@@ -0,0 +1,2 @@
ALTER TABLE BATCH_STEP_EXECUTION ADD CREATE_TIME DATETIME(6) NOT NULL DEFAULT '1970-01-01 00:00:00';
ALTER TABLE BATCH_STEP_EXECUTION MODIFY START_TIME DATETIME(6) NULL;
@@ -1,3 +1,6 @@
ALTER SEQUENCE BATCH_STEP_EXECUTION_SEQ ORDER;
ALTER SEQUENCE BATCH_JOB_EXECUTION_SEQ ORDER;
ALTER SEQUENCE BATCH_JOB_SEQ ORDER;
ALTER SEQUENCE BATCH_JOB_SEQ ORDER;

ALTER TABLE BATCH_STEP_EXECUTION ADD CREATE_TIME TIMESTAMP DEFAULT TO_TIMESTAMP('1970-01-01 00:00:00', 'yyyy-MM-dd HH24:mi:ss') NOT NULL;
ALTER TABLE BATCH_STEP_EXECUTION MODIFY START_TIME TIMESTAMP NULL;
@@ -0,0 +1,2 @@
ALTER TABLE BATCH_STEP_EXECUTION ADD CREATE_TIME TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:00';
ALTER TABLE BATCH_STEP_EXECUTION ALTER COLUMN START_TIME DROP NOT NULL;
@@ -1,2 +1,5 @@
ALTER TABLE BATCH_STEP_EXECUTION_CONTEXT ALTER COLUMN SERIALIZED_CONTEXT VARCHAR(MAX) NULL;
ALTER TABLE BATCH_JOB_EXECUTION_CONTEXT ALTER COLUMN SERIALIZED_CONTEXT VARCHAR(MAX) NULL;
ALTER TABLE BATCH_JOB_EXECUTION_CONTEXT ALTER COLUMN SERIALIZED_CONTEXT VARCHAR(MAX) NULL;

ALTER TABLE BATCH_STEP_EXECUTION ADD CREATE_TIME DATETIME NOT NULL DEFAULT '1970-01-01 00:00:00';
ALTER TABLE BATCH_STEP_EXECUTION ALTER COLUMN START_TIME DATETIME NULL;
@@ -0,0 +1,3 @@
ALTER TABLE BATCH_STEP_EXECUTION ADD CREATE_TIME DATETIME DEFAULT '1970-01-01 00:00:00' NOT NULL;
ALTER TABLE BATCH_STEP_EXECUTION MODIFY START_TIME DATETIME NULL;
??
Expand Up @@ -41,7 +41,8 @@ CREATE TABLE BATCH_STEP_EXECUTION (
VERSION BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
JOB_EXECUTION_ID BIGINT NOT NULL,
START_TIME TIMESTAMP NOT NULL ,
CREATE_TIME TIMESTAMP NOT NULL,
START_TIME TIMESTAMP DEFAULT NULL ,
END_TIME TIMESTAMP DEFAULT NULL ,
STATUS VARCHAR(10) ,
COMMIT_COUNT BIGINT ,
Expand Down
Expand Up @@ -41,7 +41,8 @@ CREATE TABLE BATCH_STEP_EXECUTION (
VERSION BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
JOB_EXECUTION_ID BIGINT NOT NULL,
START_TIME TIMESTAMP NOT NULL ,
CREATE_TIME TIMESTAMP NOT NULL,
START_TIME TIMESTAMP DEFAULT NULL ,
END_TIME TIMESTAMP DEFAULT NULL ,
STATUS VARCHAR(10) ,
COMMIT_COUNT BIGINT ,
Expand Down
Expand Up @@ -41,7 +41,8 @@ CREATE TABLE BATCH_STEP_EXECUTION (
VERSION BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
JOB_EXECUTION_ID BIGINT NOT NULL,
START_TIME TIMESTAMP NOT NULL ,
CREATE_TIME TIMESTAMP NOT NULL,
START_TIME TIMESTAMP DEFAULT NULL ,
END_TIME TIMESTAMP DEFAULT NULL ,
STATUS VARCHAR(10) ,
COMMIT_COUNT BIGINT ,
Expand Down
Expand Up @@ -41,7 +41,8 @@ CREATE TABLE BATCH_STEP_EXECUTION (
VERSION BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
JOB_EXECUTION_ID BIGINT NOT NULL,
START_TIME TIMESTAMP NOT NULL ,
CREATE_TIME TIMESTAMP NOT NULL ,
START_TIME TIMESTAMP DEFAULT NULL ,
END_TIME TIMESTAMP DEFAULT NULL ,
STATUS VARCHAR(10) ,
COMMIT_COUNT BIGINT ,
Expand Down
Expand Up @@ -41,7 +41,8 @@ CREATE TABLE BATCH_STEP_EXECUTION (
VERSION BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
JOB_EXECUTION_ID BIGINT NOT NULL,
START_TIME DATETIME(6) NOT NULL ,
CREATE_TIME DATETIME(6) NOT NULL,
START_TIME DATETIME(6) DEFAULT NULL ,
END_TIME DATETIME(6) DEFAULT NULL ,
STATUS VARCHAR(10) ,
COMMIT_COUNT BIGINT ,
Expand Down
Expand Up @@ -41,7 +41,8 @@ CREATE TABLE BATCH_STEP_EXECUTION (
VERSION NUMBER(19,0) NOT NULL,
STEP_NAME VARCHAR2(100 char) NOT NULL,
JOB_EXECUTION_ID NUMBER(19,0) NOT NULL,
START_TIME TIMESTAMP NOT NULL ,
CREATE_TIME TIMESTAMP NOT NULL,
START_TIME TIMESTAMP DEFAULT NULL ,
END_TIME TIMESTAMP DEFAULT NULL ,
STATUS VARCHAR2(10 char) ,
COMMIT_COUNT NUMBER(19,0) ,
Expand Down
Expand Up @@ -41,7 +41,8 @@ CREATE TABLE BATCH_STEP_EXECUTION (
VERSION BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
JOB_EXECUTION_ID BIGINT NOT NULL,
START_TIME TIMESTAMP NOT NULL ,
CREATE_TIME TIMESTAMP NOT NULL,
START_TIME TIMESTAMP DEFAULT NULL ,
END_TIME TIMESTAMP DEFAULT NULL ,
STATUS VARCHAR(10) ,
COMMIT_COUNT BIGINT ,
Expand Down
Expand Up @@ -41,7 +41,8 @@ CREATE TABLE BATCH_STEP_EXECUTION (
VERSION INTEGER NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
JOB_EXECUTION_ID INTEGER NOT NULL,
START_TIME TIMESTAMP NOT NULL ,
CREATE_TIME TIMESTAMP NOT NULL,
START_TIME TIMESTAMP DEFAULT NULL ,
END_TIME TIMESTAMP DEFAULT NULL ,
STATUS VARCHAR(10) ,
COMMIT_COUNT INTEGER ,
Expand Down
Expand Up @@ -41,7 +41,8 @@ CREATE TABLE BATCH_STEP_EXECUTION (
VERSION BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
JOB_EXECUTION_ID BIGINT NOT NULL,
START_TIME DATETIME NOT NULL ,
CREATE_TIME DATETIME NOT NULL,
START_TIME DATETIME DEFAULT NULL ,
END_TIME DATETIME DEFAULT NULL ,
STATUS VARCHAR(10) NULL,
COMMIT_COUNT BIGINT NULL,
Expand Down
Expand Up @@ -41,7 +41,8 @@ CREATE TABLE BATCH_STEP_EXECUTION (
VERSION BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
JOB_EXECUTION_ID BIGINT NOT NULL,
START_TIME DATETIME NOT NULL ,
CREATE_TIME DATETIME NOT NULL,
START_TIME DATETIME DEFAULT NULL NULL,
END_TIME DATETIME DEFAULT NULL NULL,
STATUS VARCHAR(10) NULL,
COMMIT_COUNT BIGINT NULL,
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -75,13 +75,13 @@ public void testGetEndTime() {

/**
* Test method for
* {@link org.springframework.batch.core.JobExecution#getStartTime()}.
* {@link StepExecution#getCreateTime()}.
*/
@Test
public void testGetStartTime() {
assertNotNull(execution.getStartTime());
execution.setStartTime(new Date(10L));
assertEquals(10L, execution.getStartTime().getTime());
public void testGetCreateTime() {
assertNotNull(execution.getCreateTime());
execution.setCreateTime(new Date(10L));
assertEquals(10L, execution.getCreateTime().getTime());
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -335,5 +335,6 @@ private void assertStepExecutionsAreEqual(StepExecution expected, StepExecution
assertEquals(expected.getLastUpdated(), actual.getLastUpdated());
assertEquals(expected.getExitStatus(), actual.getExitStatus());
assertEquals(expected.getJobExecutionId(), actual.getJobExecutionId());
assertEquals(expected.getCreateTime(), actual.getCreateTime());
}
}
Expand Up @@ -50,7 +50,8 @@ CREATE TABLE PREFIX_STEP_EXECUTION (
VERSION BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
JOB_EXECUTION_ID BIGINT NOT NULL,
START_TIME TIMESTAMP NOT NULL ,
CREATE_TIME TIMESTAMP NOT NULL ,
START_TIME TIMESTAMP DEFAULT NULL ,
END_TIME TIMESTAMP DEFAULT NULL ,
STATUS VARCHAR(10) ,
COMMIT_COUNT BIGINT ,
Expand Down
Expand Up @@ -45,7 +45,8 @@ CREATE TABLE BATCH_STEP_EXECUTION (
VERSION BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
JOB_EXECUTION_ID BIGINT NOT NULL,
START_TIME TIMESTAMP NOT NULL ,
CREATE_TIME TIMESTAMP NOT NULL ,
START_TIME TIMESTAMP DEFAULT NULL ,
END_TIME TIMESTAMP DEFAULT NULL ,
STATUS VARCHAR(10) ,
COMMIT_COUNT BIGINT ,
Expand Down

0 comments on commit 464985b

Please sign in to comment.