Skip to content

Conversation

abh1sar
Copy link
Collaborator

@abh1sar abh1sar commented Aug 29, 2025

Description

This PR fixes #10985

Some users have asked to show the virtualmachineid also in volume usage, to track it per instance.

There will be two kinds of records now.

  • Without the virtualmachineid - tracking the cumulative volume usage in the usage window. This is to be consistent with the current behaviour.
  • With virtualmachineid - tracking the volume usage for the time it was attached to the vm in the usage window.

If the volume was attached to multiple instances in the same usage window, we will have separate entries for each VM.

Schema changes

  • Add column vm_id to cloud.usage_event and cloud_usage.usage_event
  • Add column vm_id to cloud_usage.usage_volume

I have also done some code refactoring in createVolumeHelperEvent() to make it easier to follow.

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • build/CI
  • test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Feature/Enhancement Scale

  • Major
  • Minor

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

Screenshots (if appropriate):

listUsageRecords for a volume in a 2 minute (0.033333 hours) window.
The volume was created, then attached to VM-00a42c28-74ae-4а0c-b38a-4e3b88f11241. It was then detached and attached to another VM QA-d6df4657-2fb4-4e26-b7de-21743c5463cd. So we have 3 records.

Screenshot 2025-08-29 at 2 42 20 AM

How Has This Been Tested?

How did you try to break this feature and the system with this change?

@abh1sar
Copy link
Collaborator Author

abh1sar commented Aug 29, 2025

@blueorangutan package

@blueorangutan
Copy link

@abh1sar a [SL] Jenkins job has been kicked to build packages. It will be bundled with KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress.

Copy link

codecov bot commented Aug 29, 2025

Codecov Report

❌ Patch coverage is 2.87356% with 169 lines in your changes missing coverage. Please review.
✅ Project coverage is 17.36%. Comparing base (3d6ec29) to head (025ae71).

Files with missing lines Patch % Lines
...rc/main/java/com/cloud/usage/UsageManagerImpl.java 0.00% 83 Missing ⚠️
...n/java/com/cloud/usage/dao/UsageVolumeDaoImpl.java 0.00% 20 Missing ⚠️
...ma/src/main/java/com/cloud/event/UsageEventVO.java 0.00% 17 Missing ⚠️
...java/com/cloud/usage/parser/VolumeUsageParser.java 0.00% 13 Missing ⚠️
...src/main/java/com/cloud/event/UsageEventUtils.java 0.00% 8 Missing ⚠️
...a/src/main/java/com/cloud/usage/UsageVolumeVO.java 0.00% 8 Missing ⚠️
...n/java/com/cloud/storage/VolumeApiServiceImpl.java 22.22% 7 Missing ⚠️
...er/src/main/java/com/cloud/hypervisor/KVMGuru.java 0.00% 4 Missing ⚠️
.../src/main/java/com/cloud/vm/UserVmManagerImpl.java 40.00% 3 Missing ⚠️
...stack/engine/orchestration/VolumeOrchestrator.java 0.00% 2 Missing ⚠️
... and 3 more
Additional details and impacted files
@@             Coverage Diff              @@
##               main   #11531      +/-   ##
============================================
- Coverage     17.36%   17.36%   -0.01%     
  Complexity    15237    15237              
============================================
  Files          5888     5888              
  Lines        525741   525802      +61     
  Branches      64164    64168       +4     
============================================
+ Hits          91274    91281       +7     
- Misses       424167   424219      +52     
- Partials      10300    10302       +2     
Flag Coverage Δ
uitests 3.63% <ø> (ø)
unittests 18.39% <2.87%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.


public UsageVolumeDaoImpl() {
}

@Override
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of update method is not required since #5785 added auto-increment primary keys. We can directly use the GenericDao.update()

if (volumesVOs.size() > 0) {
//This is a safeguard to avoid double counting of volumes.
s_logger.error("Found duplicate usage entry for volume: " + volId + " assigned to account: " + event.getAccountId() + "; marking as deleted...");
if (event.getVmId() != null) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VOLUME_CREATE event will contain the vmId also when it is created during Instance deployment. Need to add a vm specific entry also in that case.

@blueorangutan
Copy link

Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ debian ✔️ suse15. SL-JID 14760

Copy link
Contributor

@DaanHoogland DaanHoogland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, except for the upgrade path (and needs testing)

@sudo87
Copy link
Collaborator

sudo87 commented Aug 29, 2025

clgtm

@DaanHoogland
Copy link
Contributor

code looks good in isolation @abh1sar , but I see some bears on the road: what if someone upgrades from 20.1 to 21 and then on to 22; How do we guarantee they get the DB updates?
It may be wiser to put this in version 22.

Sorry, I overlooked this when I asked you to rebase earlier.

@weizhouapache weizhouapache added this to the 4.22.0 milestone Sep 1, 2025
@abh1sar abh1sar changed the base branch from 4.20 to main September 9, 2025 11:05
@abh1sar abh1sar closed this Sep 9, 2025
@abh1sar abh1sar reopened this Sep 9, 2025
@abh1sar
Copy link
Collaborator Author

abh1sar commented Sep 9, 2025

code looks good in isolation @abh1sar , but I see some bears on the road: what if someone upgrades from 20.1 to 21 and then on to 22; How do we guarantee they get the DB updates? It may be wiser to put this in version 22.

Sorry, I overlooked this when I asked you to rebase earlier.

Thanks @DaanHoogland. Rebased to main.

Comment on lines +4333 to +4335
if (vmInstance != null) {
builder.append(" attached to VM ").append(vmInstance.getHostName()).append(" (").append(vmInstance.getUuid()).append(")");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is adding only three lines to a 530 line method. No issue in the context but public UsageRecordResponse createUsageResponse(Usage usageRecord) needs to be refactorred (as many pieces of code).

Copy link
Contributor

@DaanHoogland DaanHoogland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[cloud_usage] vm_instance_id is null for volume usage (usage_type = 6)
5 participants