forked from zstackio/zstack
-
Notifications
You must be signed in to change notification settings - Fork 0
[ZSTAC-86445] Backport ZSTAC-85281 to 5.4.10 #4380
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
4
commits into
5.4.10
Choose a base branch
from
sync/shixin.ruan/shixin-ZSTAC-86445
base: 5.4.10
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
4 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
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
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
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
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 |
|---|---|---|
|
|
@@ -2,9 +2,8 @@ | |
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.zstack.core.db.Q; | ||
| import org.zstack.core.thread.PeriodicTask; | ||
| import org.zstack.core.thread.Task; | ||
| import org.zstack.core.thread.ThreadFacade; | ||
| import org.zstack.header.managementnode.ManagementNodeReadyExtensionPoint; | ||
| import org.zstack.header.network.l2.L2NetworkVO; | ||
| import org.zstack.header.network.l2.L2NetworkVO_; | ||
| import org.zstack.header.vm.VmNicVO; | ||
|
|
@@ -19,39 +18,57 @@ | |
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.concurrent.Future; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| public class TfZstackPortSync implements ManagementNodeReadyExtensionPoint { | ||
| public class TfZstackPortSync { | ||
| private static final long SYNC_INTERVAL_MILLIS = TimeUnit.DAYS.toMillis(1); | ||
| private static final int MAX_DELETE_COUNT = 10; | ||
|
|
||
| @Autowired | ||
| protected ThreadFacade thdf; | ||
| private Future<Void> trackerThread = null; | ||
| @Autowired | ||
| private TfPortService tfPortService; | ||
| private final static CLogger logger = Utils.getLogger(TfZstackPortSync.class); | ||
| private final List<String> excludeTypes = new ArrayList<String>(Arrays.asList("neutron:LOADBALANCER", "VIP", "BMS")); | ||
| private final Map<String, Long> lastSyncTime = new ConcurrentHashMap<>(); | ||
| private final Set<String> runningSyncs = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>()); | ||
|
|
||
| @Override | ||
| public void managementNodeReady() { | ||
| if (trackerThread != null) { | ||
| trackerThread.cancel(true); | ||
| public void triggerSyncIfDue(String sdnControllerUuid) { | ||
| if (sdnControllerUuid == null) { | ||
| logger.warn("Port_Sync_Task: skip sync because sdn controller uuid is null."); | ||
| return; | ||
| } | ||
| trackerThread = thdf.submitPeriodicTask(new SyncPort()); | ||
| } | ||
|
|
||
| private class SyncPort implements PeriodicTask { | ||
| long now = System.currentTimeMillis(); | ||
| Long lastSync = lastSyncTime.get(sdnControllerUuid); | ||
| if (lastSync != null && now - lastSync < SYNC_INTERVAL_MILLIS) { | ||
| return; | ||
| } | ||
| if (!runningSyncs.add(sdnControllerUuid)) { | ||
| return; | ||
| } | ||
|
|
||
| @Override | ||
| public TimeUnit getTimeUnit() { | ||
| return TimeUnit.DAYS; | ||
| lastSyncTime.put(sdnControllerUuid, now); | ||
| try { | ||
| thdf.submit(new SyncPort(sdnControllerUuid)); | ||
| } catch (RuntimeException e) { | ||
| lastSyncTime.remove(sdnControllerUuid); | ||
| runningSyncs.remove(sdnControllerUuid); | ||
| throw e; | ||
| } | ||
|
Comment on lines
+42
to
64
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. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win 修正同步节流状态的竞态和失败标记。 当前 建议调整- public void triggerSyncIfDue(String sdnControllerUuid) {
+ public synchronized void triggerSyncIfDue(String sdnControllerUuid) {
if (sdnControllerUuid == null) {
logger.warn("Port_Sync_Task: skip sync because sdn controller uuid is null.");
return;
}
@@
if (!runningSyncs.add(sdnControllerUuid)) {
return;
}
- lastSyncTime.put(sdnControllerUuid, now);
try {
thdf.submit(new SyncPort(sdnControllerUuid));
} catch (RuntimeException e) {
- lastSyncTime.remove(sdnControllerUuid);
runningSyncs.remove(sdnControllerUuid);
throw e;
}
@@
} catch (Exception e) {
logger.error(String.format("Port_Sync_Task: Fetch tf VirtualMachineInterface failed: %s.", e));
- return new HashSet<>();
+ throw new RuntimeException("Fetch tf VirtualMachineInterface failed", e);
}
@@
public Void call() {
logger.info("Port_Sync_Task: begin.");
+ boolean syncSucceeded = false;
try {
HashSet<String> portsToDelete = getPortToDelete();
int maxDeleteCount = MAX_DELETE_COUNT;
for (String portUuid: portsToDelete) {
@@
break;
}
}
+ syncSucceeded = true;
} catch (Exception e) {
logger.error(String.format("Port_Sync_Task failed: %s.", e));
} finally {
+ if (syncSucceeded) {
+ lastSyncTime.put(sdnControllerUuid, System.currentTimeMillis());
+ }
runningSyncs.remove(sdnControllerUuid);
}
return null;
}Also applies to: 104-139 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| @Override | ||
| public long getInterval() { | ||
| return 1; | ||
| private class SyncPort implements Task<Void> { | ||
| private final String sdnControllerUuid; | ||
|
|
||
| private SyncPort(String sdnControllerUuid) { | ||
| this.sdnControllerUuid = sdnControllerUuid; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -64,7 +81,10 @@ private HashSet<String> getPortToDelete() { | |
| List<String> zstackL2NetworksUuid = Q.New(L2NetworkVO.class).select(L2NetworkVO_.uuid).listValues(); | ||
| List<String> tfPortsUuid = new ArrayList<>(); | ||
| try{ | ||
| List<VirtualMachineInterface> tfPorts = tfPortService.getTfPortsDetail(); | ||
| List<VirtualMachineInterface> tfPorts = tfPortService.getTfPortsDetail(sdnControllerUuid); | ||
| if (tfPorts == null) { | ||
| return new HashSet<>(); | ||
| } | ||
| for (VirtualMachineInterface vmi : tfPorts) { | ||
| // skip port if it's network not in zstack | ||
| List<ObjectReference<ApiPropertyBase>> tfNetworks = vmi.getVirtualNetwork(); | ||
|
|
@@ -83,7 +103,7 @@ private HashSet<String> getPortToDelete() { | |
| } | ||
| } catch (Exception e) { | ||
| logger.error(String.format("Port_Sync_Task: Fetch tf VirtualMachineInterface failed: %s.", e)); | ||
| return null; | ||
| return new HashSet<>(); | ||
| } | ||
| HashSet<String> result = new HashSet<>(tfPortsUuid); | ||
| result.removeAll(zstackPortsUuid); | ||
|
|
@@ -92,13 +112,13 @@ private HashSet<String> getPortToDelete() { | |
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| public Void call() { | ||
| logger.info("Port_Sync_Task: begin."); | ||
| try { | ||
| HashSet<String> portsToDelete = getPortToDelete(); | ||
| int maxDeleteCount = 10; | ||
| int maxDeleteCount = MAX_DELETE_COUNT; | ||
| for (String portUuid: portsToDelete) { | ||
| TfPortResponse response = tfPortService.deleteTfPort(portUuid); | ||
| TfPortResponse response = tfPortService.deleteTfPort(sdnControllerUuid, portUuid); | ||
| if (response.getCode() == 200) { | ||
| logger.info(String.format("Port_Sync_Task: VirtualMachineInterface: %s delete success.", | ||
| portUuid)); | ||
|
|
@@ -113,7 +133,10 @@ public void run() { | |
| } | ||
| } catch (Exception e) { | ||
| logger.error(String.format("Port_Sync_Task failed: %s.", e)); | ||
| } finally { | ||
| runningSyncs.remove(sdnControllerUuid); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
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
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.