Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.zstack.compute.vm;

import org.springframework.beans.factory.annotation.Autowired;
import org.zstack.core.cloudbus.CloudBusCallBack;
import org.zstack.core.gc.GC;
import org.zstack.core.gc.GCCompletion;
import org.zstack.core.gc.TimeBasedGarbageCollector;
import org.zstack.core.thread.ChainTask;
import org.zstack.core.thread.SyncTaskChain;
import org.zstack.core.thread.ThreadFacade;
import org.zstack.header.core.progress.ChainInfo;
import org.zstack.header.message.MessageReply;
import org.zstack.header.vm.UpdateVmInstanceMetadataMsg;
import org.zstack.header.volume.VolumeConstant;
import org.zstack.header.volume.VolumeVO;

public class UpdateVmInstanceMetadataGC extends TimeBasedGarbageCollector {
@GC
public String vmInstanceUuid;

@Autowired
protected ThreadFacade thdf;

static public String getUpdateVmInstanceMetadataSyncSignature(String vmInstanceUuid) {
return String.format("update-vm-%s-metadata", vmInstanceUuid);
}

@Override
protected void triggerNow(GCCompletion completion) {
if (!dbf.isExist(vmInstanceUuid, VolumeVO.class)) {
completion.cancel();
return;
}

String queueName = getUpdateVmInstanceMetadataSyncSignature(vmInstanceUuid);
ChainInfo chainInfo = thdf.getChainTaskInfo(queueName);
if (!chainInfo.getPendingTask().isEmpty()) {
completion.cancel();
return;
}

thdf.chainSubmit(new ChainTask(completion) {
@Override
public String getSyncSignature() {
return queueName;
}

@Override
public void run(final SyncTaskChain chain) {
UpdateVmInstanceMetadataMsg msg = new UpdateVmInstanceMetadataMsg();
msg.setUuid(vmInstanceUuid);
bus.makeTargetServiceIdByResourceUuid(msg, VolumeConstant.SERVICE_ID, vmInstanceUuid);
Comment on lines +50 to +52
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

消息路由使用了错误的 SERVICE_ID

UpdateVmInstanceMetadataMsg 实现了 VmInstanceMessage 接口,应路由到 VM 服务,而非 Volume 服务。当前使用 VolumeConstant.SERVICE_ID 会导致消息被发送到错误的服务处理。

🛠️ 建议修改
-import org.zstack.header.volume.VolumeConstant;
-import org.zstack.header.volume.VolumeVO;
+import org.zstack.header.vm.VmInstanceConstant;
+import org.zstack.header.vm.VmInstanceVO;
-                bus.makeTargetServiceIdByResourceUuid(msg, VolumeConstant.SERVICE_ID, vmInstanceUuid);
+                bus.makeTargetServiceIdByResourceUuid(msg, VmInstanceConstant.SERVICE_ID, vmInstanceUuid);
🤖 Prompt for AI Agents
In `@compute/src/main/java/org/zstack/compute/vm/UpdateVmInstanceMetadataGC.java`
around lines 50 - 52, The message routing uses the wrong service id:
UpdateVmInstanceMetadataMsg implements VmInstanceMessage and should be routed to
the VM service, not the volume service. In the call to
bus.makeTargetServiceIdByResourceUuid(msg, VolumeConstant.SERVICE_ID,
vmInstanceUuid) replace VolumeConstant.SERVICE_ID with the VM service constant
(e.g., VmInstanceConstant.SERVICE_ID) so UpdateVmInstanceMetadataMsg is sent to
the VM service using the same vmInstanceUuid.

bus.send(msg, new CloudBusCallBack(completion) {
@Override
public void run(MessageReply reply) {
if (!reply.isSuccess()) {
completion.fail(reply.getError());
} else {
completion.success();
}
}
});
Comment on lines +49 to +62
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

异步回调未释放链,可能阻塞后续任务。
当前回调里没有调用 chain.next(),链会被永久占用,后续相同队列任务无法执行。建议在回调 finally 中释放链。

🛠 建议修改
-                bus.send(msg, new CloudBusCallBack(completion) {
+                bus.send(msg, new CloudBusCallBack(completion) {
                     `@Override`
                     public void run(MessageReply reply) {
-                        if (!reply.isSuccess()) {
-                            completion.fail(reply.getError());
-                        } else {
-                            completion.success();
-                        }
+                        try {
+                            if (!reply.isSuccess()) {
+                                completion.fail(reply.getError());
+                            } else {
+                                completion.success();
+                            }
+                        } finally {
+                            chain.next();
+                        }
                     }
                 });
🤖 Prompt for AI Agents
In `@compute/src/main/java/org/zstack/compute/vm/UpdateVmInstanceMetadataGC.java`
around lines 54 - 67, The callback in UpdateVmInstanceMetadataGC.run sends
UpdateVmInstanceMetadataMsg but never releases the SyncTaskChain, which will
block subsequent tasks; modify the CloudBusCallBack anonymous run(MessageReply
reply) to ensure chain.next() is always invoked in a finally block (after
calling completion.success() or completion.fail(reply.getError())), so the
SyncTaskChain is advanced regardless of reply success or exceptions; locate the
bus.send(...) call and update its callback to call chain.next() in finally.

}

@Override
public String getName() {
return queueName;
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,7 @@ public class VmGlobalConfig {
@GlobalConfigValidation(validValues = {"None", "AuthenticAMD"})
@BindResourceConfig(value = {VmInstanceVO.class})
public static GlobalConfig VM_CPUID_VENDOR = new GlobalConfig(CATEGORY, "vm.cpuid.vendor");

// @GlobalConfigValidation
// public static GlobalConfig GC_INTERVAL = new GlobalConfig(CATEGORY, "deletion.gcInterval");
}
Loading