Skip to content

Commit

Permalink
Add abstract job
Browse files Browse the repository at this point in the history
### 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
jja725 authored Mar 16, 2023
1 parent 8e08e77 commit 71521f6
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 961 deletions.
112 changes: 112 additions & 0 deletions core/server/master/src/main/java/alluxio/master/job/AbstractJob.java
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;
}
}
Loading

0 comments on commit 71521f6

Please sign in to comment.