-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Allow copy of templates from secondary storages of other zone when adding a new secondary storage #12296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
harikrishna-patnala
wants to merge
15
commits into
apache:4.20
Choose a base branch
from
shapeblue:improve_template_copy_btw_secondary_storages
base: 4.20
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Allow copy of templates from secondary storages of other zone when adding a new secondary storage #12296
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
988ca9a
Allow copy of templates from secondary storages of other zone when ad…
harikrishna-patnala 9a60a92
Add API param and UI changes on add secondary storage page
harikrishna-patnala 19b6f52
Make copy template across zones non blocking
harikrishna-patnala 1e0feca
Code fixes
harikrishna-patnala 8edf96f
unused imports
harikrishna-patnala 555e6b3
Add copy template flag in zone wizard and remove NFS checks
harikrishna-patnala b7c67d0
Fix UI
harikrishna-patnala a3d1c94
Label fixes
harikrishna-patnala 4928d9b
code optimizations
harikrishna-patnala fb053ca
code refactoring
harikrishna-patnala 7ccaf4e
missing changes
harikrishna-patnala 55e211c
Combine template copy and download into a single asynchronous operation
harikrishna-patnala d17dabe
unused import and fixed conflicts
harikrishna-patnala e472e3a
unused code
harikrishna-patnala f8e5a6c
update config message
harikrishna-patnala File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,8 @@ | |
|
|
||
| import javax.inject.Inject; | ||
|
|
||
| import com.cloud.exception.StorageUnavailableException; | ||
| import org.apache.cloudstack.context.CallContext; | ||
| import org.apache.cloudstack.engine.orchestration.service.StorageOrchestrationService; | ||
| import org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult; | ||
| import org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult; | ||
|
|
@@ -70,6 +72,7 @@ | |
| import org.apache.commons.lang3.StringUtils; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.ThreadContext; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import com.cloud.agent.api.Answer; | ||
|
|
@@ -548,10 +551,7 @@ public void handleTemplateSync(DataStore store) { | |
| } | ||
|
|
||
| if (availHypers.contains(tmplt.getHypervisorType())) { | ||
| boolean copied = isCopyFromOtherStoragesEnabled(zoneId) && tryCopyingTemplateToImageStore(tmplt, store); | ||
| if (!copied) { | ||
| tryDownloadingTemplateToImageStore(tmplt, store); | ||
| } | ||
| storageOrchestrator.orchestrateTemplateCopyFromSecondaryStores(tmplt.getId(), store); | ||
| } else { | ||
| logger.info("Skip downloading template {} since current data center does not have hypervisor {}", tmplt, tmplt.getHypervisorType()); | ||
| } | ||
|
|
@@ -598,6 +598,16 @@ public void handleTemplateSync(DataStore store) { | |
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void handleTemplateCopyFromSecondaryStores(long templateId, DataStore destStore) { | ||
| VMTemplateVO template = _templateDao.findById(templateId); | ||
| long zoneId = destStore.getScope().getScopeId(); | ||
| boolean copied = imageStoreDetailsUtil.isCopyTemplatesFromOtherStoragesEnabled(destStore.getId(), zoneId) && tryCopyingTemplateToImageStore(template, destStore); | ||
| if (!copied) { | ||
| tryDownloadingTemplateToImageStore(template, destStore); | ||
| } | ||
| } | ||
|
|
||
| protected void tryDownloadingTemplateToImageStore(VMTemplateVO tmplt, DataStore destStore) { | ||
| if (tmplt.getUrl() == null) { | ||
| logger.info("Not downloading template [{}] to image store [{}], as it has no URL.", tmplt.getUniqueName(), | ||
|
|
@@ -615,28 +625,134 @@ protected void tryDownloadingTemplateToImageStore(VMTemplateVO tmplt, DataStore | |
| } | ||
|
|
||
| protected boolean tryCopyingTemplateToImageStore(VMTemplateVO tmplt, DataStore destStore) { | ||
| Long zoneId = destStore.getScope().getScopeId(); | ||
| List<DataStore> storesInZone = _storeMgr.getImageStoresByZoneIds(zoneId); | ||
| for (DataStore sourceStore : storesInZone) { | ||
| Map<String, TemplateProp> existingTemplatesInSourceStore = listTemplate(sourceStore); | ||
| if (existingTemplatesInSourceStore == null || !existingTemplatesInSourceStore.containsKey(tmplt.getUniqueName())) { | ||
| logger.debug("Template [{}] does not exist on image store [{}]; searching on another one.", | ||
| tmplt.getUniqueName(), sourceStore.getName()); | ||
| if (searchAndCopyWithinZone(tmplt, destStore)) { | ||
| return true; | ||
| } | ||
|
|
||
| Long destZoneId = destStore.getScope().getScopeId(); | ||
| logger.debug("Template [{}] not found in any image store of zone [{}]. Checking other zones.", | ||
| tmplt.getUniqueName(), destZoneId); | ||
|
|
||
| return searchAndCopyAcrossZones(tmplt, destStore, destZoneId); | ||
| } | ||
|
|
||
| private boolean searchAndCopyAcrossZones(VMTemplateVO tmplt, DataStore destStore, Long destZoneId) { | ||
| List<Long> allZoneIds = _dcDao.listAllIds(); | ||
| for (Long otherZoneId : allZoneIds) { | ||
| if (otherZoneId.equals(destZoneId)) { | ||
| continue; | ||
| } | ||
| TemplateObject sourceTmpl = (TemplateObject) _templateFactory.getTemplate(tmplt.getId(), sourceStore); | ||
| if (sourceTmpl.getInstallPath() == null) { | ||
| logger.warn("Can not copy template [{}] from image store [{}], as it returned a null install path.", tmplt.getUniqueName(), | ||
| sourceStore.getName()); | ||
|
|
||
| List<DataStore> storesInOtherZone = _storeMgr.getImageStoresByZoneIds(otherZoneId); | ||
| logger.debug("Checking zone [{}] for template [{}]...", otherZoneId, tmplt.getUniqueName()); | ||
|
|
||
| if (storesInOtherZone == null || storesInOtherZone.isEmpty()) { | ||
| logger.debug("Zone [{}] has no image stores. Skipping.", otherZoneId); | ||
| continue; | ||
| } | ||
| storageOrchestrator.orchestrateTemplateCopyToImageStore(sourceTmpl, destStore); | ||
| return true; | ||
|
|
||
| TemplateObject sourceTmpl = findUsableTemplate(tmplt, storesInOtherZone); | ||
| if (sourceTmpl == null) { | ||
| logger.debug("Template [{}] not found with a valid install path in any image store of zone [{}].", | ||
| tmplt.getUniqueName(), otherZoneId); | ||
| continue; | ||
| } | ||
|
|
||
| logger.info("Template [{}] found in zone [{}]. Initiating cross-zone copy to zone [{}].", | ||
| tmplt.getUniqueName(), otherZoneId, destZoneId); | ||
|
|
||
| return copyTemplateAcrossZones(destStore, sourceTmpl); | ||
| } | ||
| logger.debug("Can't copy template [{}] from another image store.", tmplt.getUniqueName()); | ||
|
|
||
| logger.debug("Template [{}] was not found in any zone. Cannot perform zone-to-zone copy.", tmplt.getUniqueName()); | ||
| return false; | ||
| } | ||
|
|
||
| protected TemplateObject findUsableTemplate(VMTemplateVO tmplt, List<DataStore> imageStores) { | ||
| for (DataStore store : imageStores) { | ||
|
|
||
| Map<String, TemplateProp> templates = listTemplate(store); | ||
| if (templates == null || !templates.containsKey(tmplt.getUniqueName())) { | ||
| continue; | ||
| } | ||
|
|
||
| TemplateObject tmpl = (TemplateObject) _templateFactory.getTemplate(tmplt.getId(), store); | ||
| if (tmpl.getInstallPath() == null) { | ||
| logger.debug("Template [{}] found in image store [{}] but install path is null. Skipping.", | ||
| tmplt.getUniqueName(), store.getName()); | ||
| continue; | ||
| } | ||
| return tmpl; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to keep the check with |
||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private boolean searchAndCopyWithinZone(VMTemplateVO tmplt, DataStore destStore) { | ||
| Long destZoneId = destStore.getScope().getScopeId(); | ||
| List<DataStore> storesInSameZone = _storeMgr.getImageStoresByZoneIds(destZoneId); | ||
|
|
||
| TemplateObject sourceTmpl = findUsableTemplate(tmplt, storesInSameZone); | ||
| if (sourceTmpl == null) { | ||
| return false; | ||
| } | ||
|
|
||
| TemplateApiResult result; | ||
| AsyncCallFuture<TemplateApiResult> future = copyTemplateToImageStore(sourceTmpl, destStore); | ||
| try { | ||
| result = future.get(); | ||
| } catch (ExecutionException | InterruptedException e) { | ||
| logger.warn("Exception while copying template [{}] from image store [{}] to image store [{}]: {}", | ||
| sourceTmpl.getUniqueName(), sourceTmpl.getDataStore().getName(), destStore.getName(), e.toString()); | ||
| result = new TemplateApiResult(sourceTmpl); | ||
| result.setResult(e.getMessage()); | ||
| } | ||
| return result.isSuccess(); | ||
| } | ||
|
|
||
| private boolean copyTemplateAcrossZones(DataStore destStore, TemplateObject sourceTmpl) { | ||
| Long dstZoneId = destStore.getScope().getScopeId(); | ||
| DataCenterVO dstZone = _dcDao.findById(dstZoneId); | ||
|
|
||
| if (dstZone == null) { | ||
| logger.warn("Destination zone [{}] not found for template [{}].", dstZoneId, sourceTmpl.getUniqueName()); | ||
| return false; | ||
| } | ||
|
|
||
| TemplateApiResult result; | ||
| try { | ||
| VMTemplateVO template = _templateDao.findById(sourceTmpl.getId()); | ||
| try { | ||
| DataStore sourceStore = sourceTmpl.getDataStore(); | ||
| long userId = CallContext.current().getCallingUserId(); | ||
| boolean success = _tmpltMgr.copy(userId, template, sourceStore, dstZone); | ||
|
|
||
| result = new TemplateApiResult(sourceTmpl); | ||
| if (!success) { | ||
| result.setResult("Cross-zone template copy failed"); | ||
| } | ||
| } catch (StorageUnavailableException | ResourceAllocationException e) { | ||
| logger.error("Exception while copying template [{}] from zone [{}] to zone [{}]", | ||
| template, | ||
| sourceTmpl.getDataStore().getScope().getScopeId(), | ||
| dstZone.getId(), | ||
| e); | ||
| result = new TemplateApiResult(sourceTmpl); | ||
| result.setResult(e.getMessage()); | ||
| } finally { | ||
| ThreadContext.clearAll(); | ||
| } | ||
| } catch (Exception e) { | ||
| logger.error("Failed to copy template [{}] from zone [{}] to zone [{}].", | ||
| sourceTmpl.getUniqueName(), | ||
| sourceTmpl.getDataStore().getScope().getScopeId(), | ||
| dstZoneId, | ||
| e); | ||
| return false; | ||
| } | ||
|
|
||
| return result.isSuccess(); | ||
| } | ||
|
|
||
| @Override | ||
| public AsyncCallFuture<TemplateApiResult> copyTemplateToImageStore(DataObject source, DataStore destStore) { | ||
| TemplateObject sourceTmpl = (TemplateObject) source; | ||
|
|
@@ -680,10 +796,6 @@ protected Void copyTemplateToImageStoreCallback(AsyncCallbackDispatcher<Template | |
| return null; | ||
| } | ||
|
|
||
| protected boolean isCopyFromOtherStoragesEnabled(Long zoneId) { | ||
| return StorageManager.COPY_PUBLIC_TEMPLATES_FROM_OTHER_STORAGES.valueIn(zoneId); | ||
| } | ||
|
|
||
| protected void publishTemplateCreation(TemplateInfo tmplt) { | ||
| VMTemplateVO tmpltVo = _templateDao.findById(tmplt.getId()); | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.