use of org.datatransferproject.spi.cloud.types.PortabilityJob in project data-transfer-project by google.
the class JobPollingService method pollUntilJobIsReady.
/**
* Polls for job with populated auth data and stops this service when found.
*/
private void pollUntilJobIsReady() {
monitor.debug(() -> "pollUntilJobIsReady");
UUID jobId = JobMetadata.getJobId();
PortabilityJob job = store.findJob(jobId);
if (job == null) {
monitor.severe(() -> format("Could not poll job %s, it was not present in the key-value store", jobId), EventCode.WORKER_JOB_ERRORED);
this.stopAsync();
} else if (job.state() == PortabilityJob.State.CANCELED) {
monitor.info(() -> format("Could not poll job %s, it was cancelled", jobId), EventCode.WORKER_JOB_CANCELED);
this.stopAsync();
} else if (job.jobAuthorization().state() == JobAuthorization.State.CREDS_STORED) {
monitor.debug(() -> format("Polled job %s in state CREDS_STORED", jobId));
JobAuthorization jobAuthorization = job.jobAuthorization();
if (!Strings.isNullOrEmpty(jobAuthorization.encryptedAuthData())) {
monitor.debug(() -> format("Polled job %s has auth data as expected. Done polling.", jobId), EventCode.WORKER_CREDS_STORED);
} else {
monitor.severe(() -> format("Polled job %s does not have auth data as expected. " + "Done polling this job since it's in a bad state! Starting over.", jobId), EventCode.WORKER_JOB_ERRORED);
}
this.stopAsync();
} else {
monitor.debug(() -> format("Polling job %s until it's in state CREDS_STORED. " + "It's currently in state: %s", jobId, job.jobAuthorization().state()));
}
}
use of org.datatransferproject.spi.cloud.types.PortabilityJob in project data-transfer-project by google.
the class JobPollingService method tryToClaimJob.
/**
* Claims {@link PortabilityJob} {@code jobId} and updates it with our public key in storage.
* Returns true if the claim was successful; otherwise it returns false.
*/
private boolean tryToClaimJob(UUID jobId, WorkerKeyPair keyPair) {
// Lookup the job so we can append to its existing properties.
PortabilityJob existingJob = store.findJob(jobId);
monitor.debug(() -> format("JobPollingService: tryToClaimJob: jobId: %s", existingJob));
// Verify no transfer worker key
if (existingJob.jobAuthorization().authPublicKey() != null) {
monitor.debug(() -> "A public key cannot be persisted again");
return false;
}
// TODO: Consider moving this check earlier in the flow
String scheme = existingJob.jobAuthorization().encryptionScheme();
if (publicKeySerializer == null) {
monitor.severe(() -> format("Public key serializer not found for scheme %s processing job: %s", scheme, jobId));
return false;
}
String serializedKey = publicKeySerializer.serialize(keyPair.getEncodedPublicKey());
PortabilityJob updatedJob = existingJob.toBuilder().setAndValidateJobAuthorization(existingJob.jobAuthorization().toBuilder().setInstanceId(keyPair.getInstanceId()).setAuthPublicKey(serializedKey).setState(JobAuthorization.State.CREDS_ENCRYPTION_KEY_GENERATED).build()).build();
// to CREDS_ENCRYPTION_KEY_GENERATED.
try {
store.claimJob(jobId, updatedJob);
monitor.debug(() -> format("Stored updated job: tryToClaimJob: jobId: %s", existingJob));
} catch (IllegalStateException | IOException e) {
monitor.debug(() -> format("Could not claim job %s. It was probably already claimed by another transfer" + " worker. Error msg: %s", jobId, e.getMessage()), e);
return false;
}
if (monitor instanceof JobAwareMonitor) {
((JobAwareMonitor) monitor).setJobId(jobId.toString());
}
JobMetadata.init(jobId, keyPair.getEncodedPrivateKey(), existingJob.transferDataType(), existingJob.exportService(), existingJob.importService(), Stopwatch.createUnstarted());
monitor.debug(() -> format("Stored updated job: tryToClaimJob: JobMetadata initialized: %s", jobId));
return true;
}
use of org.datatransferproject.spi.cloud.types.PortabilityJob in project data-transfer-project by google.
the class JobStoreWithValidator method markJobAsTimedOut.
@Override
public void markJobAsTimedOut(UUID jobId) throws IOException {
PortabilityJob job = findJob(jobId);
updateJob(jobId, job.toBuilder().setState(PortabilityJob.State.ERROR).setAndValidateJobAuthorization(job.jobAuthorization().toBuilder().setState(JobAuthorization.State.TIMED_OUT).build()).build());
}
use of org.datatransferproject.spi.cloud.types.PortabilityJob in project data-transfer-project by google.
the class JobStoreWithValidator method addFailureReasonToJob.
public void addFailureReasonToJob(UUID jobId, String failureReason) throws IOException {
PortabilityJob existingJob = findJob(jobId);
PortabilityJob updatedJob = existingJob.toBuilder().setFailureReason(failureReason).build();
updateJob(jobId, updatedJob);
}
use of org.datatransferproject.spi.cloud.types.PortabilityJob in project data-transfer-project by google.
the class JobStoreWithValidator method updateJobState.
private void updateJobState(UUID jobId, State state, State prevState, JobAuthorization.State prevAuthState) throws IOException {
PortabilityJob existingJob = findJob(jobId);
PortabilityJob updatedJob = existingJob.toBuilder().setState(state).build();
updateJob(jobId, updatedJob, ((previous, updated) -> {
Preconditions.checkState(previous.state() == prevState);
Preconditions.checkState(previous.jobAuthorization().state() == prevAuthState);
}));
}
Aggregations