Fix duplicate RUNNING_VM helper record creation on repeated VM.START events#12606
Fix duplicate RUNNING_VM helper record creation on repeated VM.START events#12606SURYAS1306 wants to merge 1 commit intoapache:mainfrom
Conversation
|
Congratulations on your first Pull Request and welcome to the Apache CloudStack community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/cloudstack/blob/main/CONTRIBUTING.md)
|
|
Thank you @SURYAS1306 , can you rebase this on the 4.20 branch? |
abh1sar
left a comment
There was a problem hiding this comment.
Thanks for working on this @SURYAS1306 .
But skipping insertion into the usage_vm_instance table is not the right approach.
| id | type | account_id | created | count | resource_id | name |
|---|---|---|---|---|---|---|
| 106 | VM.START | 6 | 2025-10-27 13:02:07 | 2 | 200 | fedora |
| 107 | VM.STOP | 6 | 2025-10-27 13:02:06 | 2 | 200 | fedora |
| 108 | VM.START | 6 | 2025-10-27 13:02:07 | 2 | 200 | fedora |
In the example above, if we skip the last event, it will cause usage to never register the Instance as started and it will not be billed properly.
|
Thanks for the review. That makes sense skipping the helper record creation could indeed cause the VM state to be inconsistent and affect billing. I'll revisit the approach and focus on handling duplicate VM.START events in a way that keeps the usage state correct (likely by making the processing idempotent rather than skipping the event). I'll rework the fix and update the PR. |
Thanks, can you turn the PR into Draft until then? |
Description
Repeated
VM.STARTevents for the same VM could result in duplicateRUNNING_VMhelper usage records being created in the usage database.This PR adds a defensive check in
UsageManagerImpl#createVMHelperEventto detect an existing active
RUNNING_VMusage record (same VM instance,endDate = NULL) and skip helper record creation when a duplicate STARTevent is received.
A regression test has been added to ensure that duplicate
RUNNING_VMhelper records are not created when repeated
VM.STARTevents areprocessed.
Fixes: #12590
Expected behavior
Only one active
RUNNING_VMusage helper record should exist per VM atany given time.
Actual behavior
Multiple
RUNNING_VMhelper records could be created for the same VMwhen duplicate
VM.STARTevents were processed.Types of changes
Bug Severity
How Has This Been Tested?
UsageManagerImplTestto verify that duplicateRUNNING_VMhelper records are not created when repeatedVM.STARTevents are handled.
cloud-usagemodule successfully using Maven.How did you try to break this feature and the system with this change?
VM.STARTevents for the same VM instance to ensurethat helper record creation is skipped when an active
RUNNING_VMrecord already exists.