Skip to content

Commit 35ba14d

Browse files
committed
CLOUDSTACK-5648:CopyTemplate and CopyISO across zones fails after NFS
migration to S3.
1 parent 7e54ca8 commit 35ba14d

5 files changed

Lines changed: 56 additions & 25 deletions

File tree

‎api/src/org/apache/cloudstack/api/command/user/template/CopyTemplateCmd.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.List;
2020

21+
import org.apache.log4j.Logger;
22+
2123
import org.apache.cloudstack.api.APICommand;
2224
import org.apache.cloudstack.api.ApiCommandJobType;
2325
import org.apache.cloudstack.api.ApiConstants;
@@ -26,12 +28,9 @@
2628
import org.apache.cloudstack.api.Parameter;
2729
import org.apache.cloudstack.api.ServerApiException;
2830
import org.apache.cloudstack.api.response.TemplateResponse;
29-
import org.apache.cloudstack.api.response.UserVmResponse;
3031
import org.apache.cloudstack.api.response.ZoneResponse;
3132
import org.apache.cloudstack.context.CallContext;
3233

33-
import org.apache.log4j.Logger;
34-
3534
import com.cloud.event.EventTypes;
3635
import com.cloud.exception.ResourceAllocationException;
3736
import com.cloud.exception.StorageUnavailableException;
@@ -56,7 +55,7 @@ public class CopyTemplateCmd extends BaseAsyncCmd {
5655
private Long id;
5756

5857
@Parameter(name=ApiConstants.SOURCE_ZONE_ID, type=CommandType.UUID, entityType = ZoneResponse.class,
59-
required=true, description="ID of the zone the template is currently hosted on.")
58+
description = "ID of the zone the template is currently hosted on. If not specified and template is cross-zone, then we will sync this template to region wide image store")
6059
private Long sourceZoneId;
6160

6261

@@ -110,10 +109,12 @@ public String getEventDescription() {
110109
return "copying template: " + getId() + " from zone: " + getSourceZoneId() + " to zone: " + getDestinationZoneId();
111110
}
112111

112+
@Override
113113
public ApiCommandJobType getInstanceType() {
114114
return ApiCommandJobType.Template;
115115
}
116116

117+
@Override
117118
public Long getInstanceId() {
118119
return getId();
119120
}
@@ -132,7 +133,7 @@ public void execute() throws ResourceAllocationException{
132133
}
133134

134135
response.setResponseName(getCommandName());
135-
this.setResponseObject(response);
136+
setResponseObject(response);
136137
} else {
137138
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to copy template");
138139
}

