Change job scheduling to be relative rather than absolute.

This commit is contained in:
Greyson Parrelli
2023-08-30 09:51:08 -04:00
committed by Nicholas Tinsley
parent 64babe2e42
commit a911a007d2
10 changed files with 199 additions and 121 deletions

View File

@@ -38,7 +38,8 @@ public abstract class Job {
private final Parameters parameters;
private int runAttempt;
private long nextRunAttemptTime;
private long lastRunAttemptTime;
private long nextBackoffInterval;
private volatile boolean canceled;
@@ -60,8 +61,12 @@ public abstract class Job {
return runAttempt;
}
public final long getNextRunAttemptTime() {
return nextRunAttemptTime;
public final long getLastRunAttemptTime() {
return lastRunAttemptTime;
}
public final long getNextBackoffInterval() {
return nextBackoffInterval;
}
public final @Nullable byte[] getInputData() {
@@ -86,8 +91,13 @@ public abstract class Job {
}
/** Should only be invoked by {@link JobController} */
final void setNextRunAttemptTime(long nextRunAttemptTime) {
this.nextRunAttemptTime = nextRunAttemptTime;
final void setLastRunAttemptTime(long lastRunAttemptTime) {
this.lastRunAttemptTime = lastRunAttemptTime;
}
/** Should only be invoked by {@link JobController} */
final void setNextBackoffInterval(long nextBackoffInterval) {
this.nextBackoffInterval = nextBackoffInterval;
}
/** Should only be invoked by {@link JobController} */

View File

@@ -250,11 +250,10 @@ class JobController {
throw new IllegalArgumentException("Invalid backoff interval! " + backoffInterval);
}
int nextRunAttempt = job.getRunAttempt() + 1;
long nextRunAttemptTime = System.currentTimeMillis() + backoffInterval;
byte[] serializedData = job.serialize();
int nextRunAttempt = job.getRunAttempt() + 1;
byte[] serializedData = job.serialize();
jobStorage.updateJobAfterRetry(job.getId(), false, nextRunAttempt, nextRunAttemptTime, serializedData);
jobStorage.updateJobAfterRetry(job.getId(), false, nextRunAttempt, backoffInterval, serializedData);
jobTracker.onStateChange(job, JobTracker.JobState.PENDING);
List<Constraint> constraints = Stream.of(jobStorage.getConstraintSpecs(job.getId()))
@@ -263,10 +262,8 @@ class JobController {
.toList();
long delay = Math.max(0, nextRunAttemptTime - System.currentTimeMillis());
Log.i(TAG, JobLogger.format(job, "Scheduling a retry in " + delay + " ms."));
scheduler.schedule(delay, constraints);
Log.i(TAG, JobLogger.format(job, "Scheduling a retry in " + backoffInterval + " ms."));
scheduler.schedule(backoffInterval, constraints);
notifyAll();
}
@@ -338,7 +335,7 @@ class JobController {
wait();
}
jobStorage.updateJobRunningState(job.getId(), true);
jobStorage.markJobAsRunning(job.getId(), System.currentTimeMillis());
runningJobs.put(job.getId(), job);
jobTracker.onStateChange(job, JobTracker.JobState.RUNNING);
@@ -445,7 +442,8 @@ class JobController {
job.getFactoryKey(),
job.getParameters().getQueue(),
System.currentTimeMillis(),
job.getNextRunAttemptTime(),
job.getLastRunAttemptTime(),
job.getNextBackoffInterval(),
job.getRunAttempt(),
job.getParameters().getMaxAttempts(),
job.getParameters().getLifespan(),
@@ -511,7 +509,8 @@ class JobController {
Job job = jobInstantiator.instantiate(jobSpec.getFactoryKey(), parameters, jobSpec.getSerializedData());
job.setRunAttempt(jobSpec.getRunAttempt());
job.setNextRunAttemptTime(jobSpec.getNextRunAttemptTime());
job.setLastRunAttemptTime(jobSpec.getLastRunAttemptTime());
job.setNextBackoffInterval(jobSpec.getNextBackoffInterval());
job.setContext(application);
return job;
@@ -547,7 +546,8 @@ class JobController {
jobSpec.getFactoryKey(),
jobSpec.getQueueKey(),
jobSpec.getCreateTime(),
jobSpec.getNextRunAttemptTime(),
jobSpec.getLastRunAttemptTime(),
jobSpec.getNextBackoffInterval(),
jobSpec.getRunAttempt(),
jobSpec.getMaxAttempts(),
jobSpec.getLifespan(),

View File

@@ -65,7 +65,8 @@ public class JobMigrator {
updatedJobData.getFactoryKey(),
updatedJobData.getQueueKey(),
jobSpec.getCreateTime(),
jobSpec.getNextRunAttemptTime(),
jobSpec.getLastRunAttemptTime(),
jobSpec.getNextBackoffInterval(),
jobSpec.getRunAttempt(),
jobSpec.getMaxAttempts(),
jobSpec.getLifespan(),

View File

@@ -5,7 +5,8 @@ data class JobSpec(
val factoryKey: String,
val queueKey: String?,
val createTime: Long,
val nextRunAttemptTime: Long,
val lastRunAttemptTime: Long,
val nextBackoffInterval: Long,
val runAttempt: Int,
val maxAttempts: Int,
val lifespan: Long,
@@ -15,8 +16,8 @@ data class JobSpec(
val isMemoryOnly: Boolean
) {
fun withNextRunAttemptTime(updated: Long): JobSpec {
return copy(nextRunAttemptTime = updated)
fun withNextBackoffInterval(updated: Long): JobSpec {
return copy(nextBackoffInterval = updated)
}
fun withData(updatedSerializedData: ByteArray?): JobSpec {
@@ -24,7 +25,7 @@ data class JobSpec(
}
override fun toString(): String {
return "id: JOB::$id | factoryKey: $factoryKey | queueKey: $queueKey | createTime: $createTime | nextRunAttemptTime: $nextRunAttemptTime | runAttempt: $runAttempt | maxAttempts: $maxAttempts | lifespan: $lifespan | isRunning: $isRunning | memoryOnly: $isMemoryOnly"
return "id: JOB::$id | factoryKey: $factoryKey | queueKey: $queueKey | createTime: $createTime | lastRunAttemptTime: $lastRunAttemptTime | nextBackoffInterval: $nextBackoffInterval | runAttempt: $runAttempt | maxAttempts: $maxAttempts | lifespan: $lifespan | isRunning: $isRunning | memoryOnly: $isMemoryOnly"
}
override fun equals(other: Any?): Boolean {
@@ -37,7 +38,8 @@ data class JobSpec(
if (factoryKey != other.factoryKey) return false
if (queueKey != other.queueKey) return false
if (createTime != other.createTime) return false
if (nextRunAttemptTime != other.nextRunAttemptTime) return false
if (lastRunAttemptTime != other.lastRunAttemptTime) return false
if (nextBackoffInterval != other.nextBackoffInterval) return false
if (runAttempt != other.runAttempt) return false
if (maxAttempts != other.maxAttempts) return false
if (lifespan != other.lifespan) return false
@@ -60,7 +62,8 @@ data class JobSpec(
result = 31 * result + factoryKey.hashCode()
result = 31 * result + (queueKey?.hashCode() ?: 0)
result = 31 * result + createTime.hashCode()
result = 31 * result + nextRunAttemptTime.hashCode()
result = 31 * result + lastRunAttemptTime.hashCode()
result = 31 * result + nextBackoffInterval.hashCode()
result = 31 * result + runAttempt
result = 31 * result + maxAttempts
result = 31 * result + lifespan.hashCode()

View File

@@ -37,7 +37,7 @@ public interface JobStorage {
boolean areQueuesEmpty(@NonNull Set<String> queueKeys);
@WorkerThread
void updateJobRunningState(@NonNull String id, boolean isRunning);
void markJobAsRunning(@NonNull String id, long currentTime);
@WorkerThread
void updateJobAfterRetry(@NonNull String id, boolean isRunning, int runAttempt, long nextRunAttemptTime, @Nullable byte[] serializedData);