Skip to content

Commit fd6900a

Browse files
author
kishan
committed
bug 10382: wrapped usage events in txn
1 parent 1dcbfe0 commit fd6900a

5 files changed

Lines changed: 51 additions & 26 deletions

File tree

‎server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public boolean deleteLoadBalancerRule(long loadBalancerId, boolean apply, Accoun
340340
return true;
341341
}
342342

343-
@Override
343+
@Override @DB
344344
@ActionEvent(eventType = EventTypes.EVENT_LOAD_BALANCER_CREATE, eventDescription = "creating load balancer")
345345
public LoadBalancer createLoadBalancerRule(LoadBalancer lb) throws NetworkRuleConflictException {
346346
UserContext caller = UserContext.current();
@@ -394,6 +394,9 @@ public LoadBalancer createLoadBalancerRule(LoadBalancer lb) throws NetworkRuleCo
394394
throw new InvalidParameterValueException("LB service is not supported in network id=" + networkId);
395395
}
396396

397+
Transaction txn = Transaction.currentTxn();
398+
txn.start();
399+
397400
LoadBalancerVO newRule = new LoadBalancerVO(lb.getXid(), lb.getName(), lb.getDescription(), lb.getSourceIpAddressId(), lb.getSourcePortEnd(), lb.getDefaultPortStart(), lb.getAlgorithm(),
398401
networkId, ipAddr.getAccountId(), ipAddr.getDomainId());
399402

@@ -408,6 +411,7 @@ public LoadBalancer createLoadBalancerRule(LoadBalancer lb) throws NetworkRuleCo
408411
UserContext.current().setEventDetails("Load balancer Id: " + newRule.getId());
409412
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_LOAD_BALANCER_CREATE, ipAddr.getAllocatedToAccountId(), ipAddr.getDataCenterId(), newRule.getId(), null);
410413
_usageEventDao.persist(usageEvent);
414+
txn.commit();
411415
return newRule;
412416
} catch (Exception e) {
413417
_lbDao.remove(newRule.getId());

‎server/src/com/cloud/network/rules/RulesManagerImpl.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void checkRuleAndUserVm(FirewallRule rule, UserVm userVm, Account caller)
188188
}
189189
}
190190

191-
@Override
191+
@Override @DB
192192
@ActionEvent(eventType = EventTypes.EVENT_NET_RULE_ADD, eventDescription = "creating forwarding rule", create = true)
193193
public PortForwardingRule createPortForwardingRule(PortForwardingRule rule, Long vmId) throws NetworkRuleConflictException {
194194
UserContext ctx = UserContext.current();
@@ -257,6 +257,9 @@ public PortForwardingRule createPortForwardingRule(PortForwardingRule rule, Long
257257
dstIp = new Ip(guestNic.getIp4Address());
258258
}
259259

260+
Transaction txn = Transaction.currentTxn();
261+
txn.start();
262+
260263
PortForwardingRuleVO newRule = new PortForwardingRuleVO(rule.getXid(), rule.getSourceIpAddressId(), rule.getSourcePortStart(), rule.getSourcePortEnd(), dstIp, rule.getDestinationPortStart(),
261264
rule.getDestinationPortEnd(), rule.getProtocol().toLowerCase(), rule.getSourceCidrList(), networkId, accountId, domainId, vmId);
262265
newRule = _forwardingDao.persist(newRule);
@@ -269,6 +272,7 @@ public PortForwardingRule createPortForwardingRule(PortForwardingRule rule, Long
269272
UserContext.current().setEventDetails("Rule Id: " + newRule.getId());
270273
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_NET_RULE_ADD, newRule.getAccountId(), ipAddress.getDataCenterId(), newRule.getId(), null);
271274
_usageEventDao.persist(usageEvent);
275+
txn.commit();
272276
return newRule;
273277
} catch (Exception e) {
274278
_forwardingDao.remove(newRule.getId());
@@ -279,7 +283,7 @@ public PortForwardingRule createPortForwardingRule(PortForwardingRule rule, Long
279283
}
280284
}
281285

282-
@Override
286+
@Override @DB
283287
@ActionEvent(eventType = EventTypes.EVENT_NET_RULE_ADD, eventDescription = "creating static nat rule", create = true)
284288
public StaticNatRule createStaticNatRule(StaticNatRule rule) throws NetworkRuleConflictException {
285289
Account caller = UserContext.current().getCaller();
@@ -330,6 +334,9 @@ public StaticNatRule createStaticNatRule(StaticNatRule rule) throws NetworkRuleC
330334
throw new InvalidParameterValueException("Start port can't be bigger than end port");
331335
}
332336

