[
https://issues.apache.org/jira/browse/CLOUDSTACK-10202?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16307640#comment-16307640
]
ASF GitHub Bot commented on CLOUDSTACK-10202:
---------------------------------------------
rhtyd closed pull request #2373: CLOUDSTACK-10202:createSnapshotPolicy API
create multiple entries in DB for same volume.
URL: https://github.com/apache/cloudstack/pull/2373
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
index f074c332e3c..2c4de7e1042 100755
--- a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
+++ b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
@@ -17,6 +17,7 @@
package com.cloud.storage.snapshot;
import com.cloud.agent.api.Answer;
+import com.cloud.utils.db.GlobalLock;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.DeleteSnapshotsDirCommand;
import com.cloud.alert.AlertManager;
@@ -785,6 +786,7 @@ public boolean deleteSnapshotDirsForAccount(long accountId)
{
public SnapshotPolicyVO createPolicy(CreateSnapshotPolicyCmd cmd, Account
policyOwner) {
Long volumeId = cmd.getVolumeId();
boolean display = cmd.isDisplay();
+ SnapshotPolicyVO policy = null;
VolumeVO volume = _volsDao.findById(cmd.getVolumeId());
if (volume == null) {
throw new InvalidParameterValueException("Failed to create
snapshot policy, unable to find a volume with id " + volumeId);
@@ -854,33 +856,39 @@ public SnapshotPolicyVO
createPolicy(CreateSnapshotPolicyCmd cmd, Account policy
throw new InvalidParameterValueException("Max number of
snapshots shouldn't exceed the " + message + " level snapshot limit");
}
}
- SnapshotPolicyVO policy =
_snapshotPolicyDao.findOneByVolumeInterval(volumeId, intvType);
- if (policy == null) {
- policy = new SnapshotPolicyVO(volumeId, cmd.getSchedule(),
timezoneId, intvType, cmd.getMaxSnaps(), display);
- policy = _snapshotPolicyDao.persist(policy);
- _snapSchedMgr.scheduleNextSnapshotJob(policy);
- } else {
+
+ final GlobalLock createSnapshotPolicyLock =
GlobalLock.getInternLock("createSnapshotPolicy_" + volumeId);
+ boolean isLockAcquired = createSnapshotPolicyLock.lock(5);
+ if (isLockAcquired) {
+ s_logger.debug("Acquired lock for creating snapshot policy of
volume : " + volume.getName());
try {
- boolean previousDisplay = policy.isDisplay();
- policy = _snapshotPolicyDao.acquireInLockTable(policy.getId());
- policy.setSchedule(cmd.getSchedule());
- policy.setTimezone(timezoneId);
- policy.setInterval((short)intvType.ordinal());
- policy.setMaxSnaps(cmd.getMaxSnaps());
- policy.setActive(true);
- policy.setDisplay(display);
- _snapshotPolicyDao.update(policy.getId(), policy);
-
_snapSchedMgr.scheduleOrCancelNextSnapshotJobOnDisplayChange(policy,
previousDisplay);
- } finally {
- if (policy != null) {
- _snapshotPolicyDao.releaseFromLockTable(policy.getId());
+ policy = _snapshotPolicyDao.findOneByVolumeInterval(volumeId,
intvType);
+ if (policy == null) {
+ policy = new SnapshotPolicyVO(volumeId, cmd.getSchedule(),
timezoneId, intvType, cmd.getMaxSnaps(), display);
+ policy = _snapshotPolicyDao.persist(policy);
+ _snapSchedMgr.scheduleNextSnapshotJob(policy);
+ } else {
+ boolean previousDisplay = policy.isDisplay();
+ policy.setSchedule(cmd.getSchedule());
+ policy.setTimezone(timezoneId);
+ policy.setInterval((short)intvType.ordinal());
+ policy.setMaxSnaps(cmd.getMaxSnaps());
+ policy.setActive(true);
+ policy.setDisplay(display);
+ _snapshotPolicyDao.update(policy.getId(), policy);
+
_snapSchedMgr.scheduleOrCancelNextSnapshotJobOnDisplayChange(policy,
previousDisplay);
}
+ } finally {
+ createSnapshotPolicyLock.unlock();
}
+ // TODO - Make createSnapshotPolicy - BaseAsyncCreate and remove
this.
+ CallContext.current().putContextParameter(SnapshotPolicy.class,
policy.getUuid());
+ return policy;
+ } else {
+ s_logger.warn("Unable to acquire lock for creating snapshot policy
of volume : " + volume.getName());
+ return null;
}
- // TODO - Make createSnapshotPolicy - BaseAsyncCreate and remove this.
- CallContext.current().putContextParameter(SnapshotPolicy.class,
policy.getUuid());
- return policy;
}
protected boolean deletePolicy(long userId, Long policyId) {
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> createSnapshotPolicy API create multiple entries in DB for same volume.
> -----------------------------------------------------------------------
>
> Key: CLOUDSTACK-10202
> URL: https://issues.apache.org/jira/browse/CLOUDSTACK-10202
> Project: CloudStack
> Issue Type: Bug
> Security Level: Public(Anyone can view this level - this is the
> default.)
> Reporter: Nitesh Sarda
>
> *ISSUE :*
> createSnapshotPolicy API create multiple entries in DB for same parameters,
> if multiple threads are executed in parallel.
> *STEPS TO REPRODUCE :*
> # Created a new machine having root and data disk.
> # Make sure that no existing snapshot policy is present for the volume.
> # Execute multiple threads in parallel for createSnapshotPolicy API having
> all required parameters exactly same.
> # Verify table snapshot_policy in DB, will get multiple entries for same
> policy.
> # Once again execute same multiple threads, by changing any API parameter,
> will see that existing entries are getting modified in DB and no new entries
> are added.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)