forked from zstackio/zstack
-
Notifications
You must be signed in to change notification settings - Fork 0
<feature>[thread]: support coalesce queue for batch dhcp (ZSTAC-83039) #3447
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
zstack-robot-1
wants to merge
1
commit into
5.5.12
Choose a base branch
from
sync/jin.ma/fix/ZSTAC-83039
base: 5.5.12
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
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); | ||
| } | ||
| }; | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -18,8 +18,7 @@ | |
| import org.zstack.core.db.SQL; | ||
| import org.zstack.core.defer.Defer; | ||
| import org.zstack.core.defer.Deferred; | ||
| import org.zstack.core.thread.SyncTask; | ||
| import org.zstack.core.thread.ThreadFacade; | ||
| import org.zstack.core.thread.*; | ||
| import org.zstack.core.upgrade.GrayVersion; | ||
| import org.zstack.core.workflow.SimpleFlowChain; | ||
| import org.zstack.header.AbstractService; | ||
|
|
@@ -121,6 +120,54 @@ public class FlatDhcpBackend extends AbstractService implements NetworkServiceDh | |
|
|
||
| private Map<String, L3NetworkGetIpStatisticExtensionPoint> getIpStatisticExts = new HashMap<>(); | ||
|
|
||
| /** | ||
| * Request wrapper for DHCP apply coalescing. | ||
| */ | ||
| private static class DhcpApplyRequest { | ||
| final String hostUuid; | ||
| final List<DhcpInfo> dhcpInfos; | ||
| final boolean rebuild; | ||
|
|
||
| DhcpApplyRequest(String hostUuid, List<DhcpInfo> dhcpInfos, boolean rebuild) { | ||
| this.hostUuid = hostUuid; | ||
| this.dhcpInfos = dhcpInfos; | ||
| this.rebuild = rebuild; | ||
| } | ||
| } | ||
|
Comment on lines
+126
to
+136
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. DhcpApplyRequest 应对 dhcpInfos 进行防御性复制
🛡️ 建议的修复 private static class DhcpApplyRequest {
final String hostUuid;
final List<DhcpInfo> dhcpInfos;
final boolean rebuild;
DhcpApplyRequest(String hostUuid, List<DhcpInfo> dhcpInfos, boolean rebuild) {
this.hostUuid = hostUuid;
- this.dhcpInfos = dhcpInfos;
+ this.dhcpInfos = new ArrayList<>(dhcpInfos);
this.rebuild = rebuild;
}
}🤖 Prompt for AI Agents |
||
|
|
||
| private class DhcpApplyQueue extends CoalesceQueue<DhcpApplyRequest> { | ||
| @Override | ||
| protected String getName() { | ||
| return "flat-dhcp-apply"; | ||
| } | ||
|
|
||
| @Override | ||
| protected void executeBatch(List<DhcpApplyRequest> requests, Completion completion) { | ||
| if (requests.isEmpty()) { | ||
| completion.success(); | ||
| return; | ||
| } | ||
|
|
||
| // All requests in the same batch have the same hostUuid | ||
| String hostUuid = requests.get(0).hostUuid; | ||
|
|
||
| // Merge all DhcpInfo from all requests, grouped by L3 network | ||
| // TODO: unify DHCP apply logic and switch to merged/batch flow everywhere | ||
| boolean anyRebuild = false; | ||
| List<DhcpInfo> mergedInfos = new ArrayList<>(); | ||
| for (DhcpApplyRequest req : requests) { | ||
| anyRebuild = anyRebuild || req.rebuild; | ||
| mergedInfos.addAll(req.dhcpInfos); | ||
| } | ||
|
|
||
| logger.debug(String.format("Coalesced %d DHCP apply requests for host[uuid:%s]", requests.size(), hostUuid)); | ||
|
|
||
| applyDhcpToHosts(mergedInfos, hostUuid, anyRebuild, completion); | ||
| } | ||
| } | ||
|
|
||
| private final DhcpApplyQueue dhcpApplyCoalesceQueue = new DhcpApplyQueue(); | ||
|
|
||
| public static final String APPLY_DHCP_PATH = "/flatnetworkprovider/dhcp/apply"; | ||
| public static final String BATCH_APPLY_DHCP_PATH = "/flatnetworkprovider/dhcp/batchApply"; | ||
| public static final String PREPARE_DHCP_PATH = "/flatnetworkprovider/dhcp/prepare"; | ||
|
|
@@ -2074,7 +2121,10 @@ public void applyDhcpService(List<DhcpStruct> dhcpStructList, VmInstanceSpec spe | |
| return; | ||
| } | ||
|
|
||
| applyDhcpToHosts(toDhcpInfo(dhcpStructList), spec.getDestHost().getUuid(), false, completion); | ||
| String hostUuid = spec.getDestHost().getUuid(); | ||
| DhcpApplyRequest request = new DhcpApplyRequest(hostUuid, toDhcpInfo(dhcpStructList), false); | ||
| // Use coalesce queue: requests to the same host will be merged into a single batch | ||
| dhcpApplyCoalesceQueue.submit(hostUuid, request, completion); | ||
| } | ||
|
|
||
| private void releaseDhcpService(List<DhcpInfo> info, final String vmUuid, final String hostUuid, final NoErrorCompletion completion) { | ||
|
|
||
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.
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: 44
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 232
🏁 Script executed:
# Get an overview of the file structure and key methods wc -l core/src/main/java/org/zstack/core/thread/AbstractCoalesceQueue.javaRepository: MatheMatrix/zstack
Length of output: 134
🏁 Script executed:
# Read the file to understand the class structure, focusing on cleanup, doSubmit, and synchronization cat -n core/src/main/java/org/zstack/core/thread/AbstractCoalesceQueue.javaRepository: MatheMatrix/zstack
Length of output: 8011
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 757
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 232
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 614
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 232
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 1794
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 394
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 1171
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 44
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 232
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 194
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 44
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 1729
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 1999
cleanup() 中存在竞态条件导致请求丢失
在
computeIfPresent的 lambda 中,queue.isEmpty()和返回 null 之间存在竞态窗口。另一个线程可能在doSubmit中获取到相同的队列对象并调用queue.add(),之后该队列被 cleanup() 从 signatureQueues 中移除,导致新添加的请求永久丢失。具体场景:
queue.takeAll()清空队列并处理请求cleanup(syncSignature),lambda 执行queue.isEmpty()返回 truedoSubmit(同一 syncSignature, request),通过computeIfAbsent()获取已存在的队列对象,并执行queue.add()建议通过以下方式修复:在 SignatureQueue 中提供原子性的 "takeAllAndCheckEmpty" 操作,确保 isEmpty() 检查和队列移除在同步块内进行;或在 cleanup() 中加入显式的同步机制,协调 computeIfPresent 的执行与并发的 doSubmit() 调用。
🤖 Prompt for AI Agents