Skip to content

Commit 97569b9

Browse files
committed
Enhance template replication logic to respect secondary storage copy limits and schedule additional copies for cross-zone templates
1 parent df1ad66 commit 97569b9

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

engine/storage/image/src/main/java/org/apache/cloudstack/storage/image/TemplateServiceImpl.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,19 +357,29 @@ public void replicateTemplateUpToCap(long templateId, long zoneId) {
357357
if (template == null) {
358358
return;
359359
}
360+
int copyLimit = _tmpltMgr.getSecStorageCopyLimit(template, zoneId);
361+
if (copyLimit <= 0) {
362+
return;
363+
}
364+
int needed = copyLimit - countActiveSecStorageCopies(templateId, zoneId);
365+
if (needed <= 0) {
366+
return;
367+
}
360368
List<DataStore> stores = _storeMgr.getImageStoresByScope(new ZoneScope(zoneId));
361369
if (stores == null || stores.isEmpty()) {
362370
return;
363371
}
372+
int kicked = 0;
364373
for (DataStore store : stores) {
374+
if (kicked >= needed) {
375+
break;
376+
}
365377
if (hasActiveTemplateCopyOnStore(templateId, store.getId())) {
366378
continue;
367379
}
368-
if (!canCopyTemplateToImageStore(templateId, zoneId)) {
369-
break;
370-
}
371380
try {
372381
storageOrchestrator.orchestrateTemplateCopyFromSecondaryStores(templateId, store);
382+
kicked++;
373383
} catch (Exception e) {
374384
logger.warn("Failed to proactively replicate template [{}] to image store [{}] in zone [{}]: {}",
375385
template.getUniqueName(), store.getName(), zoneId, e.getMessage());
@@ -1602,6 +1612,15 @@ protected Void copyTemplateCrossZoneCallBack(AsyncCallbackDispatcher<TemplateSer
16021612
destTemplate.processEvent(Event.OperationFailed);
16031613
} else {
16041614
destTemplate.processEvent(Event.OperationSucceeded, result.getAnswer());
1615+
try {
1616+
DataStore destStore = destTemplate.getDataStore();
1617+
if (destStore != null && destStore.getScope() != null && destStore.getScope().getScopeId() != null) {
1618+
replicateTemplateUpToCap(destTemplate.getId(), destStore.getScope().getScopeId());
1619+
}
1620+
} catch (Exception e) {
1621+
logger.warn("Failed to schedule additional copies for cross-zone copied template [{}]: {}",
1622+
destTemplate.getUuid(), e.getMessage());
1623+
}
16051624
}
16061625
future.complete(res);
16071626
} catch (Exception e) {

0 commit comments

Comments
 (0)