forked from Alluxio/alluxio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### What changes are proposed in this pull request? Add abstract job and delete copy implementation ### Why are the changes needed? Please clarify why the changes are needed. For instance, clean code ### Does this PR introduce any user facing changes? na pr-link: Alluxio#17103 change-id: cid-af24bee40f5998adb59ced8642f038591b0d7c96
- Loading branch information
Showing
6 changed files
with
127 additions
and
961 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
core/server/master/src/main/java/alluxio/master/job/AbstractJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0 | ||
* (the "License"). You may not use this work except in compliance with the License, which is | ||
* available at www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied, as more fully set forth in the License. | ||
* | ||
* See the NOTICE file distributed with this work for information regarding copyright ownership. | ||
*/ | ||
|
||
package alluxio.master.job; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import alluxio.scheduler.job.Job; | ||
import alluxio.scheduler.job.JobState; | ||
import alluxio.scheduler.job.Task; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Optional; | ||
import java.util.OptionalLong; | ||
|
||
/** | ||
* Abstract class for job. It provides basic job information and state management. | ||
* | ||
* @param <T> the type of the task of the job | ||
*/ | ||
public abstract class AbstractJob<T extends Task<?>> implements Job<T> { | ||
private static final Logger LOG = LoggerFactory.getLogger(LoadJob.class); | ||
protected final String mJobId; | ||
protected JobState mState; | ||
protected OptionalLong mEndTime = OptionalLong.empty(); | ||
protected final long mStartTime; | ||
protected final Optional<String> mUser; | ||
|
||
/** | ||
* Creates a new instance of {@link AbstractJob}. | ||
* | ||
* @param user the user who submitted the job | ||
* @param jobId the job id | ||
*/ | ||
public AbstractJob(Optional<String> user, String jobId) { | ||
mUser = requireNonNull(user, "user is null"); | ||
mJobId = requireNonNull(jobId, "jobId is null"); | ||
mState = JobState.RUNNING; | ||
mStartTime = System.currentTimeMillis(); | ||
} | ||
|
||
@Override | ||
public String getJobId() { | ||
return mJobId; | ||
} | ||
|
||
/** | ||
* Get end time. | ||
* | ||
* @return end time | ||
*/ | ||
@Override | ||
public OptionalLong getEndTime() { | ||
return mEndTime; | ||
} | ||
|
||
/** | ||
* Update end time. | ||
* | ||
* @param time time in ms | ||
*/ | ||
public void setEndTime(long time) { | ||
mEndTime = OptionalLong.of(time); | ||
} | ||
|
||
/** | ||
* Get load status. | ||
* | ||
* @return the load job's status | ||
*/ | ||
@Override | ||
public JobState getJobState() { | ||
return mState; | ||
} | ||
|
||
/** | ||
* Set load state. | ||
* | ||
* @param state new state | ||
*/ | ||
@Override | ||
public void setJobState(JobState state) { | ||
LOG.debug("Change JobState to {} for job {}", state, this); | ||
mState = state; | ||
if (!isRunning()) { | ||
mEndTime = OptionalLong.of(System.currentTimeMillis()); | ||
} | ||
if (state == JobState.SUCCEEDED) { | ||
LoadJob.JOB_LOAD_SUCCESS.inc(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isRunning() { | ||
return mState == JobState.RUNNING || mState == JobState.VERIFYING; | ||
} | ||
|
||
@Override | ||
public boolean isDone() { | ||
return mState == JobState.SUCCEEDED || mState == JobState.FAILED; | ||
} | ||
} |
Oops, something went wrong.