forked from zstackio/zstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Batch prepare dhcp server and apply dhcp info #3310
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
Open
MatheMatrix
wants to merge
2
commits into
3.10.33
Choose a base branch
from
sync/jin.ma/support-batch-dhcp-310@@3
base: 3.10.33
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
core/src/main/java/org/zstack/core/thread/AbstractCoalesceQueue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| package org.zstack.core.thread; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowire; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Configurable; | ||
| import org.zstack.header.core.AbstractCompletion; | ||
| import org.zstack.header.core.Completion; | ||
| import org.zstack.header.core.ReturnValueCompletion; | ||
| import org.zstack.header.errorcode.ErrorCode; | ||
| import org.zstack.utils.Utils; | ||
| import org.zstack.utils.logging.CLogger; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Base implementation for coalesce queues. | ||
| * | ||
| * @param <T> Request Item Type | ||
| * @param <R> Batch Execution Result Type | ||
| * @param <V> Single Request Result Type | ||
| */ | ||
| @Configurable(preConstruction = true, autowire = Autowire.BY_TYPE) | ||
| public abstract class AbstractCoalesceQueue<T, R, V> { | ||
| private static final CLogger logger = Utils.getLogger(AbstractCoalesceQueue.class); | ||
|
|
||
| @Autowired | ||
| private ThreadFacade thdf; | ||
|
|
||
| private final ConcurrentHashMap<String, SignatureQueue> signatureQueues = new ConcurrentHashMap<>(); | ||
|
|
||
| protected class PendingRequest { | ||
| final T item; | ||
| final AbstractCompletion completion; | ||
|
|
||
| PendingRequest(T item, AbstractCompletion completion) { | ||
| this.item = item; | ||
| this.completion = completion; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| void notifySuccess(V result) { | ||
| if (completion == null) { | ||
| return; | ||
| } | ||
|
|
||
| if (completion instanceof ReturnValueCompletion) { | ||
| ((ReturnValueCompletion<V>) completion).success(result); | ||
| } else if (completion instanceof Completion) { | ||
| ((Completion) completion).success(); | ||
| } | ||
| } | ||
|
|
||
| void notifyFailure(ErrorCode errorCode) { | ||
| if (completion == null) { | ||
| return; | ||
| } | ||
|
|
||
| if (completion instanceof ReturnValueCompletion) { | ||
| ((ReturnValueCompletion<V>) completion).fail(errorCode); | ||
| } else if (completion instanceof Completion) { | ||
| ((Completion) completion).fail(errorCode); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private class SignatureQueue { | ||
| final String syncSignature; | ||
| List<PendingRequest> pendingList = Collections.synchronizedList(new ArrayList<>()); | ||
|
|
||
| SignatureQueue(String syncSignature) { | ||
| this.syncSignature = syncSignature; | ||
| } | ||
|
|
||
| synchronized List<PendingRequest> takeAll() { | ||
| List<PendingRequest> toProcess = pendingList; | ||
| pendingList = Collections.synchronizedList(new ArrayList<>()); | ||
| return toProcess; | ||
| } | ||
|
|
||
| synchronized void add(PendingRequest request) { | ||
| pendingList.add(request); | ||
| } | ||
|
|
||
| synchronized boolean isEmpty() { | ||
| return pendingList.isEmpty(); | ||
| } | ||
| } | ||
|
|
||
| protected abstract String getName(); | ||
|
|
||
| // Changed to take AbstractCompletion, subclasses cast it to specific type | ||
| protected abstract void executeBatch(List<T> items, AbstractCompletion completion); | ||
|
|
||
| protected abstract AbstractCompletion createBatchCompletion(String syncSignature, List<PendingRequest> requests, SyncTaskChain chain); | ||
|
|
||
| protected abstract V calculateResult(T item, R batchResult); | ||
|
|
||
| protected final void handleSuccess(String syncSignature, List<PendingRequest> requests, R batchResult, SyncTaskChain chain) { | ||
| for (PendingRequest req : requests) { | ||
| try { | ||
| V singleResult = calculateResult(req.item, batchResult); | ||
| req.notifySuccess(singleResult); | ||
| } catch (Throwable t) { | ||
| logger.warn(String.format("[%s] failed to calculate result for item %s", getName(), req.item), t); | ||
| req.notifyFailure(org.zstack.core.Platform.operr("failed to calculate result: %s", t.getMessage())); | ||
| } | ||
| } | ||
| cleanup(syncSignature); | ||
| chain.next(); | ||
| } | ||
|
|
||
| protected final void handleFailure(String syncSignature, List<PendingRequest> requests, ErrorCode errorCode, SyncTaskChain chain) { | ||
| for (PendingRequest req : requests) { | ||
| req.notifyFailure(errorCode); | ||
| } | ||
| cleanup(syncSignature); | ||
| chain.next(); | ||
| } | ||
|
|
||
| void setThreadFacade(ThreadFacade thdf) { | ||
| this.thdf = thdf; | ||
| } | ||
|
|
||
| protected final void submitRequest(String syncSignature, T item, AbstractCompletion completion) { | ||
| doSubmit(syncSignature, new PendingRequest(item, completion)); | ||
| } | ||
|
|
||
| private void doSubmit(String syncSignature, PendingRequest request) { | ||
| SignatureQueue queue = signatureQueues.computeIfAbsent(syncSignature, SignatureQueue::new); | ||
| queue.add(request); | ||
|
|
||
| thdf.chainSubmit(new ChainTask(null) { | ||
| @Override | ||
| public String getSyncSignature() { | ||
| return String.format("coalesce-queue-%s-%s", AbstractCoalesceQueue.this.getName(), syncSignature); | ||
| } | ||
|
|
||
| @Override | ||
| public void run(SyncTaskChain chain) { | ||
| List<PendingRequest> requests = queue.takeAll(); | ||
|
|
||
| if (requests.isEmpty()) { | ||
| chain.next(); | ||
| return; | ||
| } | ||
|
|
||
| String name = getName(); | ||
| logger.debug(String.format("[%s] coalescing %d requests for signature[%s]", | ||
| name, requests.size(), syncSignature)); | ||
|
|
||
|
|
||
| // Create the specific completion type (Completion or ReturnValueCompletion) | ||
| AbstractCompletion batchCompletion = createBatchCompletion(syncSignature, requests, chain); | ||
|
|
||
| // Execute batch with the direct completion object | ||
| List<T> items = requests.stream().map(req -> req.item).collect(Collectors.toList()); | ||
| executeBatch(items, batchCompletion); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return String.format("%s-coalesced-batch-%s", AbstractCoalesceQueue.this.getName(), syncSignature); | ||
| } | ||
|
|
||
| @Override | ||
| protected int getSyncLevel() { | ||
| return 1; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private void cleanup(String syncSignature) { | ||
| signatureQueues.computeIfPresent(syncSignature, (k, queue) -> { | ||
| if (queue.isEmpty()) { | ||
| return null; | ||
| } | ||
| return queue; | ||
| }); | ||
| } | ||
|
|
||
| // For testing | ||
| int getActiveQueueCount() { | ||
| return signatureQueues.size(); | ||
| } | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
core/src/main/java/org/zstack/core/thread/CoalesceQueue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package org.zstack.core.thread; | ||
|
|
||
| import org.zstack.header.core.AbstractCompletion; | ||
| import org.zstack.header.core.Completion; | ||
| import org.zstack.header.errorcode.ErrorCode; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * A coalesce queue for requests that do NOT expect a return value. | ||
| * | ||
| * @param <T> Request Item Type | ||
| */ | ||
| public abstract class CoalesceQueue<T> extends AbstractCoalesceQueue<T, Void, Void> { | ||
|
|
||
| /** | ||
| * Submit a request. | ||
| * | ||
| * @param syncSignature the sync signature; requests with the same signature will be coalesced | ||
| * @param item the request item | ||
| * @param completion the completion callback | ||
| */ | ||
| public void submit(String syncSignature, T item, Completion completion) { | ||
| submitRequest(syncSignature, item, completion); | ||
| } | ||
|
|
||
| /** | ||
| * Executes the batched requests. | ||
| * <p> | ||
| * Subclasses must implement this method to process the coalesced items. | ||
| * | ||
| * @param items the list of coalesced request items | ||
| * @param completion the completion callback for the batch execution | ||
| */ | ||
| protected abstract void executeBatch(List<T> items, Completion completion); | ||
|
|
||
| @Override | ||
| protected final void executeBatch(List<T> items, AbstractCompletion batchCompletion) { | ||
| executeBatch(items, (Completion) batchCompletion); | ||
| } | ||
|
|
||
| @Override | ||
| protected final AbstractCompletion createBatchCompletion(String syncSignature, List<PendingRequest> requests, SyncTaskChain chain) { | ||
| return new Completion(chain) { | ||
| @Override | ||
| public void success() { | ||
| handleSuccess(syncSignature, requests, null, chain); | ||
| } | ||
|
|
||
| @Override | ||
| public void fail(ErrorCode errorCode) { | ||
| handleFailure(syncSignature, requests, errorCode, chain); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| protected final Void calculateResult(T item, Void batchResult) { | ||
| return null; | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
core/src/main/java/org/zstack/core/thread/ReturnValueCoalesceQueue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package org.zstack.core.thread; | ||
|
|
||
| import org.zstack.header.core.AbstractCompletion; | ||
| import org.zstack.header.core.ReturnValueCompletion; | ||
| import org.zstack.header.errorcode.ErrorCode; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * A coalesce queue for requests that expect a return value. | ||
| * | ||
| * @param <T> Request Item Type | ||
| * @param <R> Batch Execution Result Type | ||
| * @param <V> Single Request Result Type | ||
| */ | ||
| public abstract class ReturnValueCoalesceQueue<T, R, V> extends AbstractCoalesceQueue<T, R, V> { | ||
|
|
||
| public void submit(String syncSignature, T item, ReturnValueCompletion<V> completion) { | ||
| submitRequest(syncSignature, item, completion); | ||
| } | ||
|
|
||
| protected abstract void executeBatch(List<T> items, ReturnValueCompletion<R> completion); | ||
|
|
||
| @Override | ||
| protected final void executeBatch(List<T> items, AbstractCompletion batchCompletion) { | ||
| executeBatch(items, (ReturnValueCompletion<R>) batchCompletion); | ||
| } | ||
|
|
||
| @Override | ||
| protected final AbstractCompletion createBatchCompletion(String syncSignature, List<PendingRequest> requests, SyncTaskChain chain) { | ||
| return new ReturnValueCompletion<R>(null) { | ||
| @Override | ||
| public void success(R batchResult) { | ||
| handleSuccess(syncSignature, requests, batchResult, chain); | ||
| } | ||
|
|
||
| @Override | ||
| public void fail(ErrorCode errorCode) { | ||
| handleFailure(syncSignature, requests, errorCode, chain); | ||
| } | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
🧩 Analysis chain
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 132
🏁 Script executed:
# Look for the full AbstractCoalesceQueue class to understand executeBatch cat core/src/main/java/org/zstack/core/thread/AbstractCoalesceQueue.javaRepository: MatheMatrix/zstack
Length of output: 6688
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 15493
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 10940
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 1670
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 123
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 5545
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 10714
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 4498
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 263
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 120
🏁 Script executed:
# Look at ChainTask implementation and how it handles exceptions cat core/src/main/java/org/zstack/core/thread/ChainTask.javaRepository: MatheMatrix/zstack
Length of output: 1309
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 44
🏁 Script executed:
# Check test file to see what exception scenarios are tested head -300 test/src/test/java/org/zstack/test/core/thread/TestCoalesceQueue.javaRepository: MatheMatrix/zstack
Length of output: 11639
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 13298
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 14184
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 44
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 191
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 6101
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 124
🏁 Script executed:
# Get the full DispatchQueueImpl to see the doChainSyncSubmit implementation cat core/src/main/java/org/zstack/core/thread/DispatchQueueImpl.javaRepository: MatheMatrix/zstack
Length of output: 21663
🏁 Script executed:
# Also check SyncTaskChain interface cat core/src/main/java/org/zstack/core/thread/SyncTaskChain.javaRepository: MatheMatrix/zstack
Length of output: 210
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 2913
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 1019
executeBatch 同步异常会导致批次请求永久挂起
如果 executeBatch 同步抛异常,当前批次的 completion 回调不会被触发,pending 请求永远不会收到成功或失败通知,导致请求长期挂起且队列不会清理。虽然 chain.next() 最终会被框架的 ChainFuture 异常处理机制调用,但 batchCompletion 的成功/失败路径被跳过,cleanup() 也无法执行。建议使用 try-catch 包装 executeBatch,在异常时调用 batchCompletion.fail() 确保统一的失败处理路径:
建议修复
// Execute batch with the direct completion object List<T> items = requests.stream().map(req -> req.item).collect(Collectors.toList()); - executeBatch(items, batchCompletion); + try { + executeBatch(items, batchCompletion); + } catch (Throwable t) { + logger.warn(String.format("[%s] executeBatch failed for signature[%s]", name, syncSignature), t); + batchCompletion.fail(org.zstack.core.Platform.operr("executeBatch failed: %s", t.getMessage())); + }🤖 Prompt for AI Agents
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.
Comment from jin.ma:
为啥会这样呢,我跑了 UT 你说的确实是对的,不应该有 completion aspectj 来确保吗
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.
🧩 Analysis chain
🏁 Script executed:
Length of output: 73
🏁 Script executed:
Length of output: 1006
🏁 Script executed:
Length of output: 3103
🏁 Script executed:
Length of output: 6349
🏁 Script executed:
Length of output: 6555
@ZStack-Robot你提出了一个很好的问题!确实代码库中有 AspectJ completion aspect,但它在这个场景下不起作用,原因如下:
技术原因
我找到了相关的 AspectJ aspect 实现:
Completion、NoErrorCompletion或ReturnValueCompletion类型的方法,如果方法抛异常,会自动调用 completion.fail()问题所在
关键问题:
executeBatch的方法签名使用的是AbstractCompletion而不是具体的Completion或ReturnValueCompletion:AspectJ 的 pointcut 是基于方法签名中声明的参数类型进行匹配的,而不是运行时类型。由于
AbstractCompletion是基类,不是Completion或ReturnValueCompletion,所以 AsyncSafeAspect 的切点不会匹配这个方法,因此不会提供异常保护。解决方案
有几种方式可以解决:
在调用处添加 try-catch(推荐):就像我建议的那样,在 ChainTask.run() 中包装 executeBatch 调用
修改 executeBatch 签名:让子类使用具体的 Completion 类型,但这会破坏当前的抽象设计
扩展 AspectJ 切点:在 AsyncSafeAspect 中添加 AbstractCompletion 的切点支持,但这可能影响其他使用 AbstractCompletion 的代码
从架构角度看,方案1最安全且不影响现有代码,建议采用。
🧠 Learnings used
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.
Comment from jin.ma:
但是 protected abstract void executeBatch(List items, ReturnValueCompletion completion); protected abstract void executeBatch(List items, Completion completion); 这两个方法的签名是有 Completion 的啊
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.
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.
Comment from jin.ma:
@coderabbitai 但是 protected abstract void executeBatch(List items, ReturnValueCompletion completion); protected abstract void executeBatch(List items, Completion completion); 这两个方法的签名是有 Completion 的啊,再解释一下为什么没有生效
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.
🧩 Analysis chain
@MatheMatrix你说得对,我需要重新分析这个问题。让我深入检查一下实际的代码流程。🏁 Script executed:
Length of output: 1184
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 1937
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 4011
🏁 Script executed:
Length of output: 5257
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 2523
🏁 Script executed:
Length of output: 2606
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 1399
🏁 Script executed:
Length of output: 1932
🏁 Script executed:
Length of output: 3492
🏁 Script executed:
Length of output: 90
🏁 Script executed:
Length of output: 130
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 44
🏁 Script executed:
Length of output: 47
🏁 Script executed:
Length of output: 0
@MatheMatrix你说得对,理论上 AspectJ 应该能拦截。但既然你实际跑 UT 确认了问题存在,让我分析一下可能的原因:可能的技术原因
1. 桥接方法的拦截时机问题
调用链是这样的:
问题可能在于:如果异常在类型转换这一行抛出,或者在桥接方法内部抛出(而不是实际的实现方法内部),那这个异常就不在 AsyncSafeAspect 的拦截范围内,因为桥接方法的签名是
executeBatch(List, AbstractCompletion),不匹配execution(* *.*(.., Completion, ..))。2. 测试环境的 AspectJ 编织可能不完整
可能在测试环境中 AspectJ 的编织配置不完整,导致某些场景下拦截失败。
建议的解决方案
最稳妥的做法是添加显式的 try-catch,原因是:
建议的修改保持不变:
这样可以:
你觉得这个分析合理吗?