Repository navigation
Backup usage: bill each size for the time it applied, and publish the metric only on change #14310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.22
Are you sure you want to change the base?
Changes from all commits
e8cb77d
5a64aad
48169b4
8086034
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // 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; | ||
|
|
||
| import java.util.Date; | ||
|
|
||
| import javax.persistence.Column; | ||
| import javax.persistence.Entity; | ||
| import javax.persistence.GeneratedValue; | ||
| import javax.persistence.GenerationType; | ||
| import javax.persistence.Id; | ||
| import javax.persistence.Table; | ||
| import javax.persistence.Temporal; | ||
| import javax.persistence.TemporalType; | ||
|
|
||
| import org.apache.cloudstack.api.InternalIdentity; | ||
|
|
||
| /** | ||
| * The backup usage metric last published for a VM and backup offering. | ||
| */ | ||
| @Entity | ||
| @Table(name = "backup_usage_metric") | ||
| public class BackupUsageMetricVO implements InternalIdentity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "id") | ||
| private long id; | ||
|
|
||
| @Column(name = "vm_id") | ||
| private long vmId; | ||
|
|
||
| @Column(name = "backup_offering_id") | ||
| private long backupOfferingId; | ||
|
|
||
| @Column(name = "size") | ||
| private long size; | ||
|
|
||
| @Column(name = "protected_size") | ||
| private long protectedSize; | ||
|
|
||
| @Column(name = "updated") | ||
| @Temporal(value = TemporalType.TIMESTAMP) | ||
| private Date updated; | ||
|
|
||
| protected BackupUsageMetricVO() { | ||
| } | ||
|
|
||
| public BackupUsageMetricVO(long vmId, long backupOfferingId, long size, long protectedSize, Date updated) { | ||
| this.vmId = vmId; | ||
| this.backupOfferingId = backupOfferingId; | ||
| this.size = size; | ||
| this.protectedSize = protectedSize; | ||
| this.updated = updated; | ||
| } | ||
|
|
||
| @Override | ||
| public long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public long getVmId() { | ||
| return vmId; | ||
| } | ||
|
|
||
| public long getBackupOfferingId() { | ||
| return backupOfferingId; | ||
| } | ||
|
|
||
| public long getSize() { | ||
| return size; | ||
| } | ||
|
|
||
| public void setSize(long size) { | ||
| this.size = size; | ||
| } | ||
|
|
||
| public long getProtectedSize() { | ||
| return protectedSize; | ||
| } | ||
|
|
||
| public void setProtectedSize(long protectedSize) { | ||
| this.protectedSize = protectedSize; | ||
| } | ||
|
|
||
| public Date getUpdated() { | ||
| return updated; | ||
| } | ||
|
|
||
| public void setUpdated(Date updated) { | ||
| this.updated = updated; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // 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 org.apache.cloudstack.backup.BackupUsageMetricVO; | ||
|
|
||
| import com.cloud.utils.db.GenericDao; | ||
|
|
||
| public interface BackupUsageMetricDao extends GenericDao<BackupUsageMetricVO, Long> { | ||
| BackupUsageMetricVO findByVmAndOffering(long vmId, long backupOfferingId); | ||
|
|
||
| int removeByVmAndOffering(long vmId, long backupOfferingId); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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` ( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PR 13933 has added the checker. I'll update after that gets merged.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense, waiting on #13933 works for me |
||
| `id` bigint unsigned NOT NULL auto_increment COMMENT 'id', | ||
| `vm_id` bigint unsigned NOT NULL COMMENT 'VM ID', | ||
| `backup_offering_id` bigint unsigned NOT NULL COMMENT 'Backup offering ID', | ||
| `size` bigint unsigned NOT NULL COMMENT 'Backup size last published', | ||
| `protected_size` bigint unsigned NOT NULL COMMENT 'Protected size last published', | ||
| `updated` datetime NOT NULL COMMENT 'Date the metric was last published', | ||
| PRIMARY KEY (`id`), | ||
| UNIQUE KEY `uk_backup_usage_metric__vm_id__backup_offering_id` (`vm_id`, `backup_offering_id`) | ||
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it reach the usage job though? this catch only logs it and doesnt throw again, so the event still gets marked as done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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