Search in sources :

Example 11 with Checkpoint

use of org.apache.heron.spi.statefulstorage.Checkpoint in project heron by twitter.

the class LocalFileSystemStorageTest method testRestore.

@Test
public void testRestore() throws Exception {
    PowerMockito.spy(FileUtils.class);
    PowerMockito.doReturn(checkpoint.toByteArray()).when(FileUtils.class, "readFromFile", anyString());
    final CheckpointInfo info = new CheckpointInfo(StatefulStorageTestContext.CHECKPOINT_ID, instance);
    Checkpoint ckpt = localFileSystemStorage.restoreCheckpoint(info);
    assertEquals(checkpoint, ckpt.getCheckpoint());
}
Also used : InstanceStateCheckpoint(org.apache.heron.proto.ckptmgr.CheckpointManager.InstanceStateCheckpoint) Checkpoint(org.apache.heron.spi.statefulstorage.Checkpoint) CheckpointInfo(org.apache.heron.spi.statefulstorage.CheckpointInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with Checkpoint

use of org.apache.heron.spi.statefulstorage.Checkpoint in project heron by twitter.

the class LocalFileSystemStorageTest method testStore.

@Test
public void testStore() throws Exception {
    PowerMockito.spy(FileUtils.class);
    PowerMockito.doReturn(true).when(FileUtils.class, "createDirectory", anyString());
    PowerMockito.doReturn(true).when(FileUtils.class, "isFileExists", anyString());
    PowerMockito.doReturn(true).when(FileUtils.class, "isDirectoryExists", anyString());
    PowerMockito.doReturn(true).when(FileUtils.class, "writeToFile", anyString(), any(byte[].class), anyBoolean());
    PowerMockito.doReturn(false).when(FileUtils.class, "hasChildren", anyString());
    Checkpoint mockCheckpoint = mock(Checkpoint.class);
    when(mockCheckpoint.getCheckpoint()).thenReturn(checkpoint);
    final CheckpointInfo info = new CheckpointInfo(StatefulStorageTestContext.CHECKPOINT_ID, instance);
    localFileSystemStorage.storeCheckpoint(info, mockCheckpoint);
    PowerMockito.verifyStatic(times(1));
    FileUtils.writeToFile(anyString(), eq(checkpoint.toByteArray()), eq(true));
}
Also used : InstanceStateCheckpoint(org.apache.heron.proto.ckptmgr.CheckpointManager.InstanceStateCheckpoint) Checkpoint(org.apache.heron.spi.statefulstorage.Checkpoint) CheckpointInfo(org.apache.heron.spi.statefulstorage.CheckpointInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with Checkpoint

use of org.apache.heron.spi.statefulstorage.Checkpoint in project heron by twitter.

the class CheckpointManagerServer method handleGetInstanceStateRequest.

protected void handleGetInstanceStateRequest(REQID rid, SocketChannel channel, CheckpointManager.GetInstanceStateRequest request) {
    CheckpointInfo info = new CheckpointInfo(request.getCheckpointId(), request.getInstance());
    LOG.info(String.format("Got a get checkpoint request for checkpointId %s " + " component %s instanceId %d on connection %s", info.getCheckpointId(), info.getComponent(), info.getInstanceId(), channel.socket().getRemoteSocketAddress()));
    CheckpointManager.GetInstanceStateResponse.Builder responseBuilder = CheckpointManager.GetInstanceStateResponse.newBuilder();
    responseBuilder.setInstance(request.getInstance());
    responseBuilder.setCheckpointId(request.getCheckpointId());
    String errorMessage = "";
    Common.StatusCode statusCode = Common.StatusCode.OK;
    if (!request.hasCheckpointId() || request.getCheckpointId().isEmpty()) {
        LOG.info("The checkpoint id was empty, this sending empty state");
        CheckpointManager.InstanceStateCheckpoint dummyState = CheckpointManager.InstanceStateCheckpoint.newBuilder().setCheckpointId(request.getCheckpointId()).setState(ByteString.EMPTY).build();
        responseBuilder.setCheckpoint(dummyState);
    } else {
        try {
            Checkpoint checkpoint = statefulStorage.restoreCheckpoint(info);
            LOG.info(String.format("Get checkpoint successful for checkpointId %s " + "component %s instanceId %d", info.getCheckpointId(), info.getComponent(), info.getInstanceId()));
            // Set the checkpoint-state in response
            if (spillState) {
                CheckpointManager.InstanceStateCheckpoint ckpt = checkpoint.getCheckpoint();
                String checkpointId = ckpt.getCheckpointId();
                // clean any possible existing states
                FileUtils.cleanDir(spillStateLocation);
                // spill state to local disk
                String stateLocation = spillStateLocation + checkpointId + "-" + UUID.randomUUID();
                if (!FileUtils.writeToFile(stateLocation, ckpt.getState().toByteArray(), true)) {
                    throw new RuntimeException("failed to spill state. Bailing out...");
                }
                LOG.info("spilled state to: " + stateLocation);
                ckpt = CheckpointManager.InstanceStateCheckpoint.newBuilder().setStateLocation(stateLocation).setCheckpointId(checkpointId).build();
                responseBuilder.setCheckpoint(ckpt);
            } else {
                responseBuilder.setCheckpoint(checkpoint.getCheckpoint());
            }
        } catch (StatefulStorageException e) {
            errorMessage = String.format("Get checkpoint not successful for checkpointId %s " + "component %s instanceId %d", info.getCheckpointId(), info.getComponent(), info.getInstanceId());
            LOG.log(Level.WARNING, errorMessage, e);
            statusCode = Common.StatusCode.NOTOK;
        }
    }
    responseBuilder.setStatus(Common.Status.newBuilder().setStatus(statusCode).setMessage(errorMessage));
    sendResponse(rid, channel, responseBuilder.build());
}
Also used : StatefulStorageException(org.apache.heron.spi.statefulstorage.StatefulStorageException) Checkpoint(org.apache.heron.spi.statefulstorage.Checkpoint) CheckpointManager(org.apache.heron.proto.ckptmgr.CheckpointManager) ByteString(com.google.protobuf.ByteString) CheckpointInfo(org.apache.heron.spi.statefulstorage.CheckpointInfo) Common(org.apache.heron.proto.system.Common)

Aggregations

Checkpoint (org.apache.heron.spi.statefulstorage.Checkpoint)13 CheckpointManager (org.apache.heron.proto.ckptmgr.CheckpointManager)9 CheckpointInfo (org.apache.heron.spi.statefulstorage.CheckpointInfo)9 Test (org.junit.Test)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 StatefulStorageException (org.apache.heron.spi.statefulstorage.StatefulStorageException)5 ByteString (com.google.protobuf.ByteString)3 Path (org.apache.hadoop.fs.Path)3 IOException (java.io.IOException)2 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)2 DLInputStream (org.apache.heron.dlog.DLInputStream)2 InstanceStateCheckpoint (org.apache.heron.proto.ckptmgr.CheckpointManager.InstanceStateCheckpoint)2 Common (org.apache.heron.proto.system.Common)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Message (com.google.protobuf.Message)1 InputStream (java.io.InputStream)1 AppendOnlyStreamWriter (org.apache.distributedlog.AppendOnlyStreamWriter)1 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)1 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)1 TopologyAPI (org.apache.heron.api.generated.TopologyAPI)1