-
Notifications
You must be signed in to change notification settings - Fork 0
fix(portForwarding): serialize concurrent PF rule creation per VIP to prevent duplicates #3345
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: 5.5.6
Are you sure you want to change the base?
Changes from all commits
6b4521b
3b5bda3
80df074
a84a36e
24d4f3b
f563992
6545350
76490a5
461e8a2
32e1e94
addec8c
be53c72
e1dee9f
3bd062b
252bdc7
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -682,10 +682,34 @@ public void fail(ErrorCode errorCode) { | |||||||||
| private void handle(APICreatePortForwardingRuleMsg msg) { | ||||||||||
| final APICreatePortForwardingRuleEvent evt = new APICreatePortForwardingRuleEvent(msg.getId()); | ||||||||||
|
|
||||||||||
| int vipPortEnd = msg.getVipPortEnd() == null ? msg.getVipPortStart() : msg.getVipPortEnd(); | ||||||||||
| int privatePortEnd = msg.getPrivatePortEnd() == null ? msg.getPrivatePortStart() : msg.getPrivatePortEnd(); | ||||||||||
| thdf.chainSubmit(new ChainTask(msg) { | ||||||||||
| @Override | ||||||||||
| public String getSyncSignature() { | ||||||||||
| return String.format("portforwardingrule-vip-%s", msg.getVipUuid()); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| VipVO vip = dbf.findByUuid(msg.getVipUuid(), VipVO.class); | ||||||||||
| @Override | ||||||||||
| public void run(SyncTaskChain chain) { | ||||||||||
| int vipPortEnd = msg.getVipPortEnd() == null ? msg.getVipPortStart() : msg.getVipPortEnd(); | ||||||||||
| int privatePortEnd = msg.getPrivatePortEnd() == null ? msg.getPrivatePortStart() : msg.getPrivatePortEnd(); | ||||||||||
|
|
||||||||||
| // re-check VIP port overlap under sync to prevent concurrent duplicate rules | ||||||||||
| boolean overlap = Q.New(PortForwardingRuleVO.class) | ||||||||||
| .eq(PortForwardingRuleVO_.vipUuid, msg.getVipUuid()) | ||||||||||
| .eq(PortForwardingRuleVO_.protocolType, PortForwardingProtocolType.valueOf(msg.getProtocolType())) | ||||||||||
| .lte(PortForwardingRuleVO_.vipPortStart, vipPortEnd) | ||||||||||
| .gte(PortForwardingRuleVO_.vipPortEnd, msg.getVipPortStart()) | ||||||||||
| .isExists(); | ||||||||||
| if (overlap) { | ||||||||||
| evt.setError(operr(ORG_ZSTACK_NETWORK_SERVICE_PORTFORWARDING_10017, | ||||||||||
| "vip port range[vipStartPort:%s, vipEndPort:%s] overlaps with an existing port forwarding rule on vip[uuid:%s]", | ||||||||||
| msg.getVipPortStart(), vipPortEnd, msg.getVipUuid())); | ||||||||||
| bus.publish(evt); | ||||||||||
| chain.next(); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| VipVO vip = dbf.findByUuid(msg.getVipUuid(), VipVO.class); | ||||||||||
| final PortForwardingRuleVO vo = new PortForwardingRuleVO(); | ||||||||||
| if (msg.getResourceUuid() != null) { | ||||||||||
| vo.setUuid(msg.getResourceUuid()); | ||||||||||
|
|
@@ -713,8 +737,8 @@ protected void scripts() { | |||||||||
| } | ||||||||||
| }.execute(); | ||||||||||
|
|
||||||||||
| FlowChain chain = FlowChainBuilder.newShareFlowChain(); | ||||||||||
| chain.setName("create-portforwading"); | ||||||||||
| FlowChain flowChain = FlowChainBuilder.newShareFlowChain(); | ||||||||||
| flowChain.setName("create-portforwading"); | ||||||||||
|
Comment on lines
+740
to
+741
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. FlowChain 名称存在拼写错误。
🐛 建议修复 FlowChain flowChain = FlowChainBuilder.newShareFlowChain();
-flowChain.setName("create-portforwading");
+flowChain.setName("create-portforwarding");📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
| VipInventory vipInventory = VipInventory.valueOf(vip); | ||||||||||
| if (msg.getVmNicUuid() == null) { | ||||||||||
| ModifyVipAttributesStruct struct = new ModifyVipAttributesStruct(); | ||||||||||
|
|
@@ -727,13 +751,15 @@ protected void scripts() { | |||||||||
| public void success() { | ||||||||||
| evt.setInventory(PortForwardingRuleInventory.valueOf(vo)); | ||||||||||
| bus.publish(evt); | ||||||||||
| chain.next(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Override | ||||||||||
| public void fail(ErrorCode errorCode) { | ||||||||||
| dbf.remove(vo); | ||||||||||
| evt.setError(errorCode); | ||||||||||
| bus.publish(evt); | ||||||||||
| chain.next(); | ||||||||||
| } | ||||||||||
| }); | ||||||||||
|
|
||||||||||
|
|
@@ -757,20 +783,22 @@ public void fail(ErrorCode errorCode) { | |||||||||
| public void success() { | ||||||||||
| evt.setInventory(PortForwardingRuleInventory.valueOf(vo)); | ||||||||||
| bus.publish(evt); | ||||||||||
| chain.next(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Override | ||||||||||
| public void fail(ErrorCode errorCode) { | ||||||||||
| dbf.remove(vo); | ||||||||||
| evt.setError(errorCode); | ||||||||||
| bus.publish(evt); | ||||||||||
| chain.next(); | ||||||||||
| } | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| chain.then(new ShareFlow() { | ||||||||||
| flowChain.then(new ShareFlow() { | ||||||||||
| @Override | ||||||||||
| public void setup() { | ||||||||||
| vo.setVmNicUuid(vmNic.getUuid()); | ||||||||||
|
|
@@ -853,20 +881,29 @@ public void fail(ErrorCode errorCode) { | |||||||||
| }); | ||||||||||
|
|
||||||||||
|
|
||||||||||
| chain.done(new FlowDoneHandler(msg) { | ||||||||||
| flowChain.done(new FlowDoneHandler(msg) { | ||||||||||
| @Override | ||||||||||
| public void handle(Map data) { | ||||||||||
| evt.setInventory(PortForwardingRuleInventory.valueOf(dbf.reload(vo))); | ||||||||||
| bus.publish(evt); | ||||||||||
| chain.next(); | ||||||||||
| } | ||||||||||
| }).error(new FlowErrorHandler(msg) { | ||||||||||
| @Override | ||||||||||
| public void handle(ErrorCode errCode, Map data) { | ||||||||||
| dbf.remove(vo); | ||||||||||
| evt.setError(errCode); | ||||||||||
| bus.publish(evt); | ||||||||||
| chain.next(); | ||||||||||
| } | ||||||||||
| }).start(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Override | ||||||||||
| public String getName() { | ||||||||||
| return String.format("api-create-portforwardingrule-vip-%s", msg.getVipUuid()); | ||||||||||
| } | ||||||||||
| }); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private void populateExtensions() { | ||||||||||
|
|
||||||||||
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.
IP 版本不匹配可能导致异常
当前代码在比较 free IP 与 reserved IP range 时,未按 IP 版本过滤。若 free IP 是 IPv4 而 reserved range 是 IPv6(或相反),
NetworkUtils.isInRange内部会调用错误的转换方法(如ipv4StringToLong处理 IPv6 地址),可能抛出异常。参考同文件第 486-487 行的已有模式,应先按 IP 版本过滤 reserved ranges:
🔧 建议修复
Set<ReservedIpRangeVO> reservedIpRanges = self.getReservedIpRanges(); if (reservedIpRanges != null && !reservedIpRanges.isEmpty()) { - freeIpInventorys.removeIf(freeIp -> reservedIpRanges.stream().anyMatch( - r -> NetworkUtils.isInRange(freeIp.getIp(), r.getStartIp(), r.getEndIp()))); + freeIpInventorys.removeIf(freeIp -> reservedIpRanges.stream() + .filter(r -> r.getIpVersion() == freeIp.getIpVersion()) + .anyMatch(r -> NetworkUtils.isInRange(freeIp.getIp(), r.getStartIp(), r.getEndIp()))); }🤖 Prompt for AI Agents