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