Skip to content

Add instance id into volume usage records. #11203

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 2 commits 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
11 changes: 11 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 @@ -75,6 +75,9 @@ public enum DynamicParameters {
@Column(name = "virtual_size")
private Long virtualSize;

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

public UsageEventVO() {
}

Expand Down Expand Up @@ -248,4 +251,12 @@ public void setVirtualSize(Long virtualSize) {
this.virtualSize = virtualSize;
}

public long getVmInstanceId() {
return vmInstanceId;
}

public void setVmInstanceId(long vmInstanceId) {
this.vmInstanceId = vmInstanceId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,6 @@ public interface VolumeDao extends GenericDao<VolumeVO, Long>, StateDao<Volume.S
List<VolumeVO> listByIds(List<Long> ids);

VolumeVO findOneByIScsiName(String iScsiName);

Long getInstanceIdByVolumeId(long volumeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol
protected final SearchBuilder<VolumeVO> RootDiskStateSearch;
private final SearchBuilder<VolumeVO> storeAndInstallPathSearch;
private final SearchBuilder<VolumeVO> volumeIdSearch;
private final SearchBuilder<VolumeVO> instanceIdByVolumeIdSearch;
protected GenericSearchBuilder<VolumeVO, Long> CountByAccount;
protected GenericSearchBuilder<VolumeVO, SumCount> primaryStorageSearch;
protected GenericSearchBuilder<VolumeVO, SumCount> primaryStorageSearch2;
Expand Down Expand Up @@ -494,6 +495,11 @@ public VolumeDaoImpl() {
poolAndPathSearch.and("poolId", poolAndPathSearch.entity().getPoolId(), Op.EQ);
poolAndPathSearch.and("path", poolAndPathSearch.entity().getPath(), Op.EQ);
poolAndPathSearch.done();

instanceIdByVolumeIdSearch = createSearchBuilder();
instanceIdByVolumeIdSearch.selectFields(instanceIdByVolumeIdSearch.entity().getInstanceId());
instanceIdByVolumeIdSearch.and("id", instanceIdByVolumeIdSearch.entity().getId(), Op.EQ);
instanceIdByVolumeIdSearch.done();
}

@Override
Expand Down Expand Up @@ -847,4 +853,12 @@ public VolumeVO findOneByIScsiName(String iScsiName) {
sc.setParameters("iScsiName", iScsiName);
return findOneIncludingRemovedBy(sc);
}

@Override
public Long getInstanceIdByVolumeId(long volumeId) {
SearchCriteria<VolumeVO> sc = instanceIdByVolumeIdSearch.create();
sc.setParameters("id", volumeId);
List<VolumeVO> volumes = search(sc, null);
return volumes.isEmpty() ? null : volumes.get(0).getInstanceId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.upgrade.dao;

import java.io.InputStream;
import java.sql.Connection;

import com.cloud.utils.exception.CloudRuntimeException;

public class Upgrade41930to41940 implements DbUpgrade {

@Override
public String[] getUpgradableVersionRange() {
return new String[]{"4.19.3.0", "4.19.4.0"};
}

@Override
public String getUpgradedVersion() {
return "4.19.4.0";
}

@Override
public boolean supportsRollingUpgrade() {
return false;
}

@Override
public InputStream[] getPrepareScripts() {
final String scriptFile = "META-INF/db/schema-41930to41940.sql";
final InputStream script = Thread.currentThread().getContextClassLoader().getResourceAsStream(scriptFile);
if (script == null) {
throw new CloudRuntimeException("Unable to find " + scriptFile);
}

return new InputStream[]{script};
}

@Override
public void performDataMigration(Connection conn) {
}

@Override
public InputStream[] getCleanupScripts() {
return new InputStream[0];
}
}
11 changes: 10 additions & 1 deletion engine/schema/src/main/java/com/cloud/usage/UsageVolumeVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,21 @@ public class UsageVolumeVO implements InternalIdentity {
@Temporal(value = TemporalType.TIMESTAMP)
private Date deleted = null;

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

protected UsageVolumeVO() {
}

public UsageVolumeVO(long id, long zoneId, long accountId, long domainId, Long diskOfferingId, Long templateId, long size, Date created, Date deleted) {
public UsageVolumeVO(long id, long zoneId, long accountId, long domainId, Long diskOfferingId, Long templateId,
Long vmInstanceId, long size, Date created, Date deleted) {
this.volumeId = id;
this.zoneId = zoneId;
this.accountId = accountId;
this.domainId = domainId;
this.diskOfferingId = diskOfferingId;
this.templateId = templateId;
this.vmInstanceId = vmInstanceId;
this.size = size;
this.created = created;
this.deleted = deleted;
Expand Down Expand Up @@ -126,4 +131,8 @@ public void setDeleted(Date deleted) {
public long getVolumeId() {
return volumeId;
}

public long getVmInstanceId() {
return vmInstanceId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public class UsageVolumeDaoImpl extends GenericDaoBase<UsageVolumeVO, Long> impl

protected static final String REMOVE_BY_USERID_VOLID = "DELETE FROM usage_volume WHERE account_id = ? AND volume_id = ?";
protected static final String UPDATE_DELETED = "UPDATE usage_volume SET deleted = ? WHERE account_id = ? AND volume_id = ? and deleted IS NULL";
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 "
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 "
+ "FROM usage_volume " + "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 volume_id, zone_id, account_id, domain_id, disk_offering_id, template_id, size, created, deleted "
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 "
+ "FROM usage_volume " + "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 volume_id, zone_id, account_id, domain_id, disk_offering_id, template_id, size, created, deleted "
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 "
+ "FROM usage_volume " + "WHERE (deleted IS NULL) OR (created BETWEEN ? AND ?) OR " + " (deleted BETWEEN ? AND ?) OR ((created <= ?) AND (deleted >= ?))";

public UsageVolumeDaoImpl() {
Expand Down Expand Up @@ -152,11 +152,12 @@ public List<UsageVolumeVO> getUsageRecords(Long accountId, Long domainId, Date s
if (tId == 0) {
tId = null;
}
long size = Long.valueOf(rs.getLong(7));
Long vmId = rs.getLong(7);
long size = Long.valueOf(rs.getLong(8));
Date createdDate = null;
Date deletedDate = null;
String createdTS = rs.getString(8);
String deletedTS = rs.getString(9);
String createdTS = rs.getString(9);
String deletedTS = rs.getString(10);

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

usageRecords.add(new UsageVolumeVO(vId, zoneId, acctId, dId, doId, tId, size, createdDate, deletedDate));
usageRecords.add(new UsageVolumeVO(vId, zoneId, acctId, dId, doId, tId, vmId, size, createdDate, deletedDate));
}
} catch (Exception e) {
txn.rollback();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.

--;
-- Schema upgrade from 4.19.3.0 to 4.19.4.0
--;
ALTER TABLE `cloud`.`usage_event` ADD COLUMN `vm_instance_id` bigint unsigned DEFAULT NULL;
ALTER TABLE `cloud_usage`.`usage_volume` ADD COLUMN `vm_instance_id` bigint unsigned DEFAULT NULL;
7 changes: 6 additions & 1 deletion usage/src/main/java/com/cloud/usage/UsageManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.concurrent.TimeUnit;

import com.cloud.network.Network;
import com.cloud.storage.dao.VolumeDao;
import com.cloud.usage.dao.UsageNetworksDao;
import com.cloud.usage.parser.NetworksUsageParser;
import com.cloud.network.vpc.Vpc;
Expand Down Expand Up @@ -180,6 +181,8 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna

@Inject
private UsageVpcDao usageVpcDao;
@Inject
private VolumeDao volumeDao;

private String _version = null;
private final Calendar _jobExecTime = Calendar.getInstance();
Expand Down Expand Up @@ -1573,7 +1576,9 @@ private void createVolumeHelperEvent(UsageEventVO event) {
s_logger.debug("create volume with id : " + volId + " for account: " + event.getAccountId());
}
Account acct = _accountDao.findByIdIncludingRemoved(event.getAccountId());
UsageVolumeVO volumeVO = new UsageVolumeVO(volId, event.getZoneId(), event.getAccountId(), acct.getDomainId(), event.getOfferingId(), event.getTemplateId(), event.getSize(), event.getCreateDate(), null);
Long vmId = volumeDao.getInstanceIdByVolumeId(volId);
UsageVolumeVO volumeVO = new UsageVolumeVO(volId, event.getZoneId(), event.getAccountId(), acct.getDomainId(),
event.getOfferingId(), event.getTemplateId(), vmId, event.getSize(), event.getCreateDate(), null);
_usageVolumeDao.persist(volumeVO);
} else if (EventTypes.EVENT_VOLUME_DELETE.equals(event.getType())) {
SearchCriteria<UsageVolumeVO> sc = _usageVolumeDao.createSearchCriteria();
Expand Down
24 changes: 15 additions & 9 deletions usage/src/main/java/com/cloud/usage/parser/VolumeUsageParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
Long doId = usageVol.getDiskOfferingId();
long zoneId = usageVol.getZoneId();
Long templateId = usageVol.getTemplateId();
Long instanceId = usageVol.getVmInstanceId();
long size = usageVol.getSize();
String key = volId + "-" + doId + "-" + size;

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

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

Expand All @@ -140,8 +141,8 @@ 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,
Long templateId, long size) {
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account,
long volId, long zoneId, Long doId, Long templateId, Long instanceId, long size) {
// Our smallest increment is hourly for now
if (s_logger.isDebugEnabled()) {
s_logger.debug("Total running time " + runningTime + "ms");
Expand All @@ -165,9 +166,8 @@ private static void createUsageRecord(int type, long runningTime, Date startDate
} else if (doId != null) {
usageDesc += " (DiskOffering: " + doId + ")";
}

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

Expand All @@ -176,13 +176,15 @@ private static class VolInfo {
private long zoneId;
private Long diskOfferingId;
private Long templateId;
private Long instanceId;
private long size;

public VolInfo(long volId, long zoneId, Long diskOfferingId, Long templateId, long size) {
public VolInfo(long volId, long zoneId, Long diskOfferingId, Long templateId, Long instanceId, long size) {
this.volId = volId;
this.zoneId = zoneId;
this.diskOfferingId = diskOfferingId;
this.templateId = templateId;
this.instanceId = instanceId;
this.size = size;
}

Expand All @@ -202,6 +204,10 @@ public Long getTemplateId() {
return templateId;
}

public Long getInstanceId() {
return instanceId;
}

public long getSize() {
return size;
}
Expand Down
Loading