Search in sources :

Example 1 with BlockStartNode

use of org.jenkinsci.plugins.workflow.graph.BlockStartNode in project workflow-cps-plugin by jenkinsci.

the class CpsFlowExecution method initializeStorage.

@SuppressFBWarnings(value = "IS2_INCONSISTENT_SYNC", justification = "Storage does not actually NEED to be synchronized but the rest does.")
protected synchronized void initializeStorage() throws IOException {
    // Maybe storage didn't get to persist properly or files were deleted.
    boolean storageErrors = false;
    try {
        storage = createStorage();
        heads = new TreeMap<Integer, FlowHead>();
        for (Map.Entry<Integer, String> entry : headsSerial.entrySet()) {
            FlowHead h = new FlowHead(this, entry.getKey());
            FlowNode n = storage.getNode(entry.getValue());
            if (n != null) {
                h.setForDeserialize(storage.getNode(entry.getValue()));
                heads.put(h.getId(), h);
            } else {
                storageErrors = true;
                break;
            }
        }
        headsSerial = null;
        if (!storageErrors) {
            // Same for startNodes:
            storageErrors = false;
            startNodes = new Stack<BlockStartNode>();
            for (String id : startNodesSerial) {
                FlowNode node = storage.getNode(id);
                if (node != null) {
                    startNodes.add((BlockStartNode) storage.getNode(id));
                } else {
                    // TODO if possible, consider trying to close out unterminated blocks using heads, to keep existing graph history
                    storageErrors = true;
                    break;
                }
            }
        }
        startNodesSerial = null;
    } catch (IOException ioe) {
        LOGGER.log(Level.WARNING, "Error initializing storage and loading nodes", ioe);
        storageErrors = true;
    }
    if (storageErrors) {
        // 
        // Avoid overwriting data
        this.storageDir = (this.storageDir != null) ? this.storageDir + "-fallback" : "workflow-fallback";
        // Empty storage
        this.storage = createStorage();
        // Need to find a way to mimic up the heads and fail cleanly, far enough to let the canResume do its thing
        rebuildEmptyGraph();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BlockStartNode(org.jenkinsci.plugins.workflow.graph.BlockStartNode) IOException(java.io.IOException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ImmutableMap(com.google.common.collect.ImmutableMap) NavigableMap(java.util.NavigableMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with BlockStartNode

use of org.jenkinsci.plugins.workflow.graph.BlockStartNode in project workflow-cps-plugin by jenkinsci.

the class CpsFlowExecution method rebuildEmptyGraph.

/**
 * Handle failures where we can't load heads.
 */
private void rebuildEmptyGraph() {
    synchronized (this) {
        // something went catastrophically wrong and there's no live head. fake one
        if (this.startNodes == null) {
            this.startNodes = new Stack<BlockStartNode>();
        }
        this.heads.clear();
        this.startNodes.clear();
        FlowHead head = new FlowHead(this);
        heads.put(head.getId(), head);
        try {
            FlowStartNode start = new FlowStartNode(this, iotaStr());
            startNodes.push(start);
            head.newStartNode(start);
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, "Failed to persist", e);
        }
        persistedClean = false;
        startNodesSerial = null;
        headsSerial = null;
    }
}
Also used : FlowStartNode(org.jenkinsci.plugins.workflow.graph.FlowStartNode) BlockStartNode(org.jenkinsci.plugins.workflow.graph.BlockStartNode) IOException(java.io.IOException)

Example 3 with BlockStartNode

use of org.jenkinsci.plugins.workflow.graph.BlockStartNode in project workflow-job-plugin by jenkinsci.

the class GraphVizAction method writeDot.

private void writeDot(PrintWriter w) throws IOException {
    try {
        w.println("digraph G {");
        FlowGraphWalker walker = new FlowGraphWalker(run.getExecution());
        for (FlowNode n : walker) {
            for (FlowNode p : n.getParents()) {
                w.printf("%s -> %s%n", p.getId(), n.getId());
            }
            if (n instanceof BlockStartNode) {
                BlockStartNode sn = (BlockStartNode) n;
                w.printf("%s [shape=trapezium]%n", n.getId());
            } else if (n instanceof BlockEndNode) {
                BlockEndNode sn = (BlockEndNode) n;
                w.printf("%s [shape=invtrapezium]%n", n.getId());
                w.printf("%s -> %s [style=dotted]%n", sn.getStartNode().getId(), n.getId());
            }
            w.printf("%s [label=\"%s: %s\"]%n", n.getId(), n.getId(), n.getDisplayName());
        }
        w.println("}");
    } finally {
        w.close();
    }
}
Also used : BlockStartNode(org.jenkinsci.plugins.workflow.graph.BlockStartNode) FlowGraphWalker(org.jenkinsci.plugins.workflow.graph.FlowGraphWalker) BlockEndNode(org.jenkinsci.plugins.workflow.graph.BlockEndNode) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode)

Example 4 with BlockStartNode

use of org.jenkinsci.plugins.workflow.graph.BlockStartNode in project blueocean-plugin by jenkinsci.

the class GraphDumpAction method doIndex.

public HttpResponse doIndex() throws IOException {
    JSONArray response = new JSONArray();
    FlowGraphWalker walker = new FlowGraphWalker(run.getExecution());
    for (FlowNode node : walker) {
        JSONObject outputNode = new JSONObject();
        outputNode.put("id", node.getId());
        outputNode.put("name", node.getDisplayName());
        outputNode.put("functionName", node.getDisplayFunctionName());
        outputNode.put("className", node.getClass().getName());
        outputNode.put("enclosingId", node.getEnclosingId());
        outputNode.put("isBegin", node instanceof BlockStartNode);
        outputNode.put("isEnd", node instanceof BlockEndNode);
        outputNode.put("isStepNode", node instanceof StepNode);
        if (node instanceof StepNode) {
            StepNode sn = (StepNode) node;
            StepDescriptor descriptor = sn.getDescriptor();
            if (descriptor != null) {
                JSONObject outputDescriptor = new JSONObject();
                outputDescriptor.put("getDisplayName", descriptor.getDisplayName());
                outputDescriptor.put("getFunctionName", descriptor.getFunctionName());
                outputNode.put("stepDescriptor", outputDescriptor);
            }
        }
        JSONArray parents = new JSONArray();
        for (FlowNode parent : node.getParents()) {
            parents.add(parent.getId());
        }
        outputNode.put("parents", parents);
        if (node instanceof BlockStartNode) {
            BlockStartNode startNode = (BlockStartNode) node;
            final BlockEndNode endNode = startNode.getEndNode();
            outputNode.put("endNodeId", endNode == null ? null : endNode.getId());
        } else if (node instanceof BlockEndNode) {
            BlockEndNode endNode = (BlockEndNode) node;
            outputNode.put("startNodeId", endNode.getStartNode().getId());
        }
        JSONArray actions = new JSONArray();
        for (Action action : node.getAllActions()) {
            JSONObject outputAction = new JSONObject();
            outputAction.put("className", action.getClass().getName());
            outputAction.put("displayName", action.getDisplayName());
            actions.add(outputAction);
        }
        outputNode.put("actions", actions);
        response.add(outputNode);
    }
    return HttpResponses.okJSON(response);
}
Also used : Action(hudson.model.Action) StepNode(org.jenkinsci.plugins.workflow.graph.StepNode) JSONObject(net.sf.json.JSONObject) BlockStartNode(org.jenkinsci.plugins.workflow.graph.BlockStartNode) JSONArray(net.sf.json.JSONArray) FlowGraphWalker(org.jenkinsci.plugins.workflow.graph.FlowGraphWalker) StepDescriptor(org.jenkinsci.plugins.workflow.steps.StepDescriptor) BlockEndNode(org.jenkinsci.plugins.workflow.graph.BlockEndNode) FlowNode(org.jenkinsci.plugins.workflow.graph.FlowNode)

Aggregations

BlockStartNode (org.jenkinsci.plugins.workflow.graph.BlockStartNode)4 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)3 IOException (java.io.IOException)2 BlockEndNode (org.jenkinsci.plugins.workflow.graph.BlockEndNode)2 FlowGraphWalker (org.jenkinsci.plugins.workflow.graph.FlowGraphWalker)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Action (hudson.model.Action)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 NavigableMap (java.util.NavigableMap)1 TreeMap (java.util.TreeMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 JSONArray (net.sf.json.JSONArray)1 JSONObject (net.sf.json.JSONObject)1 FlowStartNode (org.jenkinsci.plugins.workflow.graph.FlowStartNode)1 StepNode (org.jenkinsci.plugins.workflow.graph.StepNode)1 StepDescriptor (org.jenkinsci.plugins.workflow.steps.StepDescriptor)1