Skip to content

Commit 36d7ee5

Browse files
author
kishan
committed
bug 10372: Added Vm State Machine listener for usage events
status 10372: resolved fixed
1 parent 7677a2b commit 36d7ee5

8 files changed

Lines changed: 108 additions & 38 deletions

File tree

‎api/src/com/cloud/vm/VirtualMachine.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ public static boolean isVmMigrated(State oldState, Event e, State newState) {
127127
}
128128
return false;
129129
}
130+
131+
public static boolean isVmCreated(State oldState, Event e, State newState) {
132+
if (oldState == State.Destroyed && newState == State.Stopped) {
133+
// VM recover
134+
return true;
135+
}
136+
return false;
137+
}
138+
139+
public static boolean isVmDestroyed(State oldState, Event e, State newState) {
140+
if (oldState == State.Stopped && newState == State.Destroyed) {
141+
return true;
142+
}
143+
return false;
144+
}
130145
}
131146

132147
public enum Event {

‎core/src/com/cloud/event/dao/UsageEventDao.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323

2424
import com.cloud.event.UsageEventVO;
2525
import com.cloud.exception.UsageServerException;
26-
import com.cloud.utils.db.Filter;
2726
import com.cloud.utils.db.GenericDao;
28-
import com.cloud.utils.db.SearchCriteria;
2927

3028
public interface UsageEventDao extends GenericDao<UsageEventVO, Long> {
3129

‎server/src/com/cloud/storage/StorageManagerImpl.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,11 @@ protected VolumeVO createVolumeFromSnapshot(VolumeVO volume, long snapshotId) {
581581
VolumeVO createdVolume = null;
582582
SnapshotVO snapshot = _snapshotDao.findById(snapshotId); // Precondition: snapshot is not null and not removed.
583583

584+
Transaction txn = Transaction.currentTxn();
585+
txn.start();
584586
Pair<VolumeVO, String> volumeDetails = createVolumeFromSnapshot(volume, snapshot);
585587
createdVolume = volumeDetails.first();
586588

587-
Transaction txn = Transaction.currentTxn();
588-
txn.start();
589589
Long diskOfferingId = volume.getDiskOfferingId();
590590

591591
if (createdVolume.getPath() != null) {
@@ -1756,11 +1756,10 @@ public VolumeVO allocVolume(CreateVolumeCmd cmd) throws ResourceAllocationExcept
17561756
volume.setDomainId((account == null) ? Domain.ROOT_DOMAIN : account.getDomainId());
17571757
volume.setState(Volume.State.Allocated);
17581758
volume = _volsDao.persist(volume);
1759-
UserContext.current().setEventDetails("Volume Id: " + volume.getId());
1760-
17611759
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VOLUME_CREATE, volume.getAccountId(), volume.getDataCenterId(), volume.getId(), volume.getName(), diskOfferingId, null, size);
1762-
17631760
_usageEventDao.persist(usageEvent);
1761+
1762+
UserContext.current().setEventDetails("Volume Id: " + volume.getId());
17641763

17651764
// Increment resource count during allocation; if actual creation fails, decrement it
17661765
_accountMgr.incrementResourceCount(volume.getAccountId(), ResourceType.volume);

‎server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,9 @@ public boolean backupSnapshotToSecondaryStorage(SnapshotVO ss) {
596596

597597
} else {
598598
s_logger.warn("Failed to back up snapshot on secondary storage, deleting the record from the DB");
599+
_snapshotDao.remove(snapshotId);
599600
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_SNAPSHOT_DELETE, snapshot.getAccountId(), snapshot.getDataCenterId(), snapshotId, snapshot.getName(), null, null, 0L);
600601
_usageEventDao.persist(usageEvent);
601-
_snapshotDao.remove(snapshotId);
602602
}
603603
txn.commit();
604604

‎server/src/com/cloud/user/AccountManagerImpl.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,9 +1087,6 @@ public boolean cleanupAccount(AccountVO account, long callerUserId, Account call
10871087
s_logger.error("Unable to destroy vm: " + vm.getId());
10881088
accountCleanupNeeded = true;
10891089
}
1090-
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VM_DESTROY, vm.getAccountId(), vm.getDataCenterIdToDeployIn(), vm.getId(), vm.getHostName(), vm.getServiceOfferingId(),
1091-
vm.getTemplateId(), vm.getHypervisorType().toString());
1092-
_usageEventDao.persist(usageEvent);
10931090
}
10941091

