forked from zstackio/zstack
-
Notifications
You must be signed in to change notification settings - Fork 0
<refactor>[compute]: split VmInstanceManagerImpl 2808L into 6 SubManagers #3400
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/ye.zou/refactor/vm-instance-manager-split
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.
+2,890
−2,505
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
1,174 changes: 1,174 additions & 0 deletions
1,174
compute/src/main/java/org/zstack/compute/vm/VmCreationSubManager.java
Large diffs are not rendered by default.
Oops, something went wrong.
202 changes: 202 additions & 0 deletions
202
compute/src/main/java/org/zstack/compute/vm/VmExpungeSubManager.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,202 @@ | ||
| package org.zstack.compute.vm; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.zstack.core.cloudbus.CloudBus; | ||
| import org.zstack.core.cloudbus.CloudBusListCallBack; | ||
| import org.zstack.core.cloudbus.ResourceDestinationMaker; | ||
| import org.zstack.core.config.GlobalConfig; | ||
| import org.zstack.core.config.GlobalConfigUpdateExtensionPoint; | ||
| import org.zstack.core.db.DatabaseFacade; | ||
| import org.zstack.core.db.SimpleQuery; | ||
| import org.zstack.core.db.SimpleQuery.Op; | ||
| import org.zstack.core.thread.CancelablePeriodicTask; | ||
| import org.zstack.core.thread.ThreadFacade; | ||
| import org.zstack.header.Component; | ||
| import org.zstack.header.managementnode.ManagementNodeReadyExtensionPoint; | ||
| import org.zstack.header.message.MessageReply; | ||
| import org.zstack.header.vm.*; | ||
| import org.zstack.header.vm.VmInstanceDeletionPolicyManager.VmInstanceDeletionPolicy; | ||
| import org.zstack.utils.Utils; | ||
| import org.zstack.utils.logging.CLogger; | ||
|
|
||
| import javax.persistence.Tuple; | ||
| import java.sql.Timestamp; | ||
| import java.util.List; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.zstack.utils.CollectionUtils.transformToList; | ||
|
|
||
| /** | ||
| * Manages VM expunge lifecycle — periodic task that expunges destroyed VMs | ||
| * after their retention period expires. | ||
| * Extracted from VmInstanceManagerImpl to reduce God Class complexity. | ||
| */ | ||
| public class VmExpungeSubManager implements ManagementNodeReadyExtensionPoint, Component { | ||
| private static final CLogger logger = Utils.getLogger(VmExpungeSubManager.class); | ||
|
|
||
| @Autowired | ||
| private ThreadFacade thdf; | ||
| @Autowired | ||
| private DatabaseFacade dbf; | ||
| @Autowired | ||
| private VmInstanceDeletionPolicyManager deletionPolicyMgr; | ||
| @Autowired | ||
| private CloudBus bus; | ||
| @Autowired | ||
| private ResourceDestinationMaker destMaker; | ||
|
|
||
| private Future<Void> expungeVmTask; | ||
|
|
||
| @Override | ||
| public void managementNodeReady() { | ||
| startVmExpungeTask(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean start() { | ||
| installGlobalConfigUpdater(); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean stop() { | ||
| return true; | ||
| } | ||
|
|
||
| private void installGlobalConfigUpdater() { | ||
| VmGlobalConfig.VM_EXPUNGE_INTERVAL.installUpdateExtension(new GlobalConfigUpdateExtensionPoint() { | ||
| @Override | ||
| public void updateGlobalConfig(GlobalConfig oldConfig, GlobalConfig newConfig) { | ||
| startVmExpungeTask(); | ||
| } | ||
| }); | ||
| VmGlobalConfig.VM_EXPUNGE_PERIOD.installUpdateExtension(new GlobalConfigUpdateExtensionPoint() { | ||
| @Override | ||
| public void updateGlobalConfig(GlobalConfig oldConfig, GlobalConfig newConfig) { | ||
| startVmExpungeTask(); | ||
| } | ||
| }); | ||
| VmGlobalConfig.VM_DELETION_POLICY.installUpdateExtension(new GlobalConfigUpdateExtensionPoint() { | ||
| @Override | ||
| public void updateGlobalConfig(GlobalConfig oldConfig, GlobalConfig newConfig) { | ||
| startVmExpungeTask(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private synchronized void startVmExpungeTask() { | ||
| if (expungeVmTask != null) { | ||
| expungeVmTask.cancel(true); | ||
| } | ||
|
|
||
| expungeVmTask = thdf.submitCancelablePeriodicTask(new CancelablePeriodicTask() { | ||
|
|
||
| private List<Tuple> getVmDeletedStateManagedByUs() { | ||
| int qun = 10000; | ||
| SimpleQuery q = dbf.createQuery(VmInstanceVO.class); | ||
| q.add(VmInstanceVO_.state, Op.EQ, VmInstanceState.Destroyed); | ||
| long amount = q.count(); | ||
| int times = (int) (amount / qun) + (amount % qun != 0 ? 1 : 0); | ||
| int start = 0; | ||
| List<Tuple> ret = new java.util.ArrayList<>(); | ||
| for (int i = 0; i < times; i++) { | ||
| q = dbf.createQuery(VmInstanceVO.class); | ||
| q.select(VmInstanceVO_.uuid, VmInstanceVO_.lastOpDate); | ||
| q.add(VmInstanceVO_.state, Op.EQ, VmInstanceState.Destroyed); | ||
| q.setLimit(qun); | ||
| q.setStart(start); | ||
| List<Tuple> ts = q.listTuple(); | ||
| start += qun; | ||
|
|
||
| for (Tuple t : ts) { | ||
| String vmUuid = t.get(0, String.class); | ||
| if (!destMaker.isManagedByUs(vmUuid)) { | ||
| continue; | ||
| } | ||
| ret.add(t); | ||
| } | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized boolean run() { | ||
| final List<Tuple> vms = getVmDeletedStateManagedByUs(); | ||
| if (vms.isEmpty()) { | ||
| logger.debug("[VM Expunging Task]: no vm to expunge"); | ||
| return false; | ||
| } | ||
|
|
||
| final Timestamp current = dbf.getCurrentSqlTime(); | ||
|
|
||
| final List<ExpungeVmMsg> msgs = transformToList(vms, new org.zstack.utils.function.Function<ExpungeVmMsg, Tuple>() { | ||
| @Override | ||
| public ExpungeVmMsg call(Tuple t) { | ||
| String uuid = t.get(0, String.class); | ||
| Timestamp date = t.get(1, Timestamp.class); | ||
| long end = date.getTime() + TimeUnit.SECONDS.toMillis(VmGlobalConfig.VM_EXPUNGE_PERIOD.value(Long.class)); | ||
| if (current.getTime() >= end) { | ||
| VmInstanceDeletionPolicy deletionPolicy = deletionPolicyMgr.getDeletionPolicy(uuid); | ||
|
|
||
| if (deletionPolicy == VmInstanceDeletionPolicy.Never) { | ||
| logger.debug(String.format("[VM Expunging Task]: the deletion policy of the vm[uuid:%s] is Never, don't expunge it", | ||
| uuid)); | ||
| return null; | ||
| } else { | ||
| ExpungeVmMsg msg = new ExpungeVmMsg(); | ||
| msg.setVmInstanceUuid(uuid); | ||
| bus.makeTargetServiceIdByResourceUuid(msg, VmInstanceConstant.SERVICE_ID, uuid); | ||
| return msg; | ||
| } | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| if (msgs.isEmpty()) { | ||
| logger.debug("[VM Expunging Task]: no vm to expunge"); | ||
| return false; | ||
| } | ||
|
|
||
| bus.send(msgs, 100, new CloudBusListCallBack(null) { | ||
| @Override | ||
| public void run(List<MessageReply> replies) { | ||
| for (MessageReply r : replies) { | ||
| ExpungeVmMsg msg = msgs.get(replies.indexOf(r)); | ||
| if (!r.isSuccess()) { | ||
| logger.warn(String.format("failed to expunge the vm[uuid:%s], %s", | ||
| msg.getVmInstanceUuid(), r.getError())); | ||
| } else { | ||
| logger.debug(String.format("successfully expunged the vm[uuid:%s]", | ||
| msg.getVmInstanceUuid())); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public TimeUnit getTimeUnit() { | ||
| return TimeUnit.SECONDS; | ||
| } | ||
|
|
||
| @Override | ||
| public long getInterval() { | ||
| return VmGlobalConfig.VM_EXPUNGE_INTERVAL.value(Long.class); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "expunge-vm-task"; | ||
| } | ||
| }); | ||
|
|
||
| logger.debug(String.format("vm expunging task starts running, [period: %s seconds, interval: %s seconds]", | ||
| VmGlobalConfig.VM_EXPUNGE_PERIOD.value(Long.class), VmGlobalConfig.VM_EXPUNGE_INTERVAL.value(Long.class))); | ||
| } | ||
| } | ||
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.
stop()未取消周期任务,存在生命周期泄漏风险组件停止时应主动取消
expungeVmTask,避免后台任务继续运行。🔧 建议修复
`@Override` public boolean stop() { + if (expungeVmTask != null) { + expungeVmTask.cancel(true); + expungeVmTask = null; + } return true; }📝 Committable suggestion
🤖 Prompt for AI Agents