CLOUDSTACK-2674: Secondary Storage garbage collector failed with NPE in

case of S3 storage provider.
This commit is contained in:
Min Chen 2013-05-24 11:10:45 -07:00
parent 3c7fb2f790
commit c614c6a424
4 changed files with 13 additions and 33 deletions

View File

@ -87,6 +87,7 @@ import com.cloud.storage.dao.VolumeDao;
import com.cloud.storage.download.DownloadMonitor;
import com.cloud.storage.template.TemplateConstants;
import com.cloud.storage.template.TemplateProp;
import com.cloud.template.TemplateManager;
import com.cloud.user.AccountManager;
import com.cloud.user.ResourceLimitService;
import com.cloud.utils.UriUtils;
@ -138,6 +139,8 @@ public class TemplateServiceImpl implements TemplateService {
EndPointSelector _epSelector;
@Inject
ImageDataManager imageMgr;
@Inject
TemplateManager _tmpltMgr;
class TemplateOpContext<T> extends AsyncRpcConext<T> {
@ -430,9 +433,7 @@ public class TemplateServiceImpl implements TemplateService {
for (String uniqueName : templateInfos.keySet()) {
TemplateProp tInfo = templateInfos.get(uniqueName);
List<UserVmJoinVO> userVmUsingIso = _userVmJoinDao.listActiveByIsoId(tInfo.getId());
//check if there is any Vm using this ISO.
if (userVmUsingIso == null || userVmUsingIso.isEmpty()) {
if (_tmpltMgr.templateIsDeleteable(tInfo.getId())) {
//TODO: we cannot directly call deleteTemplateSync here to reuse delete logic since in this case, our db does not have this template at all.
VMTemplateVO template = _templateDao.findById(tInfo.getId());
DeleteTemplateCommand dtCommand = new DeleteTemplateCommand(store.getTO(), tInfo.getInstallPath(), null, null);

View File

@ -1133,7 +1133,7 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C
s_logger.debug("Secondary storage garbage collector found " + destroyedTemplateStoreVOs.size()
+ " templates to cleanup on secondary storage host: " + store.getName());
for (TemplateDataStoreVO destroyedTemplateStoreVO : destroyedTemplateStoreVOs) {
if (!_tmpltMgr.templateIsDeleteable(destroyedTemplateStoreVO)) {
if (!_tmpltMgr.templateIsDeleteable(destroyedTemplateStoreVO.getTemplateId())) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Not deleting template at: " + destroyedTemplateStoreVO);
}

View File

@ -92,7 +92,7 @@ public interface TemplateManager extends TemplateApiService{
boolean templateIsDeleteable(VMTemplateHostVO templateHostRef);
boolean templateIsDeleteable(TemplateDataStoreVO templateStoreRef);
boolean templateIsDeleteable(long templateId);
Pair<String, String> getAbsoluteIsoPath(long templateId, long dataCenterId);

View File

@ -945,35 +945,15 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
}
@Override
public boolean templateIsDeleteable(TemplateDataStoreVO templateStoreRef) {
VMTemplateVO template = _tmpltDao.findByIdIncludingRemoved(templateStoreRef.getTemplateId());
long templateId = template.getId();
ImageStoreVO imageStore = _imageStoreDao.findById(templateStoreRef.getDataStoreId());
long zoneId = imageStore.getDataCenterId();
DataCenterVO zone = _dcDao.findById(zoneId);
// Check if there are VMs running in the template host ref's zone that use the template
List<VMInstanceVO> nonExpungedVms = _vmInstanceDao.listNonExpungedByZoneAndTemplate(zoneId, templateId);
if (!nonExpungedVms.isEmpty()) {
s_logger.debug("Template " + template.getName() + " in zone " + zone.getName() + " is not deleteable because there are non-expunged VMs deployed from this template.");
return false;
}
List<UserVmVO> userVmUsingIso = _userVmDao.listByIsoId(templateId);
//check if there is any VM using this ISO.
public boolean templateIsDeleteable(long templateId) {
List<UserVmJoinVO> userVmUsingIso = _userVmJoinDao.listActiveByIsoId(templateId);
//check if there is any Vm using this ISO. We only need to check the case where templateId is an ISO since
// VM can be launched from ISO in secondary storage, while template will always be copied to
// primary storage before deploying VM.
if (!userVmUsingIso.isEmpty()) {
s_logger.debug("ISO " + template.getName() + " in zone " + zone.getName() + " is not deleteable because it is attached to " + userVmUsingIso.size() + " VMs");
s_logger.debug("ISO " + templateId + " is not deleteable because it is attached to " + userVmUsingIso.size() + " VMs");
return false;
}
// Check if there are any snapshots for the template in the template host ref's zone
List<VolumeVO> volumes = _volumeDao.findByTemplateAndZone(templateId, zoneId);
for (VolumeVO volume : volumes) {
List<SnapshotVO> snapshots = _snapshotDao.listByVolumeIdVersion(volume.getId(), "2.1");
if (!snapshots.isEmpty()) {
s_logger.debug("Template " + template.getName() + " in zone " + zone.getName() + " is not deleteable because there are 2.1 snapshots using this template.");
return false;
}
}
return true;
}
@ -1153,9 +1133,8 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
throw new InvalidParameterValueException("Please specify a valid iso.");
}
List<UserVmJoinVO> userVmUsingIso = _userVmJoinDao.listActiveByIsoId(templateId);
// check if there is any VM using this ISO.
if (userVmUsingIso != null && !userVmUsingIso.isEmpty()) {
if (!templateIsDeleteable(templateId)) {
throw new InvalidParameterValueException("Unable to delete iso, as it's used by other vms");
}