10951092
// Mark the account's volumes as destroyed

‎server/src/com/cloud/vm/UserVmManagerImpl.java‎

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,10 +1113,6 @@ public UserVm recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationE
11131113

11141114
_accountMgr.incrementResourceCount(account.getId(), ResourceType.volume, new Long(volumes.size()));
11151115

1116-
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VM_CREATE, vm.getAccountId(), vm.getDataCenterIdToDeployIn(), vm.getId(), vm.getHostName(), vm.getServiceOfferingId(), vm.getTemplateId(), vm
1117-
.getHypervisorType().toString());
1118-
_usageEventDao.persist(usageEvent);
1119-
11201116
_accountMgr.incrementResourceCount(account.getId(), ResourceType.user_vm);
11211117

11221118
txn.commit();
@@ -1153,6 +1149,8 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
11531149
_executor = Executors.newScheduledThreadPool(wrks, new NamedThreadFactory("UserVm-Scavenger"));
11541150

11551151
_itMgr.registerGuru(VirtualMachine.Type.User, this);
1152+
1153+
VirtualMachine.State.getStateMachine().registerListener(new UserVmStateListener(_usageEventDao, _networkDao, _nicDao));
11561154

11571155
s_logger.info("User VM Manager is configured.");
11581156

@@ -1659,8 +1657,6 @@ private void updateVmStateForFailedVmCreation(Long vmId) {
16591657
s_logger.warn("Unable to delete volume:" + volume.getId() + " for vm:" + vmId + " whilst transitioning to error state");
16601658
}
16611659
}
1662-
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VM_DESTROY, vm.getAccountId(), vm.getDataCenterIdToDeployIn(), vm.getId(), vm.getHostName());
1663-
_usageEventDao.persist(usageEvent);
16641660
String msg = "Failed to deploy Vm with Id: " + vmId;
16651661
_alertMgr.sendAlert(AlertManager.ALERT_TYPE_USERVM, vm.getDataCenterIdToDeployIn(), vm.getPodIdToDeployIn(), msg, msg);
16661662

@@ -2526,9 +2522,10 @@ protected UserVm createVirtualMachine(DataCenter zone, ServiceOffering serviceOf
25262522
s_logger.debug("Successfully allocated DB entry for " + vm);
25272523
}
25282524
UserContext.current().setEventDetails("Vm Id: " + vm.getId());
2525+
25292526
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VM_CREATE, accountId, zone.getId(), vm.getId(), vm.getHostName(), offering.getId(), template.getId(), hypervisorType.toString());
25302527
_usageEventDao.persist(usageEvent);
2531-
2528+
25322529
_accountMgr.incrementResourceCount(accountId, ResourceType.user_vm);
25332530

