Skip to content

Commit 0ca37b4

Browse files
Add vm instance id to volume usage record
1 parent 4bd0b1c commit 0ca37b4

File tree

7 files changed

+53
-6
lines changed

7 files changed

+53
-6
lines changed

api/src/main/java/com/cloud/event/EventTypes.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ public class EventTypes {
319319
public static final String EVENT_VOLUME_IMPORT = "VOLUME.IMPORT";
320320
public static final String EVENT_VOLUME_UNMANAGE = "VOLUME.UNMANAGE";
321321
public static final String EVENT_VOLUME_CHANGE_DISK_OFFERING = "VOLUME.CHANGE.DISK.OFFERING";
322+
public static final String EVENT_VOLUME_ASSIGN = "VOLUME.ASSIGN";
323+
public static final String EVENT_VOLUME_RELEASE = "VOLUME.RELEASE";
322324

323325
// Domains
324326
public static final String EVENT_DOMAIN_CREATE = "DOMAIN.CREATE";
@@ -884,6 +886,8 @@ public class EventTypes {
884886
entityEventDetails.put(EVENT_VOLUME_DESTROY, Volume.class);
885887
entityEventDetails.put(EVENT_VOLUME_RECOVER, Volume.class);
886888
entityEventDetails.put(EVENT_VOLUME_CHANGE_DISK_OFFERING, Volume.class);
889+
entityEventDetails.put(EVENT_VOLUME_ASSIGN, Volume.class);
890+
entityEventDetails.put(EVENT_VOLUME_RELEASE, Volume.class);
887891

888892
// Domains
889893
entityEventDetails.put(EVENT_DOMAIN_CREATE, Domain.class);

api/src/main/java/com/cloud/event/UsageEvent.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public interface UsageEvent extends InternalIdentity {
2929

3030
Long getSize();
3131

32+
Long getVmInstanceId();
33+
3234
Long getTemplateId();
3335

3436
Long getOfferingId();

engine/schema/src/main/java/com/cloud/event/UsageEventVO.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public enum DynamicParameters {
5757
@Column(name = "resource_name")
5858
private String resourceName;
5959

60+
@Column(name = "vm_instance_id")
61+
private Long vmInstanceId;
62+
6063
@Column(name = "offering_id")
6164
private Long offeringId;
6265

@@ -134,6 +137,18 @@ public UsageEventVO(String usageType, long accountId, long zoneId, long resource
134137
this.resourceType = resourceType;
135138
}
136139

140+
public UsageEventVO(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long vmInstanceId, Long offeringId, Long templateId, String resourceType) {
141+
this.type = usageType;
142+
this.accountId = accountId;
143+
this.zoneId = zoneId;
144+
this.resourceId = resourceId;
145+
this.resourceName = resourceName;
146+
this.vmInstanceId = vmInstanceId;
147+
this.offeringId = offeringId;
148+
this.templateId = templateId;
149+
this.resourceType = resourceType;
150+
}
151+
137152
//Security Group usage event
138153
public UsageEventVO(String usageType, long accountId, long zoneId, long vmId, long securityGroupId) {
139154
this.type = usageType;
@@ -201,6 +216,11 @@ public String getResourceName() {
201216
return resourceName;
202217
}
203218

219+
@Override
220+
public Long getVmInstanceId() {
221+
return vmInstanceId;
222+
}
223+
204224
public void setOfferingId(long offeringId) {
205225
this.offeringId = offeringId;
206226
}

engine/schema/src/main/java/com/cloud/usage/UsageVolumeVO.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public class UsageVolumeVO implements InternalIdentity {
5050
@Column(name = "volume_id")
5151
private long volumeId;
5252

53+
@Column(name = "vm_instance_id")
54+
private Long vmInstanceId;
55+
5356
@Column(name = "disk_offering_id")
5457
private Long diskOfferingId;
5558

@@ -99,6 +102,10 @@ public long getId() {
99102
return id;
100103
}
101104

105+
public Long getVmInstanceId() {
106+
return vmInstanceId;
107+
}
108+
102109
public Long getDiskOfferingId() {
103110
return diskOfferingId;
104111
}

engine/schema/src/main/resources/META-INF/db/schema-41900to41910.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,7 @@ CREATE TABLE IF NOT EXISTS `cloud_usage`.`usage_vpc` (
6565
CALL `cloud_usage`.`IDEMPOTENT_ADD_COLUMN`('cloud_usage.cloud_usage', 'state', 'VARCHAR(100) DEFAULT NULL');
6666

6767
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.user_data', 'removed', 'datetime COMMENT "date removed or null, if still present"');
68+
69+
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.usage_event', 'vm_instance_id', 'bigint(20) unsigned DEFAULT NULL');
70+
71+
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud_usage.usage_volume', 'vm_instance_id', 'bigint(20) unsigned DEFAULT NULL');

usage/src/main/java/com/cloud/usage/UsageManagerImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,10 @@ private boolean isIPEvent(String eventType) {
11121112

11131113
private boolean isVolumeEvent(String eventType) {
11141114
return eventType != null &&
1115-
(eventType.equals(EventTypes.EVENT_VOLUME_CREATE) || eventType.equals(EventTypes.EVENT_VOLUME_DELETE) || eventType.equals(EventTypes.EVENT_VOLUME_RESIZE) || eventType.equals(EventTypes.EVENT_VOLUME_UPLOAD));
1115+
(eventType.equals(EventTypes.EVENT_VOLUME_CREATE) || eventType.equals(EventTypes.EVENT_VOLUME_DELETE) ||
1116+
eventType.equals(EventTypes.EVENT_VOLUME_RESIZE) || eventType.equals(EventTypes.EVENT_VOLUME_UPLOAD) ||
1117+
eventType.equals(EventTypes.EVENT_VOLUME_ASSIGN) || eventType.equals(EventTypes.EVENT_VOLUME_RELEASE) ||
1118+
eventType.equals(EventTypes.EVENT_VOLUME_ATTACH) || eventType.equals(EventTypes.EVENT_VOLUME_DETACH));
11161119
}
11171120

11181121
private boolean isTemplateEvent(String eventType) {

usage/src/main/java/com/cloud/usage/parser/VolumeUsageParser.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
8787
long zoneId = usageVol.getZoneId();
8888
Long templateId = usageVol.getTemplateId();
8989
long size = usageVol.getSize();
90+
Long vmId = usageVol.getVmInstanceId();
9091
String key = volId + "-" + doId + "-" + size;
9192

92-
diskOfferingMap.put(key, new VolInfo(volId, zoneId, doId, templateId, size));
93+
diskOfferingMap.put(key, new VolInfo(volId, vmId, zoneId, doId, templateId, size));
9394

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

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

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

174175
private static class VolInfo {
175176
private long volId;
177+
private Long vmId;
176178
private long zoneId;
177179
private Long diskOfferingId;
178180
private Long templateId;
179181
private long size;
180182

181-
public VolInfo(long volId, long zoneId, Long diskOfferingId, Long templateId, long size) {
183+
public VolInfo(long volId, Long vmId, long zoneId, Long diskOfferingId, Long templateId, long size) {
182184
this.volId = volId;
185+
this.vmId = vmId;
183186
this.zoneId = zoneId;
184187
this.diskOfferingId = diskOfferingId;
185188
this.templateId = templateId;
@@ -194,6 +197,10 @@ public long getVolumeId() {
194197
return volId;
195198
}
196199

200+
public Long getVmInstanceId() {
201+
return vmId;
202+
}
203+
197204
public Long getDiskOfferingId() {
198205
return diskOfferingId;
199206
}

0 commit comments

Comments
 (0)