Skip to content

Commit

Permalink
feature: add workflow definition graph and service
Browse files Browse the repository at this point in the history
  • Loading branch information
wangqi committed Sep 6, 2024
1 parent 4e28b56 commit 1576b6e
Show file tree
Hide file tree
Showing 18 changed files with 476 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.jgrapht.Graph;
import org.jgrapht.GraphPath;
import org.jgrapht.alg.shortestpath.AllDirectedPaths;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.builder.GraphTypeBuilder;
import org.jgrapht.traverse.BreadthFirstIterator;
import org.jgrapht.traverse.TopologicalOrderIterator;
Expand All @@ -32,11 +31,27 @@

public class DAG<N> {

private Graph<N, DefaultEdge> jgrapht = GraphTypeBuilder.<N, DefaultEdge>directed()
private Graph<N, DefaultDagEdge<N>> jgrapht = GraphTypeBuilder.<N, DefaultDagEdge<N>>directed()
.allowingSelfLoops(false)
.weighted(false)
.buildGraph();

public void addNode(N node) {
jgrapht.addVertex(node);
}

public void addEdge(N source, N target) {
jgrapht.addEdge(source, target, new DefaultDagEdge<>(source, target));
}

public Set<N> nodes() {
return jgrapht.vertexSet();
}

public Set<DefaultDagEdge<N>> edges() {
return jgrapht.edgeSet();
}

public Set<N> getSources() {
return jgrapht.vertexSet().stream()
.filter(node -> jgrapht.inDegreeOf(node) == 0)
Expand All @@ -50,22 +65,22 @@ public Set<N> getSinks() {
}

public Integer getMaxDepth() {
AllDirectedPaths<N, DefaultEdge> paths = new AllDirectedPaths<>(jgrapht);
AllDirectedPaths<N, DefaultDagEdge<N>> paths = new AllDirectedPaths<>(jgrapht);
return paths.getAllPaths(getSources(), getSinks(), true, null)
.stream().map(GraphPath::getLength)
.sorted()
.findFirst().get();
}

public void topologyTraversal(Consumer<N> consumer) {
TopologicalOrderIterator<N, DefaultEdge> iterator = new TopologicalOrderIterator<>(jgrapht);
TopologicalOrderIterator<N, DefaultDagEdge<N>> iterator = new TopologicalOrderIterator<>(jgrapht);
while (iterator.hasNext()) {
consumer.accept(iterator.next());
}
}

public void breadthFirstTraversal(Consumer<N> consumer) {
BreadthFirstIterator<N, DefaultEdge> iterator = new BreadthFirstIterator<>(jgrapht);
BreadthFirstIterator<N, DefaultDagEdge<N>> iterator = new BreadthFirstIterator<>(jgrapht);
while (iterator.hasNext()) {
N node = iterator.next();
int depth = iterator.getDepth(node);
Expand All @@ -82,7 +97,7 @@ public void executeBFS(Consumer<N> consumer) {

public void doExecuteBFS(Integer depth, Integer maxDepth, Consumer consumer) {
// 使用 BFS,计算每个节点所在的层级
BreadthFirstIterator<N, DefaultEdge> iterator = new BreadthFirstIterator<>(jgrapht);
BreadthFirstIterator<N, DefaultDagEdge<N>> iterator = new BreadthFirstIterator<>(jgrapht);
while (iterator.hasNext()) {
N node = iterator.next();
if (iterator.getDepth(node) == depth) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.sliew.carp.framework.dag.algorithm;

import lombok.Getter;

@Getter
public class DefaultDagEdge<N> {

private final N source;
private final N target;

public DefaultDagEdge(N source, N target) {
this.source = source;
this.target = target;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
* limitations under the License.
*/

package cn.sliew.carp.module.workflow.api.service.dto;
package cn.sliew.carp.module.workflow.api.graph;

import lombok.Data;

@Data
public class WorkflowDefinitionDagMeta {

private Long scheduleJobInstanceId;
public class WorkflowDefinitionAttrs {

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

package cn.sliew.carp.module.workflow.api.graph;

import cn.sliew.carp.framework.dag.service.dto.DagConfigStepDTO;
import com.google.common.graph.Graph;
import cn.sliew.carp.framework.dag.algorithm.DAG;
import lombok.Data;

public interface WorkflowDefinitionGraph {
@Data
public class WorkflowDefinitionGraph {

Graph<DagConfigStepDTO> getGraph();
private DAG<WorkflowTaskDefinition> dag;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.sliew.carp.module.workflow.api.graph;

import lombok.Data;

@Data
public class WorkflowDefinitionMeta {

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

package cn.sliew.carp.module.workflow.api.graph;

import cn.sliew.carp.framework.dag.service.dto.DagStepDTO;
import com.google.common.graph.Graph;
import cn.sliew.carp.framework.dag.algorithm.DAG;
import lombok.Data;

public interface WorkflowExecutionGraph {
@Data
public class WorkflowExecutionGraph {

Graph<DagStepDTO> getGraph();
private DAG<WorkflowTaskInstance> dag;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.sliew.carp.module.workflow.api.graph;

import cn.sliew.carp.framework.common.model.BaseDTO;
import lombok.Data;

@Data
public class WorkflowTaskDefinition extends BaseDTO {

private Long dagId;

private String stepId;

private String name;

private WorkflowTaskDefinitionMeta meta;

private WorkflowTaskDefinitionAttrs attrs;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.sliew.carp.module.workflow.api.graph;

import lombok.Data;

@Data
public class WorkflowTaskDefinitionAttrs {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.sliew.carp.module.workflow.api.graph;

import lombok.Data;

@Data
public class WorkflowTaskDefinitionMeta {

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,34 @@
* limitations under the License.
*/

package cn.sliew.carp.module.workflow.api.service.dto;
package cn.sliew.carp.module.workflow.api.graph;

import cn.sliew.carp.framework.common.dict.workflow.WorkflowType;
import cn.sliew.carp.framework.common.model.BaseDTO;
import cn.sliew.carp.framework.dag.service.dto.DagConfigComplexDTO;
import cn.sliew.carp.framework.dag.service.dto.DagConfigStepDTO;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.Map;
import java.util.Date;

@Data
public class WorkflowDefinitionDTO extends BaseDTO {
public class WorkflowTaskInstance extends BaseDTO {

@Schema(description = "workflow type")
private WorkflowType type;
private Long dagInstanceId;

@Schema(description = "workflow name")
private String name;
@Schema(description = "步骤")
private WorkflowTaskDefinition definition;

@Schema(description = "workflow param")
private Map<String, Object> param;
private String uuid;

@Schema(description = "备注")
private String remark;
private JsonNode inputs;

@Schema(description = "schedule")
private WorkflowScheduleDTO schedule;
private JsonNode outputs;

private String status;

private Date startTime;

private Date endTime;

@Schema(description = "dag")
private DagConfigComplexDTO dag;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.sliew.carp.module.workflow.api.manager;

public interface WorkflowInstanceManager {

void deploy(Long workflowDefinitionId);

void shutdown(Long id);

void suspend(Long id);

void resume(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.sliew.carp.module.workflow.api.manager;

public interface WorkflowTaskInstanceManager {

void deploy(Long id);

void shutdown(Long id);

void suspend(Long id);

void resume(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import cn.sliew.carp.framework.dag.service.dto.DagConfigComplexDTO;
import cn.sliew.carp.framework.dag.service.dto.DagConfigStepDTO;
import cn.sliew.carp.framework.dag.service.dto.DagInstanceComplexDTO;
import cn.sliew.carp.framework.dag.x6.graph.DagGraphVO;

public interface WorkflowDagService {
Expand All @@ -34,4 +35,5 @@ public interface WorkflowDagService {

void update(Long dagId, DagGraphVO graph);

DagInstanceComplexDTO getDagInstance(Long dagInstanceId);
}
Loading

0 comments on commit 1576b6e

Please sign in to comment.