Search in sources :

Example 1 with Value

use of io.cdap.cdap.api.workflow.Value in project cdap by caskdata.

the class MetadataSubscriberServiceTest method testWorkflow.

@Test
public void testWorkflow() throws InterruptedException, ExecutionException, TimeoutException {
    ProgramRunId workflowRunId = workflow1.run(RunIds.generate());
    // Try to read, should have nothing
    Store store = getInjector().getInstance(DefaultStore.class);
    WorkflowToken workflowToken = store.getWorkflowToken(workflow1, workflowRunId.getRun());
    Assert.assertNull(workflowToken.get("key"));
    BasicWorkflowToken token = new BasicWorkflowToken(1024);
    token.setCurrentNode("node1");
    token.put("key", "value");
    // Publish some workflow states
    WorkflowStateWriter workflowStateWriter = getInjector().getInstance(MessagingWorkflowStateWriter.class);
    workflowStateWriter.setWorkflowToken(workflowRunId, token);
    workflowStateWriter.addWorkflowNodeState(workflowRunId, new WorkflowNodeStateDetail("action1", NodeStatus.RUNNING));
    // Verify the WorkflowToken
    Tasks.waitFor("value", () -> Optional.ofNullable(store.getWorkflowToken(workflow1, workflowRunId.getRun()).get("key")).map(Value::toString).orElse(null), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    // Verify the workflow node state
    Tasks.waitFor(NodeStatus.RUNNING, () -> store.getWorkflowNodeStates(workflowRunId).stream().findFirst().map(WorkflowNodeStateDetail::getNodeStatus).orElse(null), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    // Update the node state
    workflowStateWriter.addWorkflowNodeState(workflowRunId, new WorkflowNodeStateDetail("action1", NodeStatus.COMPLETED));
    // Verify the updated node state
    Tasks.waitFor(NodeStatus.COMPLETED, () -> store.getWorkflowNodeStates(workflowRunId).stream().findFirst().map(WorkflowNodeStateDetail::getNodeStatus).orElse(null), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
}
Also used : Value(io.cdap.cdap.api.workflow.Value) DefaultStore(io.cdap.cdap.internal.app.store.DefaultStore) Store(io.cdap.cdap.app.store.Store) WorkflowToken(io.cdap.cdap.api.workflow.WorkflowToken) BasicWorkflowToken(io.cdap.cdap.internal.app.runtime.workflow.BasicWorkflowToken) MessagingWorkflowStateWriter(io.cdap.cdap.internal.app.runtime.workflow.MessagingWorkflowStateWriter) WorkflowStateWriter(io.cdap.cdap.internal.app.runtime.workflow.WorkflowStateWriter) BasicWorkflowToken(io.cdap.cdap.internal.app.runtime.workflow.BasicWorkflowToken) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) WorkflowNodeStateDetail(io.cdap.cdap.proto.WorkflowNodeStateDetail) Test(org.junit.Test)

Example 2 with Value

use of io.cdap.cdap.api.workflow.Value in project cdap by caskdata.

the class WorkflowHttpHandler method getWorkflowToken.

@GET
@Path("/apps/{app-id}/workflows/{workflow-id}/runs/{run-id}/nodes/{node-id}/token")
public void getWorkflowToken(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("workflow-id") String workflowId, @PathParam("run-id") String runId, @PathParam("node-id") String nodeId, @QueryParam("scope") @DefaultValue("user") String scope, @QueryParam("key") @DefaultValue("") String key) throws NotFoundException {
    WorkflowToken workflowToken = getWorkflowToken(namespaceId, appId, workflowId, runId);
    WorkflowToken.Scope tokenScope = WorkflowToken.Scope.valueOf(scope.toUpperCase());
    Map<String, Value> workflowTokenFromNode = workflowToken.getAllFromNode(nodeId, tokenScope);
    WorkflowTokenNodeDetail tokenAtNode = WorkflowTokenNodeDetail.of(workflowTokenFromNode);
    Type workflowTokenNodeDetailType = new TypeToken<WorkflowTokenNodeDetail>() {
    }.getType();
    if (key.isEmpty()) {
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(tokenAtNode, workflowTokenNodeDetailType));
        return;
    }
    if (!workflowTokenFromNode.containsKey(key)) {
        throw new NotFoundException(key);
    }
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(WorkflowTokenNodeDetail.of(Collections.singletonMap(key, workflowTokenFromNode.get(key))), workflowTokenNodeDetailType));
}
Also used : Type(java.lang.reflect.Type) ProgramType(io.cdap.cdap.proto.ProgramType) WorkflowTokenNodeDetail(io.cdap.cdap.proto.WorkflowTokenNodeDetail) DefaultValue(javax.ws.rs.DefaultValue) Value(io.cdap.cdap.api.workflow.Value) NodeValue(io.cdap.cdap.api.workflow.NodeValue) WorkflowToken(io.cdap.cdap.api.workflow.WorkflowToken) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) InstanceNotFoundException(io.cdap.cdap.api.dataset.InstanceNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) ProgramNotFoundException(io.cdap.cdap.common.ProgramNotFoundException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 3 with Value

use of io.cdap.cdap.api.workflow.Value in project cdap by caskdata.

the class AppWithMultipleSchedules method addTokens.

private static void addTokens(List<ProgramStatusTriggerInfo> programStatusTriggerInfos, Map<String, String> runtimeArgs, String triggeringKeyNodePair, String key) {
    for (ProgramStatusTriggerInfo triggerInfo : programStatusTriggerInfos) {
        WorkflowToken token = triggerInfo.getWorkflowToken();
        if (token == null) {
            continue;
        }
        String[] keyNode = triggeringKeyNodePair.split(":");
        Value value = token.get(keyNode[0], keyNode[1]);
        if (value == null) {
            continue;
        }
        runtimeArgs.put(key, value.toString());
    }
}
Also used : ProgramStatusTriggerInfo(io.cdap.cdap.api.schedule.ProgramStatusTriggerInfo) Value(io.cdap.cdap.api.workflow.Value) WorkflowToken(io.cdap.cdap.api.workflow.WorkflowToken)

Example 4 with Value

use of io.cdap.cdap.api.workflow.Value 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(io.cdap.cdap.api.workflow.Value) BasicWorkflowToken(io.cdap.cdap.internal.app.runtime.workflow.BasicWorkflowToken) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) Test(org.junit.Test)