337+
Transaction txn = Transaction.currentTxn();
338+
txn.start();
339+
333340
FirewallRuleVO newRule = new FirewallRuleVO(rule.getXid(), rule.getSourceIpAddressId(), rule.getSourcePortStart(), rule.getSourcePortEnd(), rule.getProtocol().toLowerCase(),
334341
networkId, accountId, domainId, rule.getPurpose());
335342
newRule = _firewallDao.persist(newRule);
@@ -343,6 +350,7 @@ public StaticNatRule createStaticNatRule(StaticNatRule rule) throws NetworkRuleC
343350
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_NET_RULE_ADD, newRule.getAccountId(), 0, newRule.getId(), null);
344351
_usageEventDao.persist(usageEvent);
345352

353+
txn.commit();
346354
StaticNatRule staticNatRule = new StaticNatRuleImpl(newRule, dstIp);
347355

348356
return staticNatRule;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,7 @@ public VolumeVO moveVolume(VolumeVO volume, long destPoolDcId, Long destPoolPodI
15911591
* Just allocate a volume in the database, don't send the createvolume cmd to hypervisor. The volume will be finally created
15921592
* only when it's attached to a VM.
15931593
*/
1594-
@Override
1594+
@Override @DB
15951595
@ActionEvent(eventType = EventTypes.EVENT_VOLUME_CREATE, eventDescription = "creating volume", create = true)
15961596
public VolumeVO allocVolume(CreateVolumeCmd cmd) throws ResourceAllocationException {
15971597
// FIXME: some of the scheduled event stuff might be missing here...
@@ -1743,6 +1743,9 @@ public VolumeVO allocVolume(CreateVolumeCmd cmd) throws ResourceAllocationExcept
17431743
userSpecifiedName = getRandomVolumeName();
17441744
}
17451745

1746+
Transaction txn = Transaction.currentTxn();
1747+
txn.start();
1748+
17461749
VolumeVO volume = new VolumeVO(userSpecifiedName, -1, -1, -1, -1, new Long(-1), null, null, 0, Volume.Type.DATADISK);
17471750
volume.setPoolId(null);
17481751
volume.setDataCenterId(zoneId);
@@ -1758,6 +1761,7 @@ public VolumeVO allocVolume(CreateVolumeCmd cmd) throws ResourceAllocationExcept
17581761
volume = _volsDao.persist(volume);
17591762
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VOLUME_CREATE, volume.getAccountId(), volume.getDataCenterId(), volume.getId(), volume.getName(), diskOfferingId, null, size);
17601763
_usageEventDao.persist(usageEvent);
1764+
txn.commit();
17611765

17621766
UserContext.current().setEventDetails("Volume Id: " + volume.getId());
17631767

‎server/src/com/cloud/storage/download/DownloadMonitorImpl.java‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@
7373
import com.cloud.storage.template.TemplateInfo;
7474
import com.cloud.user.Account;
7575
import com.cloud.utils.component.Inject;
76+
import com.cloud.utils.db.DB;
7677
import com.cloud.utils.db.JoinBuilder;
7778
import com.cloud.utils.db.SearchBuilder;
7879
import com.cloud.utils.db.SearchCriteria;
80+
import com.cloud.utils.db.Transaction;
7981
import com.cloud.vm.SecondaryStorageVm;
8082
import com.cloud.vm.SecondaryStorageVmVO;
8183
import com.cloud.vm.UserVmManager;
@@ -381,6 +383,7 @@ private void initiateTemplateDownload(Long templateId, HostVO ssHost) {
381383

382384
}
383385

