-
Notifications
You must be signed in to change notification settings - Fork 229
✨ 新功能 实现普通节点的一拖多/多对一的分支并行处理 #600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,7 +49,7 @@ export const variableAggregationNodeState = (id, x, y, width, height, parent, dr | |
| * @override | ||
| */ | ||
| self.maxNumToLink = () => { | ||
| return 10; | ||
| return self.graph?.connectionLimitDisabled ? 100 : 10; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个是聚合连线的数量,当前可以默认为10个,先不要放到100这个量级 |
||
| }; | ||
|
|
||
| return self; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,6 +91,8 @@ public static RunContext from(CreateAppChatRequest request, OperationContext con | |
| .orElseGet(() -> CreateAppChatRequest.Context.builder().build()); | ||
| RunContext runContext = new RunContext(new HashMap<>(), context); | ||
| runContext.putAllToBusiness(requestContext.getUserContext()); | ||
|
|
||
| // runContext请求操作 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 无意义的注释,删除 |
||
| runContext.setUseMemory(requestContext.getUseMemory()); | ||
| runContext.setDimension(requestContext.getDimension()); | ||
| runContext.setChatId(request.getChatId()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ public final class FlowContext<T> extends IdGenerator { | |
| /** | ||
| * 通过from.offer(data)而不是.offer(context)发起的数据会新增一个trace,这个trace会延续到flow end | ||
| */ | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不应该存在空行 |
||
| @Getter | ||
| private final Set<String> traceId; | ||
|
|
||
|
|
@@ -265,6 +266,29 @@ public <R> List<FlowContext<R>> generate(List<R> data, String position) { | |
| return data.stream().map(d -> this.generate(d, position, LocalDateTime.now())).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| /** | ||
| * fork一个新的context用于一拖多分支,继承当前context的运行元数据,但生成新的contextId。 | ||
| * | ||
| * @return 新的分支context | ||
| */ | ||
| public FlowContext<T> fork() { | ||
| return this.convertData(this.data); | ||
| } | ||
|
|
||
| /** | ||
| * convertData | ||
| * | ||
| * @param <R> 转换后的数据类型 | ||
| * @param data 转换后的数据 | ||
| * @return 转换后的context | ||
| */ | ||
| public <R> FlowContext<R> convertData(R data) { | ||
| FlowContext<R> context = this.copyContext(data); | ||
| context.previous = this.id; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 作为fork生成数据的previous不应该是他自己,应该是保持一致的 |
||
| context.nextPositionId = this.nextPositionId; | ||
| return context; | ||
| } | ||
|
|
||
| /** | ||
| * 用于when.convert数据时候的转换context,除了包裹的数据类型不一样,所有其他信息都一样 | ||
| * | ||
|
|
@@ -274,12 +298,17 @@ public <R> List<FlowContext<R>> generate(List<R> data, String position) { | |
| * @return 转换后的context | ||
| */ | ||
| public <R> FlowContext<R> convertData(R data, String id) { | ||
| FlowContext<R> context = this.copyContext(data); | ||
| context.previous = this.previous; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 看下copyContext整体调整后,这里应该不再需要为previous赋值 |
||
| context.id = id; | ||
| return context; | ||
| } | ||
|
|
||
| private <R> FlowContext<R> copyContext(R data) { | ||
| FlowContext<R> context = new FlowContext<>(this.streamId, this.rootId, data, this.traceId, this.position, | ||
| this.parallel, this.parallelMode, LocalDateTime.now()); | ||
| context.previous = this.previous; | ||
| context.status = this.status; | ||
| context.trans = this.trans; | ||
| context.id = id; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 如果是copyContext的含义,不应该删除previous和id的赋值 |
||
| context.batchId = this.batchId; | ||
| context.toBatch = this.toBatch; | ||
| context.createAt = this.createAt; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -175,10 +175,14 @@ public FitStream.Publisher<FlowData> convertToFlow(FlowContextRepo<FlowData> rep | |
| nodeMap.values().forEach((fromNode) -> { | ||
| fromNode.setParentFlow(this); | ||
| Optional.ofNullable(fromNode.getJober()).ifPresent(jober -> jober.setContextRepo(repo)); | ||
|
|
||
| fromNode.getEvents().forEach(event -> { | ||
| // startNode不能出现在event的to属性, endNode不能出现在event的from属性 | ||
|
|
||
| FlowNode toNode = nodeMap.get(event.getTo()); | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| fromNode.subscribe(streamId, flowEnv, toNode, event); | ||
|
|
||
| }); | ||
| }); | ||
| return getFlowNode(FlowNodeType.START).getPublisher(streamId, repo, messenger, locks); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ | |
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.concurrent.locks.Lock; | ||
| import java.util.function.Consumer; | ||
| import java.util.stream.Collectors; | ||
|
|
||
|
|
@@ -322,26 +323,64 @@ public void offer(List<FlowContext<I>> contexts, Consumer<PreSendCallbackInfo<I> | |
| // qualifiedWhens表示的与from节点连接的所有事件,条件节点符合条件的事件在这里筛选,在事件上处理需要下发的context | ||
| java.util.Map<FitStream.Subscription<I, ?>, List<FlowContext<I>>> matchedContexts = new LinkedHashMap<>(); | ||
| Set<FlowContext<I>> matchedContextSet = new HashSet<>(); | ||
| qualifiedWhens.forEach( | ||
| w -> { | ||
| List<FlowContext<I>> afterContexts = contexts | ||
| .stream() | ||
| .filter(c -> w.getWhether().is(c)) | ||
| .peek(c -> c.setNextPositionId(w.getId())) | ||
| .collect(Collectors.toList()); | ||
| matchedContexts.put(w, afterContexts); | ||
| matchedContextSet.addAll(afterContexts); | ||
| List<FlowContext<I>> forkedContexts = new ArrayList<>(); | ||
| for (FlowContext<I> contextItem : contexts) { | ||
| List<FitStream.Subscription<I, ?>> matchedSubscriptions = qualifiedWhens.stream() | ||
| .filter(w -> w.getWhether().is(contextItem)) | ||
| .collect(Collectors.toList()); | ||
| if (CollectionUtils.isEmpty(matchedSubscriptions)) { | ||
| continue; | ||
| } | ||
| matchedContextSet.add(contextItem); | ||
| for (int index = 0; index < matchedSubscriptions.size(); index++) { | ||
| FitStream.Subscription<I, ?> matchedSubscription = matchedSubscriptions.get(index); | ||
| FlowContext<I> branchContext = index == 0 ? contextItem : contextItem.fork(); | ||
| branchContext.setNextPositionId(matchedSubscription.getId()); | ||
| matchedContexts.computeIfAbsent(matchedSubscription, key -> new ArrayList<>()).add(branchContext); | ||
| if (index > 0) { | ||
| forkedContexts.add(branchContext); | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| qualifiedWhens.forEach(w -> matchedContexts.computeIfAbsent(w, key -> new ArrayList<>())); | ||
| List<FlowContext<I>> unMatchedContexts = contexts | ||
| .stream() | ||
| .filter(c -> !matchedContextSet.contains(c)) | ||
| .collect(Collectors.toList()); | ||
| PreSendCallbackInfo<I> callbackInfo = new PreSendCallbackInfo<>(matchedContexts, unMatchedContexts); | ||
| preSendCallback.accept(callbackInfo); | ||
| persistForkedContexts(forkedContexts, matchedContexts); | ||
| matchedContexts.forEach(FitStream.Subscription::cache); | ||
| } | ||
|
|
||
| private void persistForkedContexts(List<FlowContext<I>> forkedContexts, | ||
| java.util.Map<FitStream.Subscription<I, ?>, List<FlowContext<I>>> matchedContexts) { | ||
| if (CollectionUtils.isEmpty(forkedContexts)) { | ||
| return; | ||
| } | ||
| Set<String> forkedIds = forkedContexts.stream().map(FlowContext::getId).collect(Collectors.toSet()); | ||
| List<FlowContext<I>> effectiveForkedContexts = matchedContexts.values() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| .stream() | ||
| .flatMap(List::stream) | ||
| .filter(context -> forkedIds.contains(context.getId())) | ||
| .collect(Collectors.toList()); | ||
| if (CollectionUtils.isEmpty(effectiveForkedContexts)) { | ||
| return; | ||
| } | ||
| Set<String> traces = effectiveForkedContexts.stream() | ||
| .flatMap(context -> context.getTraceId().stream()) | ||
| .collect(Collectors.toSet()); | ||
| Lock lock = this.locks.getDistributedLock(this.locks.streamNodeLockKey(this.streamId, this.id, | ||
| "ForkContextPool")); | ||
| lock.lock(); | ||
| try { | ||
| this.repo.updateContextPool(effectiveForkedContexts, traces); | ||
| this.repo.save(effectiveForkedContexts); | ||
| } finally { | ||
| lock.unlock(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 是否有publisher目标 | ||
| * 用于stream闭环时将没有subscribed的publisher关闭到close subscriber | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个是聚合连线的数量,当前可以默认为10个,先不要放到100这个量级