Aggregations

Value (io.cdap.cdap.api.workflow.Value)4 WorkflowToken (io.cdap.cdap.api.workflow.WorkflowToken)3 BasicWorkflowToken (io.cdap.cdap.internal.app.runtime.workflow.BasicWorkflowToken)2 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)2 Test (org.junit.Test)2 InstanceNotFoundException (io.cdap.cdap.api.dataset.InstanceNotFoundException)1 ProgramStatusTriggerInfo (io.cdap.cdap.api.schedule.ProgramStatusTriggerInfo)1 NodeValue (io.cdap.cdap.api.workflow.NodeValue)1 Store (io.cdap.cdap.app.store.Store)1 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)1 NotFoundException (io.cdap.cdap.common.NotFoundException)1 ProgramNotFoundException (io.cdap.cdap.common.ProgramNotFoundException)1 MessagingWorkflowStateWriter (io.cdap.cdap.internal.app.runtime.workflow.MessagingWorkflowStateWriter)1 WorkflowStateWriter (io.cdap.cdap.internal.app.runtime.workflow.WorkflowStateWriter)1 DefaultStore (io.cdap.cdap.internal.app.store.DefaultStore)1 ProgramType (io.cdap.cdap.proto.ProgramType)1 WorkflowNodeStateDetail (io.cdap.cdap.proto.WorkflowNodeStateDetail)1 WorkflowTokenNodeDetail (io.cdap.cdap.proto.WorkflowTokenNodeDetail)1 Type (java.lang.reflect.Type)1 DefaultValue (javax.ws.rs.DefaultValue)1