forked from zstackio/zstack
-
Notifications
You must be signed in to change notification settings - Fork 0
<feature>[vm]: add vm metadata header definitions #3781
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
7
commits into
zsv_5.0.0
Choose a base branch
from
sync/tao.gan/vmmetadata-ZSV-11559@@3
base: zsv_5.0.0
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
7 commits
Select commit
Hold shift + click to select a range
29c8def
<feature>[vm]: add vm metadata header definitions
taogan21 6fb1beb
<feature>[vm]: annotate API msgs with MetadataImpact
taogan21 da18d23
<feature>[vm]: add metadata compute core and config
taogan21 06b465e
<feature>[vm]: add metadata local and nfs storage backend
taogan21 a03eb1c
<feature>[vm]: add metadata SDK actions and testlib
taogan21 16311e3
<fix>[metadata]: extractOldPrefix use lastIndexOf
taogan21 9907964
<fix>[storage]: update APIScanVmInstanceMetadataFromPrimaryStorageMsg
taogan21 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
106 changes: 106 additions & 0 deletions
106
compute/src/main/java/org/zstack/compute/vm/CleanupVmInstanceMetadataOnPrimaryStorageGC.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,106 @@ | ||
| package org.zstack.compute.vm; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.zstack.core.cloudbus.CloudBusCallBack; | ||
| import org.zstack.core.componentloader.PluginRegistry; | ||
| import org.zstack.core.db.Q; | ||
| import org.zstack.core.gc.GC; | ||
| import org.zstack.core.gc.GCCompletion; | ||
| import org.zstack.core.gc.TimeBasedGarbageCollector; | ||
| import org.zstack.header.host.HostVO; | ||
| import org.zstack.header.message.MessageReply; | ||
| import org.zstack.header.storage.primary.CleanupVmInstanceMetadataOnPrimaryStorageMsg; | ||
| import org.zstack.header.storage.primary.PrimaryStorageConstant; | ||
| import org.zstack.header.storage.primary.PrimaryStorageVO; | ||
| import org.zstack.header.storage.primary.PrimaryStorageVO_; | ||
| import org.zstack.header.vm.metadata.VmMetadataPathBuildExtensionPoint; | ||
| import org.zstack.utils.Utils; | ||
| import org.zstack.utils.logging.CLogger; | ||
|
|
||
| public class CleanupVmInstanceMetadataOnPrimaryStorageGC extends TimeBasedGarbageCollector { | ||
| private static final CLogger logger = Utils.getLogger(CleanupVmInstanceMetadataOnPrimaryStorageGC.class); | ||
|
|
||
| @Autowired | ||
| private PluginRegistry pluginRgty; | ||
|
|
||
| @GC | ||
| public String primaryStorageUuid; | ||
| @GC | ||
| public String vmUuid; | ||
| @GC | ||
| public String rootVolumeUuid; | ||
| @GC | ||
| public String metadataPath; | ||
| @GC | ||
| public String hostUuid; | ||
|
|
||
| public static String getGCName(String vmUuid) { | ||
| return String.format("gc-cleanup-vm-metadata-%s", vmUuid); | ||
| } | ||
|
|
||
| @Override | ||
| protected void triggerNow(GCCompletion completion) { | ||
| if (!dbf.isExist(primaryStorageUuid, PrimaryStorageVO.class)) { | ||
| logger.debug(String.format("[MetadataCleanupGC] primary storage[uuid:%s] no longer exists, " + | ||
| "cancel gc for vm[uuid:%s]", primaryStorageUuid, vmUuid)); | ||
| completion.cancel(); | ||
| return; | ||
| } | ||
|
|
||
| String psType = Q.New(PrimaryStorageVO.class).select(PrimaryStorageVO_.type).eq(PrimaryStorageVO_.uuid, primaryStorageUuid).findValue(); | ||
| if (psType == null) { | ||
| logger.debug(String.format("[MetadataCleanupGC] primary storage[uuid:%s] type not found, " + | ||
| "cancel gc for vm[uuid:%s]", primaryStorageUuid, vmUuid)); | ||
| completion.cancel(); | ||
| return; | ||
| } | ||
|
|
||
| VmMetadataPathBuildExtensionPoint ext = pluginRgty.getExtensionFromMap(psType, VmMetadataPathBuildExtensionPoint.class); | ||
| boolean requireHost = ext != null && ext.requireHostForCleanup(); | ||
|
|
||
| // Determine effective hostUuid based on whether the PS type requires a host for cleanup. | ||
| String effectiveHostUuid = hostUuid; | ||
| if (!requireHost) { | ||
| effectiveHostUuid = null; | ||
| } else { | ||
| if (effectiveHostUuid == null) { | ||
| logger.debug(String.format("[MetadataCleanupGC] hostUuid is null and ps[uuid:%s, type:%s] " + | ||
| "requires host for cleanup, cancel gc for vm[uuid:%s]", | ||
| primaryStorageUuid, psType, vmUuid)); | ||
| completion.cancel(); | ||
| return; | ||
| } | ||
| if (!dbf.isExist(effectiveHostUuid, HostVO.class)) { | ||
| logger.debug(String.format("[MetadataCleanupGC] host[uuid:%s] no longer exists " + | ||
| "and ps[uuid:%s, type:%s] requires host for cleanup, " + | ||
| "metadata is unreachable, cancel gc for vm[uuid:%s]", | ||
| effectiveHostUuid, primaryStorageUuid, psType, vmUuid)); | ||
| completion.cancel(); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| CleanupVmInstanceMetadataOnPrimaryStorageMsg msg = new CleanupVmInstanceMetadataOnPrimaryStorageMsg(); | ||
| msg.setPrimaryStorageUuid(primaryStorageUuid); | ||
| msg.setVmInstanceUuid(vmUuid); | ||
| msg.setRootVolumeUuid(rootVolumeUuid); | ||
| msg.setMetadataPath(metadataPath); | ||
| msg.setHostUuid(effectiveHostUuid); | ||
|
|
||
| bus.makeTargetServiceIdByResourceUuid(msg, PrimaryStorageConstant.SERVICE_ID, primaryStorageUuid); | ||
| bus.send(msg, new CloudBusCallBack(completion) { | ||
| @Override | ||
| public void run(MessageReply reply) { | ||
| if (reply.isSuccess()) { | ||
| logger.info(String.format("[MetadataCleanupGC] successfully cleaned up metadata " + | ||
| "for vm[uuid:%s] on ps[uuid:%s]", vmUuid, primaryStorageUuid)); | ||
| completion.success(); | ||
| } else { | ||
| logger.warn(String.format("[MetadataCleanupGC] failed to clean up metadata " + | ||
| "for vm[uuid:%s] on ps[uuid:%s]: %s", vmUuid, primaryStorageUuid, reply.getError())); | ||
| completion.fail(reply.getError()); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| } | ||
143 changes: 143 additions & 0 deletions
143
compute/src/main/java/org/zstack/compute/vm/VmExpungeMetadataFlow.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,143 @@ | ||
| package org.zstack.compute.vm; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowire; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Configurable; | ||
| import org.zstack.core.cloudbus.CloudBus; | ||
| import org.zstack.core.cloudbus.CloudBusCallBack; | ||
| import org.zstack.core.componentloader.PluginRegistry; | ||
| import org.zstack.core.db.Q; | ||
| import org.zstack.header.core.workflow.FlowTrigger; | ||
| import org.zstack.header.core.workflow.NoRollbackFlow; | ||
| import org.zstack.header.message.MessageReply; | ||
| import org.zstack.header.storage.primary.CleanupVmInstanceMetadataOnPrimaryStorageMsg; | ||
| import org.zstack.header.storage.primary.PrimaryStorageConstant; | ||
| import org.zstack.header.storage.primary.PrimaryStorageVO; | ||
| import org.zstack.header.storage.primary.PrimaryStorageVO_; | ||
| import org.zstack.header.vm.VmInstanceConstant; | ||
| import org.zstack.header.vm.VmInstanceSpec; | ||
| import org.zstack.header.vm.metadata.VmMetadataPathBuildExtensionPoint; | ||
| import org.zstack.header.volume.VolumeInventory; | ||
| import org.zstack.utils.Utils; | ||
| import org.zstack.utils.logging.CLogger; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @Configurable(preConstruction = true, autowire = Autowire.BY_TYPE) | ||
| public class VmExpungeMetadataFlow extends NoRollbackFlow { | ||
| private static final CLogger logger = Utils.getLogger(VmExpungeMetadataFlow.class); | ||
|
|
||
| @Autowired | ||
| private CloudBus bus; | ||
| @Autowired | ||
| private PluginRegistry pluginRgty; | ||
|
|
||
| @Override | ||
| public void run(FlowTrigger trigger, Map data) { | ||
| if (!VmGlobalConfig.VM_METADATA_ENABLED.value(Boolean.class)) { | ||
| trigger.next(); | ||
| return; | ||
| } | ||
|
|
||
| final VmInstanceSpec spec = (VmInstanceSpec) data.get(VmInstanceConstant.Params.VmInstanceSpec.toString()); | ||
| if (spec == null || spec.getVmInventory() == null) { | ||
| logger.warn("[MetadataExpunge] missing VmInstanceSpec or VmInventory, skip metadata cleanup"); | ||
| trigger.next(); | ||
| return; | ||
| } | ||
|
|
||
| final String vmUuid = spec.getVmInventory().getUuid(); | ||
|
|
||
| VolumeInventory rootVolume = spec.getVmInventory().getRootVolume(); | ||
| String psUuid = rootVolume != null ? rootVolume.getPrimaryStorageUuid() : null; | ||
| if (psUuid == null) { | ||
| logger.debug(String.format("[MetadataExpunge] vm[uuid:%s] root volume has no primaryStorageUuid, " + | ||
| "skipping metadata cleanup", vmUuid)); | ||
| trigger.next(); | ||
| return; | ||
| } | ||
|
|
||
|
|
||
| String psType = Q.New(PrimaryStorageVO.class).select(PrimaryStorageVO_.type).eq(PrimaryStorageVO_.uuid, psUuid).findValue(); | ||
| if (psType == null) { | ||
| logger.warn(String.format("[MetadataExpunge] primary storage[uuid:%s] not found for vm[uuid:%s], " + | ||
| "skip metadata cleanup", psUuid, vmUuid)); | ||
| trigger.next(); | ||
| return; | ||
| } | ||
|
|
||
| VmMetadataPathBuildExtensionPoint ext = pluginRgty.getExtensionFromMap(psType, VmMetadataPathBuildExtensionPoint.class); | ||
| if (ext == null) { | ||
| logger.warn(String.format("[MetadataExpunge] no VmMetadataPathBuildExtensionPoint found for ps[uuid:%s, type:%s], " + | ||
| "skip metadata cleanup", psUuid, psType)); | ||
| trigger.next(); | ||
| return; | ||
| } | ||
| final String metadataPath; | ||
| try { | ||
| metadataPath = ext.buildVmMetadataPath(psUuid, vmUuid); | ||
| } catch (Exception e) { | ||
| logger.warn(String.format("[MetadataExpunge] failed to build metadata path for vm[uuid:%s] on ps[uuid:%s], " + | ||
| "skip metadata cleanup: %s", vmUuid, psUuid, e.getMessage())); | ||
| trigger.next(); | ||
| return; | ||
| } | ||
|
|
||
| String hostUuid = null; | ||
| if (ext.requireHostForCleanup()) { | ||
| hostUuid = spec.getVmInventory().getHostUuid(); | ||
| if (hostUuid == null) { | ||
| hostUuid = spec.getVmInventory().getLastHostUuid(); | ||
| } | ||
|
|
||
| if (hostUuid == null) { | ||
| logger.warn(String.format("[MetadataExpunge] vm[uuid:%s] hostUuid is null, " + | ||
| "ps[uuid:%s, type:%s] requires host for cleanup, skip without submitting GC", | ||
| vmUuid, psUuid, psType)); | ||
| trigger.next(); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| String rootVolumeUuid = rootVolume.getUuid(); | ||
| CleanupVmInstanceMetadataOnPrimaryStorageMsg cmsg = new CleanupVmInstanceMetadataOnPrimaryStorageMsg(); | ||
| cmsg.setPrimaryStorageUuid(psUuid); | ||
| cmsg.setVmInstanceUuid(vmUuid); | ||
| cmsg.setMetadataPath(metadataPath); | ||
| cmsg.setRootVolumeUuid(rootVolumeUuid); | ||
| cmsg.setHostUuid(hostUuid); | ||
| final String finalPsUuid = psUuid; | ||
| final String finalHostUuid = hostUuid; | ||
|
|
||
| bus.makeTargetServiceIdByResourceUuid(cmsg, PrimaryStorageConstant.SERVICE_ID, psUuid); | ||
| bus.send(cmsg, new CloudBusCallBack(trigger) { | ||
| @Override | ||
| public void run(MessageReply reply) { | ||
| if (reply.isSuccess()) { | ||
| logger.info(String.format("[MetadataExpunge] successfully deleted metadata for vm[uuid:%s] on ps[uuid:%s]", | ||
| vmUuid, finalPsUuid)); | ||
| } else { | ||
| logger.warn(String.format("[MetadataExpunge] failed to delete metadata for vm[uuid:%s] on ps[uuid:%s]: %s, " + | ||
| "submitting GC job for retry", vmUuid, finalPsUuid, reply.getError())); | ||
| submitGC(finalPsUuid, vmUuid, rootVolumeUuid, metadataPath, finalHostUuid); | ||
| } | ||
| trigger.next(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private void submitGC(String psUuid, String vmUuid, String rootVolumeUuid, String metadataPath, String hostUuid) { | ||
| CleanupVmInstanceMetadataOnPrimaryStorageGC gc = new CleanupVmInstanceMetadataOnPrimaryStorageGC(); | ||
| gc.NAME = CleanupVmInstanceMetadataOnPrimaryStorageGC.getGCName(vmUuid); | ||
| gc.primaryStorageUuid = psUuid; | ||
| gc.vmUuid = vmUuid; | ||
| gc.rootVolumeUuid = rootVolumeUuid; | ||
| gc.metadataPath = metadataPath; | ||
| gc.hostUuid = hostUuid; | ||
| long gcIntervalSec = TimeUnit.HOURS.toSeconds(VmGlobalConfig.VM_METADATA_CLEANUP_GC_INTERVAL.value(Long.class)); | ||
| gc.deduplicateSubmit(gcIntervalSec, TimeUnit.SECONDS); | ||
|
|
||
| logger.info(String.format("[MetadataExpunge] submitted GC job [%s] for vm[uuid:%s] on ps[uuid:%s]", gc.NAME, vmUuid, psUuid)); | ||
| } | ||
| } |
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
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.
缺少 metadata 扩展时应直接取消 GC。
这里把
ext == null当成“无需 host”继续往下走,会把不支持元数据清理的主存储也送进 cleanup 流程。最终通常只会收到operation not supported,GC 会进入无意义的周期性失败重试。可参考的修正方式
🤖 Prompt for AI Agents