From 0fac4a2bb1edc60caf34cca69eae1fe1b8e038fa Mon Sep 17 00:00:00 2001 From: Edison Su Date: Wed, 11 Jan 2012 15:04:50 -0800 Subject: [PATCH] bug 10380: after finishing download template, caculate checksum and report to mgt server status 10380: resolved fixed --- .../agent/manager/MockStorageManagerImpl.java | 2 +- .../agent/api/storage/DownloadAnswer.java | 8 ++- .../storage/template/DownloadManagerImpl.java | 60 +++++++++++++++++-- .../storage/download/DownloadListener.java | 11 +++- .../storage/download/DownloadMonitorImpl.java | 4 +- 5 files changed, 76 insertions(+), 9 deletions(-) diff --git a/agent-simulator/src/com/cloud/agent/manager/MockStorageManagerImpl.java b/agent-simulator/src/com/cloud/agent/manager/MockStorageManagerImpl.java index 82f1258c8b8..0a2c89fa4a0 100644 --- a/agent-simulator/src/com/cloud/agent/manager/MockStorageManagerImpl.java +++ b/agent-simulator/src/com/cloud/agent/manager/MockStorageManagerImpl.java @@ -325,7 +325,7 @@ public class MockStorageManagerImpl implements MockStorageManager { volume.setStatus(Status.DOWNLOAD_IN_PROGRESS); volume = _mockVolumeDao.persist(volume); - return new DownloadAnswer(String.valueOf(volume.getId()), 0, "Downloading", Status.DOWNLOAD_IN_PROGRESS, cmd.getName(), cmd.getName(), volume.getSize(), volume.getSize()); + return new DownloadAnswer(String.valueOf(volume.getId()), 0, "Downloading", Status.DOWNLOAD_IN_PROGRESS, cmd.getName(), cmd.getName(), volume.getSize(), volume.getSize(), null); } @Override diff --git a/api/src/com/cloud/agent/api/storage/DownloadAnswer.java b/api/src/com/cloud/agent/api/storage/DownloadAnswer.java index 4a3f5775941..e2158d3feed 100755 --- a/api/src/com/cloud/agent/api/storage/DownloadAnswer.java +++ b/api/src/com/cloud/agent/api/storage/DownloadAnswer.java @@ -33,6 +33,11 @@ public class DownloadAnswer extends Answer { private String installPath; private long templateSize = 0L; private long templatePhySicalSize = 0L; + private String checkSum; + + public String getCheckSum() { + return checkSum; + } public int getDownloadPct() { return downloadPct; @@ -72,7 +77,7 @@ public class DownloadAnswer extends Answer { } public DownloadAnswer(String jobId, int downloadPct, String errorString, - Status downloadStatus, String fileSystemPath, String installPath, long templateSize, long templatePhySicalSize ) { + Status downloadStatus, String fileSystemPath, String installPath, long templateSize, long templatePhySicalSize, String checkSum) { super(); this.jobId = jobId; this.downloadPct = downloadPct; @@ -83,6 +88,7 @@ public class DownloadAnswer extends Answer { this.installPath = fixPath(installPath); this.templateSize = templateSize; this.templatePhySicalSize = templatePhySicalSize; + this.checkSum = checkSum; } public DownloadAnswer(String jobId, int downloadPct, Command command, diff --git a/core/src/com/cloud/storage/template/DownloadManagerImpl.java b/core/src/com/cloud/storage/template/DownloadManagerImpl.java index 8bf37b2ee68..21e0a67f79e 100755 --- a/core/src/com/cloud/storage/template/DownloadManagerImpl.java +++ b/core/src/com/cloud/storage/template/DownloadManagerImpl.java @@ -19,9 +19,14 @@ package com.cloud.storage.template; import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; +import java.math.BigInteger; import java.net.URI; import java.net.URISyntaxException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; @@ -39,6 +44,7 @@ import javax.naming.ConfigurationException; import org.apache.log4j.Logger; +import com.cloud.agent.api.Answer; import com.cloud.agent.api.storage.DownloadAnswer; import com.cloud.agent.api.storage.DownloadCommand; import com.cloud.agent.api.storage.DownloadProgressCommand; @@ -196,6 +202,10 @@ public class DownloadManagerImpl implements DownloadManager { public long getTemplatePhysicalSize() { return templatePhysicalSize; } + + public void setCheckSum(String checksum) { + this.checksum = checksum; + } } public static final Logger s_logger = Logger.getLogger(DownloadManagerImpl.class); @@ -262,6 +272,37 @@ public class DownloadManagerImpl implements DownloadManager { break; } } + + private String computeCheckSum(File f) { + byte[] buffer = new byte[8192]; + int read = 0; + MessageDigest digest; + String checksum = null; + InputStream is = null; + try { + digest = MessageDigest.getInstance("MD5"); + is = new FileInputStream(f); + while( (read = is.read(buffer)) > 0) { + digest.update(buffer, 0, read); + } + byte[] md5sum = digest.digest(); + BigInteger bigInt = new BigInteger(1, md5sum); + checksum = bigInt.toString(16); + return checksum; + }catch(IOException e) { + return null; + }catch (NoSuchAlgorithmException e) { + return null; + } + finally { + try { + if(is != null) + is.close(); + } catch (IOException e) { + return null; + } + } + } /** * Post download activity (install and cleanup). Executed in context of downloader thread @@ -355,7 +396,10 @@ public class DownloadManagerImpl implements DownloadManager { break; } } - + + String checkSum = computeCheckSum(downloadedTemplate); + dnld.setCheckSum(checkSum); + if (!loc.save()) { s_logger.warn("Cleaning up because we're unable to save the formats"); loc.purge(); @@ -450,6 +494,14 @@ public class DownloadManagerImpl implements DownloadManager { } return 0; } + + public String getDownloadCheckSum(String jobId) { + DownloadJob dj = jobs.get(jobId); + if (dj != null) { + return dj.getChecksum(); + } + return null; + } public long getDownloadTemplatePhysicalSize(String jobId) { DownloadJob dj = jobs.get(jobId); @@ -536,7 +588,7 @@ public class DownloadManagerImpl implements DownloadManager { return new DownloadAnswer("Internal Error", VMTemplateStorageResourceAssoc.Status.DOWNLOAD_ERROR); } return new DownloadAnswer(jobId, getDownloadPct(jobId), getDownloadError(jobId), getDownloadStatus2(jobId), getDownloadLocalPath(jobId), getInstallPath(jobId), - getDownloadTemplateSize(jobId), getDownloadTemplateSize(jobId)); + getDownloadTemplateSize(jobId), getDownloadTemplateSize(jobId), getDownloadCheckSum(jobId)); } private void sleep() { @@ -578,14 +630,14 @@ public class DownloadManagerImpl implements DownloadManager { case PURGE: td.stopDownload(); answer = new DownloadAnswer(jobId, getDownloadPct(jobId), getDownloadError(jobId), getDownloadStatus2(jobId), getDownloadLocalPath(jobId), - getInstallPath(jobId), getDownloadTemplateSize(jobId), getDownloadTemplatePhysicalSize(jobId)); + getInstallPath(jobId), getDownloadTemplateSize(jobId), getDownloadTemplatePhysicalSize(jobId), getDownloadCheckSum(jobId)); jobs.remove(jobId); return answer; default: break; // TODO } return new DownloadAnswer(jobId, getDownloadPct(jobId), getDownloadError(jobId), getDownloadStatus2(jobId), getDownloadLocalPath(jobId), - getInstallPath(jobId), getDownloadTemplateSize(jobId), getDownloadTemplatePhysicalSize(jobId)); + getInstallPath(jobId), getDownloadTemplateSize(jobId), getDownloadTemplatePhysicalSize(jobId), getDownloadCheckSum(jobId)); } private String getInstallPath(String jobId) { diff --git a/server/src/com/cloud/storage/download/DownloadListener.java b/server/src/com/cloud/storage/download/DownloadListener.java index 02117ca4c2b..1ffb7de2652 100755 --- a/server/src/com/cloud/storage/download/DownloadListener.java +++ b/server/src/com/cloud/storage/download/DownloadListener.java @@ -46,6 +46,7 @@ import com.cloud.storage.Storage; import com.cloud.storage.VMTemplateHostVO; import com.cloud.storage.VMTemplateStorageResourceAssoc.Status; import com.cloud.storage.VMTemplateVO; +import com.cloud.storage.dao.VMTemplateDao; import com.cloud.storage.dao.VMTemplateHostDao; import com.cloud.storage.download.DownloadState.DownloadEvent; import com.cloud.utils.exception.CloudRuntimeException; @@ -106,6 +107,7 @@ public class DownloadListener implements Listener { private boolean downloadActive = true; private VMTemplateHostDao vmTemplateHostDao; + private VMTemplateDao _vmTemplateDao; private final DownloadMonitorImpl downloadMonitor; @@ -123,7 +125,7 @@ public class DownloadListener implements Listener { private final Map stateMap = new HashMap(); private Long templateHostId; - public DownloadListener(HostVO ssAgent, HostVO host, VMTemplateVO template, Timer _timer, VMTemplateHostDao dao, Long templHostId, DownloadMonitorImpl downloadMonitor, DownloadCommand cmd) { + public DownloadListener(HostVO ssAgent, HostVO host, VMTemplateVO template, Timer _timer, VMTemplateHostDao dao, Long templHostId, DownloadMonitorImpl downloadMonitor, DownloadCommand cmd, VMTemplateDao templateDao) { this.ssAgent = ssAgent; this.sserver = host; this.template = template; @@ -136,6 +138,7 @@ public class DownloadListener implements Listener { this.timer = _timer; this.timeoutTask = new TimeoutTask(this); this.timer.schedule(timeoutTask, 3*STATUS_POLL_INTERVAL); + this._vmTemplateDao = templateDao; updateDatabase(Status.NOT_DOWNLOADED, ""); } @@ -261,6 +264,12 @@ public class DownloadListener implements Listener { updateBuilder.setPhysicalSize(answer.getTemplatePhySicalSize()); vmTemplateHostDao.update(getTemplateHostId(), updateBuilder); + + if (answer.getCheckSum() != null) { + VMTemplateVO templateDaoBuilder = _vmTemplateDao.createForUpdate(); + templateDaoBuilder.setChecksum(answer.getCheckSum()); + _vmTemplateDao.update(template.getId(), templateDaoBuilder); + } } @Override diff --git a/server/src/com/cloud/storage/download/DownloadMonitorImpl.java b/server/src/com/cloud/storage/download/DownloadMonitorImpl.java index ac9c92d67e0..891cfa280af 100755 --- a/server/src/com/cloud/storage/download/DownloadMonitorImpl.java +++ b/server/src/com/cloud/storage/download/DownloadMonitorImpl.java @@ -260,7 +260,7 @@ public class DownloadMonitorImpl implements DownloadMonitor { s_logger.warn("There is no secondary storage VM for secondary storage host " + destServer.getName()); return false; } - DownloadListener dl = new DownloadListener(ssAhost, destServer, template, _timer, _vmTemplateHostDao, destTmpltHost.getId(), this, dcmd); + DownloadListener dl = new DownloadListener(ssAhost, destServer, template, _timer, _vmTemplateHostDao, destTmpltHost.getId(), this, dcmd, _templateDao); if (downloadJobExists) { dl.setCurrState(destTmpltHost.getDownloadState()); } @@ -346,7 +346,7 @@ public class DownloadMonitorImpl implements DownloadMonitor { s_logger.warn("There is no secondary storage VM for secondary storage host " + sserver.getName()); return; } - DownloadListener dl = new DownloadListener(ssAhost, sserver, template, _timer, _vmTemplateHostDao, vmTemplateHost.getId(), this, dcmd); + DownloadListener dl = new DownloadListener(ssAhost, sserver, template, _timer, _vmTemplateHostDao, vmTemplateHost.getId(), this, dcmd, _templateDao); if (downloadJobExists) { dl.setCurrState(vmTemplateHost.getDownloadState()); }