CLOUDSTACK-2508: Template usage should report both physical and virtual sizes

This commit is contained in:
Saksham Srivastava 2013-07-22 12:52:55 +05:30 committed by Kishan Kavala
parent 64c120c0db
commit 135a2e66c5
13 changed files with 140 additions and 26 deletions

View File

@ -78,6 +78,9 @@ public class UsageRecordResponse extends BaseResponse implements ControlledEntit
@SerializedName(ApiConstants.SIZE) @Param(description="resource size")
private Long size;
@SerializedName("virtualsize") @Param(description="virtual size of resource")
private Long virtualSize;
@SerializedName(ApiConstants.START_DATE) @Param(description="start date of the usage record")
private String startDate;
@ -196,4 +199,8 @@ public class UsageRecordResponse extends BaseResponse implements ControlledEntit
public void setDefault(Boolean isDefault) {
this.isDefault = isDefault;
}
public void setVirtualSize(Long virtualSize) {
this.virtualSize = virtualSize;
}
}

View File

@ -66,4 +66,6 @@ public interface Usage {
public Date getStartDate();
public Date getEndDate();
public Long getVirtualSize();
}

View File

@ -69,6 +69,9 @@ public class UsageEventVO implements UsageEvent {
@Column(name="processed")
boolean processed;
@Column(name="virtual_size")
private Long virtualSize;
public UsageEventVO() {
}
@ -103,7 +106,20 @@ public class UsageEventVO implements UsageEvent {
this.resourceType = guestType;
this.templateId = (isSystem ? 1L : 0L);
}
//Snapshot usage event
//Snapshots have size as the actual (physical) size and virtual_size as the allocated size
public UsageEventVO(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId, Long size, Long virtualSize) {
this.type = usageType;
this.accountId = accountId;
this.zoneId = zoneId;
this.resourceId = resourceId;
this.resourceName = resourceName;
this.offeringId = offeringId;
this.templateId = templateId;
this.size = size;
this.virtualSize = virtualSize;
}
public UsageEventVO(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId, String resourceType) {
this.type = usageType;
this.accountId = accountId;
@ -213,4 +229,12 @@ public class UsageEventVO implements UsageEvent {
return resourceType;
}
}
public Long getVirtualSize() {
return virtualSize;
}
public void setVirtualSize(Long virtualSize) {
this.virtualSize = virtualSize;
}
}

View File

@ -46,10 +46,10 @@ public class UsageEventDaoImpl extends GenericDaoBase<UsageEventVO, Long> implem
private final SearchBuilder<UsageEventVO> latestEventsSearch;
private final SearchBuilder<UsageEventVO> IpeventsSearch;
private static final String COPY_EVENTS = "INSERT INTO cloud_usage.usage_event (id, type, account_id, created, zone_id, resource_id, resource_name, offering_id, template_id, size, resource_type) " +
"SELECT id, type, account_id, created, zone_id, resource_id, resource_name, offering_id, template_id, size, resource_type FROM cloud.usage_event vmevt WHERE vmevt.id > ? and vmevt.id <= ? ";
private static final String COPY_ALL_EVENTS = "INSERT INTO cloud_usage.usage_event (id, type, account_id, created, zone_id, resource_id, resource_name, offering_id, template_id, size, resource_type) " +
"SELECT id, type, account_id, created, zone_id, resource_id, resource_name, offering_id, template_id, size, resource_type FROM cloud.usage_event vmevt WHERE vmevt.id <= ?";
private static final String COPY_EVENTS = "INSERT INTO cloud_usage.usage_event (id, type, account_id, created, zone_id, resource_id, resource_name, offering_id, template_id, size, resource_type, virtual_size) " +
"SELECT id, type, account_id, created, zone_id, resource_id, resource_name, offering_id, template_id, size, resource_type, virtual_size FROM cloud.usage_event vmevt WHERE vmevt.id > ? and vmevt.id <= ? ";
private static final String COPY_ALL_EVENTS = "INSERT INTO cloud_usage.usage_event (id, type, account_id, created, zone_id, resource_id, resource_name, offering_id, template_id, size, resource_type, virtual_size) " +
"SELECT id, type, account_id, created, zone_id, resource_id, resource_name, offering_id, template_id, size, resource_type, virtual_size FROM cloud.usage_event vmevt WHERE vmevt.id <= ?";
private static final String MAX_EVENT = "select max(id) from cloud.usage_event where created <= ?";

