Skip to content

Commit 3d04dd4

Browse files
committed
add vmId in usageVolume
1 parent 0c7d471 commit 3d04dd4

8 files changed

Lines changed: 137 additions & 18 deletions

File tree

engine/schema/src/main/java/com/cloud/storage/dao/VolumeDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,6 @@ public interface VolumeDao extends GenericDao<VolumeVO, Long>, StateDao<Volume.S
158158
List<VolumeVO> listByIds(List<Long> ids);
159159

160160
VolumeVO findOneByIScsiName(String iScsiName);
161+
162+
Long getInstanceIdByVolumeId(long volumeId);
161163
}

engine/schema/src/main/java/com/cloud/storage/dao/VolumeDaoImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol
6767
protected final SearchBuilder<VolumeVO> RootDiskStateSearch;
6868
private final SearchBuilder<VolumeVO> storeAndInstallPathSearch;
6969
private final SearchBuilder<VolumeVO> volumeIdSearch;
70+
private final SearchBuilder<VolumeVO> instanceIdByVolumeIdSearch;
7071
protected GenericSearchBuilder<VolumeVO, Long> CountByAccount;
7172
protected GenericSearchBuilder<VolumeVO, SumCount> primaryStorageSearch;
7273
protected GenericSearchBuilder<VolumeVO, SumCount> primaryStorageSearch2;
@@ -494,6 +495,11 @@ public VolumeDaoImpl() {
494495
poolAndPathSearch.and("poolId", poolAndPathSearch.entity().getPoolId(), Op.EQ);
495496
poolAndPathSearch.and("path", poolAndPathSearch.entity().getPath(), Op.EQ);
496497
poolAndPathSearch.done();
498+
499+
instanceIdByVolumeIdSearch = createSearchBuilder();
500+
instanceIdByVolumeIdSearch.selectFields(instanceIdByVolumeIdSearch.entity().getInstanceId());
501+
instanceIdByVolumeIdSearch.and("id", instanceIdByVolumeIdSearch.entity().getId(), Op.EQ);
502+
instanceIdByVolumeIdSearch.done();
497503
}
498504

