winterhazel opened a new pull request, #12518:
URL: https://github.com/apache/cloudstack/pull/12518

   ### Description
   
   `removeRawUsageRecords` calculates the next moment in which Usage will 
execute in order to prevent the removal of usage records in a window of 15 
minutes before and after the Usage job executes. However, this calculation is 
performed incorrectly because the date is not rolled to the next day in case 
the initial calculated date has already passed. Due to this, if the Usage job 
is scheduled to execute at 00:00 and the current time is 23:59 (the job will 
execute in a minute), for instance, this check fails and the usage records are 
removed.
   
   This PR fixes the calculation.
   
   ### Types of changes
   
   - [ ] Breaking change (fix or feature that would cause existing 
functionality to change)
   - [ ] New feature (non-breaking change which adds functionality)
   - [X] 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
   
   #### Bug Severity
   
   - [ ] BLOCKER
   - [ ] Critical
   - [ ] Major
   - [X] Minor
   - [ ] Trivial
   
   ### Screenshots (if appropriate):
   
   ### How Has This Been Tested?
   
   The tests were performed in a VM with its date set to 2026-01-26 GMT-3.
   
   ### Before the changes
   
   - I scheduled Usage to execute at 00:00 and set the Management Server's date 
to 23:50. I executed `removeRawUsageRecords`. I verified that the calculated 
execution date was 2026-01-26 00:00 (incorrect value), and that the usage 
records were removed.
   
     ```logs
     2026-01-27 02:50:37,340 DEBUG [c.c.u.UsageServiceImpl] 
(qtp1438988851-19:[ctx-406f7af2, ctx-443960b1]) (logid:3f310c43) Trying to 
remove old raw cloud_usage records older than 30 day(s), current 
time=1769482237337 next job execution time=1769472000000
     ```
   
   ### With the patch
   
   Regarding `removeRawUsageRecords`:
   
   - I scheduled Usage to execute at 00:00 and set the Management Server's date 
to 23:50. I executed `removeRawUsageRecords`. I verified that the calculated 
execution date was correct, and that the usage records were not removed.
   
     ```logs
     2026-01-27 02:51:48,514 DEBUG [c.c.u.UsageServiceImpl] 
(qtp1404565079-24:[ctx-41b413d8, ctx-9398b56c]) (logid:e7cc40c5) Next Usage job 
is scheduled to execute at [2026-01-27T00:00:00-0300]; previous execution was 
at [2026-01-26T00:00:00-0300].
     2026-01-27 02:51:48,514 INFO  [c.c.u.UsageServiceImpl] 
(qtp1404565079-24:[ctx-41b413d8, ctx-9398b56c]) (logid:e7cc40c5) Not removing 
any cloud_usage records because the next Usage job is scheduled to execute in 
less than 15 minute(s).
     ```
   
   - I scheduled Usage to execute at 00:00 and set the Management Server's date 
to 00:05. I executed `removeRawUsageRecords`. I verified that the calculated 
execution date was correct, and that the usage records were not removed.
   
     ```logs
     2026-01-26 03:07:20,495 DEBUG [c.c.u.UsageServiceImpl] 
(qtp1404565079-23:[ctx-bec9b411, ctx-07e46009]) (logid:c37addd5) Next Usage job 
is scheduled to execute at [2026-01-27T00:00:00-0300]; previous execution was 
at [2026-01-26T00:00:00-0300].
     2026-01-26 03:07:20,496 INFO  [c.c.u.UsageServiceImpl] 
(qtp1404565079-23:[ctx-bec9b411, ctx-07e46009]) (logid:c37addd5) Not removing 
any cloud_usage records because the last Usage job executed in less than 15 
minute(s) ago.
     ```
   
   - I scheduled Usage to execute at 00:00 and set the Management Server's date 
to 23:40. I executed `removeRawUsageRecords`. I verified that the calculated 
execution date was correct, and that the usage records were removed.
   
     ```logs
     2026-01-27 02:40:34,826 DEBUG [c.c.u.UsageServiceImpl] 
(qtp1404565079-23:[ctx-0b325ec7, ctx-c3715fae]) (logid:cf95c7ea) Next Usage job 
is scheduled to execute at [2026-01-27T00:00:00-0300]; previous execution was 
at [2026-01-26T00:00:00-0300].
     2026-01-27 02:40:34,826 INFO  [c.c.u.UsageServiceImpl] 
(qtp1404565079-23:[ctx-0b325ec7, ctx-c3715fae]) (logid:cf95c7ea) Removing 
cloud_usage records older than 30 day(s).
     ```
   
   - I scheduled Usage to execute at 00:00 and set the Management Server's date 
