Search in sources :

Example 11 with TemporaryPath

use of com.walmartlabs.concord.common.TemporaryPath in project concord by walmartlabs.

the class StateManager method archive.

public static void archive(Path baseDir, Serializable state, Path result) throws IOException {
    try (TemporaryPath tmp = IOUtils.tempDir("state-archive")) {
        saveProcessState(tmp.path(), state);
        try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(Files.newOutputStream(result))) {
            zip(zip, Constants.Files.JOB_ATTACHMENTS_DIR_NAME + "/", tmp.path().resolve(Constants.Files.JOB_ATTACHMENTS_DIR_NAME));
            zip(zip, Constants.Files.CONCORD_SYSTEM_DIR_NAME + "/", baseDir.resolve(Constants.Files.CONCORD_SYSTEM_DIR_NAME));
        }
    }
}
Also used : ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) TemporaryPath(com.walmartlabs.concord.common.TemporaryPath)

Example 12 with TemporaryPath

use of com.walmartlabs.concord.common.TemporaryPath in project concord by walmartlabs.

the class StateManager method saveProcessState.

/**
 * Serializes the specified process state object into a file
 * in the standard location inside the provided {@code baseDir}.
 */
public static void saveProcessState(Path baseDir, Serializable state) throws IOException {
    Path stateDir = baseDir.resolve(Constants.Files.JOB_ATTACHMENTS_DIR_NAME).resolve(Constants.Files.JOB_STATE_DIR_NAME);
    if (Files.notExists(stateDir)) {
        Files.createDirectories(stateDir);
    }
    Path dst = stateDir.resolve("instance");
    try (TemporaryPath tmp = IOUtils.tempFile("instance", "state");
        OutputStream out = Files.newOutputStream(tmp.path())) {
        SerializationUtils.serialize(out, state);
        Files.move(tmp.path(), dst, REPLACE_EXISTING);
    }
}
Also used : Path(java.nio.file.Path) TemporaryPath(com.walmartlabs.concord.common.TemporaryPath) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) TemporaryPath(com.walmartlabs.concord.common.TemporaryPath)

Example 13 with TemporaryPath

use of com.walmartlabs.concord.common.TemporaryPath in project concord by walmartlabs.

the class DefaultCheckpointService method create.

@Override
public void create(ThreadId threadId, UUID correlationId, String name, Runtime runtime, ProcessSnapshot snapshot) {
    validate(threadId, snapshot);
    UUID checkpointId = UUID.randomUUID();
    try (StateArchive archive = new StateArchive()) {
        // the goal here is to create a process state snapshot with
        // a "synthetic" event that can be used to continue the process
        // after the checkpoint step
        String resumeEventRef = checkpointId.toString();
        State state = clone(snapshot.vmState(), classLoader);
        state.setEventRef(threadId, resumeEventRef);
        state.setStatus(threadId, ThreadStatus.SUSPENDED);
        archive.withResumeEvent(resumeEventRef).withProcessState(ProcessSnapshot.builder().from(snapshot).vmState(state).build()).withSystemDirectory(workingDirectory.getValue());
        try (TemporaryPath zip = archive.zip()) {
            checkpointUploader.upload(checkpointId, correlationId, name, zip.path());
        }
    } catch (Exception e) {
        throw new RuntimeException("Checkpoint upload error", e);
    }
    log.info("Checkpoint '{}' created", name);
}
Also used : State(com.walmartlabs.concord.svm.State) UUID(java.util.UUID) TemporaryPath(com.walmartlabs.concord.common.TemporaryPath)

Example 14 with TemporaryPath

use of com.walmartlabs.concord.common.TemporaryPath in project concord by walmartlabs.

the class ProcessCheckpointResource method uploadCheckpoint.

