Skip to content

Conversation

@erikbocks
Copy link
Collaborator

Description

Currently, all Usage parsers implement a static method with an identical signature, which is invoked by the UsageManagerImpl#parseHelperTables() method. This PR refactors these parser classes by standardizing them to extend the UsageParser class and requiring an override of the parse method, improving code consistency, maintainability, and extensibility.

Additionally, by leveraging Spring Dependency Injection, it was possible to provide a major cleanup in the UsageManagerImpl#parseHelperTables() method, by injecting all UsageParser implementations into a list, then iterating over it and calling the parse method. The parse log was also improved to provide better information about the parsing result, the object being parsed and the target account.

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

Screenshots (if appropriate):

How Has This Been Tested?

  1. First, I triggered the Usage job execution without the changes and validated that the current logs contained minimal information about the process.
  2. Then, installed the packages with the changes in my local env.
  3. After the update, I triggered the Usage job again and accompanied the process via debug, to ensure that all the implementations were being injected correctly, and the enhanced log was being displayed after each parser execution.

Update log example 1:

2025-06-03 13:55:51,183 DEBUG [usage.parser.VMSnapshotUsageParser] (Usage-Job-1:[]) (logid:) Parsing all VM Snapshot usage events for account: [null]
2025-06-03 13:55:51,186 DEBUG [usage.parser.VMSnapshotUsageParser] (Usage-Job-1:[]) (logid:) No VM snapshot usage events for this period
2025-06-03 13:55:51,186 DEBUG [cloud.usage.UsageManagerImpl_EnhancerByCloudStack_fe18a690] (Usage-Job-1:[]) (logid:) VM Snapshot usage was successfully parsed for [Account [{"accountName":"system","id":1,"uuid":null}]].

