bug 10380: after finishing download template, caculate checksum and report to mgt server

status 10380: resolved fixed
This commit is contained in:
Edison Su 2012-01-11 15:04:50 -08:00
parent cb6f1b619f
commit 0fac4a2bb1
5 changed files with 76 additions and 9 deletions

View File

@ -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

View File

@ -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,

View File

@ -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) {

View File

@ -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<String, DownloadState> stateMap = new HashMap<String, DownloadState>();
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

View File

@ -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());
}