@POST
@javax.ws.rs.Path("{id}/checkpoint")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadCheckpoint(@PathParam("id") UUID instanceId, @ApiParam MultipartInput input) {
    // TODO replace with ProcessKeyCache
    ProcessEntry entry = processManager.assertProcess(instanceId);
    ProcessKey processKey = new ProcessKey(entry.instanceId(), entry.createdAt());
    UUID checkpointId = MultipartUtils.assertUuid(input, "id");
    UUID correlationId = MultipartUtils.assertUuid(input, "correlationId");
    String checkpointName = MultipartUtils.assertString(input, "name");
    try (InputStream data = MultipartUtils.assertStream(input, "data");
        TemporaryPath tmpIn = IOUtils.tempFile("checkpoint", ".zip")) {
        Files.copy(data, tmpIn.path(), StandardCopyOption.REPLACE_EXISTING);
        checkpointManager.importCheckpoint(processKey, checkpointId, correlationId, checkpointName, tmpIn.path());
    } catch (ValidationErrorsException e) {
        throw new ConcordApplicationException(e.getMessage(), Response.Status.BAD_REQUEST);
    } catch (IOException e) {
        log.error("uploadCheckpoint ['{}'] -> error", processKey, e);
        throw new ConcordApplicationException("upload error: " + e.getMessage());
    }
    log.info("uploadCheckpoint ['{}'] -> done", processKey);
}
Also used : InputStream(java.io.InputStream) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ProcessEntry(com.walmartlabs.concord.server.process.ProcessEntry) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) IOException(java.io.IOException) UUID(java.util.UUID) TemporaryPath(com.walmartlabs.concord.common.TemporaryPath) ValidationErrorsException(org.sonatype.siesta.ValidationErrorsException)

Example 15 with TemporaryPath

use of com.walmartlabs.concord.common.TemporaryPath in project concord by walmartlabs.

the class ProcessCheckpointManager method restoreCheckpoint.

/**
 * Restore process to a saved checkpoint.
 */
public CheckpointInfo restoreCheckpoint(ProcessKey processKey, UUID checkpointId) {
    try (TemporaryPath checkpointArchive = IOUtils.tempFile("checkpoint", ".zip")) {
        String checkpointName = export(processKey, checkpointId, checkpointArchive.path());
        if (checkpointName == null) {
            return null;
        }
        try (TemporaryPath extractedDir = IOUtils.tempDir("unzipped-checkpoint")) {
            IOUtils.unzip(checkpointArchive.path(), extractedDir.path());
            // TODO: only for v1 runtime
            String eventName = readCheckpointEventName(extractedDir.path());
            stateManager.tx(tx -> {
                stateManager.deleteDirectory(tx, processKey, Constants.Files.CONCORD_SYSTEM_DIR_NAME);
                stateManager.deleteDirectory(tx, processKey, Constants.Files.JOB_ATTACHMENTS_DIR_NAME);
                stateManager.importPath(tx, processKey, null, extractedDir.path(), (p, attrs) -> true);
            });
            Map<String, Object> out = OutVariablesUtils.read(extractedDir.path().resolve(Constants.Files.JOB_ATTACHMENTS_DIR_NAME));
            if (out.isEmpty()) {
                queueDao.removeMeta(processKey, "out");
            } else {
                queueDao.updateMeta(processKey, Collections.singletonMap("out", out));
            }
            return CheckpointInfo.of(checkpointName, eventName);
        }
    } catch (Exception e) {
        throw new RuntimeException("Restore checkpoint '" + checkpointId + "' error", e);
    }
}
Also used : TemporaryPath(com.walmartlabs.concord.common.TemporaryPath) IOException(java.io.IOException) UnauthorizedException(org.apache.shiro.authz.UnauthorizedException)

Aggregations

TemporaryPath (com.walmartlabs.concord.common.TemporaryPath)20 Path (java.nio.file.Path)10 Test (org.junit.jupiter.api.Test)8 RevCommit (org.eclipse.jgit.revwalk.RevCommit)6 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)4 UUID (java.util.UUID)3 ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)2 State (com.walmartlabs.concord.svm.State)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Git (org.eclipse.jgit.api.Git)2 ValidationErrorsException (org.sonatype.siesta.ValidationErrorsException)2 Repository (com.walmartlabs.concord.repository.Repository)1 Secret (com.walmartlabs.concord.sdk.Secret)1 RepositoryUtils.assertRepository (com.walmartlabs.concord.server.org.project.RepositoryUtils.assertRepository)1 ProcessEntry (com.walmartlabs.concord.server.process.ProcessEntry)1 RepositoryRefreshListener (com.walmartlabs.concord.server.repository.listeners.RepositoryRefreshListener)1 ProcessKey (com.walmartlabs.concord.server.sdk.ProcessKey)1 ExecutionException (io.takari.bpm.api.ExecutionException)1 URI (java.net.URI)1