Footnotes

  1. During the tests, I validated that the UUID column of cloud_usage.accounts is set to NULL after the accounts import from the cloud database. An issue was mapped to fix this behaviour (NULL value in uuid column of cloud_usage.account table #11077)

@DaanHoogland
Copy link
Contributor

thanks for this initiative @erikbocks , I’ll look at this (including doing some testing)

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors individual Usage parser classes to inherit from a new abstract UsageParser, replaces static methods/fields with instance methods and injected DAOs, unifies logging behavior, and simplifies UsageManagerImpl.parseHelperTables() by injecting and iterating a list of parser beans.

  • All parser classes now extend UsageParser, remove static parsing logic, and use an injected UsageDao
  • Introduced common logging (beforeParse) and standardized debug messages
  • Updated UsageManagerImpl to inject List<UsageParser> and loop through parsers instead of calling each statically

Reviewed Changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 3 comments.

File Description
UsageParser.java New base class with doParsing(), beforeParse() and common usageDao
UsageManagerImpl.java Injects List<UsageParser> and iterates in parseHelperTables()
usage/src/main/java/com/cloud/usage/parser/*.java Each parser now extends UsageParser, removes static methods, replaces DAO calls, and switches to instance logger
Comments suppressed due to low confidence (1)

usage/src/main/java/com/cloud/usage/parser/NetworksUsageParser.java:40

  • [nitpick] The parser name is pluralized here, while most other parsers use a singular form. Consider using "Network" for consistency.
        return "Networks";

@DaanHoogland
Copy link
Contributor

@blueorangutan package

@blueorangutan
Copy link

@DaanHoogland 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.

@codecov
Copy link

codecov bot commented Jun 27, 2025

Codecov Report

Attention: Patch coverage is 0% with 177 lines in your changes missing coverage. Please review.

Project coverage is 16.58%. Comparing base (7632814) to head (33665fd).
Report is 78 commits behind head on main.

Files with missing lines Patch % Lines
.../cloud/usage/parser/VMSnapshotOnPrimaryParser.java 0.00% 12 Missing ⚠️
.../com/cloud/usage/parser/VMSnapshotUsageParser.java 0.00% 12 Missing ⚠️
...om/cloud/usage/parser/LoadBalancerUsageParser.java 0.00% 11 Missing ⚠️
.../cloud/usage/parser/PortForwardingUsageParser.java 0.00% 11 Missing ⚠️
...m/cloud/usage/parser/SecurityGroupUsageParser.java 0.00% 11 Missing ⚠️
...ava/com/cloud/usage/parser/VPNUserUsageParser.java 0.00% 11 Missing ⚠️
...a/com/cloud/usage/parser/IPAddressUsageParser.java 0.00% 10 Missing ⚠️
...cloud/usage/parser/NetworkOfferingUsageParser.java 0.00% 10 Missing ⚠️
...ava/com/cloud/usage/parser/StorageUsageParser.java 0.00% 10 Missing ⚠️
...java/com/cloud/usage/parser/VolumeUsageParser.java 0.00% 10 Missing ⚠️
... and 9 more
Additional details and impacted files
@@             Coverage Diff             @@
##               main   #11097     +/-   ##
===========================================
  Coverage     16.57%   16.58%             
- Complexity    13868    13987    +119     
===========================================
  Files          5719     5745     +26     
  Lines        507178   510707   +3529     
  Branches      61571    62091    +520     
===========================================
+ Hits          84085    84696    +611     
- Misses       413674   416537   +2863     
- Partials       9419     9474     +55     
Flag Coverage Δ
uitests 3.91% <ø> (-0.06%) ⬇️
unittests 17.48% <0.00%> (+0.02%) ⬆️

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.

@blueorangutan
Copy link

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

@DaanHoogland
Copy link
Contributor

@blueorangutan test

@blueorangutan
Copy link

@DaanHoogland a [SL] Trillian-Jenkins test job (ol8 mgmt + kvm-ol8) has been kicked to run smoke tests

@winterhazel
Copy link
Member

The code looks good. I think we can still do some more refactoring inside the parse methods in the future, but this is already a very good improvement :)

I will do some testing too.

@blueorangutan
Copy link

[SF] Trillian test result (tid-13631)
Environment: kvm-ol8 (x2), Advanced Networking with Mgmt server ol8
Total time taken: 54003 seconds
Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr11097-t13631-kvm-ol8.zip
Smoke tests completed. 141 look OK, 0 have errors, 0 did not run
Only failed and skipped tests results shown below:

Test Result Time (s) Test File

@blueorangutan
Copy link

[SF] Trillian test result (tid-13652)
Environment: kvm-ol8 (x2), Advanced Networking with Mgmt server ol8
Total time taken: 61456 seconds
Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr11097-t13652-kvm-ol8.zip
Smoke tests completed. 141 look OK, 0 have errors, 0 did not run
Only failed and skipped tests results shown below:

Test Result Time (s) Test File

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 (with one improvement to propose.
tested (very coursely) and usage is generated as expected. On studying the logs, the only ERROR entry

2025-07-02 00:15:31,984 ERROR [cloud.usage.UsageManagerImpl_EnhancerByCloudStack_5fb70218] (Usage-Job-1:[]) (logid:) Found duplicate usage entry for volume: 365 assigned to account: 230; marking as deleted…

is actually due to a fix by @winterhazel

the remark on further improvement; at the moment the parsers are called in sequence. I think we can call them in parallel, in the future.

Copy link
Member

@winterhazel winterhazel left a comment

Choose a reason for hiding this comment

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

Tested by running the Usage job and verifying the usage record creation. It is working as expected. Just one minor comment.

@winterhazel
Copy link
Member

@blueorangutan package

@blueorangutan
Copy link

@winterhazel 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.

@blueorangutan
Copy link

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

@DaanHoogland
Copy link
Contributor

@blueorangutan test

@blueorangutan
Copy link

@DaanHoogland a [SL] Trillian-Jenkins test job (ol8 mgmt + kvm-ol8) has been kicked to run smoke tests

@blueorangutan
Copy link

[SF] Trillian test result (tid-13762)
Environment: kvm-ol8 (x2), Advanced Networking with Mgmt server ol8
Total time taken: 90613 seconds
Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr11097-t13762-kvm-ol8.zip
Smoke tests completed. 102 look OK, 39 have errors, 0 did not run
Only failed and skipped tests results shown below:

Test Result Time (s) Test File
test_nic_secondaryip_add_remove Error 22.80 test_multipleips_per_nic.py
test_network_acl Error 2.33 test_network_acl.py
test_01_verify_ipv6_network Error 3.17 test_network_ipv6.py
test_01_verify_ipv6_network Error 3.17 test_network_ipv6.py
test_03_network_operations_on_created_vm_of_otheruser Error 2.68 test_network_permissions.py
test_03_network_operations_on_created_vm_of_otheruser Error 2.69 test_network_permissions.py
test_04_deploy_vm_for_other_user_and_test_vm_operations Failure 1.43 test_network_permissions.py
ContextSuite context=TestNetworkPermissions>:teardown Error 1.49 test_network_permissions.py
test_delete_account Error 22.29 test_network.py
test_delete_network_while_vm_on_it Error 2.45 test_network.py
test_delete_network_while_vm_on_it Error 2.45 test_network.py
test_deploy_vm_l2network Error 2.49 test_network.py
test_deploy_vm_l2network Error 2.49 test_network.py
test_l2network_restart Error 3.57 test_network.py
test_l2network_restart Error 3.57 test_network.py
ContextSuite context=TestL2Networks>:teardown Error 4.70 test_network.py
ContextSuite context=TestPortForwarding>:setup Error 11.38 test_network.py
ContextSuite context=TestPublicIP>:setup Error 11.61 test_network.py
test_reboot_router Error 6.47 test_network.py
test_releaseIP Error 6.67 test_network.py
test_releaseIP_using_IP Error 6.64 test_network.py
ContextSuite context=TestRouterRules>:setup Error 13.61 test_network.py
test_01_deployVMInSharedNetwork Failure 1.29 test_network.py
test_02_verifyRouterIpAfterNetworkRestart Failure 1.09 test_network.py
test_03_destroySharedNetwork Failure 1.09 test_network.py
ContextSuite context=TestSharedNetwork>:teardown Error 2.19 test_network.py
test_01_deployVMInSharedNetwork Failure 1.36 test_network.py
ContextSuite context=TestSharedNetworkWithConfigDrive>:teardown Error 2.48 test_network.py
test_01_nic Error 55.13 test_nic.py
test_01_non_strict_host_anti_affinity Error 1.55 test_nonstrict_affinity_group.py
test_02_non_strict_host_affinity Error 2.56 test_nonstrict_affinity_group.py
ContextSuite context=TestIsolatedNetworksPasswdServer>:setup Error 0.00 test_password_server.py
test_01_isolated_persistent_network Error 0.27 test_persistent_network.py
test_02_L2_persistent_network Error 1.32 test_persistent_network.py
test_03_deploy_and_destroy_VM_and_verify_network_resources_persist Failure 2.56 test_persistent_network.py
test_03_deploy_and_destroy_VM_and_verify_network_resources_persist Error 2.56 test_persistent_network.py
ContextSuite context=TestL2PersistentNetworks>:teardown Error 2.62 test_persistent_network.py
test_01_create_delete_portforwarding_fornonvpc Error 7.09 test_portforwardingrules.py
test_01_add_primary_storage_disabled_host Error 0.28 test_primary_storage.py
test_01_primary_storage_nfs Error 0.23 test_primary_storage.py
ContextSuite context=TestStorageTags>:setup Error 0.41 test_primary_storage.py
test_01_primary_storage_scope_change Error 0.10 test_primary_storage_scope.py
test_01_vpc_privategw_acl Failure 8.00 test_privategw_acl.py
test_02_vpc_privategw_static_routes Failure 8.33 test_privategw_acl.py
test_03_vpc_privategw_restart_vpc_cleanup Failure 8.08 test_privategw_acl.py
test_04_rvpc_privategw_static_routes Failure 7.92 test_privategw_acl.py
test_09_project_suspend Error 2.53 test_projects.py
test_10_project_activation Error 2.39 test_projects.py
test_01_purge_expunged_api_vm_start_date Error 3.73 test_purge_expunged_vms.py
test_02_purge_expunged_api_vm_end_date Error 3.84 test_purge_expunged_vms.py
test_03_purge_expunged_api_vm_start_end_date Error 2.46 test_purge_expunged_vms.py
test_04_purge_expunged_api_vm_no_date Error 3.46 test_purge_expunged_vms.py
test_05_purge_expunged_vm_service_offering Error 1.67 test_purge_expunged_vms.py
test_06_purge_expunged_vm_background_task Error 368.01 test_purge_expunged_vms.py
test_CRUD_operations_userdata Error 1524.60 test_register_userdata.py
test_deploy_vm_with_registered_userdata Error 8.67 test_register_userdata.py
test_deploy_vm_with_registered_userdata_with_override_policy_allow Error 8.85 test_register_userdata.py
test_deploy_vm_with_registered_userdata_with_override_policy_append Error 8.53 test_register_userdata.py
test_deploy_vm_with_registered_userdata_with_override_policy_deny Error 8.63 test_register_userdata.py
test_deploy_vm_with_registered_userdata_with_params Error 8.35 test_register_userdata.py
test_link_and_unlink_userdata_to_template Error 8.09 test_register_userdata.py
test_user_userdata_crud Error 7.93 test_register_userdata.py
ContextSuite context=TestResetVmOnReboot>:setup Error 0.00 test_reset_vm_on_reboot.py
ContextSuite context=TestRAMCPUResourceAccounting>:setup Error 0.00 test_resource_accounting.py
ContextSuite context=TestResourceNames>:setup Error 0.00 test_resource_names.py
ContextSuite context=TestRestoreVM>:setup Error 0.00 test_restore_vm.py
ContextSuite context=TestRouterDHCPHosts>:setup Error 0.00 test_router_dhcphosts.py
ContextSuite context=TestRouterDHCPOpts>:setup Error 0.00 test_router_dhcphosts.py
ContextSuite context=TestRouterDns>:setup Error 0.00 test_router_dns.py
ContextSuite context=TestRouterDnsService>:setup Error 0.00 test_router_dnsservice.py
ContextSuite context=TestRouterIpTablesPolicies>:setup Error 0.00 test_routers_iptables_default_policy.py
ContextSuite context=TestVPCIpTablesPolicies>:setup Error 0.00 test_routers_iptables_default_policy.py
test_01_migrate_vm_strict_tags_success Error 0.25 test_vm_strict_host_tags.py
test_02_migrate_vm_strict_tags_failure Error 0.27 test_vm_strict_host_tags.py
test_01_restore_vm_strict_tags_success Error 0.24 test_vm_strict_host_tags.py
test_02_restore_vm_strict_tags_failure Error 0.24 test_vm_strict_host_tags.py
test_01_scale_vm_strict_tags_success Error 0.25 test_vm_strict_host_tags.py
test_02_scale_vm_strict_tags_failure Error 0.30 test_vm_strict_host_tags.py
test_01_deploy_vm_on_specific_host_without_strict_tags Error 0.24 test_vm_strict_host_tags.py
test_02_deploy_vm_on_any_host_without_strict_tags Error 2.69 test_vm_strict_host_tags.py
test_03_deploy_vm_on_specific_host_with_strict_tags_success Error 0.30 test_vm_strict_host_tags.py
test_04_deploy_vm_on_any_host_with_strict_tags_success Error 5.85 test_vm_strict_host_tags.py
test_05_deploy_vm_on_specific_host_with_strict_tags_failure Failure 0.33 test_vm_strict_host_tags.py
ContextSuite context=TestIsolatedNetworks>:setup Error 0.00 test_routers_network_ops.py
ContextSuite context=TestRedundantIsolateNetworks>:setup Error 0.00 test_routers_network_ops.py
ContextSuite context=TestRouterServices>:setup Error 0.00 test_routers.py
test_01_sys_vm_start Failure 0.12 test_secondary_storage.py
ContextSuite context=TestCpuCapServiceOfferings>:setup Error 0.00 test_service_offerings.py
ContextSuite context=TestServiceOfferings>:setup Error 0.37 test_service_offerings.py
ContextSuite context=TestSetSourceNatIp>:setup Error 0.00 test_set_sourcenat.py
ContextSuite context=TestSharedFSLifecycle>:setup Error 0.00 test_sharedfs_lifecycle.py
ContextSuite context=TestSnapshotRootDisk>:setup Error 0.00 test_snapshots.py
ContextSuite context=TestSnapshotStandaloneBackup>:setup Error 0.00 test_snapshots.py
test_01_list_sec_storage_vm Failure 0.05 test_ssvm.py
test_02_list_cpvm_vm Failure 0.05 test_ssvm.py
test_03_ssvm_internals Failure 0.05 test_ssvm.py
test_04_cpvm_internals Failure 0.05 test_ssvm.py
test_05_stop_ssvm Failure 0.04 test_ssvm.py
test_06_stop_cpvm Failure 0.04 test_ssvm.py
test_07_reboot_ssvm Failure 0.05 test_ssvm.py
test_08_reboot_cpvm Failure 0.05 test_ssvm.py
test_09_reboot_ssvm_forced Failure 0.04 test_ssvm.py
test_10_reboot_cpvm_forced Failure 0.04 test_ssvm.py
test_11_destroy_ssvm Failure 0.05 test_ssvm.py
test_12_destroy_cpvm Failure 0.04 test_ssvm.py
ContextSuite context=TestVMWareStoragePolicies>:setup Error 0.00 test_storage_policy.py
test_02_create_template_with_checksum_sha1 Error 65.66 test_templates.py
test_03_create_template_with_checksum_sha256 Error 65.68 test_templates.py
test_04_create_template_with_checksum_md5 Error 65.93 test_templates.py
test_05_create_template_with_no_checksum Error 65.65 test_templates.py
test_01_register_template_direct_download_flag Error 0.06 test_templates.py
test_02_deploy_vm_from_direct_download_template Error 0.00 test_templates.py
test_03_deploy_vm_wrong_checksum Error 0.06 test_templates.py
ContextSuite context=TestTemplates>:setup Error 14.52 test_templates.py
ContextSuite context=TestISOUsage>:setup Error 0.00 test_usage.py
ContextSuite context=TestLBRuleUsage>:setup Error 0.00 test_usage.py
ContextSuite context=TestNatRuleUsage>:setup Error 0.00 test_usage.py
ContextSuite context=TestPublicIPUsage>:setup Error 0.00 test_usage.py
ContextSuite context=TestSnapshotUsage>:setup Error 0.00 test_usage.py
ContextSuite context=TestVmUsage>:setup Error 0.00 test_usage.py
ContextSuite context=TestVolumeUsage>:setup Error 0.00 test_usage.py
ContextSuite context=TestVpnUsage>:setup Error 0.00 test_usage.py
test_01_scale_up_verify Failure 35.06 test_vm_autoscaling.py
test_02_update_vmprofile_and_vmgroup Failure 245.70 test_vm_autoscaling.py
test_03_scale_down_verify Failure 304.63 test_vm_autoscaling.py
test_04_stop_remove_vm_in_vmgroup Failure 0.02 test_vm_autoscaling.py
test_06_autoscaling_vmgroup_on_project_network Failure 42.56 test_vm_autoscaling.py
test_06_autoscaling_vmgroup_on_project_network Error 42.57 test_vm_autoscaling.py
test_07_autoscaling_vmgroup_on_vpc_network Error 1.21 test_vm_autoscaling.py
ContextSuite context=TestVmAutoScaling>:teardown Error 12.29 test_vm_autoscaling.py
test_01_deploy_vm_on_specific_host Error 0.11 test_vm_deployment_planner.py
test_02_deploy_vm_on_specific_cluster Error 1.42 test_vm_deployment_planner.py
test_03_deploy_vm_on_specific_pod Error 1.32 test_vm_deployment_planner.py
test_04_deploy_vm_on_host_override_pod_and_cluster Error 0.12 test_vm_deployment_planner.py
test_05_deploy_vm_on_cluster_override_pod Error 1.32 test_vm_deployment_planner.py
test_01_migrate_VM_and_root_volume Error 107.62 test_vm_life_cycle.py
test_02_migrate_VM_with_two_data_disks Error 54.27 test_vm_life_cycle.py
test_01_secure_vm_migration Error 85.54 test_vm_life_cycle.py
test_02_unsecure_vm_migration Error 231.02 test_vm_life_cycle.py
test_04_nonsecured_to_secured_vm_migration Error 160.22 test_vm_life_cycle.py
test_08_migrate_vm Error 0.07 test_vm_life_cycle.py

@DaanHoogland
Copy link
Contributor

@blueorangutan test

@blueorangutan
Copy link

@DaanHoogland a [SL] Trillian-Jenkins test job (ol8 mgmt + kvm-ol8) has been kicked to run smoke tests

@blueorangutan
Copy link

[SF] Trillian test result (tid-13780)
Environment: kvm-ol8 (x2), Advanced Networking with Mgmt server ol8
Total time taken: 64482 seconds
Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr11097-t13780-kvm-ol8.zip
Smoke tests completed. 140 look OK, 1 have errors, 0 did not run
Only failed and skipped tests results shown below:

Test Result Time (s) Test File
test_08_upgrade_kubernetes_ha_cluster Failure 651.91 test_kubernetes_clusters.py

@DaanHoogland DaanHoogland merged commit bb75abc into apache:main Jul 16, 2025
23 of 26 checks passed
dhslove pushed a commit to ablecloud-team/ablestack-cloud that referenced this pull request Aug 1, 2025
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.

4 participants