Search in sources :

Example 51 with ProgramRunId

use of co.cask.cdap.proto.id.ProgramRunId in project cdap by caskdata.

the class SparkExecutionServiceTest method testWorkflowToken.

@Test
public void testWorkflowToken() throws Exception {
    ProgramRunId programRunId = new ProgramRunId("ns", "app", ProgramType.SPARK, "test", RunIds.generate().getId());
    // Start a service with empty workflow token
    BasicWorkflowToken token = new BasicWorkflowToken(10);
    token.setCurrentNode("spark");
    SparkExecutionService service = new SparkExecutionService(locationFactory, InetAddress.getLoopbackAddress().getCanonicalHostName(), programRunId, token);
    service.startAndWait();
    try {
        SparkExecutionClient client = new SparkExecutionClient(service.getBaseURI(), programRunId);
        // Update token via heartbeat
        BasicWorkflowToken clientToken = new BasicWorkflowToken(10);
        clientToken.setCurrentNode("spark");
        for (int i = 0; i < 5; i++) {
            clientToken.put("key", "value" + i);
            client.heartbeat(clientToken);
            // The server side token should get updated
            Assert.assertEquals(Value.of("value" + i), token.get("key", "spark"));
        }
        clientToken.put("completed", "true");
        client.completed(clientToken);
    } finally {
        service.stopAndWait();
    }
    // The token on the service side should get updated after the completed call.
    Map<String, Value> values = token.getAllFromNode("spark");
    Map<String, Value> expected = ImmutableMap.of("key", Value.of("value4"), "completed", Value.of("true"));
    Assert.assertEquals(expected, values);
}
Also used : Value(co.cask.cdap.api.workflow.Value) BasicWorkflowToken(co.cask.cdap.internal.app.runtime.workflow.BasicWorkflowToken) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) Test(org.junit.Test)

Example 52 with ProgramRunId

use of co.cask.cdap.proto.id.ProgramRunId in project cdap by caskdata.

the class SparkExecutionServiceTest method testExplicitStop.

@Test
public void testExplicitStop() throws Exception {
    ProgramRunId programRunId = new ProgramRunId("ns", "app", ProgramType.SPARK, "test", RunIds.generate().getId());
    // Start a service that no token is supported
    SparkExecutionService service = new SparkExecutionService(locationFactory, InetAddress.getLoopbackAddress().getCanonicalHostName(), programRunId, null);
    service.startAndWait();
    try {
        final SparkExecutionClient client = new SparkExecutionClient(service.getBaseURI(), programRunId);
        // Heartbeats multiple times.
        for (int i = 0; i < 5; i++) {
            Assert.assertNull(client.heartbeat(null));
            TimeUnit.MILLISECONDS.sleep(50);
        }
        // Stop the program from the service side
        ListenableFuture<Service.State> stopFuture = service.stop();
        // Expect some future heartbeats will receive the STOP command
        Tasks.waitFor(SparkCommand.STOP, new Callable<SparkCommand>() {

            @Override
            public SparkCommand call() throws Exception {
                return client.heartbeat(null);
            }
        }, 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        // Call complete to notify the service it has been stopped
        client.completed(null);
        // The stop future of the service should be completed after the client.completed call.
        stopFuture.get(5, TimeUnit.SECONDS);
    } finally {
        service.stopAndWait();
    }
}
Also used : ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) IOException(java.io.IOException) Test(org.junit.Test)

Example 53 with ProgramRunId

use of co.cask.cdap.proto.id.ProgramRunId in project cdap by caskdata.

the class SparkExecutionServiceTest method testWriteCredentials.

@Test
public void testWriteCredentials() throws Exception {
    ProgramRunId programRunId = new ProgramRunId("ns", "app", ProgramType.SPARK, "test", RunIds.generate().getId());
    // Start a service that doesn't support workflow token
    SparkExecutionService service = new SparkExecutionService(locationFactory, InetAddress.getLoopbackAddress().getCanonicalHostName(), programRunId, null);
    service.startAndWait();
    try {
        SparkExecutionClient client = new SparkExecutionClient(service.getBaseURI(), programRunId);
        Location targetLocation = locationFactory.create(UUID.randomUUID().toString()).append("credentials");
        client.writeCredentials(targetLocation);
        FileStatus status = dfsCluster.getFileSystem().getFileStatus(new Path(targetLocation.toURI()));
        // Verify the file permission is 600
        Assert.assertEquals(FsAction.READ_WRITE, status.getPermission().getUserAction());
        Assert.assertEquals(FsAction.NONE, status.getPermission().getGroupAction());
        Assert.assertEquals(FsAction.NONE, status.getPermission().getOtherAction());
        // Should be able to deserialize back to credentials
        Credentials credentials = new Credentials();
        try (DataInputStream is = new DataInputStream(targetLocation.getInputStream())) {
            credentials.readTokenStorageStream(is);
        }
        // Call complete to notify the service it has been stopped
        client.completed(null);
    } finally {
        service.stopAndWait();
    }
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) DataInputStream(java.io.DataInputStream) Credentials(org.apache.hadoop.security.Credentials) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Aggregations

ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)53 ProgramId (co.cask.cdap.proto.id.ProgramId)23 Test (org.junit.Test)22 ApplicationId (co.cask.cdap.proto.id.ApplicationId)12 RunRecordMeta (co.cask.cdap.internal.app.store.RunRecordMeta)10 Path (javax.ws.rs.Path)10 RunId (org.apache.twill.api.RunId)10 DatasetId (co.cask.cdap.proto.id.DatasetId)9 Relation (co.cask.cdap.data2.metadata.lineage.Relation)7 HashSet (java.util.HashSet)7 NotFoundException (co.cask.cdap.common.NotFoundException)6 WorkflowNodeStateDetail (co.cask.cdap.proto.WorkflowNodeStateDetail)6 HashMap (java.util.HashMap)6 GET (javax.ws.rs.GET)6 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)5 NamespacedEntityId (co.cask.cdap.proto.id.NamespacedEntityId)5 StreamId (co.cask.cdap.proto.id.StreamId)5 Map (java.util.Map)5 CommandInputError (co.cask.cdap.cli.exception.CommandInputError)4 MethodArgument (co.cask.cdap.common.internal.remote.MethodArgument)4