You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by "abh1sar (via GitHub)" <gi...@apache.org> on 2026/10/05 11:21:36 UTC
[PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
abh1sar opened a new pull request, #14310:
URL: https://github.com/apache/cloudstack/pull/14310
### Description
This PR fixes how VM backup usage is recorded and billed. One commit per change:
1. **Backup usage billed at the last reported size for the whole period (#13070).** A `BACKUP.USAGE.METRIC` event overwrote the size of the active `usage_backup` row in place, so `BackupUsageParser` applied the last size to the entire aggregation window. A size change now closes the active row at the event date and opens a new row with the new size, so each size is billed only for the time it was in effect. An unchanged size is a no-op, rows that are already removed are no longer updated, and duplicate active rows for the same VM and offering are merged into one.
2. **Backups billed twice after re-assigning an offering.** Removing a backup offering while keeping the backups publishes no usage event, so the usage row stays active (correctly, as the backups still use storage). Re-assigning the same offering then added a second active row, and both were billed. The usage server now skips the new row when one is already active.
3. **`BACKUP.USAGE.METRIC` published on every backup sync.** The backup sync published the metric for every VM on every run (`backup.framework.sync.interval`, 5 minutes by default) whether or not it changed. The management server now remembers the last value published per zone, VM and offering, and only publishes changes. The cache is in memory: after a restart or on another management server each value is published once more, which the usage server ignores since the size is unchanged.
Behaviour changes:
- `usage_backup` gains a row per size change instead of one row per assignment, and billing records are split accordingly.
- Far fewer `BACKUP.USAGE.METRIC` rows in `cloud.usage_event` and `cloud_usage.usage_event`: one per actual change instead of one per VM per sync. Existing rows are left in place.
- On upgrade, the first sync publishes each VM's metric once, which also merges any duplicate active `usage_backup` rows created by the re-assign bug.
Changes 2 and 3 depend on 1: with publish-on-change, duplicate rows are not merged until the size changes, so the re-assign guard is what keeps them from being created.
Fixes: #13070
### 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
- [x] Major
- [ ] Minor
- [ ] Trivial
### How Has This Been Tested?
Tested on 4.22 with NAS backup on an NFS repository. The usage job ran with a 15 minute aggregation range so each window could be checked.
Before the fix:
- The VM's backup size changed at 08:58, but every window from 00:00 was billed at the new size.
- The VM had two active `usage_backup` rows from an earlier remove and re-assign of the offering, so every window had two billing records.
- 98.8% of all usage events were `BACKUP.USAGE.METRIC` (73,669 events with 64 distinct values for 2 VMs).
Usage after the fix (VM backup usage records per 15 minute window):
| Scenario | Window | Period billed | Size | Duration |
|---|---|---|---|---|
| Duplicate rows merged at the first metric event | 09:26 to 09:41 | 09:26:00 to 09:38:04 | 18.81 GB and 22.19 GB (two rows) | 12.07 min each |
| | | 09:38:04 to 09:40:59 | 22.19 GB (one row) | 2.93 min |
| Backup taken mid-window | 09:41 to 09:56 | 09:41:00 to 09:43:04 | 22.19 GB | 2.07 min |
| | | 09:43:04 to 09:55:59 | 25.56 GB | 12.93 min |
| Backup taken, then deleted | 10:28 to 10:43 | 10:28:00 to 10:28:40 | 25.56 GB | 0.67 min |
| | | 10:28:40 to 10:38:40 | 28.94 GB | 10.00 min |
| | | 10:38:40 to 10:42:59 | 25.56 GB | 4.33 min |
Before the fix, each of these windows would have been billed at a single size for the full 15 minutes (twice, while the duplicate row existed).
Also:
- Removing and re-assigning the offering with backups kept: the re-assign was skipped and one active row remained.
- Over 25 minutes, the management server published 3 metric events: one after its restart, one when a backup was taken and one when it was deleted. Before, it published one every 5 minutes. The republish after the restart did not add a usage row.
#### How did you try to break this feature and the system with this change?
- Restarted the management server: each value was published once more and the usage server ignored it.
- Re-assigned the offering while its backups were kept: no second row.
- Created and deleted backups within one aggregation window: each change got its own row and billing record.
- Metric for a VM without an active usage row: ignored, as before.
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-6313115737
@blueorangutan test
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4219916343
##########
engine/schema/src/main/java/com/cloud/usage/dao/UsageBackupDaoImpl.java:
##########
@@ -42,19 +43,50 @@ public class UsageBackupDaoImpl extends GenericDaoBase<UsageBackupVO, Long> impl
" OR ((created <= ?) AND (removed >= ?)))";
@Override
- public void updateMetrics(final Long vmId, Long backupOfferingId, final Long size, final Long virtualSize) {
- try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) {
- SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
- sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
- sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
- UsageBackupVO vo = findOneBy(sc);
- if (vo != null) {
- vo.setSize(size);
- vo.setProtectedSize(virtualSize);
- update(vo.getId(), vo);
+ public List<UsageBackupVO> listActiveUsage(Long vmId, Long backupOfferingId) {
+ SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
+ sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
+ sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
+ sc.addAnd("removed", SearchCriteria.Op.NULL);
+ return listBy(sc, new Filter(UsageBackupVO.class, "created", false));
+ }
+
+ @Override
+ public void updateMetrics(final Long vmId, final Long backupOfferingId, final Long size, final Long virtualSize, final Date eventDate) {
+ final long newSize = size != null ? size : 0L;
+ final long newProtectedSize = virtualSize != null ? virtualSize : 0L;
+ TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
+ try {
+ txn.start();
+ List<UsageBackupVO> activeUsage = listActiveUsage(vmId, backupOfferingId);
+ if (activeUsage.isEmpty()) {
+ logger.warn("No active backup usage for VM [{}] and backup offering [{}], ignoring backup metrics of size [{}] and protected size [{}].",
+ vmId, backupOfferingId, newSize, newProtectedSize);
+ txn.commit();
+ return;
+ }
+
+ UsageBackupVO latest = activeUsage.get(0);
+ if (activeUsage.size() == 1 && latest.getSize() == newSize && latest.getProtectedSize() == newProtectedSize) {
+ txn.commit();
+ return;
+ }
+
+ // Close the active rows and open one with the new size; this also merges duplicates.
+ for (UsageBackupVO usage : activeUsage) {
+ usage.setRemoved(eventDate);
+ update(usage.getId(), usage);
}
+ UsageBackupVO newUsage = new UsageBackupVO(latest.getZoneId(), latest.getAccountId(), latest.getDomainId(), vmId, backupOfferingId, eventDate);
+ newUsage.setSize(newSize);
+ newUsage.setProtectedSize(newProtectedSize);
+ persist(newUsage);
+ txn.commit();
} catch (final Exception e) {
+ txn.rollback();
logger.error("Error updating backup metrics: " + e.getMessage(), e);
Review Comment:
you are right. But I don't see a graceful fix. Exception should be rare as it is doing some db operations. If DB has some issues it will hit other codepaths as well.
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-6313543752
@vladimirpetrov @sureshanaparti can we please include this in 4.22 before the freeze?
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-6012270195
@blueorangutan package
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4184708360
##########
server/src/main/java/org/apache/cloudstack/backup/BackupManagerImpl.java:
##########
@@ -2055,12 +2064,20 @@ private void updateBackupUsageRecords(final BackupProvider backupProvider, DataC
Pair<Long, Long> sizes = entry.getValue();
Long backupSize = sizes.first();
Long protectedSize = sizes.second();
+ Pair<Long, Long> key = new Pair<>(vm.getId(), offeringId);
+ Pair<Long, Long> metric = new Pair<>(backupSize, protectedSize);
+ published.put(key, metric);
+ if (metric.equals(lastPublished.get(key))) {
Review Comment:
Thanks @Damans227 . That's a big issue with the current implementation.
I'll think about an alternative.
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "Damans227 (via GitHub)" <gi...@apache.org>.
Damans227 commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4220314653
##########
engine/schema/src/main/java/com/cloud/usage/dao/UsageBackupDaoImpl.java:
##########
@@ -42,19 +43,50 @@ public class UsageBackupDaoImpl extends GenericDaoBase<UsageBackupVO, Long> impl
" OR ((created <= ?) AND (removed >= ?)))";
@Override
- public void updateMetrics(final Long vmId, Long backupOfferingId, final Long size, final Long virtualSize) {
- try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) {
- SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
- sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
- sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
- UsageBackupVO vo = findOneBy(sc);
- if (vo != null) {
- vo.setSize(size);
- vo.setProtectedSize(virtualSize);
- update(vo.getId(), vo);
+ public List<UsageBackupVO> listActiveUsage(Long vmId, Long backupOfferingId) {
+ SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
+ sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
+ sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
+ sc.addAnd("removed", SearchCriteria.Op.NULL);
+ return listBy(sc, new Filter(UsageBackupVO.class, "created", false));
+ }
+
+ @Override
+ public void updateMetrics(final Long vmId, final Long backupOfferingId, final Long size, final Long virtualSize, final Date eventDate) {
+ final long newSize = size != null ? size : 0L;
+ final long newProtectedSize = virtualSize != null ? virtualSize : 0L;
+ TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
+ try {
+ txn.start();
+ List<UsageBackupVO> activeUsage = listActiveUsage(vmId, backupOfferingId);
+ if (activeUsage.isEmpty()) {
+ logger.warn("No active backup usage for VM [{}] and backup offering [{}], ignoring backup metrics of size [{}] and protected size [{}].",
+ vmId, backupOfferingId, newSize, newProtectedSize);
+ txn.commit();
+ return;
+ }
+
+ UsageBackupVO latest = activeUsage.get(0);
+ if (activeUsage.size() == 1 && latest.getSize() == newSize && latest.getProtectedSize() == newProtectedSize) {
+ txn.commit();
+ return;
+ }
+
+ // Close the active rows and open one with the new size; this also merges duplicates.
+ for (UsageBackupVO usage : activeUsage) {
+ usage.setRemoved(eventDate);
+ update(usage.getId(), usage);
}
+ UsageBackupVO newUsage = new UsageBackupVO(latest.getZoneId(), latest.getAccountId(), latest.getDomainId(), vmId, backupOfferingId, eventDate);
+ newUsage.setSize(newSize);
+ newUsage.setProtectedSize(newProtectedSize);
+ persist(newUsage);
+ txn.commit();
} catch (final Exception e) {
+ txn.rollback();
logger.error("Error updating backup metrics: " + e.getMessage(), e);
Review Comment:
can we throw again after the rollback? then the whole usage run rolls back and picks the event up next time, like you first said
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar merged PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-6309778850
Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 19639
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-6331646895
<b>[SF] Trillian test result (tid-17174)</b>
Environment: kvm-ol8 (x2), zone: Advanced Networking with Mgmt server ol8
Total time taken: 58164 seconds
Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr14310-t17174-kvm-ol8.zip
Smoke tests completed. 148 look OK, 1 have errors, 0 did not run
Only failed and skipped tests results shown below:
Test | Result | Time (s) | Test File
--- | --- | --- | ---
test_01_redundant_vpc_site2site_vpn | `Failure` | 470.84 | test_vpc_vpn.py
test_01_vpc_site2site_vpn | `Failure` | 270.86 | test_vpc_vpn.py
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-6060136660
@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.
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-6060114957
@blueorangutan package
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "Damans227 (via GitHub)" <gi...@apache.org>.
Damans227 commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4249085374
##########
engine/schema/src/main/java/com/cloud/usage/dao/UsageBackupDaoImpl.java:
##########
@@ -42,19 +43,50 @@ public class UsageBackupDaoImpl extends GenericDaoBase<UsageBackupVO, Long> impl
" OR ((created <= ?) AND (removed >= ?)))";
@Override
- public void updateMetrics(final Long vmId, Long backupOfferingId, final Long size, final Long virtualSize) {
- try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) {
- SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
- sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
- sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
- UsageBackupVO vo = findOneBy(sc);
- if (vo != null) {
- vo.setSize(size);
- vo.setProtectedSize(virtualSize);
- update(vo.getId(), vo);
+ public List<UsageBackupVO> listActiveUsage(Long vmId, Long backupOfferingId) {
+ SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
+ sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
+ sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
+ sc.addAnd("removed", SearchCriteria.Op.NULL);
+ return listBy(sc, new Filter(UsageBackupVO.class, "created", false));
+ }
+
+ @Override
+ public void updateMetrics(final Long vmId, final Long backupOfferingId, final Long size, final Long virtualSize, final Date eventDate) {
+ final long newSize = size != null ? size : 0L;
+ final long newProtectedSize = virtualSize != null ? virtualSize : 0L;
+ TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
+ try {
+ txn.start();
+ List<UsageBackupVO> activeUsage = listActiveUsage(vmId, backupOfferingId);
+ if (activeUsage.isEmpty()) {
+ logger.warn("No active backup usage for VM [{}] and backup offering [{}], ignoring backup metrics of size [{}] and protected size [{}].",
+ vmId, backupOfferingId, newSize, newProtectedSize);
+ txn.commit();
+ return;
+ }
+
+ UsageBackupVO latest = activeUsage.get(0);
+ if (activeUsage.size() == 1 && latest.getSize() == newSize && latest.getProtectedSize() == newProtectedSize) {
+ txn.commit();
+ return;
+ }
+
+ // Close the active rows and open one with the new size; this also merges duplicates.
+ for (UsageBackupVO usage : activeUsage) {
+ usage.setRemoved(eventDate);
+ update(usage.getId(), usage);
}
+ UsageBackupVO newUsage = new UsageBackupVO(latest.getZoneId(), latest.getAccountId(), latest.getDomainId(), vmId, backupOfferingId, eventDate);
+ newUsage.setSize(newSize);
+ newUsage.setProtectedSize(newProtectedSize);
+ persist(newUsage);
+ txn.commit();
} catch (final Exception e) {
+ txn.rollback();
logger.error("Error updating backup metrics: " + e.getMessage(), e);
Review Comment:
any thoughts on throwing again after the rollback? the catch still only logs it, so a failed size update gets marked as done
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "Damans227 (via GitHub)" <gi...@apache.org>.
Damans227 commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4219296407
##########
engine/schema/src/main/java/com/cloud/usage/dao/UsageBackupDaoImpl.java:
##########
@@ -42,19 +43,50 @@ public class UsageBackupDaoImpl extends GenericDaoBase<UsageBackupVO, Long> impl
" OR ((created <= ?) AND (removed >= ?)))";
@Override
- public void updateMetrics(final Long vmId, Long backupOfferingId, final Long size, final Long virtualSize) {
- try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) {
- SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
- sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
- sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
- UsageBackupVO vo = findOneBy(sc);
- if (vo != null) {
- vo.setSize(size);
- vo.setProtectedSize(virtualSize);
- update(vo.getId(), vo);
+ public List<UsageBackupVO> listActiveUsage(Long vmId, Long backupOfferingId) {
+ SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
+ sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
+ sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
+ sc.addAnd("removed", SearchCriteria.Op.NULL);
+ return listBy(sc, new Filter(UsageBackupVO.class, "created", false));
+ }
+
+ @Override
+ public void updateMetrics(final Long vmId, final Long backupOfferingId, final Long size, final Long virtualSize, final Date eventDate) {
+ final long newSize = size != null ? size : 0L;
+ final long newProtectedSize = virtualSize != null ? virtualSize : 0L;
+ TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
+ try {
+ txn.start();
+ List<UsageBackupVO> activeUsage = listActiveUsage(vmId, backupOfferingId);
+ if (activeUsage.isEmpty()) {
+ logger.warn("No active backup usage for VM [{}] and backup offering [{}], ignoring backup metrics of size [{}] and protected size [{}].",
+ vmId, backupOfferingId, newSize, newProtectedSize);
+ txn.commit();
+ return;
+ }
+
+ UsageBackupVO latest = activeUsage.get(0);
+ if (activeUsage.size() == 1 && latest.getSize() == newSize && latest.getProtectedSize() == newProtectedSize) {
+ txn.commit();
+ return;
+ }
+
+ // Close the active rows and open one with the new size; this also merges duplicates.
+ for (UsageBackupVO usage : activeUsage) {
+ usage.setRemoved(eventDate);
+ update(usage.getId(), usage);
}
+ UsageBackupVO newUsage = new UsageBackupVO(latest.getZoneId(), latest.getAccountId(), latest.getDomainId(), vmId, backupOfferingId, eventDate);
+ newUsage.setSize(newSize);
+ newUsage.setProtectedSize(newProtectedSize);
+ persist(newUsage);
+ txn.commit();
} catch (final Exception e) {
+ txn.rollback();
logger.error("Error updating backup metrics: " + e.getMessage(), e);
Review Comment:
does it reach the usage job though? this catch only logs it and doesnt throw again, so the event still gets marked as done
##########
engine/schema/src/main/resources/META-INF/db/schema-42210to42220.sql:
##########
@@ -18,3 +18,15 @@
--;
-- Schema upgrade from 4.22.1.0 to 4.22.2.0
--;
+
+-- Last backup usage metric published per VM and backup offering
+CREATE TABLE IF NOT EXISTS `cloud`.`backup_usage_metric` (
Review Comment:
makes sense, waiting on #13933 works for me
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-6061097510
Packaging result [SF]: ✔️ el8 ✔️ el9 ✔️ el10 ✔️ debian ✔️ suse15. SL-JID 19474
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4335591660
##########
engine/schema/src/main/java/com/cloud/usage/dao/UsageBackupDaoImpl.java:
##########
@@ -42,19 +43,50 @@ public class UsageBackupDaoImpl extends GenericDaoBase<UsageBackupVO, Long> impl
" OR ((created <= ?) AND (removed >= ?)))";
@Override
- public void updateMetrics(final Long vmId, Long backupOfferingId, final Long size, final Long virtualSize) {
- try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) {
- SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
- sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
- sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
- UsageBackupVO vo = findOneBy(sc);
- if (vo != null) {
- vo.setSize(size);
- vo.setProtectedSize(virtualSize);
- update(vo.getId(), vo);
+ public List<UsageBackupVO> listActiveUsage(Long vmId, Long backupOfferingId) {
+ SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
+ sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
+ sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
+ sc.addAnd("removed", SearchCriteria.Op.NULL);
+ return listBy(sc, new Filter(UsageBackupVO.class, "created", false));
+ }
+
+ @Override
+ public void updateMetrics(final Long vmId, final Long backupOfferingId, final Long size, final Long virtualSize, final Date eventDate) {
+ final long newSize = size != null ? size : 0L;
+ final long newProtectedSize = virtualSize != null ? virtualSize : 0L;
+ TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
+ try {
+ txn.start();
+ List<UsageBackupVO> activeUsage = listActiveUsage(vmId, backupOfferingId);
+ if (activeUsage.isEmpty()) {
+ logger.warn("No active backup usage for VM [{}] and backup offering [{}], ignoring backup metrics of size [{}] and protected size [{}].",
+ vmId, backupOfferingId, newSize, newProtectedSize);
+ txn.commit();
+ return;
+ }
+
+ UsageBackupVO latest = activeUsage.get(0);
+ if (activeUsage.size() == 1 && latest.getSize() == newSize && latest.getProtectedSize() == newProtectedSize) {
+ txn.commit();
+ return;
+ }
+
+ // Close the active rows and open one with the new size; this also merges duplicates.
+ for (UsageBackupVO usage : activeUsage) {
+ usage.setRemoved(eventDate);
+ update(usage.getId(), usage);
}
+ UsageBackupVO newUsage = new UsageBackupVO(latest.getZoneId(), latest.getAccountId(), latest.getDomainId(), vmId, backupOfferingId, eventDate);
+ newUsage.setSize(newSize);
+ newUsage.setProtectedSize(newProtectedSize);
+ persist(newUsage);
+ txn.commit();
} catch (final Exception e) {
+ txn.rollback();
logger.error("Error updating backup metrics: " + e.getMessage(), e);
Review Comment:
@Damans227 I would actually like to skip this one.
updateMetrics opens a nested TransactionLegacy on the same thread, so its rollback() clears the whole transaction stack and rolls back the shared connection. That also rolls back the processed=true update that parse() writes for the event just before createHelperRecord, so the event is not marked done.
The chance of exception hitting here is also very low. just some database updates without a unique key constraint.
Other helpers also use the same pattern
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4193085590
##########
server/src/main/java/org/apache/cloudstack/backup/BackupManagerImpl.java:
##########
@@ -546,6 +549,7 @@ public boolean removeVMFromBackupOffering(final Long vmId, final boolean forced)
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VM_BACKUP_OFFERING_REMOVED_AND_BACKUPS_DELETED, vm.getAccountId(), vm.getDataCenterId(), vm.getId(),
"Backup-" + vm.getHostName() + "-" + vm.getUuid(), backupOfferingId, null, null,
Backup.class.getSimpleName(), vm.getUuid());
+ backupUsageMetricDao.removeByVmAndOffering(vm.getId(), backupOfferingId);
Review Comment:
Commit the deletion usage event and removal of its last-published metric in one transaction. If the process stops after the event row commits but before this delete, reassigning the same offering after its size returns to the stale value suppresses the first metric, leaving the new `usage_backup` row at zero until another size change. Wrap both operations in `Transaction.execute`, as `publishBackupUsageMetricIfChanged` does.
This issue also appears on line 1660 of the same file.
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-6308873777
@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.
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "Damans227 (via GitHub)" <gi...@apache.org>.
Damans227 commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4336693683
##########
engine/schema/src/main/java/com/cloud/usage/dao/UsageBackupDaoImpl.java:
##########
@@ -42,19 +43,50 @@ public class UsageBackupDaoImpl extends GenericDaoBase<UsageBackupVO, Long> impl
" OR ((created <= ?) AND (removed >= ?)))";
@Override
- public void updateMetrics(final Long vmId, Long backupOfferingId, final Long size, final Long virtualSize) {
- try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) {
- SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
- sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
- sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
- UsageBackupVO vo = findOneBy(sc);
- if (vo != null) {
- vo.setSize(size);
- vo.setProtectedSize(virtualSize);
- update(vo.getId(), vo);
+ public List<UsageBackupVO> listActiveUsage(Long vmId, Long backupOfferingId) {
+ SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
+ sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
+ sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
+ sc.addAnd("removed", SearchCriteria.Op.NULL);
+ return listBy(sc, new Filter(UsageBackupVO.class, "created", false));
+ }
+
+ @Override
+ public void updateMetrics(final Long vmId, final Long backupOfferingId, final Long size, final Long virtualSize, final Date eventDate) {
+ final long newSize = size != null ? size : 0L;
+ final long newProtectedSize = virtualSize != null ? virtualSize : 0L;
+ TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
+ try {
+ txn.start();
+ List<UsageBackupVO> activeUsage = listActiveUsage(vmId, backupOfferingId);
+ if (activeUsage.isEmpty()) {
+ logger.warn("No active backup usage for VM [{}] and backup offering [{}], ignoring backup metrics of size [{}] and protected size [{}].",
+ vmId, backupOfferingId, newSize, newProtectedSize);
+ txn.commit();
+ return;
+ }
+
+ UsageBackupVO latest = activeUsage.get(0);
+ if (activeUsage.size() == 1 && latest.getSize() == newSize && latest.getProtectedSize() == newProtectedSize) {
+ txn.commit();
+ return;
+ }
+
+ // Close the active rows and open one with the new size; this also merges duplicates.
+ for (UsageBackupVO usage : activeUsage) {
+ usage.setRemoved(eventDate);
+ update(usage.getId(), usage);
}
+ UsageBackupVO newUsage = new UsageBackupVO(latest.getZoneId(), latest.getAccountId(), latest.getDomainId(), vmId, backupOfferingId, eventDate);
+ newUsage.setSize(newSize);
+ newUsage.setProtectedSize(newProtectedSize);
+ persist(newUsage);
+ txn.commit();
} catch (final Exception e) {
+ txn.rollback();
logger.error("Error updating backup metrics: " + e.getMessage(), e);
Review Comment:
that covers it, thanks
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "harikrishna-patnala (via GitHub)" <gi...@apache.org>.
harikrishna-patnala commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4333660569
##########
engine/schema/src/main/java/org/apache/cloudstack/backup/dao/BackupUsageMetricDaoImpl.java:
##########
@@ -0,0 +1,54 @@
+// 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 org.apache.cloudstack.backup.dao;
+
+import javax.annotation.PostConstruct;
+
+import org.apache.cloudstack.backup.BackupUsageMetricVO;
+
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+
+public class BackupUsageMetricDaoImpl extends GenericDaoBase<BackupUsageMetricVO, Long> implements BackupUsageMetricDao {
+ private SearchBuilder<BackupUsageMetricVO> vmAndOfferingSearch;
+
+ @PostConstruct
+ protected void init() {
+ vmAndOfferingSearch = createSearchBuilder();
+ vmAndOfferingSearch.and("vmId", vmAndOfferingSearch.entity().getVmId(), SearchCriteria.Op.EQ);
+ vmAndOfferingSearch.and("backupOfferingId", vmAndOfferingSearch.entity().getBackupOfferingId(), SearchCriteria.Op.EQ);
+ vmAndOfferingSearch.done();
+ }
+
+ private SearchCriteria<BackupUsageMetricVO> createVmAndOfferingCriteria(long vmId, long backupOfferingId) {
+ SearchCriteria<BackupUsageMetricVO> sc = vmAndOfferingSearch.create();
+ sc.setParameters("vmId", vmId);
+ sc.setParameters("backupOfferingId", backupOfferingId);
+ return sc;
+ }
+
+ @Override
+ public BackupUsageMetricVO findByVmAndOffering(long vmId, long backupOfferingId) {
+ return findOneBy(createVmAndOfferingCriteria(vmId, backupOfferingId));
+ }
+
+ @Override
+ public int removeByVmAndOffering(long vmId, long backupOfferingId) {
+ return remove(createVmAndOfferingCriteria(vmId, backupOfferingId));
Review Comment:
@abh1sar is it better to keep the old records by maintaining removed column ?
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "Damans227 (via GitHub)" <gi...@apache.org>.
Damans227 commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4194673736
##########
server/src/main/java/org/apache/cloudstack/backup/BackupManagerImpl.java:
##########
@@ -2055,12 +2064,20 @@ private void updateBackupUsageRecords(final BackupProvider backupProvider, DataC
Pair<Long, Long> sizes = entry.getValue();
Long backupSize = sizes.first();
Long protectedSize = sizes.second();
+ Pair<Long, Long> key = new Pair<>(vm.getId(), offeringId);
+ Pair<Long, Long> metric = new Pair<>(backupSize, protectedSize);
+ published.put(key, metric);
+ if (metric.equals(lastPublished.get(key))) {
Review Comment:
the new table and the lock cover it, thanks
##########
engine/schema/src/main/resources/META-INF/db/schema-42210to42220.sql:
##########
@@ -18,3 +18,15 @@
--;
-- Schema upgrade from 4.22.1.0 to 4.22.2.0
--;
+
+-- Last backup usage metric published per VM and backup offering
+CREATE TABLE IF NOT EXISTS `cloud`.`backup_usage_metric` (
Review Comment:
can we hook the 4.22.2 upgrade step into the upgrade checker here? right now this table never gets made and the backup smoke test fails deleting a backup
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-6082919432
This pull request has merge conflicts. Dear author, please fix the conflicts and sync your branch with the base branch.
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4247235215
##########
engine/schema/src/main/resources/META-INF/db/schema-42210to42220.sql:
##########
@@ -18,3 +18,15 @@
--;
-- Schema upgrade from 4.22.1.0 to 4.22.2.0
--;
+
+-- Last backup usage metric published per VM and backup offering
+CREATE TABLE IF NOT EXISTS `cloud`.`backup_usage_metric` (
Review Comment:
https://github.com/apache/cloudstack/pull/13933 is merged.
I updated this PR to the latest code. Please check @Damans227
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-5993566167
## [Codecov](https://app.codecov.io/gh/apache/cloudstack/pull/14310?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 4.02%. Comparing base ([`2e63c60`](https://app.codecov.io/gh/apache/cloudstack/commit/2e63c601fc224de20c345172d9b805ffb6bdbeac?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`ec6e3ef`](https://app.codecov.io/gh/apache/cloudstack/commit/ec6e3efdc11590ed942b36300dad8d0d31bbb743?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
> :exclamation: There is a different number of reports uploaded between BASE (2e63c60) and HEAD (ec6e3ef). Click for more details.
>
> <details><summary>HEAD has 1 upload less than BASE</summary>
>
>| Flag | BASE (2e63c60) | HEAD (ec6e3ef) |
>|------|------|------|
>|unittests|1|0|
></details>
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## 4.22 #14310 +/- ##
=============================================
- Coverage 18.00% 4.02% -13.99%
=============================================
Files 5936 449 -5487
Lines 535716 38239 -497477
Branches 65596 7082 -58514
=============================================
- Hits 96459 1538 -94921
+ Misses 428268 36489 -391779
+ Partials 10989 212 -10777
```
| [Flag](https://app.codecov.io/gh/apache/cloudstack/pull/14310/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [uitests](https://app.codecov.io/gh/apache/cloudstack/pull/14310/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `4.02% <ø> (ø)` | |
| [unittests](https://app.codecov.io/gh/apache/cloudstack/pull/14310/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
</details>
[:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/cloudstack/pull/14310?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4335465532
##########
engine/schema/src/main/java/org/apache/cloudstack/backup/dao/BackupUsageMetricDaoImpl.java:
##########
@@ -0,0 +1,54 @@
+// 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 org.apache.cloudstack.backup.dao;
+
+import javax.annotation.PostConstruct;
+
+import org.apache.cloudstack.backup.BackupUsageMetricVO;
+
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+
+public class BackupUsageMetricDaoImpl extends GenericDaoBase<BackupUsageMetricVO, Long> implements BackupUsageMetricDao {
+ private SearchBuilder<BackupUsageMetricVO> vmAndOfferingSearch;
+
+ @PostConstruct
+ protected void init() {
+ vmAndOfferingSearch = createSearchBuilder();
+ vmAndOfferingSearch.and("vmId", vmAndOfferingSearch.entity().getVmId(), SearchCriteria.Op.EQ);
+ vmAndOfferingSearch.and("backupOfferingId", vmAndOfferingSearch.entity().getBackupOfferingId(), SearchCriteria.Op.EQ);
+ vmAndOfferingSearch.done();
+ }
+
+ private SearchCriteria<BackupUsageMetricVO> createVmAndOfferingCriteria(long vmId, long backupOfferingId) {
+ SearchCriteria<BackupUsageMetricVO> sc = vmAndOfferingSearch.create();
+ sc.setParameters("vmId", vmId);
+ sc.setParameters("backupOfferingId", backupOfferingId);
+ return sc;
+ }
+
+ @Override
+ public BackupUsageMetricVO findByVmAndOffering(long vmId, long backupOfferingId) {
+ return findOneBy(createVmAndOfferingCriteria(vmId, backupOfferingId));
+ }
+
+ @Override
+ public int removeByVmAndOffering(long vmId, long backupOfferingId) {
+ return remove(createVmAndOfferingCriteria(vmId, backupOfferingId));
Review Comment:
@harikrishna-patnala I would avoid adding a removed column. The backup_usage_metrics table is an intermediate table to make sure that backup metrics usage doesn't get logged repeatedly with the same value.
Keeping older deleted backup_usage_metrics column is not useful as the cloud_usage table already contains the recorded usage history.
We also enforce the vm_id, backup_offering_id unique key constraint which won't be possible with the removed column.
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-6082912535
This pull request has merge conflicts. Dear author, please fix the conflicts and sync your branch with the base branch.
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4219119932
##########
engine/schema/src/main/resources/META-INF/db/schema-42210to42220.sql:
##########
@@ -18,3 +18,15 @@
--;
-- Schema upgrade from 4.22.1.0 to 4.22.2.0
--;
+
+-- Last backup usage metric published per VM and backup offering
+CREATE TABLE IF NOT EXISTS `cloud`.`backup_usage_metric` (
Review Comment:
PR 13933 has added the checker. I'll update after that gets merged.
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-6313138026
@abh1sar a [SL] Trillian-Jenkins test job (ol8 mgmt + kvm-ol8) has been kicked to run smoke tests
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-6338880617
Merged based on UT, reviews and smokes
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4193052895
##########
server/src/main/java/org/apache/cloudstack/backup/BackupManagerImpl.java:
##########
@@ -2055,12 +2064,20 @@ private void updateBackupUsageRecords(final BackupProvider backupProvider, DataC
Pair<Long, Long> sizes = entry.getValue();
Long backupSize = sizes.first();
Long protectedSize = sizes.second();
+ Pair<Long, Long> key = new Pair<>(vm.getId(), offeringId);
+ Pair<Long, Long> metric = new Pair<>(backupSize, protectedSize);
+ published.put(key, metric);
+ if (metric.equals(lastPublished.get(key))) {
Review Comment:
Added a new BackupUsageMetrics table in DB to track the last updated value.
Also added a lock so that two management servers don't race while syncing the backup metrics.
Please take a look @Damans227
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-6308845223
@blueorangutan package
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "Damans227 (via GitHub)" <gi...@apache.org>.
Damans227 commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4194450249
##########
engine/schema/src/main/java/com/cloud/usage/dao/UsageBackupDaoImpl.java:
##########
@@ -42,19 +43,50 @@ public class UsageBackupDaoImpl extends GenericDaoBase<UsageBackupVO, Long> impl
" OR ((created <= ?) AND (removed >= ?)))";
@Override
- public void updateMetrics(final Long vmId, Long backupOfferingId, final Long size, final Long virtualSize) {
- try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) {
- SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
- sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
- sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
- UsageBackupVO vo = findOneBy(sc);
- if (vo != null) {
- vo.setSize(size);
- vo.setProtectedSize(virtualSize);
- update(vo.getId(), vo);
+ public List<UsageBackupVO> listActiveUsage(Long vmId, Long backupOfferingId) {
+ SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
+ sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
+ sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
+ sc.addAnd("removed", SearchCriteria.Op.NULL);
+ return listBy(sc, new Filter(UsageBackupVO.class, "created", false));
+ }
+
+ @Override
+ public void updateMetrics(final Long vmId, final Long backupOfferingId, final Long size, final Long virtualSize, final Date eventDate) {
+ final long newSize = size != null ? size : 0L;
+ final long newProtectedSize = virtualSize != null ? virtualSize : 0L;
+ TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
+ try {
+ txn.start();
+ List<UsageBackupVO> activeUsage = listActiveUsage(vmId, backupOfferingId);
+ if (activeUsage.isEmpty()) {
+ logger.warn("No active backup usage for VM [{}] and backup offering [{}], ignoring backup metrics of size [{}] and protected size [{}].",
+ vmId, backupOfferingId, newSize, newProtectedSize);
+ txn.commit();
+ return;
+ }
+
+ UsageBackupVO latest = activeUsage.get(0);
+ if (activeUsage.size() == 1 && latest.getSize() == newSize && latest.getProtectedSize() == newProtectedSize) {
+ txn.commit();
+ return;
+ }
+
+ // Close the active rows and open one with the new size; this also merges duplicates.
+ for (UsageBackupVO usage : activeUsage) {
+ usage.setRemoved(eventDate);
+ update(usage.getId(), usage);
}
+ UsageBackupVO newUsage = new UsageBackupVO(latest.getZoneId(), latest.getAccountId(), latest.getDomainId(), vmId, backupOfferingId, eventDate);
+ newUsage.setSize(newSize);
+ newUsage.setProtectedSize(newProtectedSize);
+ persist(newUsage);
+ txn.commit();
} catch (final Exception e) {
+ txn.rollback();
logger.error("Error updating backup metrics: " + e.getMessage(), e);
Review Comment:
what happens if this fails once? the size is already saved as sent, so it never gets sent again and billing stays on the old size until it changes
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "Damans227 (via GitHub)" <gi...@apache.org>.
Damans227 commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4183793917
##########
server/src/main/java/org/apache/cloudstack/backup/BackupManagerImpl.java:
##########
@@ -2055,12 +2064,20 @@ private void updateBackupUsageRecords(final BackupProvider backupProvider, DataC
Pair<Long, Long> sizes = entry.getValue();
Long backupSize = sizes.first();
Long protectedSize = sizes.second();
+ Pair<Long, Long> key = new Pair<>(vm.getId(), offeringId);
+ Pair<Long, Long> metric = new Pair<>(backupSize, protectedSize);
+ published.put(key, metric);
+ if (metric.equals(lastPublished.get(key))) {
Review Comment:
what happens with two management servers if one sends a new size and then goes down? if the size goes back after that, the other one still remembers the old value and skips it, so billing stays on the wrong size
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4219035333
##########
engine/schema/src/main/java/com/cloud/usage/dao/UsageBackupDaoImpl.java:
##########
@@ -42,19 +43,50 @@ public class UsageBackupDaoImpl extends GenericDaoBase<UsageBackupVO, Long> impl
" OR ((created <= ?) AND (removed >= ?)))";
@Override
- public void updateMetrics(final Long vmId, Long backupOfferingId, final Long size, final Long virtualSize) {
- try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) {
- SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
- sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
- sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
- UsageBackupVO vo = findOneBy(sc);
- if (vo != null) {
- vo.setSize(size);
- vo.setProtectedSize(virtualSize);
- update(vo.getId(), vo);
+ public List<UsageBackupVO> listActiveUsage(Long vmId, Long backupOfferingId) {
+ SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
+ sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
+ sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
+ sc.addAnd("removed", SearchCriteria.Op.NULL);
+ return listBy(sc, new Filter(UsageBackupVO.class, "created", false));
+ }
+
+ @Override
+ public void updateMetrics(final Long vmId, final Long backupOfferingId, final Long size, final Long virtualSize, final Date eventDate) {
+ final long newSize = size != null ? size : 0L;
+ final long newProtectedSize = virtualSize != null ? virtualSize : 0L;
+ TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
+ try {
+ txn.start();
+ List<UsageBackupVO> activeUsage = listActiveUsage(vmId, backupOfferingId);
+ if (activeUsage.isEmpty()) {
+ logger.warn("No active backup usage for VM [{}] and backup offering [{}], ignoring backup metrics of size [{}] and protected size [{}].",
+ vmId, backupOfferingId, newSize, newProtectedSize);
+ txn.commit();
+ return;
+ }
+
+ UsageBackupVO latest = activeUsage.get(0);
+ if (activeUsage.size() == 1 && latest.getSize() == newSize && latest.getProtectedSize() == newProtectedSize) {
+ txn.commit();
+ return;
+ }
+
+ // Close the active rows and open one with the new size; this also merges duplicates.
+ for (UsageBackupVO usage : activeUsage) {
+ usage.setRemoved(eventDate);
+ update(usage.getId(), usage);
}
+ UsageBackupVO newUsage = new UsageBackupVO(latest.getZoneId(), latest.getAccountId(), latest.getDomainId(), vmId, backupOfferingId, eventDate);
+ newUsage.setSize(newSize);
+ newUsage.setProtectedSize(newProtectedSize);
+ persist(newUsage);
+ txn.commit();
} catch (final Exception e) {
+ txn.rollback();
logger.error("Error updating backup metrics: " + e.getMessage(), e);
Review Comment:
It will cause the usage job to throw an exception. The event will not be marked as processed and the usage server will try processing it again in the next run
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "Damans227 (via GitHub)" <gi...@apache.org>.
Damans227 commented on code in PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#discussion_r4249085078
##########
engine/schema/src/main/resources/META-INF/db/schema-42210to42220.sql:
##########
@@ -18,3 +18,15 @@
--;
-- Schema upgrade from 4.22.1.0 to 4.22.2.0
--;
+
+-- Last backup usage metric published per VM and backup offering
+CREATE TABLE IF NOT EXISTS `cloud`.`backup_usage_metric` (
Review Comment:
that covers it, thanks
##########
engine/schema/src/main/java/com/cloud/usage/dao/UsageBackupDaoImpl.java:
##########
@@ -42,19 +43,50 @@ public class UsageBackupDaoImpl extends GenericDaoBase<UsageBackupVO, Long> impl
" OR ((created <= ?) AND (removed >= ?)))";
@Override
- public void updateMetrics(final Long vmId, Long backupOfferingId, final Long size, final Long virtualSize) {
- try (TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB)) {
- SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
- sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
- sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
- UsageBackupVO vo = findOneBy(sc);
- if (vo != null) {
- vo.setSize(size);
- vo.setProtectedSize(virtualSize);
- update(vo.getId(), vo);
+ public List<UsageBackupVO> listActiveUsage(Long vmId, Long backupOfferingId) {
+ SearchCriteria<UsageBackupVO> sc = this.createSearchCriteria();
+ sc.addAnd("vmId", SearchCriteria.Op.EQ, vmId);
+ sc.addAnd("backupOfferingId", SearchCriteria.Op.EQ, backupOfferingId);
+ sc.addAnd("removed", SearchCriteria.Op.NULL);
+ return listBy(sc, new Filter(UsageBackupVO.class, "created", false));
+ }
+
+ @Override
+ public void updateMetrics(final Long vmId, final Long backupOfferingId, final Long size, final Long virtualSize, final Date eventDate) {
+ final long newSize = size != null ? size : 0L;
+ final long newProtectedSize = virtualSize != null ? virtualSize : 0L;
+ TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
+ try {
+ txn.start();
+ List<UsageBackupVO> activeUsage = listActiveUsage(vmId, backupOfferingId);
+ if (activeUsage.isEmpty()) {
+ logger.warn("No active backup usage for VM [{}] and backup offering [{}], ignoring backup metrics of size [{}] and protected size [{}].",
+ vmId, backupOfferingId, newSize, newProtectedSize);
+ txn.commit();
+ return;
+ }
+
+ UsageBackupVO latest = activeUsage.get(0);
+ if (activeUsage.size() == 1 && latest.getSize() == newSize && latest.getProtectedSize() == newProtectedSize) {
+ txn.commit();
+ return;
+ }
+
+ // Close the active rows and open one with the new size; this also merges duplicates.
+ for (UsageBackupVO usage : activeUsage) {
+ usage.setRemoved(eventDate);
+ update(usage.getId(), usage);
}
+ UsageBackupVO newUsage = new UsageBackupVO(latest.getZoneId(), latest.getAccountId(), latest.getDomainId(), vmId, backupOfferingId, eventDate);
+ newUsage.setSize(newSize);
+ newUsage.setProtectedSize(newProtectedSize);
+ persist(newUsage);
+ txn.commit();
} catch (final Exception e) {
+ txn.rollback();
logger.error("Error updating backup metrics: " + e.getMessage(), e);
Review Comment:
any thoughts on throwing again after the rollback? the catch still only logs it, so a failed size update gets marked as done
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Backup usage: bill each size for the time it applied, and publish the metric only on change [cloudstack]
Posted by "abh1sar (via GitHub)" <gi...@apache.org>.
abh1sar commented on PR #14310:
URL: https://github.com/apache/cloudstack/pull/14310#issuecomment-6122878409
@blueorangutan package
--
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: commits-unsubscribe@cloudstack.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org