Skip to content

Commit 8e2a569

Browse files
committed
Fix retrying logic
1 parent 7fa86fe commit 8e2a569

4 files changed

Lines changed: 34 additions & 32 deletions

File tree

agent/src/com/cloud/agent/direct/download/DirectTemplateDownloaderImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ public boolean validateChecksum() {
178178
boolean valid = false;
179179
try {
180180
while (!valid && retry > 0) {
181-
s_logger.info("Performing checksum validation for downloaded template " + templateId + ", retries left: " + retry);
182-
valid = DigestHelper.check(checksum, new FileInputStream(downloadedFilePath));
183181
retry--;
182+
s_logger.info("Performing checksum validation for downloaded template " + templateId + " using " + checksum + ", retries left: " + retry);
183+
valid = DigestHelper.check(checksum, new FileInputStream(downloadedFilePath));
184184
if (!valid && retry > 0) {
185185
s_logger.info("Checksum validation failded, re-downloading template");
186186
redownload = true;

core/src/org/apache/cloudstack/agent/directdownload/DirectDownloadAnswer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ public class DirectDownloadAnswer extends Answer {
2424

2525
private Long templateSize;
2626
private String installPath;
27+
private boolean retryOnOtherHosts;
2728

28-
public DirectDownloadAnswer(final boolean result, final String msg) {
29+
public DirectDownloadAnswer(final boolean result, final String msg, final boolean retry) {
2930
super(null);
3031
this.result = result;
3132
this.details = msg;
33+
this.retryOnOtherHosts = retry;
3234
}
3335

3436
public DirectDownloadAnswer(final boolean result, final Long size, final String installPath) {
@@ -45,4 +47,8 @@ public long getTemplateSize() {
4547
public String getInstallPath() {
4648
return installPath;
4749
}
50+
51+
public boolean isRetryOnOtherHosts() {
52+
return retryOnOtherHosts;
53+
}
4854
}

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,43 +1617,34 @@ private DirectTemplateDownloader getDirectTemplateDownloaderFromCommand(DirectDo
16171617
public Answer handleDownloadTemplateToPrimaryStorage(DirectDownloadCommand cmd) {
16181618
final PrimaryDataStoreTO pool = cmd.getDestPool();
16191619
if (!pool.getPoolType().equals(StoragePoolType.NetworkFilesystem)) {
1620-
return new DirectDownloadAnswer(false, "Unsupported pool type " + pool.getPoolType().toString());
1620+
return new DirectDownloadAnswer(false, "Unsupported pool type " + pool.getPoolType().toString(), true);
16211621
}
16221622
KVMStoragePool destPool = storagePoolMgr.getStoragePool(pool.getPoolType(), pool.getUuid());
16231623
DirectTemplateDownloader downloader;
16241624

16251625
try {
16261626
downloader = getDirectTemplateDownloaderFromCommand(cmd, destPool);
16271627
} catch (IllegalArgumentException e) {
1628-
return new DirectDownloadAnswer(false, "Unable to create direct downloader: " + e.getMessage());
1628+
return new DirectDownloadAnswer(false, "Unable to create direct downloader: " + e.getMessage(), true);
16291629
}
16301630

1631-
int retries = 3;
1632-
boolean ok = false;
1633-
do {
1634-
try {
1635-
retries--;
1636-
s_logger.info("Trying to download template, retries left: " + retries);
1637-
if (!downloader.downloadTemplate()) {
1638-
s_logger.warn("Couldn't download template");
1639-
continue;
1640-
}
1641-
if (!downloader.validateChecksum()) {
1642-
s_logger.warn("Couldn't validate template checksum");
1643-
continue;
1644-
}
1645-
if (!downloader.extractAndInstallDownloadedTemplate()) {
1646-
s_logger.warn("Couldn't extract and install template");
1647-
continue;
1648-
}
1649-
ok = true;
1650-
} catch (CloudRuntimeException e) {
1651-
s_logger.warn("Error downloading template " + cmd.getTemplateId() + " due to: " + e.getMessage() + " retries left: " + retries);
1631+
try {
1632+
s_logger.info("Trying to download template");
1633+
if (!downloader.downloadTemplate()) {
1634+
s_logger.warn("Couldn't download template");
1635+
return new DirectDownloadAnswer(false, "Unable to download template", true);
16521636
}
1653-
} while (!ok && retries > 0);
1654-
1655-
if (!ok) {
1656-
return new DirectDownloadAnswer(false, "Unable to download template " + cmd.getTemplateId());
1637+
if (!downloader.validateChecksum()) {
1638+
s_logger.warn("Couldn't validate template checksum");
1639+
return new DirectDownloadAnswer(false, "Checksum validation failed", false);
1640+
}
1641+
if (!downloader.extractAndInstallDownloadedTemplate()) {
1642+
s_logger.warn("Couldn't extract and install template");
1643+
return new DirectDownloadAnswer(false, "Extraction and installation failed", false);
1644+
}
1645+
} catch (CloudRuntimeException e) {
1646+
s_logger.warn("Error downloading template " + cmd.getTemplateId() + " due to: " + e.getMessage());
1647+
return new DirectDownloadAnswer(false, "Unable to download template: " + e.getMessage(), true);
16571648
}
16581649

16591650
DirectTemplateInformation info = downloader.getTemplateInformation();

server/src/org/apache/cloudstack/direct/download/DirectDownloadManagerImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,15 @@ private Answer sendDirectDownloadCommand(DirectDownloadCommand cmd, VMTemplateVO
252252
int hostIndex = 0;
253253
Answer answer = null;
254254
Long hostToSendDownloadCmd = hostsToRetry[hostIndex];
255-
while (!downloaded && retry > 0) {
255+
boolean continueRetrying = true;
256+
while (!downloaded && retry > 0 && continueRetrying) {
256257
s_logger.debug("Sending Direct download command to host " + hostToSendDownloadCmd);
257258
answer = agentManager.easySend(hostToSendDownloadCmd, cmd);
258-
downloaded = answer != null && answer.getResult();
259+
if (answer != null) {
260+
DirectDownloadAnswer ans = (DirectDownloadAnswer)answer;
261+
downloaded = answer.getResult();
262+
continueRetrying = ans.isRetryOnOtherHosts();
263+
}
259264
hostToSendDownloadCmd = hostsToRetry[(hostIndex + 1) % hostsToRetry.length];
260265
retry --;
261266
}

0 commit comments

Comments
 (0)