View File

@ -59,6 +59,9 @@ public class UsageStorageVO implements InternalIdentity {
@Temporal(value=TemporalType.TIMESTAMP)
private Date deleted = null;
@Column(name="virtual_size")
private Long virtualSize;
protected UsageStorageVO() {
}
@ -74,6 +77,19 @@ public class UsageStorageVO implements InternalIdentity {
this.deleted = deleted;
}
public UsageStorageVO(long id, long zoneId, long accountId, long domainId, int storageType, Long sourceId, long size, Long virtualSize, Date created, Date deleted) {
this.zoneId = zoneId;
this.accountId = accountId;
this.domainId = domainId;
this.id = id;
this.storageType = storageType;
this.sourceId = sourceId;
this.size = size;
this.virtualSize = virtualSize;
this.created = created;
this.deleted = deleted;
}
public long getZoneId() {
return zoneId;
}
@ -101,7 +117,11 @@ public class UsageStorageVO implements InternalIdentity {
public long getSize(){
return size;
}
public Long getVirtualSize() {
return virtualSize;
}
public Date getCreated() {
return created;
}

View File

@ -79,7 +79,10 @@ public class UsageVO implements Usage, InternalIdentity {
@Column(name="size")
private Long size = null;
@Column(name="virtual_size")
private Long virtualSize;
@Column(name="network_id")
private Long networkId = null;
@ -114,7 +117,28 @@ public class UsageVO implements Usage, InternalIdentity {
this.startDate = startDate;
this.endDate = endDate;
}
public UsageVO(Long zoneId, Long accountId, Long domainId, String description, String usageDisplay,
int usageType, Double rawUsage, Long vmId, String vmName, Long offeringId, Long templateId,
Long usageId, Long size, Long virtualSize, Date startDate, Date endDate) {
this.zoneId = zoneId;
this.accountId = accountId;
this.domainId = domainId;
this.description = description;
this.usageDisplay = usageDisplay;
this.usageType = usageType;
this.rawUsage = rawUsage;
this.vmInstanceId = vmId;
this.vmName = vmName;
this.offeringId = offeringId;
this.templateId = templateId;
this.usageId = usageId;
this.size = size;
this.virtualSize = virtualSize;
this.startDate = startDate;
this.endDate = endDate;
}
public UsageVO(Long zoneId, Long accountId, Long domainId, String description, String usageDisplay,
int usageType, Double rawUsage, Long usageId, String type, Long networkId, Date startDate, Date endDate) {
this.zoneId = zoneId;
@ -247,7 +271,11 @@ public class UsageVO implements Usage, InternalIdentity {
public Long getSize() {
return size;
}
public Long getVirtualSize() {
return virtualSize;
}
@Override
public Date getStartDate() {
return startDate;

View File

@ -65,7 +65,7 @@ public class UsageDaoImpl extends GenericDaoBase<UsageVO, Long> implements Usage
private static final String UPDATE_VM_DISK_STATS = "UPDATE cloud_usage.vm_disk_statistics SET net_io_read=?, net_io_write=?, current_io_read=?, current_io_write=?, agg_io_read=?, agg_io_write=?, " +
"net_bytes_read=?, net_bytes_write=?, current_bytes_read=?, current_bytes_write=?, agg_bytes_read=?, agg_bytes_write=? WHERE id=?";
private static final String INSERT_USGAE_RECORDS = "INSERT INTO cloud_usage.cloud_usage (zone_id, account_id, domain_id, description, usage_display, usage_type, raw_usage, vm_instance_id, vm_name, offering_id, template_id, " +
"usage_id, type, size, network_id, start_date, end_date) VALUES (?,?,?,?,?,?,?,?,?, ?, ?, ?,?,?,?,?,?)";
"usage_id, type, size, network_id, start_date, end_date, virtual_size) VALUES (?,?,?,?,?,?,?,?,?, ?, ?, ?,?,?,?,?,?)";
protected final static TimeZone s_gmtTimeZone = TimeZone.getTimeZone("GMT");
@ -429,6 +429,11 @@ public class UsageDaoImpl extends GenericDaoBase<UsageVO, Long> implements Usage
}
pstmt.setTimestamp(16, new Timestamp(usageRecord.getStartDate().getTime()));
pstmt.setTimestamp(17, new Timestamp(usageRecord.getEndDate().getTime()));
if(usageRecord.getVirtualSize() != null){
pstmt.setLong(18, usageRecord.getSize());
} else {
pstmt.setNull(18, Types.BIGINT);
}
pstmt.addBatch();
}
pstmt.executeBatch();

View File

@ -42,15 +42,15 @@ public class UsageStorageDaoImpl extends GenericDaoBase<UsageStorageVO, Long> im
protected static final String REMOVE_BY_USERID_STORAGEID = "DELETE FROM usage_storage WHERE account_id = ? AND id = ? AND storage_type = ?";
protected static final String UPDATE_DELETED = "UPDATE usage_storage SET deleted = ? WHERE account_id = ? AND id = ? AND storage_type = ? and deleted IS NULL";
protected static final String GET_USAGE_RECORDS_BY_ACCOUNT = "SELECT id, zone_id, account_id, domain_id, storage_type, source_id, size, created, deleted " +
protected static final String GET_USAGE_RECORDS_BY_ACCOUNT = "SELECT id, zone_id, account_id, domain_id, storage_type, source_id, size, created, deleted, virtual_size " +
"FROM usage_storage " +
"WHERE account_id = ? AND ((deleted IS NULL) OR (created BETWEEN ? AND ?) OR " +
" (deleted BETWEEN ? AND ?) OR ((created <= ?) AND (deleted >= ?)))";
protected static final String GET_USAGE_RECORDS_BY_DOMAIN = "SELECT id, zone_id, account_id, domain_id, storage_type, source_id, size, created, deleted " +
protected static final String GET_USAGE_RECORDS_BY_DOMAIN = "SELECT id, zone_id, account_id, domain_id, storage_type, source_id, size, created, deleted, virtual_size " +
"FROM usage_storage " +
"WHERE domain_id = ? AND ((deleted IS NULL) OR (created BETWEEN ? AND ?) OR " +
" (deleted BETWEEN ? AND ?) OR ((created <= ?) AND (deleted >= ?)))";
protected static final String GET_ALL_USAGE_RECORDS = "SELECT id, zone_id, account_id, domain_id, storage_type, source_id, size, created, deleted " +
protected static final String GET_ALL_USAGE_RECORDS = "SELECT id, zone_id, account_id, domain_id, storage_type, source_id, size, created, deleted, virtual_size " +
"FROM usage_storage " +
"WHERE (deleted IS NULL) OR (created BETWEEN ? AND ?) OR " +
" (deleted BETWEEN ? AND ?) OR ((created <= ?) AND (deleted >= ?))";
@ -184,6 +184,7 @@ public class UsageStorageDaoImpl extends GenericDaoBase<UsageStorageVO, Long> im
Integer type = Integer.valueOf(rs.getInt(5));
Long sourceId = Long.valueOf(rs.getLong(6));
Long size = Long.valueOf(rs.getLong(7));
Long virtualSize = Long.valueOf(rs.getLong(10));
Date createdDate = null;
Date deletedDate = null;
String createdTS = rs.getString(8);
@ -197,7 +198,7 @@ public class UsageStorageDaoImpl extends GenericDaoBase<UsageStorageVO, Long> im
deletedDate = DateUtil.parseDateString(s_gmtTimeZone, deletedTS);
}
usageRecords.add(new UsageStorageVO(id, zoneId, acctId, dId, type, sourceId, size, createdDate, deletedDate));
usageRecords.add(new UsageStorageVO(id, zoneId, acctId, dId, type, sourceId, size, virtualSize, createdDate, deletedDate));
}
} catch (Exception e) {
txn.rollback();

View File

@ -3369,6 +3369,11 @@ public class ApiResponseHelper implements ResponseGenerator {
usageRecResponse.setUsageId(tmpl.getUuid());
//Template/ISO Size
usageRecResponse.setSize(usageRecord.getSize());
if(usageRecord.getUsageType() == UsageTypes.ISO) {
usageRecResponse.setVirtualSize(usageRecord.getSize());
} else {
usageRecResponse.setVirtualSize(usageRecord.getVirtualSize());
}
} else if(usageRecord.getUsageType() == UsageTypes.SNAPSHOT){
//Snapshot ID

View File

@ -67,6 +67,14 @@ public class UsageEventUtils {
publishUsageEvent(usageType, accountId, zoneId, entityType, entityUUID);
}
public static void publishUsageEvent(String usageType, long accountId, long zoneId,
long resourceId, String resourceName,
Long offeringId, Long templateId, Long size, Long virtualSize,
String entityType, String entityUUID) {
saveUsageEvent(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, size, virtualSize);
publishUsageEvent(usageType, accountId, zoneId, entityType, entityUUID);
}
public static void publishUsageEvent(String usageType, long accountId, long zoneId, long resourceId,
String resourceName, String entityType, String entityUUID) {
saveUsageEvent(usageType, accountId, zoneId, resourceId, resourceName);
@ -97,6 +105,9 @@ public class UsageEventUtils {
_usageEventDao.persist( new UsageEventVO(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, size));
}
public static void saveUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId, Long size, Long virtualSize) {
_usageEventDao.persist( new UsageEventVO(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, size, virtualSize));
}
public static void saveUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName) {
_usageEventDao.persist( new UsageEventVO(usageType, accountId, zoneId, resourceId, resourceName));
}

View File

@ -148,6 +148,7 @@ import com.cloud.storage.dao.StoragePoolHostDao;
import com.cloud.storage.dao.UploadDao;
import com.cloud.storage.dao.VMTemplateDao;
import com.cloud.storage.dao.VMTemplateDetailsDao;
import com.cloud.storage.dao.VMTemplateHostDao;
import com.cloud.storage.dao.VMTemplatePoolDao;
import com.cloud.storage.dao.VMTemplateS3Dao;
import com.cloud.storage.dao.VMTemplateZoneDao;
@ -279,6 +280,8 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
EndPointSelector _epSelector;
@Inject
UserVmJoinDao _userVmJoinDao;
@Inject
VMTemplateHostDao _vmTemplateHostDao;
@Inject
ConfigurationServer _configServer;
@ -655,7 +658,7 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
_tmpltDao.addTemplateToZone(template, dstZoneId);
if (account.getId() != Account.ACCOUNT_ID_SYSTEM) {
UsageEventUtils.publishUsageEvent(copyEventType, account.getId(), dstZoneId, tmpltId, null, null, null, srcTmpltStore.getSize(),
UsageEventUtils.publishUsageEvent(copyEventType, account.getId(), dstZoneId, tmpltId, null, null, null, srcTmpltStore.getPhysicalSize(), srcTmpltStore.getSize(),
template.getClass().getName(), template.getUuid());
}
return true;
@ -1369,8 +1372,9 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
this._tmpltZoneDao.persist(templateZone);
privateTemplate = this._tmpltDao.findById(templateId);
TemplateDataStoreVO srcTmpltStore = this._tmplStoreDao.findByStoreTemplate(store.getId(), templateId);
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_TEMPLATE_CREATE, privateTemplate.getAccountId(), zoneId,
privateTemplate.getId(), privateTemplate.getName(), null, privateTemplate.getSourceTemplateId(), privateTemplate.getSize());
privateTemplate.getId(), privateTemplate.getName(), null, privateTemplate.getSourceTemplateId(), srcTmpltStore.getPhysicalSize(), privateTemplate.getSize());
_usageEventDao.persist(usageEvent);
} catch (InterruptedException e) {
s_logger.debug("Failed to create template", e);

View File

@ -1297,7 +1297,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
}
Account acct = m_accountDao.findByIdIncludingRemoved(event.getAccountId());
UsageStorageVO storageVO = new UsageStorageVO(templateId, zoneId, event.getAccountId(), acct.getDomainId(), StorageTypes.TEMPLATE, event.getTemplateId(),
templateSize, event.getCreateDate(), null);
templateSize, event.getVirtualSize(), event.getCreateDate(), null);
m_usageStorageDao.persist(storageVO);
} else if (EventTypes.EVENT_TEMPLATE_DELETE.equals(event.getType())) {
List<UsageStorageVO> storageVOs;
@ -1339,7 +1339,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
}
Account acct = m_accountDao.findByIdIncludingRemoved(event.getAccountId());
UsageStorageVO storageVO = new UsageStorageVO( isoId, zoneId, event.getAccountId(), acct.getDomainId(), StorageTypes.ISO, null,
isoSize, event.getCreateDate(), null);
isoSize, isoSize, event.getCreateDate(), null);
m_usageStorageDao.persist(storageVO);
} else if (EventTypes.EVENT_ISO_DELETE.equals(event.getType())) {
List<UsageStorageVO> storageVOs;

View File

@ -84,13 +84,14 @@ public class StorageUsageParser {
long storageId = usageStorage.getId();
int storage_type = usageStorage.getStorageType();
long size = usageStorage.getSize();
Long virtualSize = usageStorage.getVirtualSize();
long zoneId = usageStorage.getZoneId();
Long sourceId = usageStorage.getSourceId();
String key = ""+storageId+"Z"+zoneId+"T"+storage_type;
// store the info in the storage map
storageMap.put(key, new StorageInfo(zoneId, storageId, storage_type, sourceId, size));
storageMap.put(key, new StorageInfo(zoneId, storageId, storage_type, sourceId, size, virtualSize));
Date storageCreateDate = usageStorage.getCreated();
Date storageDeleteDate = usageStorage.getDeleted();
@ -116,7 +117,7 @@ public class StorageUsageParser {
// Only create a usage record if we have a runningTime of bigger than zero.
if (useTime > 0L) {
StorageInfo info = storageMap.get(storageIdKey);
createUsageRecord(info.getZoneId(), info.getStorageType(), useTime, startDate, endDate, account, info.getStorageId(), info.getSourceId(), info.getSize());
createUsageRecord(info.getZoneId(), info.getStorageType(), useTime, startDate, endDate, account, info.getStorageId(), info.getSourceId(), info.getSize(), info.getVirtualSize());
}
}
@ -135,7 +136,7 @@ public class StorageUsageParser {
usageDataMap.put(key, volUsageInfo);
}
private static void createUsageRecord(long zoneId, int type, long runningTime, Date startDate, Date endDate, AccountVO account, long storageId, Long sourceId, long size) {
private static void createUsageRecord(long zoneId, int type, long runningTime, Date startDate, Date endDate, AccountVO account, long storageId, Long sourceId, long size, Long virtualSize) {
// Our smallest increment is hourly for now
if (s_logger.isDebugEnabled()) {
s_logger.debug("Total running time " + runningTime + "ms");
@ -163,6 +164,7 @@ public class StorageUsageParser {
case StorageTypes.ISO:
usage_type = UsageTypes.ISO;
usageDesc += "ISO ";
virtualSize = size;
break;
case StorageTypes.SNAPSHOT:
usage_type = UsageTypes.SNAPSHOT;
@ -170,11 +172,11 @@ public class StorageUsageParser {
break;
}
// Create the usage record
usageDesc += "Id:"+storageId+" Size:"+size;
usageDesc += "Id:"+storageId+" Size:"+size+ "VirtualSize:" + virtualSize;
//ToDo: get zone id
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", usage_type,
new Double(usage), null, null, null, tmplSourceId, storageId, size, startDate, endDate);
new Double(usage), null, null, null, tmplSourceId, storageId, size, virtualSize, startDate, endDate);
m_usageDao.persist(usageRecord);
}
@ -184,13 +186,19 @@ public class StorageUsageParser {
private int storageType;
private Long sourceId;
private long size;
private Long virtualSize;
public StorageInfo(long zoneId, long storageId, int storageType, Long sourceId, long size) {
public StorageInfo(long zoneId, long storageId, int storageType, Long sourceId, long size, Long virtualSize) {
this.zoneId = zoneId;
this.storageId = storageId;
this.storageType = storageType;
this.sourceId = sourceId;
this.size = size;
this.virtualSize = virtualSize;
}
public Long getVirtualSize() {
return virtualSize;
}
public long getZoneId() {
@ -209,7 +217,6 @@ public class StorageUsageParser {
return sourceId;
}
public long getSize() {
return size;
}