Fix "Count" for listSnapshotPoliciesCmd.

This commit is contained in:
Min Chen 2013-01-18 13:55:11 -08:00
parent e8c0c1a8ee
commit 2955d58976
5 changed files with 79 additions and 59 deletions

View File

@ -82,7 +82,7 @@ public interface SnapshotService {
* the command that specifies the volume criteria
* @return list of snapshot policies
*/
List<? extends SnapshotPolicy> listPoliciesforVolume(ListSnapshotPoliciesCmd cmd);
Pair<List<? extends SnapshotPolicy>, Integer> listPoliciesforVolume(ListSnapshotPoliciesCmd cmd);
boolean deleteSnapshotPolicies(DeleteSnapshotPoliciesCmd cmd);

View File

@ -29,6 +29,7 @@ import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.SnapshotPolicyResponse;
import org.apache.cloudstack.api.response.VolumeResponse;
import com.cloud.storage.snapshot.SnapshotPolicy;
import com.cloud.utils.Pair;
@APICommand(name = "listSnapshotPolicies", description="Lists snapshot policies.", responseObject=SnapshotPolicyResponse.class)
public class ListSnapshotPoliciesCmd extends BaseListCmd {
@ -63,15 +64,15 @@ public class ListSnapshotPoliciesCmd extends BaseListCmd {
@Override
public void execute(){
List<? extends SnapshotPolicy> result = _snapshotService.listPoliciesforVolume(this);
Pair<List<? extends SnapshotPolicy>, Integer> result = _snapshotService.listPoliciesforVolume(this);
ListResponse<SnapshotPolicyResponse> response = new ListResponse<SnapshotPolicyResponse>();
List<SnapshotPolicyResponse> policyResponses = new ArrayList<SnapshotPolicyResponse>();
for (SnapshotPolicy policy : result) {
for (SnapshotPolicy policy : result.first()) {
SnapshotPolicyResponse policyResponse = _responseGenerator.createSnapshotPolicyResponse(policy);
policyResponse.setObjectName("snapshotpolicy");
policyResponses.add(policyResponse);
}
response.setResponses(policyResponses);
response.setResponses(policyResponses, result.second());
response.setResponseName(getCommandName());
this.setResponseObject(response);
}

View File

@ -20,6 +20,7 @@ import java.util.List;
import com.cloud.storage.SnapshotPolicyVO;
import com.cloud.utils.DateUtil.IntervalType;
import com.cloud.utils.Pair;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDao;
@ -29,6 +30,8 @@ import com.cloud.utils.db.GenericDao;
public interface SnapshotPolicyDao extends GenericDao<SnapshotPolicyVO, Long> {
List<SnapshotPolicyVO> listByVolumeId(long volumeId);
List<SnapshotPolicyVO> listByVolumeId(long volumeId, Filter filter);
Pair<List<SnapshotPolicyVO>, Integer> listAndCountByVolumeId(long volumeId);
Pair<List<SnapshotPolicyVO>, Integer> listAndCountByVolumeId(long volumeId, Filter filter);
SnapshotPolicyVO findOneByVolumeInterval(long volumeId, IntervalType intvType);
List<SnapshotPolicyVO> listActivePolicies();
SnapshotPolicyVO findOneByVolume(long volumeId);

View File

@ -23,6 +23,7 @@ import javax.ejb.Local;
import com.cloud.storage.SnapshotPolicyVO;
import com.cloud.utils.DateUtil.IntervalType;
import com.cloud.utils.Pair;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
@ -33,7 +34,7 @@ public class SnapshotPolicyDaoImpl extends GenericDaoBase<SnapshotPolicyVO, Long
private final SearchBuilder<SnapshotPolicyVO> VolumeIdSearch;
private final SearchBuilder<SnapshotPolicyVO> VolumeIdIntervalSearch;
private final SearchBuilder<SnapshotPolicyVO> ActivePolicySearch;
@Override
public SnapshotPolicyVO findOneByVolumeInterval(long volumeId, IntervalType intvType) {
SearchCriteria<SnapshotPolicyVO> sc = VolumeIdIntervalSearch.create();
@ -41,7 +42,7 @@ public class SnapshotPolicyDaoImpl extends GenericDaoBase<SnapshotPolicyVO, Long
sc.setParameters("interval", intvType.ordinal());
return findOneBy(sc);
}
@Override
public SnapshotPolicyVO findOneByVolume(long volumeId) {
SearchCriteria<SnapshotPolicyVO> sc = VolumeIdSearch.create();
@ -49,12 +50,12 @@ public class SnapshotPolicyDaoImpl extends GenericDaoBase<SnapshotPolicyVO, Long
sc.setParameters("active", true);
return findOneBy(sc);
}
@Override
public List<SnapshotPolicyVO> listByVolumeId(long volumeId) {
return listByVolumeId(volumeId, null);
}
@Override
public List<SnapshotPolicyVO> listByVolumeId(long volumeId, Filter filter) {
SearchCriteria<SnapshotPolicyVO> sc = VolumeIdSearch.create();
@ -62,18 +63,31 @@ public class SnapshotPolicyDaoImpl extends GenericDaoBase<SnapshotPolicyVO, Long
sc.setParameters("active", true);
return listBy(sc, filter);
}
@Override
public Pair<List<SnapshotPolicyVO>, Integer> listAndCountByVolumeId(long volumeId) {
return listAndCountByVolumeId(volumeId, null);
}
@Override
public Pair<List<SnapshotPolicyVO>, Integer> listAndCountByVolumeId(long volumeId, Filter filter) {
SearchCriteria<SnapshotPolicyVO> sc = VolumeIdSearch.create();
sc.setParameters("volumeId", volumeId);
sc.setParameters("active", true);
return searchAndCount(sc, filter);
}
protected SnapshotPolicyDaoImpl() {
VolumeIdSearch = createSearchBuilder();
VolumeIdSearch.and("volumeId", VolumeIdSearch.entity().getVolumeId(), SearchCriteria.Op.EQ);
VolumeIdSearch.and("active", VolumeIdSearch.entity().isActive(), SearchCriteria.Op.EQ);
VolumeIdSearch.done();
VolumeIdIntervalSearch = createSearchBuilder();
VolumeIdIntervalSearch.and("volumeId", VolumeIdIntervalSearch.entity().getVolumeId(), SearchCriteria.Op.EQ);
VolumeIdIntervalSearch.and("interval", VolumeIdIntervalSearch.entity().getInterval(), SearchCriteria.Op.EQ);
VolumeIdIntervalSearch.done();
ActivePolicySearch = createSearchBuilder();
ActivePolicySearch.and("active", ActivePolicySearch.entity().isActive(), SearchCriteria.Op.EQ);
ActivePolicySearch.done();
@ -84,5 +98,5 @@ public class SnapshotPolicyDaoImpl extends GenericDaoBase<SnapshotPolicyVO, Long
SearchCriteria<SnapshotPolicyVO> sc = ActivePolicySearch.create();
sc.setParameters("active", true);
return listIncludingRemovedBy(sc);
}
}
}

View File

@ -69,6 +69,7 @@ import com.cloud.exception.StorageUnavailableException;
import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.network.PhysicalNetworkTrafficType;
import com.cloud.org.Grouping;
import com.cloud.projects.Project.ListProjectResourcesCriteria;
import com.cloud.resource.ResourceManager;
@ -177,7 +178,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
private SwiftManager _swiftMgr;
@Inject
private S3Manager _s3Mgr;
@Inject
@Inject
private SecondaryStorageVmManager _ssvmMgr;
@Inject
private ResourceManager _resourceMgr;
@ -187,7 +188,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
private VolumeDao _volumeDao;
@Inject
private ResourceTagDao _resourceTagDao;
String _name;
private int _totalRetries;
private int _pauseInterval;
@ -197,15 +198,15 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
protected SearchBuilder<SnapshotVO> PolicySnapshotSearch;
protected SearchBuilder<SnapshotPolicyVO> PoliciesForSnapSearch;
protected Answer sendToPool(Volume vol, Command cmd) {
StoragePool pool = _storagePoolDao.findById(vol.getPoolId());
long[] hostIdsToTryFirst = null;
Long vmHostId = getHostIdForSnapshotOperation(vol);
if (vmHostId != null) {
hostIdsToTryFirst = new long[] { vmHostId };
}
@ -280,7 +281,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
}
ManageSnapshotCommand cmd = new ManageSnapshotCommand(snapshotId, volume.getPath(), srcPool, preSnapshotPath, snapshot.getName(), vmName);
ManageSnapshotAnswer answer = (ManageSnapshotAnswer) sendToPool(volume, cmd);
// Update the snapshot in the database
if ((answer != null) && answer.getResult()) {
@ -298,7 +299,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
_snapshotDao.update(snapshotId, snapshot);
} else {
long preSnapshotId = 0;
if (preSnapshotVO != null && preSnapshotVO.getBackupSnapshotId() != null) {
preSnapshotId = preId;
// default delta snap number is 16
@ -322,7 +323,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
preSnapshotId = 0;
}
}
//If the volume is moved around, backup a full snapshot to secondary storage
if (volume.getLastPoolId() != null && !volume.getLastPoolId().equals(volume.getPoolId())) {
preSnapshotId = 0;
@ -359,24 +360,24 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
@DB
@ActionEvent(eventType = EventTypes.EVENT_SNAPSHOT_CREATE, eventDescription = "creating snapshot", async = true)
public SnapshotVO createSnapshot(Long volumeId, Long policyId, Long snapshotId, Account snapshotOwner) {
VolumeVO volume = _volsDao.findById(volumeId);
VolumeVO volume = _volsDao.findById(volumeId);
if (volume == null) {
throw new InvalidParameterValueException("No such volume exist");
}
if (volume.getState() != Volume.State.Ready) {
throw new InvalidParameterValueException("Volume is not in ready state");
}
SnapshotVO snapshot = null;
boolean backedUp = false;
UserVmVO uservm = null;
// does the caller have the authority to act on this volume
_accountMgr.checkAccess(UserContext.current().getCaller(), null, true, volume);
try {
Long poolId = volume.getPoolId();
if (poolId == null) {
throw new CloudRuntimeException("You cannot take a snapshot of a volume until it has been attached to an instance");
@ -387,7 +388,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
if (uservm != null && uservm.getType() != VirtualMachine.Type.User) {
throw new CloudRuntimeException("Can't take a snapshot on system vm ");
}
StoragePoolVO storagePool = _storagePoolDao.findById(volume.getPoolId());
ClusterVO cluster = _clusterDao.findById(storagePool.getClusterId());
List<HostVO> hosts = _resourceMgr.listAllHostsInCluster(cluster.getId());
@ -407,7 +408,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
throw new CloudRuntimeException("Creating snapshot failed due to volume:" + volumeId + " is associated with vm:" + userVm.getInstanceName() + " is in "
+ userVm.getState().toString() + " state");
}
if(userVm.getHypervisorType() == HypervisorType.VMware || userVm.getHypervisorType() == HypervisorType.KVM) {
List<SnapshotVO> activeSnapshots = _snapshotDao.listByInstanceId(volume.getInstanceId(), Snapshot.Status.Creating, Snapshot.Status.CreatedOnPrimary, Snapshot.Status.BackingUp);
if(activeSnapshots.size() > 1)
@ -560,7 +561,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
} catch (Exception e) {
throw new CloudRuntimeException("downloadSnapshotsFromSwift failed due to " + e.toString());
}
}
private List<String> determineBackupUuids(final SnapshotVO snapshot) {
@ -630,9 +631,9 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
Long dcId = volume.getDataCenterId();
Long accountId = volume.getAccountId();
HostVO secHost = getSecHost(volumeId, volume.getDataCenterId());
String secondaryStoragePoolUrl = secHost.getStorageUrl();
String snapshotUuid = snapshot.getPath();
// In order to verify that the snapshot is not empty,
@ -647,12 +648,12 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
S3TO s3 = _s3Mgr.getS3TO();
checkObjectStorageConfiguration(swift, s3);
long prevSnapshotId = snapshot.getPrevSnapshotId();
if (prevSnapshotId > 0) {
prevSnapshot = _snapshotDao.findByIdIncludingRemoved(prevSnapshotId);
if ( prevSnapshot.getBackupSnapshotId() != null && swift == null) {
if (prevSnapshot.getVersion() != null && prevSnapshot.getVersion().equals("2.2")) {
if (prevSnapshot.getVersion() != null && prevSnapshot.getVersion().equals("2.2")) {
prevBackupUuid = prevSnapshot.getBackupSnapshotId();
prevSnapshotUuid = prevSnapshot.getPath();
}
@ -724,7 +725,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
private HostVO getSecHost(long volumeId, long dcId) {
Long id = _snapshotDao.getSecHostId(volumeId);
if ( id != null) {
if ( id != null) {
return _hostDao.findById(id);
}
return _storageMgr.getSecondaryStorageHost(dcId);
@ -743,7 +744,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
public void postCreateSnapshot(Long volumeId, Long snapshotId, Long policyId, boolean backedUp) {
Long userId = getSnapshotUserId();
SnapshotVO snapshot = _snapshotDao.findById(snapshotId);
if (snapshot != null && snapshot.isRecursive()) {
postCreateRecurringSnapshotForPolicy(userId, volumeId, snapshotId, policyId);
}
@ -783,13 +784,13 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
if (snapshotCheck == null) {
throw new InvalidParameterValueException("unable to find a snapshot with id " + snapshotId);
}
_accountMgr.checkAccess(caller, null, true, snapshotCheck);
if( !Status.BackedUp.equals(snapshotCheck.getStatus() ) ) {
throw new InvalidParameterValueException("Can't delete snapshotshot " + snapshotId + " due to it is not in BackedUp Status");
}
return deleteSnapshotInternal(snapshotId);
}
@ -943,7 +944,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
String snapshotTypeStr = cmd.getSnapshotType();
String intervalTypeStr = cmd.getIntervalType();
Map<String, String> tags = cmd.getTags();
Account caller = UserContext.current().getCaller();
List<Long> permittedAccounts = new ArrayList<Long>();
@ -959,8 +960,8 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
_accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
Long domainId = domainIdRecursiveListProject.first();
Boolean isRecursive = domainIdRecursiveListProject.second();
ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
Filter searchFilter = new Filter(SnapshotVO.class, "created", false, cmd.getStartIndex(), cmd.getPageSizeVal());
SearchBuilder<SnapshotVO> sb = _snapshotDao.createSearchBuilder();
_accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
@ -971,7 +972,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
sb.and("snapshotTypeEQ", sb.entity().getsnapshotType(), SearchCriteria.Op.IN);
sb.and("snapshotTypeNEQ", sb.entity().getsnapshotType(), SearchCriteria.Op.NEQ);
if (tags != null && !tags.isEmpty()) {
SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
for (int count=0; count < tags.size(); count++) {
@ -990,7 +991,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
if (volumeId != null) {
sc.setParameters("volumeId", volumeId);
}
if (tags != null && !tags.isEmpty()) {
int count = 0;
sc.setJoinParameters("tagSearch", "resourceType", TaggedResourceType.Snapshot.toString());
@ -1133,9 +1134,9 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
if (volume == null) {
throw new InvalidParameterValueException("Failed to create snapshot policy, unable to find a volume with id " + volumeId);
}
_accountMgr.checkAccess(UserContext.current().getCaller(), null, true, volume);
if (volume.getState() != Volume.State.Ready) {
throw new InvalidParameterValueException("VolumeId: " + volumeId + " is not in " + Volume.State.Ready + " state but " + volume.getState() + ". Cannot take snapshot.");
}
@ -1191,7 +1192,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
if (owner.getType() == Account.ACCOUNT_TYPE_PROJECT) {
message = "domain/project";
}
throw new InvalidParameterValueException("Max number of snapshots shouldn't exceed the " + message + " level snapshot limit");
}
@ -1227,14 +1228,15 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
}
@Override
public List<SnapshotPolicyVO> listPoliciesforVolume(ListSnapshotPoliciesCmd cmd) {
public Pair<List<? extends SnapshotPolicy>, Integer> listPoliciesforVolume(ListSnapshotPoliciesCmd cmd) {
Long volumeId = cmd.getVolumeId();
VolumeVO volume = _volsDao.findById(volumeId);
if (volume == null) {
throw new InvalidParameterValueException("Unable to find a volume with id " + volumeId);
}
_accountMgr.checkAccess(UserContext.current().getCaller(), null, true, volume);
return listPoliciesforVolume(cmd.getVolumeId());
Pair<List<SnapshotPolicyVO>, Integer> result = _snapshotPolicyDao.listAndCountByVolumeId(volumeId);
return new Pair<List<? extends SnapshotPolicy>, Integer>(result.first(), result.second());
}
@Override
@ -1352,7 +1354,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
@Override
public SnapshotVO allocSnapshot(Long volumeId, Long policyId) throws ResourceAllocationException {
Account caller = UserContext.current().getCaller();
VolumeVO volume = _volsDao.findById(volumeId);
if (volume == null) {
throw new InvalidParameterValueException("Creating snapshot failed due to volume:" + volumeId + " doesn't exist");
@ -1361,11 +1363,11 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
if (zone == null) {
throw new InvalidParameterValueException("Can't find zone by id " + volume.getDataCenterId());
}
if (Grouping.AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(caller.getType())) {
throw new PermissionDeniedException("Cannot perform this operation, Zone is currently disabled: " + zone.getName());
}
if (volume.getState() != Volume.State.Ready) {
throw new InvalidParameterValueException("VolumeId: " + volumeId + " is not in " + Volume.State.Ready + " state but " + volume.getState() + ". Cannot take snapshot.");
}
@ -1376,7 +1378,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
throw new InvalidParameterValueException("VolumeId: " + volumeId + " is for System VM , Creating snapshot against System VM volumes is not supported");
}
}
StoragePoolVO storagePoolVO = _storagePoolDao.findById(volume.getPoolId());
if (storagePoolVO == null) {
throw new InvalidParameterValueException("VolumeId: " + volumeId + " please attach this volume to a VM before create snapshot for it");
@ -1386,7 +1388,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
if (cluster != null && cluster.getHypervisorType() == HypervisorType.Ovm) {
throw new InvalidParameterValueException("Ovm won't support taking snapshot");
}
// Verify permissions
_accountMgr.checkAccess(caller, null, true, volume);
Type snapshotType = getSnapshotType(policyId);
@ -1397,7 +1399,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
if (snapshotType != Type.MANUAL){
String msg = "Snapshot resource limit exceeded for account id : " + owner.getId() + ". Failed to create recurring snapshots";
s_logger.warn(msg);
_alertMgr.sendAlert(AlertManager.ALERT_TYPE_UPDATE_RESOURCE_COUNT, 0L, 0L, msg,
_alertMgr.sendAlert(AlertManager.ALERT_TYPE_UPDATE_RESOURCE_COUNT, 0L, 0L, msg,
"Snapshot resource limit exceeded for account id : " + owner.getId() + ". Failed to create recurring snapshots; please use updateResourceLimit to increase the limit");
}
throw e;
@ -1415,7 +1417,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
String snapshotName = vmDisplayName + "_" + volume.getName() + "_" + timeString;
// Create the Snapshot object and save it so we can return it to the
// user
// user
HypervisorType hypervisorType = this._volsDao.getHypervisorType(volumeId);
SnapshotVO snapshotVO = new SnapshotVO(volume.getDataCenterId(), volume.getAccountId(), volume.getDomainId(), volume.getId(), volume.getDiskOfferingId(), null, snapshotName,
(short) snapshotType.ordinal(), snapshotType.name(), volume.getSize(), hypervisorType);
@ -1436,7 +1438,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
if (configDao == null) {
throw new ConfigurationException("Unable to get the configuration dao.");
}
String value = configDao.getValue(Config.BackupSnapshotWait.toString());
_backupsnapshotwait = NumbersUtil.parseInt(value, Integer.parseInt(Config.BackupSnapshotWait.getDefaultValue()));
@ -1533,7 +1535,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma
}
return false;
}
@Override
public boolean canOperateOnVolume(VolumeVO volume) {
List<SnapshotVO> snapshots = _snapshotDao.listByStatus(volume.getId(), Status.Creating, Status.CreatedOnPrimary, Status.BackingUp);