use of org.apache.heron.spi.statefulstorage.StatefulStorageException in project heron by twitter.
the class CheckpointManagerServer method handleSaveInstanceStateRequest.
protected void handleSaveInstanceStateRequest(REQID rid, SocketChannel channel, CheckpointManager.SaveInstanceStateRequest request) {
CheckpointInfo info = new CheckpointInfo(request.getCheckpoint().getCheckpointId(), request.getInstance());
Checkpoint checkpoint;
if (request.getCheckpoint().hasStateLocation()) {
checkpoint = loadCheckpoint(request.getInstance(), request.getCheckpoint().getCheckpointId(), request.getCheckpoint().getStateLocation());
} else {
checkpoint = new Checkpoint(request.getCheckpoint());
}
LOG.info(String.format("Got a save checkpoint request for checkpointId %s " + " component %s instanceId %s on connection %s", info.getCheckpointId(), info.getComponent(), info.getInstanceId(), channel.socket().getRemoteSocketAddress()));
Common.StatusCode statusCode = Common.StatusCode.OK;
String errorMessage = "";
try {
statefulStorage.storeCheckpoint(info, checkpoint);
LOG.info(String.format("Saved checkpoint for checkpointId %s compnent %s instanceId %s", info.getCheckpointId(), info.getComponent(), info.getInstanceId()));
} catch (StatefulStorageException e) {
errorMessage = String.format("Save checkpoint not successful for checkpointId " + "%s component %s instanceId %s", info.getCheckpointId(), info.getComponent(), info.getInstanceId());
statusCode = Common.StatusCode.NOTOK;
LOG.log(Level.WARNING, errorMessage, e);
}
CheckpointManager.SaveInstanceStateResponse.Builder responseBuilder = CheckpointManager.SaveInstanceStateResponse.newBuilder();
responseBuilder.setStatus(Common.Status.newBuilder().setStatus(statusCode).setMessage(errorMessage));
responseBuilder.setCheckpointId(request.getCheckpoint().getCheckpointId());
responseBuilder.setInstance(request.getInstance());
sendResponse(rid, channel, responseBuilder.build());
}
use of org.apache.heron.spi.statefulstorage.StatefulStorageException in project heron by twitter.
the class DlogStorage method dispose.
@Override
public void dispose(String oldestCheckpointId, boolean deleteAll) throws StatefulStorageException {
// Currently dlog doesn't support recursive deletion. so we have to fetch all the checkpoints
// and delete individual checkpoints.
// TODO (sijie): replace the logic here once distributedlog supports recursive deletion.
String topologyCheckpointRoot = getTopologyCheckpointRoot(topologyName);
URI topologyUri = URI.create(checkpointNamespaceUriStr + topologyCheckpointRoot);
// get checkpoints
Namespace topologyNs = null;
Iterator<String> checkpoints;
try {
topologyNs = initializeNamespace(topologyUri);
checkpoints = topologyNs.getLogs();
} catch (IOException ioe) {
throw new StatefulStorageException("Failed to open topology namespace", ioe);
} finally {
if (null != topologyNs) {
topologyNs.close();
}
}
while (checkpoints.hasNext()) {
String checkpointId = checkpoints.next();
if (deleteAll || checkpointId.compareTo(oldestCheckpointId) < 0) {
URI checkpointUri = URI.create(checkpointNamespaceUriStr + topologyCheckpointRoot + "/" + checkpointId);
try {
deleteCheckpoint(checkpointUri);
} catch (IOException e) {
throw new StatefulStorageException("Failed to remove checkpoint " + checkpointId + " for topology " + topologyName, e);
}
}
}
}
use of org.apache.heron.spi.statefulstorage.StatefulStorageException in project heron by twitter.
the class DlogStorage method init.
@Override
public void init(String topology, Map<String, Object> conf) throws StatefulStorageException {
LOG.info("Initializing ... Config: " + conf.toString());
LOG.info("Class path: " + System.getProperty("java.class.path"));
this.topologyName = topology;
checkpointNamespaceUriStr = (String) conf.get(NS_URI_KEY);
checkpointNamespaceUri = URI.create(checkpointNamespaceUriStr);
Integer numReplicasValue = (Integer) conf.get(NUM_REPLICAS_KEY);
this.numReplicas = null == numReplicasValue ? 3 : numReplicasValue;
try {
this.namespace = initializeNamespace(checkpointNamespaceUri);
} catch (IOException ioe) {
throw new StatefulStorageException("Failed to open distributedlog namespace @ " + checkpointNamespaceUri, ioe);
}
}
use of org.apache.heron.spi.statefulstorage.StatefulStorageException in project heron by twitter.
the class LocalFileSystemStorage method restoreCheckpoint.
@Override
public Checkpoint restoreCheckpoint(CheckpointInfo info) throws StatefulStorageException {
String path = getCheckpointPath(info.getCheckpointId(), info.getComponent(), info.getInstanceId());
byte[] res = FileUtils.readFromFile(path);
if (res.length != 0) {
// Try to parse the protobuf
CheckpointManager.InstanceStateCheckpoint state;
try {
state = CheckpointManager.InstanceStateCheckpoint.parseFrom(res);
} catch (InvalidProtocolBufferException e) {
throw new StatefulStorageException("Failed to parse the data", e);
}
return new Checkpoint(state);
} else {
throw new StatefulStorageException("Failed to parse the data");
}
}
use of org.apache.heron.spi.statefulstorage.StatefulStorageException in project heron by twitter.
the class LocalFileSystemStorage method storeCheckpoint.
@Override
public void storeCheckpoint(CheckpointInfo info, Checkpoint checkpoint) throws StatefulStorageException {
// heron doesn't clean checkpoints stored on local disk automatically
// localFS cleans checkpoints before store and limits the number of checkpoints saved
String rootPath = getTopologyCheckpointRoot();
cleanCheckpoints(new File(rootPath), maxCheckpoints);
String path = getCheckpointPath(info.getCheckpointId(), info.getComponent(), info.getInstanceId());
// We would try to create but we would not enforce this operation successful,
// since it is possible already created by others
String checkpointDir = getCheckpointDir(info.getCheckpointId(), info.getComponent());
FileUtils.createDirectory(checkpointDir);
// Do a check after the attempt
if (!FileUtils.isDirectoryExists(checkpointDir)) {
throw new StatefulStorageException("Failed to create dir: " + checkpointDir);
}
byte[] contents = checkpoint.getCheckpoint().toByteArray();
// In fact, no need atomic write, since our mechanism requires only best effort
if (!FileUtils.writeToFile(path, contents, true)) {
throw new StatefulStorageException("Failed to persist checkpoint to: " + path);
}
}
Aggregations