|
| 1 | +package org.zstack.compute.vm; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowire; |
| 4 | +import org.springframework.beans.factory.annotation.Autowired; |
| 5 | +import org.springframework.beans.factory.annotation.Configurable; |
| 6 | +import org.zstack.core.cloudbus.CloudBus; |
| 7 | +import org.zstack.core.cloudbus.CloudBusCallBack; |
| 8 | +import org.zstack.core.componentloader.PluginRegistry; |
| 9 | +import org.zstack.core.db.Q; |
| 10 | +import org.zstack.header.core.workflow.FlowTrigger; |
| 11 | +import org.zstack.header.core.workflow.NoRollbackFlow; |
| 12 | +import org.zstack.header.message.MessageReply; |
| 13 | +import org.zstack.header.storage.primary.CleanupVmInstanceMetadataOnPrimaryStorageMsg; |
| 14 | +import org.zstack.header.storage.primary.PrimaryStorageConstant; |
| 15 | +import org.zstack.header.storage.primary.PrimaryStorageVO; |
| 16 | +import org.zstack.header.storage.primary.PrimaryStorageVO_; |
| 17 | +import org.zstack.header.vm.VmInstanceConstant; |
| 18 | +import org.zstack.header.vm.VmInstanceSpec; |
| 19 | +import org.zstack.header.vm.metadata.VmMetadataPathBuildExtensionPoint; |
| 20 | +import org.zstack.header.volume.VolumeInventory; |
| 21 | +import org.zstack.utils.Utils; |
| 22 | +import org.zstack.utils.logging.CLogger; |
| 23 | + |
| 24 | +import java.util.Map; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | + |
| 27 | +@Configurable(preConstruction = true, autowire = Autowire.BY_TYPE) |
| 28 | +public class VmExpungeMetadataFlow extends NoRollbackFlow { |
| 29 | + private static final CLogger logger = Utils.getLogger(VmExpungeMetadataFlow.class); |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private CloudBus bus; |
| 33 | + @Autowired |
| 34 | + private PluginRegistry pluginRgty; |
| 35 | + |
| 36 | + @Override |
| 37 | + public void run(FlowTrigger trigger, Map data) { |
| 38 | + if (!VmGlobalConfig.VM_METADATA_ENABLED.value(Boolean.class)) { |
| 39 | + trigger.next(); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + final VmInstanceSpec spec = (VmInstanceSpec) data.get(VmInstanceConstant.Params.VmInstanceSpec.toString()); |
| 44 | + if (spec == null || spec.getVmInventory() == null) { |
| 45 | + logger.warn("[MetadataExpunge] missing VmInstanceSpec or VmInventory, skip metadata cleanup"); |
| 46 | + trigger.next(); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + final String vmUuid = spec.getVmInventory().getUuid(); |
| 51 | + |
| 52 | + VolumeInventory rootVolume = spec.getVmInventory().getRootVolume(); |
| 53 | + String psUuid = rootVolume != null ? rootVolume.getPrimaryStorageUuid() : null; |
| 54 | + if (psUuid == null) { |
| 55 | + logger.debug(String.format("[MetadataExpunge] vm[uuid:%s] root volume has no primaryStorageUuid, " + |
| 56 | + "skipping metadata cleanup", vmUuid)); |
| 57 | + trigger.next(); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + String psType = Q.New(PrimaryStorageVO.class).select(PrimaryStorageVO_.type).eq(PrimaryStorageVO_.uuid, psUuid).findValue(); |
| 63 | + if (psType == null) { |
| 64 | + logger.warn(String.format("[MetadataExpunge] primary storage[uuid:%s] not found for vm[uuid:%s], " + |
| 65 | + "skip metadata cleanup", psUuid, vmUuid)); |
| 66 | + trigger.next(); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + VmMetadataPathBuildExtensionPoint ext = pluginRgty.getExtensionFromMap(psType, VmMetadataPathBuildExtensionPoint.class); |
| 71 | + if (ext == null) { |
| 72 | + logger.warn(String.format("[MetadataExpunge] no VmMetadataPathBuildExtensionPoint found for ps[uuid:%s, type:%s], " + |
| 73 | + "skip metadata cleanup", psUuid, psType)); |
| 74 | + trigger.next(); |
| 75 | + return; |
| 76 | + } |
| 77 | + final String metadataPath; |
| 78 | + try { |
| 79 | + metadataPath = ext.buildVmMetadataPath(psUuid, vmUuid); |
| 80 | + } catch (Exception e) { |
| 81 | + logger.warn(String.format("[MetadataExpunge] failed to build metadata path for vm[uuid:%s] on ps[uuid:%s], " + |
| 82 | + "skip metadata cleanup: %s", vmUuid, psUuid, e.getMessage())); |
| 83 | + trigger.next(); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + String hostUuid = null; |
| 88 | + if (ext.requireHostForCleanup()) { |
| 89 | + hostUuid = spec.getVmInventory().getHostUuid(); |
| 90 | + if (hostUuid == null) { |
| 91 | + hostUuid = spec.getVmInventory().getLastHostUuid(); |
| 92 | + } |
| 93 | + |
| 94 | + if (hostUuid == null) { |
| 95 | + logger.warn(String.format("[MetadataExpunge] vm[uuid:%s] hostUuid is null, " + |
| 96 | + "ps[uuid:%s, type:%s] requires host for cleanup, skip without submitting GC", |
| 97 | + vmUuid, psUuid, psType)); |
| 98 | + trigger.next(); |
| 99 | + return; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + String rootVolumeUuid = rootVolume.getUuid(); |
| 104 | + CleanupVmInstanceMetadataOnPrimaryStorageMsg cmsg = new CleanupVmInstanceMetadataOnPrimaryStorageMsg(); |
| 105 | + cmsg.setPrimaryStorageUuid(psUuid); |
| 106 | + cmsg.setVmInstanceUuid(vmUuid); |
| 107 | + cmsg.setMetadataPath(metadataPath); |
| 108 | + cmsg.setRootVolumeUuid(rootVolumeUuid); |
| 109 | + cmsg.setHostUuid(hostUuid); |
| 110 | + final String finalPsUuid = psUuid; |
| 111 | + final String finalHostUuid = hostUuid; |
| 112 | + |
| 113 | + bus.makeTargetServiceIdByResourceUuid(cmsg, PrimaryStorageConstant.SERVICE_ID, psUuid); |
| 114 | + bus.send(cmsg, new CloudBusCallBack(trigger) { |
| 115 | + @Override |
| 116 | + public void run(MessageReply reply) { |
| 117 | + if (reply.isSuccess()) { |
| 118 | + logger.info(String.format("[MetadataExpunge] successfully deleted metadata for vm[uuid:%s] on ps[uuid:%s]", |
| 119 | + vmUuid, finalPsUuid)); |
| 120 | + } else { |
| 121 | + logger.warn(String.format("[MetadataExpunge] failed to delete metadata for vm[uuid:%s] on ps[uuid:%s]: %s, " + |
| 122 | + "submitting GC job for retry", vmUuid, finalPsUuid, reply.getError())); |
| 123 | + submitGC(finalPsUuid, vmUuid, rootVolumeUuid, metadataPath, finalHostUuid); |
| 124 | + } |
| 125 | + trigger.next(); |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + private void submitGC(String psUuid, String vmUuid, String rootVolumeUuid, String metadataPath, String hostUuid) { |
| 131 | + CleanupVmInstanceMetadataOnPrimaryStorageGC gc = new CleanupVmInstanceMetadataOnPrimaryStorageGC(); |
| 132 | + gc.NAME = CleanupVmInstanceMetadataOnPrimaryStorageGC.getGCName(vmUuid); |
| 133 | + gc.primaryStorageUuid = psUuid; |
| 134 | + gc.vmUuid = vmUuid; |
| 135 | + gc.rootVolumeUuid = rootVolumeUuid; |
| 136 | + gc.metadataPath = metadataPath; |
| 137 | + gc.hostUuid = hostUuid; |
| 138 | + long gcIntervalSec = TimeUnit.HOURS.toSeconds(VmGlobalConfig.VM_METADATA_CLEANUP_GC_INTERVAL.value(Long.class)); |
| 139 | + gc.deduplicateSubmit(gcIntervalSec, TimeUnit.SECONDS); |
| 140 | + |
| 141 | + logger.info(String.format("[MetadataExpunge] submitted GC job [%s] for vm[uuid:%s] on ps[uuid:%s]", gc.NAME, vmUuid, psUuid)); |
| 142 | + } |
| 143 | +} |
0 commit comments