create vm destroyed usageVO before delete vm
Resolve
@@ -77,3 +77,42 @@ CREATE TABLE `BaremetalBondingVO` (
ALTER TABLE `BaremetalInstanceVO` ADD COLUMN `templateUuid` varchar(32) DEFAULT NULL;
ALTER TABLE `BaremetalInstanceVO` ADD CONSTRAINT `fkBaremetalInstanceVOPreconfigurationTemplateVO` FOREIGN KEY (`templateUuid`) REFERENCES `PreconfigurationTemplateVO` (`uuid`) ON DELETE SET NULL;
+
+CREATE INDEX idxVmUuid ON VmUsageVO(vmUuid) USING BTREE;
+
+DELIMITER $$
+CREATE PROCEDURE cleanExpireVmUsageVO()
+ BEGIN
+ DECLARE done INT DEFAULT FALSE;
+ DECLARE vmUuid VARCHAR(32);
+ DECLARE name VARCHAR(255);
+ DECLARE accountUuid VARCHAR(32);
+ DECLARE cpuNum INT(10);
+ DECLARE state VARCHAR(64);
+ DECLARE memorySize BIGINT(20);
+ DECLARE rootVolumeSize BIGINT(20);
+ DECLARE inventory Text;
+ DECLARE lastOpDate TIMESTAMP;
+ DEClARE cur CURSOR FOR SELECT v.vmUuid,v.name,v.accountUuid,v.state,v.cpuNum,v.memorySize,v.rootVolumeSize,v.inventory from VmUsageVO v
+ where v.id IN (select MAX(a.id) FROM VmUsageVO a GROUP BY a.vmUuid)
+ AND v.vmUuid NOT IN (select DISTINCT uuid from VmInstanceEO) AND v.state = 'Running';
+ DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
+ OPEN cur;
+ read_loop: LOOP
+ FETCH cur INTO vmUuid,name,accountUuid,state,cpuNum,memorySize,rootVolumeSize,inventory;
+ IF done THEN
+ LEAVE read_loop;
+ END IF;
+
+ INSERT zstack.VmUsageVO(vmUuid,name,accountUuid,state,cpuNum,memorySize,dateInLong,rootVolumeSize,inventory,lastOpDate,createDate)
+ VALUES (vmUuid,name,accountUuid,'Destroyed',cpuNum,memorySize,UNIX_TIMESTAMP(),rootVolumeSize,inventory,NOW(),NOW());
+
+ END LOOP;
+ CLOSE cur;
+ SELECT CURTIME();
+ END $$
+DELIMITER;
+
+call cleanExpireVmUsageVO();
+DROP PROCEDURE IF EXISTS cleanExpireVmUsageVO;
+