to 00:20. I executed `removeRawUsageRecords`. I verified that the calculated 
execution date was correct, and that the usage records were removed.
   
     ```logs
     2026-01-26 03:20:47,151 DEBUG [c.c.u.UsageServiceImpl] 
(qtp1404565079-23:[ctx-2dc17d46, ctx-8c10ccad]) (logid:7f4dbf36) Next Usage job 
is scheduled to execute at [2026-01-27T00:00:00-0300]; previous execution was 
at [2026-01-26T00:00:00-0300].
     2026-01-26 03:20:47,151 INFO  [c.c.u.UsageServiceImpl] 
(qtp1404565079-23:[ctx-2dc17d46, ctx-8c10ccad]) (logid:7f4dbf36) Removing 
cloud_usage records older than 30 day(s).
     ```
   
   Regarding the job execution time in the Usage Server itself:
   
   - I scheduled Usage to execute at 00:00 and set the Management Server's date 
to 23:50. I restarted the Usage Server. I verified that the calculated 
execution date was correct.
   
     ```logs
     2026-01-27 02:50:08,724 INFO  
[cloud.usage.UsageManagerImpl_EnhancerByCloudStack_68b35dd4] (main:[]) (logid:) 
Usage is configured to execute in time zone [GMT-03:00], at [00:00], each 
[1440] minutes; the current time in that timezone is [2026-01-26T23:50:08-0300] 
and the next job is scheduled to execute at [2026-01-27T00:00:00-0300]. During 
its execution, Usage will aggregate stats according to the time zone 
[GMT-03:00] defined in global setting [usage.aggregation.timezone].
     ```
   
   - I scheduled Usage to execute at 00:00 and set the Management Server's date 
to 00:20. I restarted the Usage Server. I verified that the calculated 
execution date was correct.
   
     ```logs
     2026-01-26 03:20:07,917 INFO  
[cloud.usage.UsageManagerImpl_EnhancerByCloudStack_68b35dd4] (main:[]) (logid:) 
Usage is configured to execute in time zone [GMT-03:00], at [00:00], each 
[1440] minutes; the current time in that timezone is [2026-01-26T00:20:07-0300] 
and the next job is scheduled to execute at [2026-01-27T00:00:00-0300]. During 
its execution, Usage will aggregate stats according to the time zone 
[GMT-03:00] defined in global setting [usage.aggregation.timezone].
     ```
   
   - I scheduled Usage to execute at 00:10 and set the Management Server's date 
to 00:05. I restarted the Usage Server. I verified that the calculated 
execution date was correct.
   
     ```logs
     2026-01-26 03:05:08,964 INFO  
[cloud.usage.UsageManagerImpl_EnhancerByCloudStack_68b35dd4] (main:[]) (logid:) 
Usage is configured to execute in time zone [GMT-03:00], at [00:10], each 
[1440] minutes; the current time in that timezone is [2026-01-26T00:05:08-0300] 
and the next job is scheduled to execute at [2026-01-26T00:10:00-0300]. During 
its execution, Usage will aggregate stats according to the time zone 
[GMT-03:00] defined in global setting [usage.aggregation.timezone].
     ```
   
   - I scheduled Usage to execute at 00:10 and set the Management Server's date 
to 00:12. I restarted the Usage Server. I verified that the calculated 
execution date was correct.
   
     ```logs
     2026-01-26 03:12:09,134 INFO  
[cloud.usage.UsageManagerImpl_EnhancerByCloudStack_68b35dd4] (main:[]) (logid:) 
Usage is configured to execute in time zone [GMT-03:00], at [00:10], each 
[1440] minutes; the current time in that timezone is [2026-01-26T00:12:09-0300] 
and the next job is scheduled to execute at [2026-01-27T00:10:00-0300]. During 
its execution, Usage will aggregate stats according to the time zone 
[GMT-03:00] defined in global setting [usage.aggregation.timezone].
     ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to