Search in sources :

Example 1 with StatefulStorageException

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());
}
Also used : StatefulStorageException(org.apache.heron.spi.statefulstorage.StatefulStorageException) Checkpoint(org.apache.heron.spi.statefulstorage.Checkpoint) ByteString(com.google.protobuf.ByteString) CheckpointInfo(org.apache.heron.spi.statefulstorage.CheckpointInfo) Common(org.apache.heron.proto.system.Common)

Example 2 with StatefulStorageException

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);
            }
        }
    }
}
Also used : StatefulStorageException(org.apache.heron.spi.statefulstorage.StatefulStorageException) IOException(java.io.IOException) URI(java.net.URI) Namespace(org.apache.distributedlog.api.namespace.Namespace)

Example 3 with StatefulStorageException

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);
    }
}
Also used : StatefulStorageException(org.apache.heron.spi.statefulstorage.StatefulStorageException) IOException(java.io.IOException)

Example 4 with StatefulStorageException

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");
    }
}
Also used : StatefulStorageException(org.apache.heron.spi.statefulstorage.StatefulStorageException) Checkpoint(org.apache.heron.spi.statefulstorage.Checkpoint) CheckpointManager(org.apache.heron.proto.ckptmgr.CheckpointManager) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException)

Example 5 with StatefulStorageException

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);
    }
}
Also used : StatefulStorageException(org.apache.heron.spi.statefulstorage.StatefulStorageException) File(java.io.File)

Aggregations

StatefulStorageException (org.apache.heron.spi.statefulstorage.StatefulStorageException)15 IOException (java.io.IOException)8 Checkpoint (org.apache.heron.spi.statefulstorage.Checkpoint)6 CheckpointManager (org.apache.heron.proto.ckptmgr.CheckpointManager)4 ByteString (com.google.protobuf.ByteString)3 Path (org.apache.hadoop.fs.Path)3 Common (org.apache.heron.proto.system.Common)3 File (java.io.File)2 CheckpointInfo (org.apache.heron.spi.statefulstorage.CheckpointInfo)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 Namespace (org.apache.distributedlog.api.namespace.Namespace)1 Configuration (org.apache.hadoop.conf.Configuration)1 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)1 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)1 FileStatus (org.apache.hadoop.fs.FileStatus)1 DLInputStream (org.apache.heron.dlog.DLInputStream)1 DLOutputStream (org.apache.heron.dlog.DLOutputStream)1