386+
@DB
384387
public void handleDownloadEvent(HostVO host, VMTemplateVO template, Status dnldStatus) {
385388
if ((dnldStatus == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) || (dnldStatus==Status.ABANDONED)){
386389
VMTemplateHostVO vmTemplateHost = new VMTemplateHostVO(host.getId(), template.getId());
@@ -391,24 +394,9 @@ public void handleDownloadEvent(HostVO host, VMTemplateVO template, Status dnldS
391394

392395
VMTemplateHostVO vmTemplateHost = _vmTemplateHostDao.findByHostTemplate(host.getId(), template.getId());
393396

394-
if (dnldStatus == Status.DOWNLOADED) {
395-
long size = -1;
396-
if(vmTemplateHost!=null){
397-
size = vmTemplateHost.getPhysicalSize();
398-
}
399-
else{
400-
s_logger.warn("Failed to get size for template" + template.getName());
401-
}
402-
String eventType = EventTypes.EVENT_TEMPLATE_CREATE;
403-
if((template.getFormat()).equals(ImageFormat.ISO)){
404-
eventType = EventTypes.EVENT_ISO_CREATE;
405-
}
406-
if(template.getAccountId() != Account.ACCOUNT_ID_SYSTEM){
407-
UsageEventVO usageEvent = new UsageEventVO(eventType, template.getAccountId(), host.getDataCenterId(), template.getId(), template.getName(), null, template.getSourceTemplateId() , size);
408-
_usageEventDao.persist(usageEvent);
409-
}
410-
}
411-
397+
Transaction txn = Transaction.currentTxn();
398+
txn.start();
399+
412400
if (vmTemplateHost != null) {
413401
Long poolId = vmTemplateHost.getPoolId();
414402
if (poolId != null) {
@@ -440,6 +428,24 @@ public void handleDownloadEvent(HostVO host, VMTemplateVO template, Status dnldS
440428
}
441429
}
442430

431+
if (dnldStatus == Status.DOWNLOADED) {
432+
long size = -1;
433+
if(vmTemplateHost!=null){
434+
size = vmTemplateHost.getPhysicalSize();
435+
}
436+
else{
437+
s_logger.warn("Failed to get size for template" + template.getName());
438+
}
439+
String eventType = EventTypes.EVENT_TEMPLATE_CREATE;
440+
if((template.getFormat()).equals(ImageFormat.ISO)){
441+
eventType = EventTypes.EVENT_ISO_CREATE;
442+
}
443+
if(template.getAccountId() != Account.ACCOUNT_ID_SYSTEM){
444+
UsageEventVO usageEvent = new UsageEventVO(eventType, template.getAccountId(), host.getDataCenterId(), template.getId(), template.getName(), null, template.getSourceTemplateId() , size);
445+
_usageEventDao.persist(usageEvent);
446+
}
447+
}
448+
txn.commit();
443449
}
444450

445451
@Override

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,8 @@ public VMTemplateVO createPrivateTemplate(CreateTemplateCmd command) throws Clou
15801580
// Specify RAW format makes it unusable for snapshots.
15811581
privateTemplate.setFormat(ImageFormat.RAW);
15821582
}
1583+
Transaction txn = Transaction.currentTxn();
1584+
txn.start();
15831585
String checkSum = getChecksum(secondaryStorageHost.getId(), answer.getPath());
15841586
privateTemplate.setChecksum(checkSum);
15851587
_templateDao.update(templateId, privateTemplate);
@@ -1598,7 +1600,7 @@ public VMTemplateVO createPrivateTemplate(CreateTemplateCmd command) throws Clou
15981600
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_TEMPLATE_CREATE, privateTemplate.getAccountId(), secondaryStorageHost.getDataCenterId(), privateTemplate.getId(),
15991601
privateTemplate.getName(), null, privateTemplate.getSourceTemplateId(), templateHostVO.getSize());
16001602
_usageEventDao.persist(usageEvent);
1601-
1603+
txn.commit();
16021604
}
16031605
} finally {
16041606
if (privateTemplate == null) {
@@ -2320,7 +2322,7 @@ public UserVm createAdvancedVirtualMachine(DataCenter zone, ServiceOffering serv
23202322
return createVirtualMachine(zone, serviceOffering, template, hostName, displayName, owner, diskOfferingId, diskSize, networkList, null, group, userData, sshKeyPair, hypervisor, caller, requestedIps, defaultIp);
23212323
}
23222324

2323-
@ActionEvent(eventType = EventTypes.EVENT_VM_CREATE, eventDescription = "deploying Vm", create = true)
2325+
@DB @ActionEvent(eventType = EventTypes.EVENT_VM_CREATE, eventDescription = "deploying Vm", create = true)
23242326
protected UserVm createVirtualMachine(DataCenter zone, ServiceOffering serviceOffering, VirtualMachineTemplate template, String hostName, String displayName, Account owner, Long diskOfferingId,
23252327
Long diskSize, List<NetworkVO> networkList, List<Long> securityGroupIdList, String group, String userData, String sshKeyPair, HypervisorType hypervisor, Account caller, Map<Long, String> requestedIps, String defaultNetworkIp) throws InsufficientCapacityException, ResourceUnavailableException, ConcurrentOperationException, StorageUnavailableException, ResourceAllocationException {
23262328

@@ -2498,7 +2500,8 @@ protected UserVm createVirtualMachine(DataCenter zone, ServiceOffering serviceOf
24982500
} else {
24992501
hypervisorType = template.getHypervisorType();
25002502
}
2501-
2503+
Transaction txn = Transaction.currentTxn();
2504+
txn.start();
25022505
UserVmVO vm = new UserVmVO(id, instanceName, displayName, template.getId(), hypervisorType, template.getGuestOSId(), offering.getOfferHA(), offering.getLimitCpuUse(), owner.getDomainId(), owner.getId(),
25032506
offering.getId(), userData, hostName);
25042507

@@ -2527,7 +2530,7 @@ protected UserVm createVirtualMachine(DataCenter zone, ServiceOffering serviceOf
25272530
_usageEventDao.persist(usageEvent);
25282531

25292532
_accountMgr.incrementResourceCount(accountId, ResourceType.user_vm);
2530-
2533+
txn.commit();
25312534
// Assign instance to the group
25322535
try {
25332536
if (group != null) {

0 commit comments

Comments
 (0)