Search in sources :

Example 16 with PortabilityJob

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()));
    }
}
Also used : PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) JobAuthorization(org.datatransferproject.spi.cloud.types.JobAuthorization) UUID(java.util.UUID)

Example 17 with PortabilityJob

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;
}
Also used : PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) IOException(java.io.IOException) JobAwareMonitor(org.datatransferproject.api.launcher.JobAwareMonitor)

Example 18 with PortabilityJob

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());
}
Also used : PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob)

Example 19 with PortabilityJob

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);
}
Also used : PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob)

Example 20 with PortabilityJob

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);
    }));
}
Also used : Strings(com.google.common.base.Strings) JobAuthorization(org.datatransferproject.spi.cloud.types.JobAuthorization) CREDS_AVAILABLE(org.datatransferproject.spi.cloud.types.JobAuthorization.State.CREDS_AVAILABLE) CREDS_STORED(org.datatransferproject.spi.cloud.types.JobAuthorization.State.CREDS_STORED) State(org.datatransferproject.spi.cloud.types.PortabilityJob.State) CREDS_ENCRYPTION_KEY_GENERATED(org.datatransferproject.spi.cloud.types.JobAuthorization.State.CREDS_ENCRYPTION_KEY_GENERATED) PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) Preconditions(com.google.common.base.Preconditions) IOException(java.io.IOException) UUID(java.util.UUID) PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob)

Aggregations

PortabilityJob (org.datatransferproject.spi.cloud.types.PortabilityJob)24 UUID (java.util.UUID)11 IOException (java.io.IOException)8 JobAuthorization (org.datatransferproject.spi.cloud.types.JobAuthorization)6 Test (org.junit.Test)6 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 SimpleDateFormat (java.text.SimpleDateFormat)3 TransferJob (org.datatransferproject.types.client.transfer.TransferJob)3 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)3 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)3 InOrder (org.mockito.InOrder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DateFormat (java.text.DateFormat)2 SecretKey (javax.crypto.SecretKey)2 LocalJobStore (org.datatransferproject.cloud.local.LocalJobStore)2 GoogleAlbum (org.datatransferproject.datatransfer.google.mediaModels.GoogleAlbum)2 AuthDataGenerator (org.datatransferproject.spi.api.auth.AuthDataGenerator)2 JobStore (org.datatransferproject.spi.cloud.storage.JobStore)2 InputStreamWrapper (org.datatransferproject.spi.cloud.storage.TemporaryPerJobDataStore.InputStreamWrapper)2