25342531
// Assign instance to the group
@@ -2715,9 +2712,6 @@ public boolean finalizeCommandsOnStart(Commands cmds, VirtualMachineProfile<User
27152712
@Override
27162713
public boolean finalizeStart(VirtualMachineProfile<UserVmVO> profile, long hostId, Commands cmds, ReservationContext context) {
27172714
UserVmVO vm = profile.getVirtualMachine();
2718-
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VM_START, vm.getAccountId(), vm.getDataCenterIdToDeployIn(), vm.getId(), vm.getHostName(), vm.getServiceOfferingId(), vm.getTemplateId(), vm
2719-
.getHypervisorType().toString());
2720-
_usageEventDao.persist(usageEvent);
27212715
Answer startAnswer = cmds.getAnswer(StartAnswer.class);
27222716
String returnedIp = null;
27232717
String originalIp = null;
@@ -2736,7 +2730,7 @@ public boolean finalizeStart(VirtualMachineProfile<UserVmVO> profile, long hostI
27362730
for (NicVO nic : nics) {
27372731
NetworkVO network = _networkDao.findById(nic.getNetworkId());
27382732
long isDefault = (nic.isDefaultNic()) ? 1 : 0;
2739-
usageEvent = new UsageEventVO(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN, vm.getAccountId(), vm.getDataCenterIdToDeployIn(), vm.getId(), vm.getHostName(), network.getNetworkOfferingId(), null, isDefault);
2733+
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN, vm.getAccountId(), vm.getDataCenterIdToDeployIn(), vm.getId(), vm.getHostName(), network.getNetworkOfferingId(), null, isDefault);
27402734
_usageEventDao.persist(usageEvent);
27412735
if (network.getTrafficType() == TrafficType.Guest) {
27422736
originalIp = nic.getIp4Address();
@@ -2827,16 +2821,6 @@ public UserVm stopVirtualMachine(long vmId, boolean forced) throws ConcurrentOpe
28272821

28282822
@Override
28292823
public void finalizeStop(VirtualMachineProfile<UserVmVO> profile, StopAnswer answer) {
2830-
VMInstanceVO vm = profile.getVirtualMachine();
2831-
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VM_STOP, vm.getAccountId(), vm.getDataCenterIdToDeployIn(), vm.getId(), vm.getHostName());
2832-
_usageEventDao.persist(usageEvent);
2833-
2834-
List<NicVO> nics = _nicDao.listByVmId(vm.getId());
2835-
for (NicVO nic : nics) {
2836-
NetworkVO network = _networkDao.findById(nic.getNetworkId());
2837-
usageEvent = new UsageEventVO(EventTypes.EVENT_NETWORK_OFFERING_REMOVE, vm.getAccountId(), vm.getDataCenterIdToDeployIn(), vm.getId(), null, network.getNetworkOfferingId(), null, 0L);
2838-
_usageEventDao.persist(usageEvent);
2839-
}
28402824
}
28412825

28422826
public String generateRandomPassword() {
@@ -2912,8 +2896,6 @@ public UserVm destroyVm(long vmId) throws ResourceUnavailableException, Concurre
29122896
_usageEventDao.persist(usageEvent);
29132897
}
29142898
}
2915-
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VM_DESTROY, vm.getAccountId(), vm.getDataCenterIdToDeployIn(), vm.getId(), vm.getHostName());
2916-
_usageEventDao.persist(usageEvent);
29172899

