2323import org .apache .doris .catalog .ScalarType ;
2424import org .apache .doris .common .io .Text ;
2525import org .apache .doris .common .io .Writable ;
26+ import org .apache .doris .common .util .LogBuilder ;
27+ import org .apache .doris .common .util .LogKey ;
2628import org .apache .doris .common .util .TimeUtils ;
2729import org .apache .doris .job .common .JobStatus ;
2830import org .apache .doris .job .common .TaskStatus ;
@@ -76,14 +78,55 @@ public abstract class AbstractJob<T extends AbstractTask, C> implements Job<T, C
7678 private JobExecutionConfiguration jobConfig ;
7779
7880 @ SerializedName (value = "ctms" )
79- private Long createTimeMs ;
81+ private long createTimeMs ;
8082
81- @ SerializedName (value = "sql " )
82- String executeSql ;
83+ @ SerializedName (value = "stm " )
84+ private long startTimeMs = - 1L ;
8385
8486 @ SerializedName (value = "ftm" )
8587 private long finishTimeMs ;
8688
89+ @ SerializedName (value = "sql" )
90+ String executeSql ;
91+
92+ public AbstractJob () {}
93+
94+ public AbstractJob (Long id ) {
95+ jobId = id ;
96+ }
97+
98+ /**
99+ * executeSql and runningTasks is not required for load.
100+ */
101+ public AbstractJob (Long jobId , String jobName , JobStatus jobStatus ,
102+ String currentDbName ,
103+ String comment ,
104+ UserIdentity createUser ,
105+ JobExecutionConfiguration jobConfig ) {
106+ this (jobId , jobName , jobStatus , currentDbName , comment ,
107+ createUser , jobConfig , System .currentTimeMillis (), null , null );
108+ }
109+
110+ public AbstractJob (Long jobId , String jobName , JobStatus jobStatus ,
111+ String currentDbName ,
112+ String comment ,
113+ UserIdentity createUser ,
114+ JobExecutionConfiguration jobConfig ,
115+ Long createTimeMs ,
116+ String executeSql ,
117+ List <T > runningTasks ) {
118+ this .jobId = jobId ;
119+ this .jobName = jobName ;
120+ this .jobStatus = jobStatus ;
121+ this .currentDbName = currentDbName ;
122+ this .comment = comment ;
123+ this .createUser = createUser ;
124+ this .jobConfig = jobConfig ;
125+ this .createTimeMs = createTimeMs ;
126+ this .executeSql = executeSql ;
127+ this .runningTasks = runningTasks ;
128+ }
129+
87130 private List <T > runningTasks = new ArrayList <>();
88131
89132 @ Override
@@ -109,6 +152,10 @@ public void cancelAllTasks() throws JobException {
109152 .add ("Comment" )
110153 .build ();
111154
155+ protected static long getNextJobId () {
156+ return System .nanoTime () + RandomUtils .nextInt ();
157+ }
158+
112159 @ Override
113160 public void cancelTaskById (long taskId ) throws JobException {
114161 if (CollectionUtils .isEmpty (runningTasks )) {
@@ -154,17 +201,18 @@ public List<T> commonCreateTasks(TaskType taskType, C taskContext) {
154201 return createTasks (taskType , taskContext );
155202 }
156203
157- public void initTasks (List <? extends AbstractTask > tasks ) {
204+ public void initTasks (Collection <? extends T > tasks , TaskType taskType ) {
205+ if (CollectionUtils .isEmpty (getRunningTasks ())) {
206+ runningTasks = new ArrayList <>();
207+ }
158208 tasks .forEach (task -> {
159- task .setJobId ( jobId );
160- task .setTaskId ( getNextId ());
209+ task .setTaskType ( taskType );
210+ task .setJobId ( getJobId ());
161211 task .setCreateTimeMs (System .currentTimeMillis ());
162212 task .setStatus (TaskStatus .PENDING );
163213 });
164- if (CollectionUtils .isEmpty (getRunningTasks ())) {
165- setRunningTasks (new ArrayList <>());
166- }
167- getRunningTasks ().addAll ((Collection <? extends T >) tasks );
214+ getRunningTasks ().addAll (tasks );
215+ this .startTimeMs = System .currentTimeMillis ();
168216 }
169217
170218 public void checkJobParams () {
@@ -208,10 +256,22 @@ public void updateJobStatus(JobStatus newJobStatus) throws JobException {
208256 public static AbstractJob readFields (DataInput in ) throws IOException {
209257 String jsonJob = Text .readString (in );
210258 AbstractJob job = GsonUtils .GSON .fromJson (jsonJob , AbstractJob .class );
211- job .setRunningTasks ( new ArrayList <>() );
259+ job .runningTasks = new ArrayList <>();
212260 return job ;
213261 }
214262
263+ public void logCreateOperation () {
264+ Env .getCurrentEnv ().getEditLog ().logCreateJob (this );
265+ }
266+
267+ public void logFinalOperation () {
268+ Env .getCurrentEnv ().getEditLog ().logEndJob (this );
269+ }
270+
271+ public void logUpdateOperation () {
272+ Env .getCurrentEnv ().getEditLog ().logUpdateJob (this );
273+ }
274+
215275 @ Override
216276 public void onTaskFail (T task ) throws JobException {
217277 updateJobStatusIfEnd ();
@@ -303,7 +363,19 @@ public ShowResultSetMetaData getJobMetaData() {
303363 return builder .build ();
304364 }
305365
306- private static long getNextId () {
307- return System .nanoTime () + RandomUtils .nextInt ();
366+ @ Override
367+ public void onRegister () throws JobException {}
368+
369+ @ Override
370+ public void onUnRegister () throws JobException {}
371+
372+ @ Override
373+ public void onReplayCreate () throws JobException {
374+ log .info (new LogBuilder (LogKey .SCHEDULER_JOB , getJobId ()).add ("msg" , "replay create scheduler job" ).build ());
375+ }
376+
377+ @ Override
378+ public void onReplayEnd (AbstractJob <?, C > replayJob ) throws JobException {
379+ log .info (new LogBuilder (LogKey .SCHEDULER_JOB , getJobId ()).add ("msg" , "replay delete scheduler job" ).build ());
308380 }
309381}
0 commit comments