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));
}
}
}
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);
}
}
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);
}
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);
}
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);
}
}
Aggregations