Skip to content

[WIP] Add vm instance id to volume usage record #9217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 4.19
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/src/main/java/com/cloud/event/EventTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ public class EventTypes {
public static final String EVENT_VOLUME_IMPORT = "VOLUME.IMPORT";
public static final String EVENT_VOLUME_UNMANAGE = "VOLUME.UNMANAGE";
public static final String EVENT_VOLUME_CHANGE_DISK_OFFERING = "VOLUME.CHANGE.DISK.OFFERING";
public static final String EVENT_VOLUME_ASSIGN = "VOLUME.ASSIGN";
public static final String EVENT_VOLUME_RELEASE = "VOLUME.RELEASE";

// Domains
public static final String EVENT_DOMAIN_CREATE = "DOMAIN.CREATE";
Expand Down Expand Up @@ -884,6 +886,8 @@ public class EventTypes {
entityEventDetails.put(EVENT_VOLUME_DESTROY, Volume.class);
entityEventDetails.put(EVENT_VOLUME_RECOVER, Volume.class);
entityEventDetails.put(EVENT_VOLUME_CHANGE_DISK_OFFERING, Volume.class);
entityEventDetails.put(EVENT_VOLUME_ASSIGN, Volume.class);
entityEventDetails.put(EVENT_VOLUME_RELEASE, Volume.class);

// Domains
entityEventDetails.put(EVENT_DOMAIN_CREATE, Domain.class);
Expand Down
2 changes: 2 additions & 0 deletions api/src/main/java/com/cloud/event/UsageEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public interface UsageEvent extends InternalIdentity {

Long getSize();

Long getVmInstanceId();

Long getTemplateId();

Long getOfferingId();
Expand Down
20 changes: 20 additions & 0 deletions engine/schema/src/main/java/com/cloud/event/UsageEventVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public enum DynamicParameters {
@Column(name = "resource_name")
private String resourceName;

@Column(name = "vm_instance_id")
private Long vmInstanceId;

@Column(name = "offering_id")
private Long offeringId;

Expand Down Expand Up @@ -134,6 +137,18 @@ public UsageEventVO(String usageType, long accountId, long zoneId, long resource
this.resourceType = resourceType;
}

public UsageEventVO(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long vmInstanceId, Long offeringId, Long templateId, String resourceType) {
this.type = usageType;
this.accountId = accountId;
this.zoneId = zoneId;
this.resourceId = resourceId;
this.resourceName = resourceName;
this.vmInstanceId = vmInstanceId;
this.offeringId = offeringId;
this.templateId = templateId;
this.resourceType = resourceType;
}

//Security Group usage event
public UsageEventVO(String usageType, long accountId, long zoneId, long vmId, long securityGroupId) {
this.type = usageType;
Expand Down Expand Up @@ -201,6 +216,11 @@ public String getResourceName() {
return resourceName;
}

@Override
public Long getVmInstanceId() {
return vmInstanceId;
}

public void setOfferingId(long offeringId) {
this.offeringId = offeringId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class UsageVolumeVO implements InternalIdentity {
@Column(name = "volume_id")
private long volumeId;

@Column(name = "vm_instance_id")
private Long vmInstanceId;

@Column(name = "disk_offering_id")
private Long diskOfferingId;

Expand Down Expand Up @@ -99,6 +102,10 @@ public long getId() {
return id;
}

public Long getVmInstanceId() {
return vmInstanceId;
}

public Long getDiskOfferingId() {
return diskOfferingId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ CREATE TABLE IF NOT EXISTS `cloud_usage`.`usage_vpc` (
CALL `cloud_usage`.`IDEMPOTENT_ADD_COLUMN`('cloud_usage.cloud_usage', 'state', 'VARCHAR(100) DEFAULT NULL');

CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.user_data', 'removed', 'datetime COMMENT "date removed or null, if still present"');

CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.usage_event', 'vm_instance_id', 'bigint(20) unsigned DEFAULT NULL');

CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud_usage.usage_volume', 'vm_instance_id', 'bigint(20) unsigned DEFAULT NULL');
5 changes: 4 additions & 1 deletion usage/src/main/java/com/cloud/usage/UsageManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,10 @@ private boolean isIPEvent(String eventType) {

private boolean isVolumeEvent(String eventType) {
return eventType != null &&
(eventType.equals(EventTypes.EVENT_VOLUME_CREATE) || eventType.equals(EventTypes.EVENT_VOLUME_DELETE) || eventType.equals(EventTypes.EVENT_VOLUME_RESIZE) || eventType.equals(EventTypes.EVENT_VOLUME_UPLOAD));
(eventType.equals(EventTypes.EVENT_VOLUME_CREATE) || eventType.equals(EventTypes.EVENT_VOLUME_DELETE) ||
eventType.equals(EventTypes.EVENT_VOLUME_RESIZE) || eventType.equals(EventTypes.EVENT_VOLUME_UPLOAD) ||
eventType.equals(EventTypes.EVENT_VOLUME_ASSIGN) || eventType.equals(EventTypes.EVENT_VOLUME_RELEASE) ||
eventType.equals(EventTypes.EVENT_VOLUME_ATTACH) || eventType.equals(EventTypes.EVENT_VOLUME_DETACH));
}

private boolean isTemplateEvent(String eventType) {
Expand Down
17 changes: 12 additions & 5 deletions usage/src/main/java/com/cloud/usage/parser/VolumeUsageParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
long zoneId = usageVol.getZoneId();
Long templateId = usageVol.getTemplateId();
long size = usageVol.getSize();
Long vmId = usageVol.getVmInstanceId();
String key = volId + "-" + doId + "-" + size;

diskOfferingMap.put(key, new VolInfo(volId, zoneId, doId, templateId, size));
diskOfferingMap.put(key, new VolInfo(volId, vmId, zoneId, doId, templateId, size));

Date volCreateDate = usageVol.getCreated();
Date volDeleteDate = usageVol.getDeleted();
Expand Down Expand Up @@ -120,7 +121,7 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
// Only create a usage record if we have a runningTime of bigger than zero.
if (useTime > 0L) {
VolInfo info = diskOfferingMap.get(volIdKey);
createUsageRecord(UsageTypes.VOLUME, useTime, startDate, endDate, account, info.getVolumeId(), info.getZoneId(), info.getDiskOfferingId(),
createUsageRecord(UsageTypes.VOLUME, useTime, startDate, endDate, account, info.getVolumeId(), info.getVmInstanceId(), info.getZoneId(), info.getDiskOfferingId(),
info.getTemplateId(), info.getSize());
}
}
Expand All @@ -140,7 +141,7 @@ private static void updateVolUsageData(Map<String, Pair<Long, Long>> usageDataMa
usageDataMap.put(key, volUsageInfo);
}

private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long volId, long zoneId, Long doId,
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long volId, Long vmId, long zoneId, Long doId,
Long templateId, long size) {
// Our smallest increment is hourly for now
if (s_logger.isDebugEnabled()) {
Expand All @@ -166,20 +167,22 @@ private static void createUsageRecord(int type, long runningTime, Date startDate
usageDesc += " (DiskOffering: " + doId + ")";
}

UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, doId, templateId, volId,
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), vmId, null, doId, templateId, volId,
size, startDate, endDate);
s_usageDao.persist(usageRecord);
}

private static class VolInfo {
private long volId;
private Long vmId;
private long zoneId;
private Long diskOfferingId;
private Long templateId;
private long size;

public VolInfo(long volId, long zoneId, Long diskOfferingId, Long templateId, long size) {
public VolInfo(long volId, Long vmId, long zoneId, Long diskOfferingId, Long templateId, long size) {
this.volId = volId;
this.vmId = vmId;
this.zoneId = zoneId;
this.diskOfferingId = diskOfferingId;
this.templateId = templateId;
Expand All @@ -194,6 +197,10 @@ public long getVolumeId() {
return volId;
}

public Long getVmInstanceId() {
return vmId;
}

public Long getDiskOfferingId() {
return diskOfferingId;
}
Expand Down
Loading