‎engine/components-api/src/com/cloud/template/TemplateManager.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public interface TemplateManager {
103103

104104
DataStore getImageStore(long zoneId, long tmpltId);
105105

106+
DataStore getImageStore(long tmpltId);
107+
106108
Long getTemplateSize(long templateId, long zoneId);
107109

108110
DataStore getImageStore(String storeUuid, Long zoneId);

‎engine/schema/src/org/apache/cloudstack/storage/datastore/db/TemplateDataStoreDao.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ TemplateDataStoreVO findByTemplateZoneDownloadStatus(long templateId, Long zoneI
6262

6363
TemplateDataStoreVO findByTemplate(long templateId, DataStoreRole role);
6464

65+
TemplateDataStoreVO findReadyByTemplate(long templateId, DataStoreRole role);
66+
6567
TemplateDataStoreVO findByTemplateZone(long templateId, Long zoneId, DataStoreRole role);
6668

6769
List<TemplateDataStoreVO> listByTemplate(long templateId);

‎engine/storage/src/org/apache/cloudstack/storage/image/db/TemplateDataStoreDaoImpl.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,20 @@ public TemplateDataStoreVO findByTemplate(long templateId, DataStoreRole role) {
343343
}
344344

345345
@Override
346-
public TemplateDataStoreVO findReadyOnCache(long templateId) {
346+
public TemplateDataStoreVO findReadyByTemplate(long templateId, DataStoreRole role) {
347347
SearchCriteria<TemplateDataStoreVO> sc = templateRoleSearch.create();
348348
sc.setParameters("template_id", templateId);
349-
sc.setParameters("store_role", DataStoreRole.ImageCache);
349+
sc.setParameters("store_role", role);
350350
sc.setParameters("destroyed", false);
351351
sc.setParameters("state", ObjectInDataStoreStateMachine.State.Ready);
352352
return findOneIncludingRemovedBy(sc);
353353
}
354354

355+
@Override
356+
public TemplateDataStoreVO findReadyOnCache(long templateId) {
357+
return findReadyByTemplate(templateId, DataStoreRole.ImageCache);
358+
}
359+
355360
@Override
356361
public List<TemplateDataStoreVO> listOnCache(long templateId) {
357362
SearchCriteria<TemplateDataStoreVO> sc = templateRoleSearch.create();

‎server/src/com/cloud/template/TemplateManagerImpl.java‎

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -694,38 +694,47 @@ public VirtualMachineTemplate copyTemplate(CopyTemplateCmd cmd) throws StorageUn
694694
Account caller = CallContext.current().getCallingAccount();
695695

696696
// Verify parameters
697-
if (sourceZoneId.equals(destZoneId)) {
698-
throw new InvalidParameterValueException("Please specify different source and destination zones.");
699-
}
700-
701-
DataCenterVO sourceZone = _dcDao.findById(sourceZoneId);
702-
if (sourceZone == null) {
703-
throw new InvalidParameterValueException("Please specify a valid source zone.");
704-
}
705-
706-
DataCenterVO dstZone = _dcDao.findById(destZoneId);
707-
if (dstZone == null) {
708-
throw new InvalidParameterValueException("Please specify a valid destination zone.");
709-
}
710-
711697
VMTemplateVO template = _tmpltDao.findById(templateId);
712698
if (template == null || template.getRemoved() != null) {
713699
throw new InvalidParameterValueException("Unable to find template with id");
714700
}
715701

716-
DataStore srcSecStore = getImageStore(sourceZoneId, templateId);
702+
DataStore srcSecStore = null;
703+
if (sourceZoneId != null) {
704+
// template is on zone-wide secondary storage
705+
srcSecStore = getImageStore(sourceZoneId, templateId);
706+
} else {
707+
// template is on region store
708+
srcSecStore = getImageStore(templateId);
709+
}
710+
717711
if (srcSecStore == null) {
718-
throw new InvalidParameterValueException("There is no template " + templateId + " in zone " + sourceZoneId);
712+
throw new InvalidParameterValueException("There is no template " + templateId + " ready on image store.");
719713
}
720714

721-
if (template.isCrossZones()){
722-
//TODO: we may need UI still enable CopyTemplate in case of cross zone template to trigger sync to region store.
715+
if (template.isCrossZones()) {
723716
// sync template from cache store to region store if it is not there, for cases where we are going to migrate existing NFS to S3.
724717
_tmpltSvr.syncTemplateToRegionStore(templateId, srcSecStore);
725718
s_logger.debug("Template " + templateId + " is cross-zone, don't need to copy");
726719
return template;
727720
}
728721

722+
if (sourceZoneId != null) {
723+
if (sourceZoneId.equals(destZoneId)) {
724+
throw new InvalidParameterValueException("Please specify different source and destination zones.");
725+
}
726+
727+
DataCenterVO sourceZone = _dcDao.findById(sourceZoneId);
728+
if (sourceZone == null) {
729+
throw new InvalidParameterValueException("Please specify a valid source zone.");
730+
}
731+
}
732+
733+
DataCenterVO dstZone = _dcDao.findById(destZoneId);
734+
if (dstZone == null) {
735+
throw new InvalidParameterValueException("Please specify a valid destination zone.");
736+
}
737+
729738
DataStore dstSecStore = getImageStore(destZoneId, templateId);
730739
if (dstSecStore != null) {
731740
s_logger.debug("There is template " + templateId + " in secondary storage " + dstSecStore.getName() + " in zone " + destZoneId
@@ -1706,6 +1715,18 @@ public DataStore getImageStore(long zoneId, long tmpltId) {
17061715
return null;
17071716
}
17081717

1718+
// get the region wide image store where a template is READY on,
1719+
// just pick one is enough.
1720+
@Override
1721+
public DataStore getImageStore(long tmpltId) {
1722+
TemplateDataStoreVO tmpltStore = _tmplStoreDao.findReadyByTemplate(tmpltId, DataStoreRole.Image);
1723+
if (tmpltStore != null) {
1724+
return _dataStoreMgr.getDataStore(tmpltStore.getDataStoreId(), DataStoreRole.Image);
1725+
}
1726+
1727+
return null;
1728+
}
1729+
17091730
@Override
17101731
public Long getTemplateSize(long templateId, long zoneId) {
17111732
TemplateDataStoreVO templateStoreRef = _tmplStoreDao.findByTemplateZoneDownloadStatus(templateId, zoneId,

0 commit comments

Comments
 (0)