Fix copyTemplateCmd.

This commit is contained in:
Min Chen 2013-04-16 16:38:14 -07:00
parent 0da2da852b
commit 1b3994e180
15 changed files with 499 additions and 395 deletions

View File

@ -147,8 +147,9 @@ public class DownloadCommand extends AbstractDownloadCommand implements Internal
this.resourceType = ResourceType.VOLUME; this.resourceType = ResourceType.VOLUME;
} }
public DownloadCommand(String secUrl, String url, VirtualMachineTemplate template, String user, String passwd, Long maxDownloadSizeInBytes) { public DownloadCommand(DataStoreTO store, String secUrl, String url, VirtualMachineTemplate template, String user, String passwd, Long maxDownloadSizeInBytes) {
super(template.getUniqueName(), url, template.getFormat(), template.getAccountId()); super(template.getUniqueName(), url, template.getFormat(), template.getAccountId());
this._store = store;
this.hvm = template.isRequiresHvm(); this.hvm = template.isRequiresHvm();
this.checksum = template.getChecksum(); this.checksum = template.getChecksum();
this.id = template.getId(); this.id = template.getId();

View File

@ -44,6 +44,8 @@ public interface TemplateDataStoreDao extends GenericDao<TemplateDataStoreVO, Lo
TemplateDataStoreVO findByStoreTemplate(long storeId, long templateId); TemplateDataStoreVO findByStoreTemplate(long storeId, long templateId);
TemplateDataStoreVO findByStoreTemplate(long storeId, long templateId, boolean lock);
TemplateDataStoreVO findByTemplate(long templateId); TemplateDataStoreVO findByTemplate(long templateId);
List<TemplateDataStoreVO> listByTemplate(long templateId); List<TemplateDataStoreVO> listByTemplate(long templateId);

View File

@ -388,8 +388,7 @@ public class TemplateServiceImpl implements TemplateService {
VMTemplateVO template = _templateDao.findById(tInfo.getId()); VMTemplateVO template = _templateDao.findById(tInfo.getId());
DeleteTemplateCommand dtCommand = new DeleteTemplateCommand(store.getTO(), store.getUri(), tInfo.getInstallPath(), template.getId(), template.getAccountId()); DeleteTemplateCommand dtCommand = new DeleteTemplateCommand(store.getTO(), store.getUri(), tInfo.getInstallPath(), template.getId(), template.getAccountId());
try { try {
HostVO ssAhost = _ssvmMgr.pickSsvmHost(store); _agentMgr.sendToSecStorage(store, dtCommand, null);
_agentMgr.sendToSecStorage(ssAhost, dtCommand, null);
} catch (AgentUnavailableException e) { } catch (AgentUnavailableException e) {
String err = "Failed to delete " + tInfo.getTemplateName() + " on secondary storage " + storeId + " which isn't in the database"; String err = "Failed to delete " + tInfo.getTemplateName() + " on secondary storage " + storeId + " which isn't in the database";
s_logger.error(err); s_logger.error(err);
@ -430,16 +429,15 @@ public class TemplateServiceImpl implements TemplateService {
} }
private Map<String, TemplateProp> listTemplate(DataStore ssHost) { private Map<String, TemplateProp> listTemplate(DataStore ssStore) {
ListTemplateCommand cmd = new ListTemplateCommand(ssHost.getUri()); ListTemplateCommand cmd = new ListTemplateCommand(ssStore.getUri());
HostVO ssAhost = _ssvmMgr.pickSsvmHost(ssHost); Answer answer = _agentMgr.sendToSecStorage(ssStore, cmd);
Answer answer = _agentMgr.sendToSecStorage(ssAhost, cmd);
if (answer != null && answer.getResult()) { if (answer != null && answer.getResult()) {
ListTemplateAnswer tanswer = (ListTemplateAnswer)answer; ListTemplateAnswer tanswer = (ListTemplateAnswer)answer;
return tanswer.getTemplateInfo(); return tanswer.getTemplateInfo();
} else { } else {
if (s_logger.isDebugEnabled()) { if (s_logger.isDebugEnabled()) {
s_logger.debug("can not list template for secondary storage host " + ssHost.getId()); s_logger.debug("can not list template for secondary storage host " + ssStore.getId());
} }
} }

View File

@ -24,6 +24,7 @@ import java.util.Map;
import javax.inject.Inject; import javax.inject.Inject;
import javax.naming.ConfigurationException; import javax.naming.ConfigurationException;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import com.cloud.agent.AgentManager; import com.cloud.agent.AgentManager;
@ -186,6 +187,19 @@ public class DirectAgentManagerSimpleImpl extends ManagerBase implements AgentMa
return null; return null;
} }
@Override
public void sendToSecStorage(DataStore ssStore, Command cmd, Listener listener) throws AgentUnavailableException {
// TODO Auto-generated method stub
}
@Override
public Answer sendToSecStorage(DataStore ssStore, Command cmd) {
// TODO Auto-generated method stub
return null;
}
@Override @Override
public boolean tapLoadingAgents(Long hostId, TapAgentsAction action) { public boolean tapLoadingAgents(Long hostId, TapAgentsAction action) {
// TODO Auto-generated method stub // TODO Auto-generated method stub

View File

@ -206,6 +206,19 @@ public class TemplateDataStoreDaoImpl extends GenericDaoBase<TemplateDataStoreVO
return findOneIncludingRemovedBy(sc); return findOneIncludingRemovedBy(sc);
} }
@Override
public TemplateDataStoreVO findByStoreTemplate(long storeId, long templateId, boolean lock) {
SearchCriteria<TemplateDataStoreVO> sc = storeTemplateSearch.create();
sc.setParameters("store_id", storeId);
sc.setParameters("template_id", templateId);
sc.setParameters("destroyed", false);
if (!lock)
return findOneIncludingRemovedBy(sc);
else
return lockOneRandomRow(sc, true);
}
@Override @Override
public TemplateDataStoreVO findByTemplate(long templateId) { public TemplateDataStoreVO findByTemplate(long templateId) {
SearchCriteria<TemplateDataStoreVO> sc = templateSearch.create(); SearchCriteria<TemplateDataStoreVO> sc = templateSearch.create();

View File

@ -814,8 +814,7 @@ public class VolumeServiceImpl implements VolumeService {
TemplateProp vInfo = volumeInfos.get(uniqueName); TemplateProp vInfo = volumeInfos.get(uniqueName);
DeleteVolumeCommand dtCommand = new DeleteVolumeCommand(store.getUri(), vInfo.getInstallPath()); DeleteVolumeCommand dtCommand = new DeleteVolumeCommand(store.getUri(), vInfo.getInstallPath());
try { try {
HostVO ssAhost = _ssvmMgr.pickSsvmHost(store); _agentMgr.sendToSecStorage(store, dtCommand, null);
_agentMgr.sendToSecStorage(ssAhost, dtCommand, null);
} catch (AgentUnavailableException e) { } catch (AgentUnavailableException e) {
String err = "Failed to delete " + vInfo.getTemplateName() + " on image store " + storeId + " which isn't in the database"; String err = "Failed to delete " + vInfo.getTemplateName() + " on image store " + storeId + " which isn't in the database";
s_logger.error(err); s_logger.error(err);
@ -830,8 +829,7 @@ public class VolumeServiceImpl implements VolumeService {
private Map<Long, TemplateProp> listVolume(DataStore store) { private Map<Long, TemplateProp> listVolume(DataStore store) {
ListVolumeCommand cmd = new ListVolumeCommand(store.getUri()); ListVolumeCommand cmd = new ListVolumeCommand(store.getUri());
HostVO ssAhost = _ssvmMgr.pickSsvmHost(store); Answer answer = _agentMgr.sendToSecStorage(store, cmd);
Answer answer = _agentMgr.sendToSecStorage(ssAhost, cmd);
if (answer != null && answer.getResult()) { if (answer != null && answer.getResult()) {
ListVolumeAnswer tanswer = (ListVolumeAnswer)answer; ListVolumeAnswer tanswer = (ListVolumeAnswer)answer;
return tanswer.getTemplateInfo(); return tanswer.getTemplateInfo();

View File

@ -236,12 +236,11 @@ public class CloudStackImageStoreDriverImpl implements ImageStoreDriver {
List<UserVmVO> userVmUsingIso = _userVmDao.listByIsoId(templateId); List<UserVmVO> userVmUsingIso = _userVmDao.listByIsoId(templateId);
// check if there is any VM using this ISO. // check if there is any VM using this ISO.
if (userVmUsingIso == null || userVmUsingIso.isEmpty()) { if (userVmUsingIso == null || userVmUsingIso.isEmpty()) {
HostVO ssAhost = _ssvmMgr.pickSsvmHost(store);
// get installpath of this template on image store // get installpath of this template on image store
TemplateDataStoreVO tmplStore = _templateStoreDao.findByStoreTemplate(storeId, templateId); TemplateDataStoreVO tmplStore = _templateStoreDao.findByStoreTemplate(storeId, templateId);
String installPath = tmplStore.getInstallPath(); String installPath = tmplStore.getInstallPath();
if (installPath != null) { if (installPath != null) {
Answer answer = _agentMgr.sendToSecStorage(ssAhost, new DeleteTemplateCommand(store.getTO(), store.getUri(), installPath, template.getId(), template.getAccountId())); Answer answer = _agentMgr.sendToSecStorage(store, new DeleteTemplateCommand(store.getTO(), store.getUri(), installPath, template.getId(), template.getAccountId()));
if (answer == null || !answer.getResult()) { if (answer == null || !answer.getResult()) {
s_logger.debug("Failed to deleted template at store: " + store.getName()); s_logger.debug("Failed to deleted template at store: " + store.getName());

View File

@ -16,6 +16,8 @@
// under the License. // under the License.
package com.cloud.agent; package com.cloud.agent;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import com.cloud.agent.api.Answer; import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command; import com.cloud.agent.api.Command;
import com.cloud.agent.api.StartupCommand; import com.cloud.agent.api.StartupCommand;
@ -139,6 +141,10 @@ public interface AgentManager extends Manager {
Answer sendToSecStorage(HostVO ssHost, Command cmd); Answer sendToSecStorage(HostVO ssHost, Command cmd);
void sendToSecStorage(DataStore ssStore, Command cmd, Listener listener) throws AgentUnavailableException;
Answer sendToSecStorage(DataStore ssStore, Command cmd);
/* working as a lock while agent is being loaded */ /* working as a lock while agent is being loaded */
public boolean tapLoadingAgents(Long hostId, TapAgentsAction action); public boolean tapLoadingAgents(Long hostId, TapAgentsAction action);

View File

@ -39,6 +39,7 @@ import javax.ejb.Local;
import javax.inject.Inject; import javax.inject.Inject;
import javax.naming.ConfigurationException; import javax.naming.ConfigurationException;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -380,6 +381,18 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl
return attache; return attache;
} }
@Override
public Answer sendToSecStorage(DataStore ssStore, Command cmd) {
HostVO ssAhost = _ssvmMgr.pickSsvmHost(ssStore);
return easySend(ssAhost.getId(), cmd);
}
@Override
public void sendToSecStorage(DataStore ssStore, Command cmd, Listener listener) throws AgentUnavailableException {
HostVO ssAhost = _ssvmMgr.pickSsvmHost(ssStore);
send(ssAhost.getId(), new Commands(cmd), listener);
}
@Override @Override
public Answer sendToSecStorage(HostVO ssHost, Command cmd) { public Answer sendToSecStorage(HostVO ssHost, Command cmd) {
if( ssHost.getType() == Host.Type.LocalSecondaryStorage ) { if( ssHost.getType() == Host.Type.LocalSecondaryStorage ) {
@ -406,6 +419,7 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl
} }
} }
private void sendToSSVM(final long dcId, final Command cmd, final Listener listener) throws AgentUnavailableException { private void sendToSSVM(final long dcId, final Command cmd, final Listener listener) throws AgentUnavailableException {
List<HostVO> ssAHosts = _ssvmMgr.listUpAndConnectingSecondaryStorageVmHost(dcId); List<HostVO> ssAHosts = _ssvmMgr.listUpAndConnectingSecondaryStorageVmHost(dcId);
if (ssAHosts == null || ssAHosts.isEmpty() ) { if (ssAHosts == null || ssAHosts.isEmpty() ) {

View File

@ -1266,11 +1266,9 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C
String installPath = destroyedTemplateStoreVO String installPath = destroyedTemplateStoreVO
.getInstallPath(); .getInstallPath();
HostVO ssAhost = this._ssvmMgr.pickSsvmHost(store);
if (installPath != null) { if (installPath != null) {
Answer answer = _agentMgr.sendToSecStorage( Answer answer = _agentMgr.sendToSecStorage(store,
ssAhost,
new DeleteTemplateCommand( new DeleteTemplateCommand(
store.getTO(), store.getTO(),
store.getUri(), store.getUri(),

View File

@ -22,7 +22,6 @@ import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
import com.cloud.exception.StorageUnavailableException; import com.cloud.exception.StorageUnavailableException;
import com.cloud.host.HostVO;
import com.cloud.storage.VMTemplateVO; import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.VolumeVO; import com.cloud.storage.VolumeVO;
import com.cloud.storage.Storage.ImageFormat; import com.cloud.storage.Storage.ImageFormat;
@ -38,7 +37,7 @@ public interface DownloadMonitor extends Manager{
public void cancelAllDownloads(Long templateId); public void cancelAllDownloads(Long templateId);
public boolean copyTemplate(VMTemplateVO template, HostVO sourceServer, HostVO destServer) public boolean copyTemplate(VMTemplateVO template, DataStore sourceStore, DataStore Store)
throws StorageUnavailableException; throws StorageUnavailableException;
//void addSystemVMTemplatesToHost(HostVO host, Map<String, TemplateProp> templateInfos); //void addSystemVMTemplatesToHost(HostVO host, Map<String, TemplateProp> templateInfos);

View File

@ -106,7 +106,6 @@ import com.cloud.vm.dao.UserVmDao;
import org.apache.cloudstack.framework.async.AsyncCompletionCallback; import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
@Component @Component
@Local(value = { DownloadMonitor.class }) @Local(value = { DownloadMonitor.class })
public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor { public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor {
@ -147,19 +146,19 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
VMTemplateDao _templateDao = null; VMTemplateDao _templateDao = null;
@Inject @Inject
private AgentManager _agentMgr; private AgentManager _agentMgr;
@Inject SecondaryStorageVmManager _secMgr; @Inject
SecondaryStorageVmManager _secMgr;
@Inject @Inject
ConfigurationDao _configDao; ConfigurationDao _configDao;
@Inject @Inject
UserVmManager _vmMgr; UserVmManager _vmMgr;
@Inject TemplateManager templateMgr; @Inject
TemplateManager templateMgr;
@Inject @Inject
private UsageEventDao _usageEventDao; private UsageEventDao _usageEventDao;
@Inject @Inject
private ClusterDao _clusterDao; private ClusterDao _clusterDao;
@Inject @Inject
@ -182,14 +181,14 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
Timer _timer; Timer _timer;
@Inject DataStoreManager storeMgr; @Inject
DataStoreManager storeMgr;
final Map<TemplateDataStoreVO, DownloadListener> _listenerTemplateMap = new ConcurrentHashMap<TemplateDataStoreVO, DownloadListener>(); final Map<TemplateDataStoreVO, DownloadListener> _listenerTemplateMap = new ConcurrentHashMap<TemplateDataStoreVO, DownloadListener>();
final Map<VMTemplateHostVO, DownloadListener> _listenerMap = new ConcurrentHashMap<VMTemplateHostVO, DownloadListener>(); final Map<VMTemplateHostVO, DownloadListener> _listenerMap = new ConcurrentHashMap<VMTemplateHostVO, DownloadListener>();
final Map<VolumeHostVO, DownloadListener> _listenerVolumeMap = new ConcurrentHashMap<VolumeHostVO, DownloadListener>(); final Map<VolumeHostVO, DownloadListener> _listenerVolumeMap = new ConcurrentHashMap<VolumeHostVO, DownloadListener>();
final Map<VolumeDataStoreVO, DownloadListener> _listenerVolMap = new ConcurrentHashMap<VolumeDataStoreVO, DownloadListener>(); final Map<VolumeDataStoreVO, DownloadListener> _listenerVolMap = new ConcurrentHashMap<VolumeDataStoreVO, DownloadListener>();
public void send(Long hostId, Command cmd, Listener listener) throws AgentUnavailableException { public void send(Long hostId, Command cmd, Listener listener) throws AgentUnavailableException {
_agentMgr.send(hostId, new Commands(cmd), listener); _agentMgr.send(hostId, new Commands(cmd), listener);
} }
@ -237,66 +236,75 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
} }
public boolean isTemplateUpdateable(Long templateId, Long storeId) { public boolean isTemplateUpdateable(Long templateId, Long storeId) {
List<TemplateDataStoreVO> downloadsInProgress = List<TemplateDataStoreVO> downloadsInProgress = _vmTemplateStoreDao.listByTemplateStoreDownloadStatus(templateId, storeId,
_vmTemplateStoreDao.listByTemplateStoreDownloadStatus(templateId, storeId, Status.DOWNLOAD_IN_PROGRESS, Status.DOWNLOADED ); Status.DOWNLOAD_IN_PROGRESS, Status.DOWNLOADED);
return (downloadsInProgress.size() == 0); return (downloadsInProgress.size() == 0);
} }
// TODO: consider using dataMotionStrategy later
@Override @Override
public boolean copyTemplate(VMTemplateVO template, HostVO sourceServer, HostVO destServer) throws StorageUnavailableException{ public boolean copyTemplate(VMTemplateVO template, DataStore sourceStore, DataStore destStore) throws StorageUnavailableException {
boolean downloadJobExists = false; boolean downloadJobExists = false;
VMTemplateHostVO destTmpltHost = null; TemplateDataStoreVO destTmpltStore = null;
VMTemplateHostVO srcTmpltHost = null; TemplateDataStoreVO srcTmpltStore = null;
srcTmpltHost = _vmTemplateHostDao.findByHostTemplate(sourceServer.getId(), template.getId()); srcTmpltStore = this._vmTemplateStoreDao.findByStoreTemplate(sourceStore.getId(), template.getId());
if (srcTmpltHost == null) { if (srcTmpltStore == null) {
throw new InvalidParameterValueException("Template " + template.getName() + " not associated with " + sourceServer.getName()); throw new InvalidParameterValueException("Template " + template.getName() + " not associated with " + sourceStore.getName());
} }
String url = generateCopyUrl(sourceServer, srcTmpltHost); // generate a storage url on ssvm to copy from
String url = generateCopyUrl(sourceStore, srcTmpltStore);
if (url == null) { if (url == null) {
s_logger.warn("Unable to start/resume copy of template " + template.getUniqueName() + " to " + destServer.getName() + ", no secondary storage vm in running state in source zone"); s_logger.warn("Unable to start/resume copy of template " + template.getUniqueName() + " to " + destStore.getName()
throw new CloudRuntimeException("No secondary VM in running state in zone " + sourceServer.getDataCenterId()); + ", no secondary storage vm in running state in source zone");
throw new CloudRuntimeException("No secondary VM in running state in zone " + sourceStore.getScope().getScopeId());
} }
destTmpltHost = _vmTemplateHostDao.findByHostTemplate(destServer.getId(), template.getId()); destTmpltStore = _vmTemplateStoreDao.findByStoreTemplate(destStore.getId(), template.getId());
if (destTmpltHost == null) { if (destTmpltStore == null) {
destTmpltHost = new VMTemplateHostVO(destServer.getId(), template.getId(), new Date(), 0, VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null, "jobid0000", null, url); destTmpltStore = new TemplateDataStoreVO(destStore.getId(), template.getId(), new Date(), 0,
destTmpltHost.setCopy(true); VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null, "jobid0000", null, url);
destTmpltHost.setPhysicalSize(srcTmpltHost.getPhysicalSize()); destTmpltStore.setCopy(true);
_vmTemplateHostDao.persist(destTmpltHost); destTmpltStore.setPhysicalSize(srcTmpltStore.getPhysicalSize());
} else if ((destTmpltHost.getJobId() != null) && (destTmpltHost.getJobId().length() > 2)) { _vmTemplateStoreDao.persist(destTmpltStore);
} else if ((destTmpltStore.getJobId() != null) && (destTmpltStore.getJobId().length() > 2)) {
downloadJobExists = true; downloadJobExists = true;
} }
Long maxTemplateSizeInBytes = getMaxTemplateSizeInBytes(); Long maxTemplateSizeInBytes = getMaxTemplateSizeInBytes();
if (srcTmpltHost.getSize() > maxTemplateSizeInBytes){ if (srcTmpltStore.getSize() > maxTemplateSizeInBytes) {
throw new CloudRuntimeException("Cant copy the template as the template's size " +srcTmpltHost.getSize()+ throw new CloudRuntimeException("Cant copy the template as the template's size " + srcTmpltStore.getSize()
" is greater than max.template.iso.size " + maxTemplateSizeInBytes); + " is greater than max.template.iso.size " + maxTemplateSizeInBytes);
} }
if(destTmpltHost != null) { if (destTmpltStore != null) {
start(); start();
String sourceChecksum = this.templateMgr.getChecksum(srcTmpltHost.getHostId(), srcTmpltHost.getInstallPath()); String sourceChecksum = this.templateMgr.getChecksum(sourceStore, srcTmpltStore.getInstallPath());
DownloadCommand dcmd = DownloadCommand dcmd = new DownloadCommand(destStore.getTO(), destStore.getUri(), url, template,
new DownloadCommand(destServer.getStorageUrl(), url, template, TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd, maxTemplateSizeInBytes); TemplateConstants.DEFAULT_HTTP_AUTH_USER, _copyAuthPasswd, maxTemplateSizeInBytes);
dcmd.setProxy(getHttpProxy()); dcmd.setProxy(getHttpProxy());
if (downloadJobExists) { if (downloadJobExists) {
dcmd = new DownloadProgressCommand(dcmd, destTmpltHost.getJobId(), RequestType.GET_OR_RESTART); dcmd = new DownloadProgressCommand(dcmd, destTmpltStore.getJobId(), RequestType.GET_OR_RESTART);
} }
dcmd.setChecksum(sourceChecksum); // We need to set the checksum as the source template might be a compressed url and have cksum for compressed image. Bug #10775 dcmd.setChecksum(sourceChecksum); // We need to set the checksum as
HostVO ssAhost = _ssvmMgr.pickSsvmHost(destServer); // the source template might be a
// compressed url and have cksum
// for compressed image. Bug
// #10775
HostVO ssAhost = _ssvmMgr.pickSsvmHost(destStore);
if (ssAhost == null) { if (ssAhost == null) {
s_logger.warn("There is no secondary storage VM for secondary storage host " + destServer.getName()); s_logger.warn("There is no secondary storage VM for secondary storage host " + destStore.getName());
return false; return false;
} }
DownloadListener dl = new DownloadListener(ssAhost, destServer, template, _timer, _vmTemplateHostDao, destTmpltHost.getId(), this, dcmd, _templateDao, _resourceLimitMgr, _alertMgr, _accountMgr); DownloadListener dl = new DownloadListener(ssAhost, destStore, template, _timer, _vmTemplateStoreDao, destTmpltStore.getId(), this, dcmd,
_templateDao, _resourceLimitMgr, _alertMgr, _accountMgr, null);
if (downloadJobExists) { if (downloadJobExists) {
dl.setCurrState(destTmpltHost.getDownloadState()); dl.setCurrState(destTmpltStore.getDownloadState());
} }
DownloadListener old = null; DownloadListener old = null;
synchronized (_listenerMap) { synchronized (_listenerTemplateMap) {
old = _listenerMap.put(destTmpltHost, dl); old = _listenerTemplateMap.put(destTmpltStore, dl);
} }
if (old != null) { if (old != null) {
old.abandon(); old.abandon();
@ -306,7 +314,7 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
send(ssAhost.getId(), dcmd, dl); send(ssAhost.getId(), dcmd, dl);
return true; return true;
} catch (AgentUnavailableException e) { } catch (AgentUnavailableException e) {
s_logger.warn("Unable to start /resume COPY of template " + template.getUniqueName() + " to " + destServer.getName(), e); s_logger.warn("Unable to start /resume COPY of template " + template.getUniqueName() + " to " + destStore.getName(), e);
dl.setDisconnected(); dl.setDisconnected();
dl.scheduleStatusCheck(RequestType.GET_OR_RESTART); dl.scheduleStatusCheck(RequestType.GET_OR_RESTART);
e.printStackTrace(); e.printStackTrace();
@ -327,22 +335,26 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
return scheme + "://" + hostname + "/copy/SecStorage/" + dir + "/" + path; return scheme + "://" + hostname + "/copy/SecStorage/" + dir + "/" + path;
} }
private String generateCopyUrl(HostVO sourceServer, VMTemplateHostVO srcTmpltHost) { private String generateCopyUrl(DataStore sourceServer, TemplateDataStoreVO srcTmpltStore) {
List<SecondaryStorageVmVO> ssVms = _secStorageVmDao.getSecStorageVmListInStates(SecondaryStorageVm.Role.templateProcessor, sourceServer.getDataCenterId(), State.Running); List<SecondaryStorageVmVO> ssVms = _secStorageVmDao.getSecStorageVmListInStates(SecondaryStorageVm.Role.templateProcessor, sourceServer
.getScope().getScopeId(), State.Running);
if (ssVms.size() > 0) { if (ssVms.size() > 0) {
SecondaryStorageVmVO ssVm = ssVms.get(0); SecondaryStorageVmVO ssVm = ssVms.get(0);
if (ssVm.getPublicIpAddress() == null) { if (ssVm.getPublicIpAddress() == null) {
s_logger.warn("A running secondary storage vm has a null public ip?"); s_logger.warn("A running secondary storage vm has a null public ip?");
return null; return null;
} }
return generateCopyUrl(ssVm.getPublicIpAddress(), sourceServer.getParent(), srcTmpltHost.getInstallPath()); //TODO: how to handle parent field from hostVO in image_store? and how we can populate that column?
// return generateCopyUrl(ssVm.getPublicIpAddress(), sourceServer.getParent(), srcTmpltStore.getInstallPath());
return generateCopyUrl(ssVm.getPublicIpAddress(), null, srcTmpltStore.getInstallPath());
} }
VMTemplateVO tmplt = _templateDao.findById(srcTmpltHost.getTemplateId()); VMTemplateVO tmplt = _templateDao.findById(srcTmpltStore.getTemplateId());
HypervisorType hyperType = tmplt.getHypervisorType(); HypervisorType hyperType = tmplt.getHypervisorType();
/*No secondary storage vm yet*/ /*No secondary storage vm yet*/
if (hyperType != null && hyperType == HypervisorType.KVM) { if (hyperType != null && hyperType == HypervisorType.KVM) {
return "file://" + sourceServer.getParent() + "/" + srcTmpltHost.getInstallPath(); //return "file://" + sourceServer.getParent() + "/" + srcTmpltStore.getInstallPath();
return "file://" + "/" + srcTmpltStore.getInstallPath();
} }
return null; return null;
} }
@ -353,11 +365,12 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
vmTemplateStore = _vmTemplateStoreDao.findByStoreTemplate(store.getId(), template.getId()); vmTemplateStore = _vmTemplateStoreDao.findByStoreTemplate(store.getId(), template.getId());
if (vmTemplateStore == null) { if (vmTemplateStore == null) {
// This method can be invoked other places, for example, handleTemplateSync, in that case, vmTemplateStore may be null // This method can be invoked other places, for example,
vmTemplateStore = new TemplateDataStoreVO(store.getId(), template.getId(), new Date(), 0, VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null, "jobid0000", null, template.getUrl()); // handleTemplateSync, in that case, vmTemplateStore may be null
vmTemplateStore = new TemplateDataStoreVO(store.getId(), template.getId(), new Date(), 0,
VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null, "jobid0000", null, template.getUrl());
_vmTemplateStoreDao.persist(vmTemplateStore); _vmTemplateStoreDao.persist(vmTemplateStore);
} else } else if ((vmTemplateStore.getJobId() != null) && (vmTemplateStore.getJobId().length() > 2)) {
if ((vmTemplateStore.getJobId() != null) && (vmTemplateStore.getJobId().length() > 2)) {
downloadJobExists = true; downloadJobExists = true;
} }
@ -365,8 +378,7 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
String secUrl = store.getUri(); String secUrl = store.getUri();
if (vmTemplateStore != null) { if (vmTemplateStore != null) {
start(); start();
DownloadCommand dcmd = DownloadCommand dcmd = new DownloadCommand(store.getTO(), secUrl, template, maxTemplateSizeInBytes);
new DownloadCommand(store.getTO(), secUrl, template, maxTemplateSizeInBytes);
dcmd.setProxy(getHttpProxy()); dcmd.setProxy(getHttpProxy());
if (downloadJobExists) { if (downloadJobExists) {
dcmd = new DownloadProgressCommand(dcmd, vmTemplateStore.getJobId(), RequestType.GET_OR_RESTART); dcmd = new DownloadProgressCommand(dcmd, vmTemplateStore.getJobId(), RequestType.GET_OR_RESTART);
@ -379,10 +391,14 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
s_logger.warn("There is no secondary storage VM for downloading template to image store " + store.getName()); s_logger.warn("There is no secondary storage VM for downloading template to image store " + store.getName());
return; return;
} }
DownloadListener dl = new DownloadListener(ssAhost, store, template, _timer, _vmTemplateStoreDao, vmTemplateStore.getId(), this, dcmd, _templateDao, _resourceLimitMgr, _alertMgr, _accountMgr, callback); DownloadListener dl = new DownloadListener(ssAhost, store, template, _timer, _vmTemplateStoreDao, vmTemplateStore.getId(), this, dcmd,
_templateDao, _resourceLimitMgr, _alertMgr, _accountMgr, callback);
if (downloadJobExists) { if (downloadJobExists) {
// due to handling existing download job issues, we still keep downloadState in template_store_ref to avoid big change in DownloadListener to use // due to handling existing download job issues, we still keep
// new ObjectInDataStore.State transition. TODO: fix this later to be able to remove downloadState from template_store_ref. // downloadState in template_store_ref to avoid big change in
// DownloadListener to use
// new ObjectInDataStore.State transition. TODO: fix this later
// to be able to remove downloadState from template_store_ref.
dl.setCurrState(vmTemplateStore.getDownloadState()); dl.setCurrState(vmTemplateStore.getDownloadState());
} }
DownloadListener old = null; DownloadListener old = null;
@ -403,8 +419,6 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
} }
} }
@Override @Override
public void downloadTemplateToStorage(VMTemplateVO template, DataStore store, AsyncCompletionCallback<CreateCmdResult> callback) { public void downloadTemplateToStorage(VMTemplateVO template, DataStore store, AsyncCompletionCallback<CreateCmdResult> callback) {
long templateId = template.getId(); long templateId = template.getId();
@ -415,22 +429,21 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
} }
} }
@Override @Override
public void downloadVolumeToStorage(VolumeVO volume, DataStore store, String url, String checkSum, ImageFormat format, AsyncCompletionCallback<CreateCmdResult> callback) { public void downloadVolumeToStorage(VolumeVO volume, DataStore store, String url, String checkSum, ImageFormat format,
AsyncCompletionCallback<CreateCmdResult> callback) {
boolean downloadJobExists = false; boolean downloadJobExists = false;
VolumeDataStoreVO volumeHost = null; VolumeDataStoreVO volumeHost = null;
volumeHost = _volumeStoreDao.findByStoreVolume(store.getId(), volume.getId()); volumeHost = _volumeStoreDao.findByStoreVolume(store.getId(), volume.getId());
if (volumeHost == null) { if (volumeHost == null) {
volumeHost = new VolumeDataStoreVO(store.getId(), volume.getId(), new Date(), 0, VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED, null, null, volumeHost = new VolumeDataStoreVO(store.getId(), volume.getId(), new Date(), 0, VMTemplateStorageResourceAssoc.Status.NOT_DOWNLOADED,
"jobid0000", null, url, checkSum, format); null, null, "jobid0000", null, url, checkSum, format);
_volumeStoreDao.persist(volumeHost); _volumeStoreDao.persist(volumeHost);
} else if ((volumeHost.getJobId() != null) && (volumeHost.getJobId().length() > 2)) { } else if ((volumeHost.getJobId() != null) && (volumeHost.getJobId().length() > 2)) {
downloadJobExists = true; downloadJobExists = true;
} }
Long maxVolumeSizeInBytes = getMaxVolumeSizeInBytes(); Long maxVolumeSizeInBytes = getMaxVolumeSizeInBytes();
String secUrl = store.getUri(); String secUrl = store.getUri();
if (volumeHost != null) { if (volumeHost != null) {
@ -447,8 +460,8 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
s_logger.warn("There is no secondary storage VM for image store " + store.getName()); s_logger.warn("There is no secondary storage VM for image store " + store.getName());
return; return;
} }
DownloadListener dl = new DownloadListener(ssAhost, store, volume, _timer, _volumeStoreDao, volumeHost.getId(), DownloadListener dl = new DownloadListener(ssAhost, store, volume, _timer, _volumeStoreDao, volumeHost.getId(), this, dcmd, _volumeDao,
this, dcmd, _volumeDao, _storageMgr, _resourceLimitMgr, _alertMgr, _accountMgr, callback); _storageMgr, _resourceLimitMgr, _alertMgr, _accountMgr, callback);
if (downloadJobExists) { if (downloadJobExists) {
dl.setCurrState(volumeHost.getDownloadState()); dl.setCurrState(volumeHost.getDownloadState());
@ -471,7 +484,6 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
} }
} }
@DB @DB
public void handleDownloadEvent(HostVO host, VMTemplateVO template, Status dnldStatus) { public void handleDownloadEvent(HostVO host, VMTemplateVO template, Status dnldStatus) {
if ((dnldStatus == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) || (dnldStatus == Status.ABANDONED)) { if ((dnldStatus == VMTemplateStorageResourceAssoc.Status.DOWNLOADED) || (dnldStatus == Status.ABANDONED)) {
@ -492,8 +504,7 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
size = vmTemplateHost.getPhysicalSize(); size = vmTemplateHost.getPhysicalSize();
template.setSize(size); template.setSize(size);
this._templateDao.update(template.getId(), template); this._templateDao.update(template.getId(), template);
} } else {
else{
s_logger.warn("Failed to get size for template" + template.getName()); s_logger.warn("Failed to get size for template" + template.getName());
} }
String eventType = EventTypes.EVENT_TEMPLATE_CREATE; String eventType = EventTypes.EVENT_TEMPLATE_CREATE;
@ -501,9 +512,8 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
eventType = EventTypes.EVENT_ISO_CREATE; eventType = EventTypes.EVENT_ISO_CREATE;
} }
if (template.getAccountId() != Account.ACCOUNT_ID_SYSTEM) { if (template.getAccountId() != Account.ACCOUNT_ID_SYSTEM) {
UsageEventUtils.publishUsageEvent(eventType, template.getAccountId(), host.getDataCenterId(), UsageEventUtils.publishUsageEvent(eventType, template.getAccountId(), host.getDataCenterId(), template.getId(), template.getName(),
template.getId(), template.getName(), null, template.getSourceTemplateId(), size, null, template.getSourceTemplateId(), size, template.getClass().getName(), template.getUuid());
template.getClass().getName(), template.getUuid());
} }
} }
txn.commit(); txn.commit();
@ -531,27 +541,23 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
size = volumeHost.getPhysicalSize(); size = volumeHost.getPhysicalSize();
volume.setSize(size); volume.setSize(size);
this._volumeDao.update(volume.getId(), volume); this._volumeDao.update(volume.getId(), volume);
} } else {
else{
s_logger.warn("Failed to get size for volume" + volume.getName()); s_logger.warn("Failed to get size for volume" + volume.getName());
} }
String eventType = EventTypes.EVENT_VOLUME_UPLOAD; String eventType = EventTypes.EVENT_VOLUME_UPLOAD;
if (volume.getAccountId() != Account.ACCOUNT_ID_SYSTEM) { if (volume.getAccountId() != Account.ACCOUNT_ID_SYSTEM) {
UsageEventUtils.publishUsageEvent(eventType, volume.getAccountId(), host.getDataCenterId(), UsageEventUtils.publishUsageEvent(eventType, volume.getAccountId(), host.getDataCenterId(), volume.getId(), volume.getName(), null,
volume.getId(), volume.getName(), null, 0l, size, volume.getClass().getName(), volume.getUuid()); 0l, size, volume.getClass().getName(), volume.getUuid());
} }
} else if (dnldStatus == Status.DOWNLOAD_ERROR || dnldStatus == Status.ABANDONED || dnldStatus == Status.UNKNOWN) { } else if (dnldStatus == Status.DOWNLOAD_ERROR || dnldStatus == Status.ABANDONED || dnldStatus == Status.UNKNOWN) {
// Decrement the volume and secondary storage space count // Decrement the volume and secondary storage space count
_resourceLimitMgr.decrementResourceCount(volume.getAccountId(), _resourceLimitMgr.decrementResourceCount(volume.getAccountId(), com.cloud.configuration.Resource.ResourceType.volume);
com.cloud.configuration.Resource.ResourceType.volume);
_resourceLimitMgr.recalculateResourceCount(volume.getAccountId(), volume.getDomainId(), _resourceLimitMgr.recalculateResourceCount(volume.getAccountId(), volume.getDomainId(),
com.cloud.configuration.Resource.ResourceType.secondary_storage.getOrdinal()); com.cloud.configuration.Resource.ResourceType.secondary_storage.getOrdinal());
} }
txn.commit(); txn.commit();
} }
/* /*
@Override @Override
public void addSystemVMTemplatesToHost(HostVO host, Map<String, TemplateProp> templateInfos){ public void addSystemVMTemplatesToHost(HostVO host, Map<String, TemplateProp> templateInfos){
@ -576,12 +582,10 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
} }
*/ */
@Override @Override
public void cancelAllDownloads(Long templateId) { public void cancelAllDownloads(Long templateId) {
List<VMTemplateHostVO> downloadsInProgress = List<VMTemplateHostVO> downloadsInProgress = _vmTemplateHostDao.listByTemplateStates(templateId,
_vmTemplateHostDao.listByTemplateStates(templateId, VMTemplateHostVO.Status.DOWNLOAD_IN_PROGRESS, VMTemplateHostVO.Status.NOT_DOWNLOADED); VMTemplateHostVO.Status.DOWNLOAD_IN_PROGRESS, VMTemplateHostVO.Status.NOT_DOWNLOADED);
if (downloadsInProgress.size() > 0) { if (downloadsInProgress.size() > 0) {
for (VMTemplateHostVO vmthvo : downloadsInProgress) { for (VMTemplateHostVO vmthvo : downloadsInProgress) {
DownloadListener dl = null; DownloadListener dl = null;
@ -645,6 +649,4 @@ public class DownloadMonitorImpl extends ManagerBase implements DownloadMonitor
} }
} }
} }

View File

@ -55,15 +55,14 @@ public interface TemplateManager extends TemplateApiService{
* Copies a template from its current secondary storage server to the secondary storage server in the specified zone. * Copies a template from its current secondary storage server to the secondary storage server in the specified zone.
* *
* @param template * @param template
* @param srcSecHost * @param srcSecStore
* @param srcZone
* @param destZone * @param destZone
* @return true if success * @return true if success
* @throws InternalErrorException * @throws InternalErrorException
* @throws StorageUnavailableException * @throws StorageUnavailableException
* @throws ResourceAllocationException * @throws ResourceAllocationException
*/ */
boolean copy(long userId, VMTemplateVO template, HostVO srcSecHost, DataCenterVO srcZone, DataCenterVO dstZone) throws StorageUnavailableException, ResourceAllocationException; boolean copy(long userId, VMTemplateVO template, DataStore srcSecStore, DataCenterVO dstZone) throws StorageUnavailableException, ResourceAllocationException;
/** /**
* Deletes a template from secondary storage servers * Deletes a template from secondary storage servers
@ -107,6 +106,8 @@ public interface TemplateManager extends TemplateApiService{
HostVO getSecondaryStorageHost(long zoneId, long tmpltId); HostVO getSecondaryStorageHost(long zoneId, long tmpltId);
DataStore getImageStore(long zoneId, long tmpltId);
VMTemplateHostVO getTemplateHostRef(long zoneId, long tmpltId, VMTemplateHostVO getTemplateHostRef(long zoneId, long tmpltId,
boolean readyOnly); boolean readyOnly);
@ -120,6 +121,8 @@ public interface TemplateManager extends TemplateApiService{
String getChecksum(Long hostId, String templatePath); String getChecksum(Long hostId, String templatePath);
String getChecksum(DataStore store, String templatePath);
List<DataStore> getImageStoreByTemplate(long templateId, Long zoneId); List<DataStore> getImageStoreByTemplate(long templateId, Long zoneId);
} }

View File

@ -125,6 +125,7 @@ import com.cloud.storage.Storage;
import com.cloud.storage.Storage.ImageFormat; import com.cloud.storage.Storage.ImageFormat;
import com.cloud.storage.Storage.TemplateType; import com.cloud.storage.Storage.TemplateType;
import com.cloud.storage.DataStoreRole; import com.cloud.storage.DataStoreRole;
import com.cloud.storage.ScopeType;
import com.cloud.storage.StorageManager; import com.cloud.storage.StorageManager;
import com.cloud.storage.StoragePool; import com.cloud.storage.StoragePool;
import com.cloud.storage.StoragePoolHostVO; import com.cloud.storage.StoragePoolHostVO;
@ -838,6 +839,20 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
return null; return null;
} }
@Override
public String getChecksum(DataStore store, String templatePath) {
String secUrl = store.getUri();
Answer answer;
answer = _agentMgr.sendToSecStorage(store, new ComputeChecksumCommand(
secUrl, templatePath));
if (answer != null && answer.getResult()) {
return answer.getDetails();
}
return null;
}
@Override @Override
@DB @DB
public VMTemplateHostVO prepareISOForCreate(VMTemplateVO template, StoragePool pool) { public VMTemplateHostVO prepareISOForCreate(VMTemplateVO template, StoragePool pool) {
@ -897,18 +912,20 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
@Override @Override
@DB @DB
public boolean copy(long userId, VMTemplateVO template, HostVO srcSecHost, DataCenterVO srcZone, DataCenterVO dstZone) throws StorageUnavailableException, ResourceAllocationException { public boolean copy(long userId, VMTemplateVO template, DataStore srcSecStore, DataCenterVO dstZone) throws StorageUnavailableException, ResourceAllocationException {
List<HostVO> dstSecHosts = _ssvmMgr.listSecondaryStorageHostsInOneZone(dstZone.getId());
long tmpltId = template.getId(); long tmpltId = template.getId();
long dstZoneId = dstZone.getId(); long dstZoneId = dstZone.getId();
if (dstSecHosts == null || dstSecHosts.isEmpty() ) { // find all eligible image stores for the destination zone
throw new StorageUnavailableException("Destination zone is not ready", DataCenter.class, dstZone.getId()); List<DataStore> dstSecStores = this.dataStoreMgr.getImageStoresByScope(new ZoneScope(dstZoneId));
if (dstSecStores == null || dstSecStores.isEmpty() ) {
throw new StorageUnavailableException("Destination zone is not ready, no image store associated", DataCenter.class, dstZone.getId());
} }
AccountVO account = _accountDao.findById(template.getAccountId()); AccountVO account = _accountDao.findById(template.getAccountId());
VMTemplateHostVO srcTmpltHost = _tmpltHostDao.findByHostTemplate(srcSecHost.getId(), tmpltId); // find the size of the template to be copied
TemplateDataStoreVO srcTmpltStore = this._tmplStoreDao.findByStoreTemplate(srcSecStore.getId(), tmpltId);
_resourceLimitMgr.checkResourceLimit(account, ResourceType.template); _resourceLimitMgr.checkResourceLimit(account, ResourceType.template);
_resourceLimitMgr.checkResourceLimit(account, ResourceType.secondary_storage, new Long(srcTmpltHost.getSize())); _resourceLimitMgr.checkResourceLimit(account, ResourceType.secondary_storage, new Long(srcTmpltStore.getSize()));
// Event details // Event details
String copyEventType; String copyEventType;
@ -924,30 +941,31 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
Transaction txn = Transaction.currentTxn(); Transaction txn = Transaction.currentTxn();
txn.start(); txn.start();
for ( HostVO dstSecHost : dstSecHosts ) { //Copy will just find one eligible image store for the destination zone and copy template there, not propagate to all image stores
VMTemplateHostVO dstTmpltHost = null; // for that zone
for ( DataStore dstSecStore : dstSecStores ) {
TemplateDataStoreVO dstTmpltStore = null;
try { try {
dstTmpltHost = _tmpltHostDao.findByHostTemplate(dstSecHost.getId(), tmpltId, true); dstTmpltStore = this._tmplStoreDao.findByStoreTemplate(dstSecStore.getId(), tmpltId, true);
if (dstTmpltHost != null) { if (dstTmpltStore != null) {
dstTmpltHost = _tmpltHostDao.lockRow(dstTmpltHost.getId(), true); dstTmpltStore = _tmplStoreDao.lockRow(dstTmpltStore.getId(), true);
if (dstTmpltHost != null && dstTmpltHost.getDownloadState() == Status.DOWNLOADED) { if (dstTmpltStore != null && dstTmpltStore.getDownloadState() == Status.DOWNLOADED) {
if (dstTmpltHost.getDestroyed() == false) { if (dstTmpltStore.getDestroyed() == false) {
return true; return true; // already downloaded on this image store
} else { } else {
dstTmpltHost.setDestroyed(false); dstTmpltStore.setDestroyed(false);
_tmpltHostDao.update(dstTmpltHost.getId(), dstTmpltHost); _tmplStoreDao.update(dstTmpltStore.getId(), dstTmpltStore);
return true; return true;
} }
} else if (dstTmpltHost != null && dstTmpltHost.getDownloadState() == Status.DOWNLOAD_ERROR){ } else if (dstTmpltStore != null && dstTmpltStore.getDownloadState() == Status.DOWNLOAD_ERROR){
if (dstTmpltHost.getDestroyed() == true) { if (dstTmpltStore.getDestroyed() == true) {
dstTmpltHost.setDestroyed(false); dstTmpltStore.setDestroyed(false);
dstTmpltHost.setDownloadState(Status.NOT_DOWNLOADED); dstTmpltStore.setDownloadState(Status.NOT_DOWNLOADED);
dstTmpltHost.setDownloadPercent(0); dstTmpltStore.setDownloadPercent(0);
dstTmpltHost.setCopy(true); dstTmpltStore.setCopy(true);
dstTmpltHost.setErrorString(""); dstTmpltStore.setErrorString("");
dstTmpltHost.setJobId(null); dstTmpltStore.setJobId(null);
_tmpltHostDao.update(dstTmpltHost.getId(), dstTmpltHost); _tmplStoreDao.update(dstTmpltStore.getId(), dstTmpltStore);
} }
} }
} }
@ -955,11 +973,11 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
txn.commit(); txn.commit();
} }
if(_downloadMonitor.copyTemplate(template, srcSecHost, dstSecHost) ) { if(_downloadMonitor.copyTemplate(template, srcSecStore, dstSecStore) ) {
_tmpltDao.addTemplateToZone(template, dstZoneId); _tmpltDao.addTemplateToZone(template, dstZoneId);
if(account.getId() != Account.ACCOUNT_ID_SYSTEM){ if(account.getId() != Account.ACCOUNT_ID_SYSTEM){
UsageEventUtils.publishUsageEvent(copyEventType, account.getId(), dstZoneId, tmpltId, null, null, null, srcTmpltHost.getSize(), UsageEventUtils.publishUsageEvent(copyEventType, account.getId(), dstZoneId, tmpltId, null, null, null, srcTmpltStore.getSize(),
template.getClass().getName(), template.getUuid()); template.getClass().getName(), template.getUuid());
} }
return true; return true;
@ -969,6 +987,8 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
} }
@Override @Override
@ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_COPY, eventDescription = "copying template", async = true) @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_COPY, eventDescription = "copying template", async = true)
public VirtualMachineTemplate copyTemplate(CopyTemplateCmd cmd) throws StorageUnavailableException, ResourceAllocationException { public VirtualMachineTemplate copyTemplate(CopyTemplateCmd cmd) throws StorageUnavailableException, ResourceAllocationException {
@ -978,6 +998,7 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
Long destZoneId = cmd.getDestinationZoneId(); Long destZoneId = cmd.getDestinationZoneId();
Account caller = UserContext.current().getCaller(); Account caller = UserContext.current().getCaller();
/*
if (_swiftMgr.isSwiftEnabled()) { if (_swiftMgr.isSwiftEnabled()) {
throw new CloudRuntimeException("copytemplate API is disabled in Swift setup, templates in Swift can be accessed by all Zones"); throw new CloudRuntimeException("copytemplate API is disabled in Swift setup, templates in Swift can be accessed by all Zones");
} }
@ -986,6 +1007,7 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
throw new CloudRuntimeException( throw new CloudRuntimeException(
"copytemplate API is disabled in S3 setup -- S3 templates are accessible in all zones."); "copytemplate API is disabled in S3 setup -- S3 templates are accessible in all zones.");
} }
*/
//Verify parameters //Verify parameters
if (sourceZoneId == destZoneId) { if (sourceZoneId == destZoneId) {
@ -1007,20 +1029,24 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
throw new InvalidParameterValueException("Unable to find template with id"); throw new InvalidParameterValueException("Unable to find template with id");
} }
HostVO dstSecHost = getSecondaryStorageHost(destZoneId, templateId); DataStore dstSecStore = getImageStore(destZoneId, templateId);
if ( dstSecHost != null ) { if ( dstSecStore != null ) {
s_logger.debug("There is template " + templateId + " in secondary storage " + dstSecHost.getId() + " in zone " + destZoneId + " , don't need to copy"); s_logger.debug("There is template " + templateId + " in secondary storage " + dstSecStore.getName() + " in zone " + destZoneId + " , don't need to copy");
return template; return template;
} }
HostVO srcSecHost = getSecondaryStorageHost(sourceZoneId, templateId); DataStore srcSecStore = getImageStore(sourceZoneId, templateId);
if ( srcSecHost == null ) { if ( srcSecStore == null ) {
throw new InvalidParameterValueException("There is no template " + templateId + " in zone " + sourceZoneId ); throw new InvalidParameterValueException("There is no template " + templateId + " in zone " + sourceZoneId );
} }
if ( srcSecStore.getScope().getScopeType() == ScopeType.REGION){
s_logger.debug("Template " + templateId + " is in region-wide secondary storage " + dstSecStore.getName() + " , don't need to copy");
return template;
}
_accountMgr.checkAccess(caller, AccessType.ModifyEntry, true, template); _accountMgr.checkAccess(caller, AccessType.ModifyEntry, true, template);
boolean success = copy(userId, template, srcSecHost, sourceZone, dstZone); boolean success = copy(userId, template, srcSecStore, dstZone);
if (success){ if (success){
return template; return template;
@ -2073,6 +2099,23 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
return secondaryStorageHost.getStorageUrl(); return secondaryStorageHost.getStorageUrl();
} }
// get the image store where a template in a given zone is downloaded to, just pick one is enough.
@Override
public DataStore getImageStore(long zoneId, long tmpltId) {
List<DataStore> stores = this.dataStoreMgr.getImageStoresByScope(new ZoneScope(zoneId));
if (stores == null || stores.size() == 0) {
return null;
}
for (DataStore host : stores) {
List<TemplateDataStoreVO> tmpltStore = this._tmplStoreDao.listByTemplateStoreDownloadStatus(
tmpltId, host.getId(), VMTemplateStorageResourceAssoc.Status.DOWNLOADED);
if ( tmpltStore != null && tmpltStore.size() > 0 ){
return host;
}
}
return null;
}
@Override @Override
public HostVO getSecondaryStorageHost(long zoneId, long tmpltId) { public HostVO getSecondaryStorageHost(long zoneId, long tmpltId) {
List<HostVO> hosts = _ssvmMgr List<HostVO> hosts = _ssvmMgr

View File

@ -21,6 +21,7 @@ import java.util.Map;
import javax.ejb.Local; import javax.ejb.Local;
import javax.naming.ConfigurationException; import javax.naming.ConfigurationException;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.cloud.agent.api.Answer; import com.cloud.agent.api.Answer;
@ -140,6 +141,19 @@ public class MockAgentManagerImpl extends ManagerBase implements AgentManager {
return null; return null;
} }
@Override
public void sendToSecStorage(DataStore ssStore, Command cmd, Listener listener) throws AgentUnavailableException {
// TODO Auto-generated method stub
}
@Override
public Answer sendToSecStorage(DataStore ssStore, Command cmd) {
// TODO Auto-generated method stub
return null;
}
@Override @Override
public boolean tapLoadingAgents(Long hostId, TapAgentsAction action) { public boolean tapLoadingAgents(Long hostId, TapAgentsAction action) {
// TODO Auto-generated method stub // TODO Auto-generated method stub