499505
@Override
@@ -847,4 +853,12 @@ public VolumeVO findOneByIScsiName(String iScsiName) {
847853
sc.setParameters("iScsiName", iScsiName);
848854
return findOneIncludingRemovedBy(sc);
849855
}
856+
857+
@Override
858+
public Long getInstanceIdByVolumeId(long volumeId) {
859+
SearchCriteria<VolumeVO> sc = instanceIdByVolumeIdSearch.create();
860+
sc.setParameters("id", volumeId);
861+
List<VolumeVO> volumes = search(sc, null);
862+
return volumes.isEmpty() ? null : volumes.get(0).getInstanceId();
863+
}
850864
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.upgrade.dao;
18+
19+
import java.io.InputStream;
20+
import java.sql.Connection;
21+
22+
import com.cloud.utils.exception.CloudRuntimeException;
23+
24+
public class Upgrade41930to41940 implements DbUpgrade {
25+
26+
@Override
27+
public String[] getUpgradableVersionRange() {
28+
return new String[]{"4.19.3.0", "4.19.4.0"};
29+
}
30+
31+
@Override
32+
public String getUpgradedVersion() {
33+
return "4.19.4.0";
34+
}
35+
36+
@Override
37+
public boolean supportsRollingUpgrade() {
38+
return false;
39+
}
40+
41+
@Override
42+
public InputStream[] getPrepareScripts() {
43+
final String scriptFile = "META-INF/db/schema-41930to41940.sql";
44+
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
45+
if (script == null) {
46+
throw new CloudRuntimeException("Unable to find " + scriptFile);
47+
}
48+
49+
return new InputStream[]{script};
50+
}
51+
52+
@Override
53+
public void performDataMigration(Connection conn) {
54+
}
55+
56+
@Override
57+
public InputStream[] getCleanupScripts() {
58+
return new InputStream[0];
59+
}
60+
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,21 @@ public class UsageVolumeVO implements InternalIdentity {
6767
@Temporal(value = TemporalType.TIMESTAMP)
6868
private Date deleted = null;
6969

70+
@Column(name = "vm_instance_id")
71+
private long vmInstanceId;
72+
7073
protected UsageVolumeVO() {
7174
}
7275

73-
public UsageVolumeVO(long id, long zoneId, long accountId, long domainId, Long diskOfferingId, Long templateId, long size, Date created, Date deleted) {
76+
public UsageVolumeVO(long id, long zoneId, long accountId, long domainId, Long diskOfferingId, Long templateId,
77+
Long vmInstanceId, long size, Date created, Date deleted) {
7478
this.volumeId = id;
7579
this.zoneId = zoneId;
7680
this.accountId = accountId;
7781
this.domainId = domainId;
7882
this.diskOfferingId = diskOfferingId;
7983
this.templateId = templateId;
84+
this.vmInstanceId = vmInstanceId;
8085
this.size = size;
8186
this.created = created;
8287
this.deleted = deleted;
@@ -126,4 +131,8 @@ public void setDeleted(Date deleted) {
126131
public long getVolumeId() {
127132
return volumeId;
128133
}
134+
135+
public long getVmInstanceId() {
136+
return vmInstanceId;
137+
}
129138
}

engine/schema/src/main/java/com/cloud/usage/dao/UsageVolumeDaoImpl.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ public class UsageVolumeDaoImpl extends GenericDaoBase<UsageVolumeVO, Long> impl
4040

4141
protected static final String REMOVE_BY_USERID_VOLID = "DELETE FROM usage_volume WHERE account_id = ? AND volume_id = ?";
4242
protected static final String UPDATE_DELETED = "UPDATE usage_volume SET deleted = ? WHERE account_id = ? AND volume_id = ? and deleted IS NULL";
43-
protected static final String GET_USAGE_RECORDS_BY_ACCOUNT = "SELECT volume_id, zone_id, account_id, domain_id, disk_offering_id, template_id, size, created, deleted "
43+
protected static final String GET_USAGE_RECORDS_BY_ACCOUNT = "SELECT volume_id, zone_id, account_id, domain_id, disk_offering_id, template_id, vm_instance_id, size, created, deleted "
4444
+ "FROM usage_volume " + "WHERE account_id = ? AND ((deleted IS NULL) OR (created BETWEEN ? AND ?) OR "
4545
+ " (deleted BETWEEN ? AND ?) OR ((created <= ?) AND (deleted >= ?)))";
46-
protected static final String GET_USAGE_RECORDS_BY_DOMAIN = "SELECT volume_id, zone_id, account_id, domain_id, disk_offering_id, template_id, size, created, deleted "
46+
protected static final String GET_USAGE_RECORDS_BY_DOMAIN = "SELECT volume_id, zone_id, account_id, domain_id, disk_offering_id, template_id, vm_instance_id, size, created, deleted "
4747
+ "FROM usage_volume " + "WHERE domain_id = ? AND ((deleted IS NULL) OR (created BETWEEN ? AND ?) OR "
4848
+ " (deleted BETWEEN ? AND ?) OR ((created <= ?) AND (deleted >= ?)))";
49-
protected static final String GET_ALL_USAGE_RECORDS = "SELECT volume_id, zone_id, account_id, domain_id, disk_offering_id, template_id, size, created, deleted "
49+
protected static final String GET_ALL_USAGE_RECORDS = "SELECT volume_id, zone_id, account_id, domain_id, disk_offering_id, template_id, vm_instance_id, size, created, deleted "
5050
+ "FROM usage_volume " + "WHERE (deleted IS NULL) OR (created BETWEEN ? AND ?) OR " + " (deleted BETWEEN ? AND ?) OR ((created <= ?) AND (deleted >= ?))";
5151

5252
public UsageVolumeDaoImpl() {
@@ -152,11 +152,12 @@ public List<UsageVolumeVO> getUsageRecords(Long accountId, Long domainId, Date s
152152
if (tId == 0) {
153153
tId = null;
154154
}
155-
long size = Long.valueOf(rs.getLong(7));
155+
Long vmId = rs.getLong(7);
156+
long size = Long.valueOf(rs.getLong(8));
156157
Date createdDate = null;
157158
Date deletedDate = null;
158-
String createdTS = rs.getString(8);
159-
String deletedTS = rs.getString(9);
159+
String createdTS = rs.getString(9);
160+
String deletedTS = rs.getString(10);
160161

161162
if (createdTS != null) {
162163
createdDate = DateUtil.parseDateString(s_gmtTimeZone, createdTS);
@@ -165,7 +166,7 @@ public List<UsageVolumeVO> getUsageRecords(Long accountId, Long domainId, Date s
165166
deletedDate = DateUtil.parseDateString(s_gmtTimeZone, deletedTS);
166167
}
167168

168-
usageRecords.add(new UsageVolumeVO(vId, zoneId, acctId, dId, doId, tId, size, createdDate, deletedDate));
169+
usageRecords.add(new UsageVolumeVO(vId, zoneId, acctId, dId, doId, tId, vmId, size, createdDate, deletedDate));
169170
}
170171
} catch (Exception e) {
171172
txn.rollback();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
--;
19+
-- Schema upgrade from 4.19.3.0 to 4.19.4.0
20+
--;
21+
ALTER TABLE `cloud`.`usage_event` ADD COLUMN `vm_instance_id` bigint unsigned DEFAULT NULL;
22+
ALTER TABLE `cloud_usage`.`usage_volume` ADD COLUMN `vm_instance_id` bigint unsigned DEFAULT NULL;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.concurrent.TimeUnit;
3434

3535
import com.cloud.network.Network;
36+
import com.cloud.storage.dao.VolumeDao;
3637
import com.cloud.usage.dao.UsageNetworksDao;
3738
import com.cloud.usage.parser.NetworksUsageParser;
3839
import com.cloud.network.vpc.Vpc;
@@ -180,6 +181,8 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
180181

181182
@Inject
182183
private UsageVpcDao usageVpcDao;
184+
@Inject
185+
private VolumeDao volumeDao;
183186

184187
private String _version = null;
185188
private final Calendar _jobExecTime = Calendar.getInstance();
@@ -1573,7 +1576,9 @@ private void createVolumeHelperEvent(UsageEventVO event) {
15731576
s_logger.debug("create volume with id : " + volId + " for account: " + event.getAccountId());
15741577
}
15751578
Account acct = _accountDao.findByIdIncludingRemoved(event.getAccountId());
1576-
UsageVolumeVO volumeVO = new UsageVolumeVO(volId, event.getZoneId(), event.getAccountId(), acct.getDomainId(), event.getOfferingId(), event.getTemplateId(), event.getSize(), event.getCreateDate(), null);
1579+
Long vmId = volumeDao.getInstanceIdByVolumeId(volId);
1580+
UsageVolumeVO volumeVO = new UsageVolumeVO(volId, event.getZoneId(), event.getAccountId(), acct.getDomainId(),
1581+
event.getOfferingId(), event.getTemplateId(), vmId, event.getSize(), event.getCreateDate(), null);
15771582
_usageVolumeDao.persist(volumeVO);
15781583
} else if (EventTypes.EVENT_VOLUME_DELETE.equals(event.getType())) {
15791584
SearchCriteria<UsageVolumeVO> sc = _usageVolumeDao.createSearchCriteria();

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
8686
Long doId = usageVol.getDiskOfferingId();
8787
long zoneId = usageVol.getZoneId();
8888
Long templateId = usageVol.getTemplateId();
89+
Long instanceId = usageVol.getVmInstanceId();
8990
long size = usageVol.getSize();
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, zoneId, doId, templateId, instanceId, size));
9394

9495
Date volCreateDate = usageVol.getCreated();
9596
Date volDeleteDate = usageVol.getDeleted();
@@ -120,8 +121,8 @@ 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-
info.getTemplateId(), info.getSize());
124+
createUsageRecord(UsageTypes.VOLUME, useTime, startDate, endDate, account, info.getVolumeId(),
125+
info.getZoneId(), info.getDiskOfferingId(), info.getTemplateId(), info.getInstanceId(), info.getSize());
125126
}
126127
}
127128

@@ -140,8 +141,8 @@ 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-
Long templateId, long size) {
144+
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account,
145+
long volId, long zoneId, Long doId, Long templateId, Long instanceId, long size) {
145146
// Our smallest increment is hourly for now
146147
if (s_logger.isDebugEnabled()) {
147148
s_logger.debug("Total running time " + runningTime + "ms");
@@ -165,9 +166,8 @@ private static void createUsageRecord(int type, long runningTime, Date startDate
165166
} else if (doId != null) {
166167
usageDesc += " (DiskOffering: " + doId + ")";
167168
}
168-
169-
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, doId, templateId, volId,
170-
size, startDate, endDate);
169+
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc,usageDisplay + " Hrs",
170+
type, new Double(usage), instanceId,null, doId, templateId, volId, size, startDate, endDate);
171171
s_usageDao.persist(usageRecord);
172172
}
173173

@@ -176,13 +176,15 @@ private static class VolInfo {
176176
private long zoneId;
177177
private Long diskOfferingId;
178178
private Long templateId;
179+
private Long instanceId;
179180
private long size;
180181

181-
public VolInfo(long volId, long zoneId, Long diskOfferingId, Long templateId, long size) {
182+
public VolInfo(long volId, long zoneId, Long diskOfferingId, Long templateId, Long instanceId, long size) {
182183
this.volId = volId;
183184
this.zoneId = zoneId;
184185
this.diskOfferingId = diskOfferingId;
185186
this.templateId = templateId;
187+
this.instanceId = instanceId;
186188
this.size = size;
187189
}
188190

@@ -202,6 +204,10 @@ public Long getTemplateId() {
202204
return templateId;
203205
}
204206

207+
public Long getInstanceId() {
208+
return instanceId;
209+
}
210+
205211
public long getSize() {
206212
return size;
207213
}

0 commit comments

Comments
 (0)