29182900
if (vmState != State.Error) {
29192901
_accountMgr.decrementResourceCount(vm.getAccountId(), ResourceType.user_vm);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
3+
*
4+
* This software is licensed under the GNU General Public License v3 or later.
5+
*
6+
* It is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or any later version.
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
*/
18+
package com.cloud.vm;
19+
20+
import java.util.List;
21+
22+
import com.cloud.event.EventTypes;
23+
import com.cloud.event.UsageEventVO;
24+
import com.cloud.event.dao.UsageEventDao;
25+
import com.cloud.network.NetworkVO;
26+
import com.cloud.network.dao.NetworkDao;
27+
import com.cloud.utils.fsm.StateListener;
28+
import com.cloud.vm.VirtualMachine.Event;
29+
import com.cloud.vm.VirtualMachine.State;
30+
import com.cloud.vm.dao.NicDao;
31+
32+
public class UserVmStateListener implements StateListener<State, VirtualMachine.Event, VirtualMachine> {
33+
34+
protected UsageEventDao _usageEventDao;
35+
protected NetworkDao _networkDao;
36+
protected NicDao _nicDao;
37+
38+
public UserVmStateListener(UsageEventDao usageEventDao, NetworkDao networkDao, NicDao nicDao) {
39+
this._usageEventDao = usageEventDao;
40+
this._networkDao = networkDao;
41+
this._nicDao = nicDao;
42+
}
43+
44+
@Override
45+
public boolean preStateTransitionEvent(State oldState, Event event, State newState, VirtualMachine vo, boolean status, Long id) {
46+
return true;
47+
}
48+
49+
@Override
50+
public boolean postStateTransitionEvent(State oldState, Event event, State newState, VirtualMachine vo, boolean status, Long id) {
51+
if(!status){
52+
return false;
53+
}
54+
55+
if(vo.getType() != VirtualMachine.Type.User){
56+
return true;
57+
}
58+
59+
if (VirtualMachine.State.isVmCreated(oldState, event, newState)) {
60+
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VM_CREATE, vo.getAccountId(), vo.getDataCenterIdToDeployIn(), vo.getId(), vo.getHostName(), vo.getServiceOfferingId(),
61+
vo.getTemplateId(), vo.getHypervisorType().toString());
62+
_usageEventDao.persist(usageEvent);
63+
} else if (VirtualMachine.State.isVmStarted(oldState, event, newState)) {
64+
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VM_START, vo.getAccountId(), vo.getDataCenterIdToDeployIn(), vo.getId(), vo.getHostName(), vo.getServiceOfferingId(),
65+
vo.getTemplateId(), vo.getHypervisorType().toString());
66+
_usageEventDao.persist(usageEvent);
67+
} else if (VirtualMachine.State.isVmStopped(oldState, event, newState)) {
68+
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VM_STOP, vo.getAccountId(), vo.getDataCenterIdToDeployIn(), vo.getId(), vo.getHostName());
69+
_usageEventDao.persist(usageEvent);
70+
List<NicVO> nics = _nicDao.listByVmId(vo.getId());
71+
for (NicVO nic : nics) {
72+
NetworkVO network = _networkDao.findById(nic.getNetworkId());
73+
usageEvent = new UsageEventVO(EventTypes.EVENT_NETWORK_OFFERING_REMOVE, vo.getAccountId(), vo.getDataCenterIdToDeployIn(), vo.getId(), null, network.getNetworkOfferingId(), null, 0L);
74+
_usageEventDao.persist(usageEvent);
75+
}
76+
} else if (VirtualMachine.State.isVmDestroyed(oldState, event, newState)) {
77+
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VM_DESTROY, vo.getAccountId(), vo.getDataCenterIdToDeployIn(), vo.getId(), vo.getHostName(), vo.getServiceOfferingId(),
78+
vo.getTemplateId(), vo.getHypervisorType().toString());
79+
_usageEventDao.persist(usageEvent);
80+
}
81+
return true;
82+
}
83+
}

‎server/src/com/cloud/vm/VirtualMachineManagerImpl.java‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import com.cloud.cluster.StackMaid;
6666
import com.cloud.configuration.Config;
6767
import com.cloud.configuration.ConfigurationManager;
68-
import com.cloud.configuration.ResourceCount.ResourceType;
6968
import com.cloud.configuration.dao.ConfigurationDao;
7069
import com.cloud.consoleproxy.ConsoleProxyManager;
7170
import com.cloud.dc.DataCenter;
@@ -79,7 +78,6 @@
7978
import com.cloud.deploy.DeploymentPlanner;
8079
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
8180
import com.cloud.domain.dao.DomainDao;
82-
import com.cloud.event.dao.UsageEventDao;
8381
import com.cloud.exception.AgentUnavailableException;
8482
import com.cloud.exception.ConcurrentOperationException;
8583
import com.cloud.exception.ConnectionException;
@@ -185,8 +183,6 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene
185183
@Inject
186184
protected SecondaryStorageVmDao _secondaryDao;
187185
@Inject
188-
protected UsageEventDao _usageEventDao;
189-
@Inject
190186
protected NicDao _nicsDao;
191187
@Inject
192188
protected AccountManager _accountMgr;

0 commit comments

Comments
 (0)