Skip to content

Commit b57b1e3

Browse files
bernardodemarcowinterhazel
authored andcommitted
Refactoring retention of backup schedules (apache#11223)
* refactor backup schedule retention workflows Co-authored-by: Fabricio Duarte <[email protected]>
1 parent aebfa48 commit b57b1e3

File tree

15 files changed

+465
-289
lines changed

15 files changed

+465
-289
lines changed

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,9 @@ public class ApiConstants {
14201420
public static final String EXPIRY_DATE = "expirydate";
14211421
public static final String LICENSE_CHECK = "licensecheck";
14221422
public static final String HAS_LICENSE = "haslicense";
1423+
public static final String PARAMETER_DESCRIPTION_MAX_BACKUPS = "The maximum number of backups to keep for a VM. " +
1424+
"If \"0\", no retention policy will be applied and, thus, no backups from the schedule will be automatically deleted. " +
1425+
"This parameter is only supported for the Dummy, NAS and EMC Networker backup provider.";
14231426

14241427
public static final String VMWARE_DC = "vmwaredc";
14251428

api/src/main/java/org/apache/cloudstack/api/command/user/backup/CreateBackupCmd.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import javax.inject.Inject;
2121

22-
import com.cloud.storage.Snapshot;
2322
import org.apache.cloudstack.acl.RoleType;
2423
import org.apache.cloudstack.api.APICommand;
2524
import org.apache.cloudstack.api.ApiCommandResourceType;
@@ -28,7 +27,6 @@
2827
import org.apache.cloudstack.api.BaseAsyncCreateCmd;
2928
import org.apache.cloudstack.api.Parameter;
3029
import org.apache.cloudstack.api.ServerApiException;
31-
import org.apache.cloudstack.api.response.BackupScheduleResponse;
3230
import org.apache.cloudstack.api.response.SuccessResponse;
3331
import org.apache.cloudstack.api.response.UserVmResponse;
3432
import org.apache.cloudstack.backup.BackupManager;
@@ -62,13 +60,6 @@ public class CreateBackupCmd extends BaseAsyncCreateCmd {
6260
description = "ID of the VM")
6361
private Long vmId;
6462

65-
@Parameter(name = ApiConstants.SCHEDULE_ID,
66-
type = CommandType.LONG,
67-
entityType = BackupScheduleResponse.class,
68-
description = "backup schedule ID of the VM, if this is null, it indicates that it is a manual backup.",
69-
since = "4.21.0")
70-
private Long scheduleId;
71-
7263
/////////////////////////////////////////////////////
7364
/////////////////// Accessors ///////////////////////
7465
/////////////////////////////////////////////////////
@@ -77,22 +68,14 @@ public Long getVmId() {
7768
return vmId;
7869
}
7970

80-
public Long getScheduleId() {
81-
if (scheduleId != null) {
82-
return scheduleId;
83-
} else {
84-
return Snapshot.MANUAL_POLICY_ID;
85-
}
86-
}
87-
8871
/////////////////////////////////////////////////////
8972
/////////////// API Implementation///////////////////
9073
/////////////////////////////////////////////////////
9174

9275
@Override
9376
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
9477
try {
95-
boolean result = backupManager.createBackup(getVmId(), getScheduleId());
78+
boolean result = backupManager.createBackup(getVmId(), getJob());
9679
if (result) {
9780
SuccessResponse response = new SuccessResponse(getCommandName());
9881
response.setResponseName(getCommandName());

api/src/main/java/org/apache/cloudstack/api/command/user/backup/CreateBackupScheduleCmd.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,8 @@ public class CreateBackupScheduleCmd extends BaseCmd {
7575
description = "Specifies a timezone for this command. For more information on the timezone parameter, see TimeZone Format.")
7676
private String timezone;
7777

78-
@Parameter(name = ApiConstants.MAX_BACKUPS,
79-
type = CommandType.INTEGER,
80-
description = "maximum number of backups to retain",
81-
since = "4.21.0")
78+
@Parameter(name = ApiConstants.MAX_BACKUPS, type = CommandType.INTEGER,
79+
since = "4.21.0", description = ApiConstants.PARAMETER_DESCRIPTION_MAX_BACKUPS)
8280
private Integer maxBackups;
8381

8482
/////////////////////////////////////////////////////

api/src/main/java/org/apache/cloudstack/backup/Backup.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,6 @@ enum Status {
3333
Allocated, Queued, BackingUp, BackedUp, Error, Failed, Restoring, Removed, Expunged
3434
}
3535

36-
public enum Type {
37-
MANUAL, HOURLY, DAILY, WEEKLY, MONTHLY;
38-
private int max = 8;
39-
40-
public void setMax(int max) {
41-
this.max = max;
42-
}
43-
44-
public int getMax() {
45-
return max;
46-
}
47-
48-
@Override
49-
public String toString() {
50-
return this.name();
51-
}
52-
53-
public boolean equals(String snapshotType) {
54-
return this.toString().equalsIgnoreCase(snapshotType);
55-
}
56-
}
57-
5836
class Metric {
5937
private Long backupSize = 0L;
6038
private Long dataSize = 0L;
@@ -166,4 +144,5 @@ public String toString() {
166144
Long getProtectedSize();
167145
List<VolumeInfo> getBackedUpVolumes();
168146
long getZoneId();
147+
Long getBackupScheduleId();
169148
}

api/src/main/java/org/apache/cloudstack/backup/BackupManager.java

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,38 +58,6 @@ public interface BackupManager extends BackupService, Configurable, PluggableSer
5858
"false",
5959
"Enable volume attach/detach operations for VMs that are assigned to Backup Offerings.", true);
6060

61-
ConfigKey<Integer> BackupHourlyMax = new ConfigKey<Integer>("Advanced", Integer.class,
62-
"backup.max.hourly",
63-
"8",
64-
"Maximum recurring hourly backups to be retained for an instance. If the limit is reached, early backups from the start of the hour are deleted so that newer ones can be saved. This limit does not apply to manual backups. If set to 0, recurring hourly backups can not be scheduled.",
65-
false,
66-
ConfigKey.Scope.Global,
67-
null);
68-
69-
ConfigKey<Integer> BackupDailyMax = new ConfigKey<Integer>("Advanced", Integer.class,
70-
"backup.max.daily",
71-
"8",
72-
"Maximum recurring daily backups to be retained for an instance. If the limit is reached, backups from the start of the day are deleted so that newer ones can be saved. This limit does not apply to manual backups. If set to 0, recurring daily backups can not be scheduled.",
73-
false,
74-
ConfigKey.Scope.Global,
75-
null);
76-
77-
ConfigKey<Integer> BackupWeeklyMax = new ConfigKey<Integer>("Advanced", Integer.class,
78-
"backup.max.weekly",
79-
"8",
80-
"Maximum recurring weekly backups to be retained for an instance. If the limit is reached, backups from the beginning of the week are deleted so that newer ones can be saved. This limit does not apply to manual backups. If set to 0, recurring weekly backups can not be scheduled.",
81-
false,
82-
ConfigKey.Scope.Global,
83-
null);
84-
85-
ConfigKey<Integer> BackupMonthlyMax = new ConfigKey<Integer>("Advanced", Integer.class,
86-
"backup.max.monthly",
87-
"8",
88-
"Maximum recurring monthly backups to be retained for an instance. If the limit is reached, backups from the beginning of the month are deleted so that newer ones can be saved. This limit does not apply to manual backups. If set to 0, recurring monthly backups can not be scheduled.",
89-
false,
90-
ConfigKey.Scope.Global,
91-
null);
92-
9361
ConfigKey<Long> DefaultMaxAccountBackups = new ConfigKey<Long>("Account Defaults", Long.class,
9462
"max.account.backups",
9563
"20",
@@ -201,10 +169,10 @@ public interface BackupManager extends BackupService, Configurable, PluggableSer
201169
/**
202170
* Creates backup of a VM
203171
* @param vmId Virtual Machine ID
204-
* @param scheduleId Virtual Machine Backup Schedule ID
172+
* @param job The async job associated with the backup retention
205173
* @return returns operation success
206174
*/
207-
boolean createBackup(final Long vmId, final Long scheduleId) throws ResourceAllocationException;
175+
boolean createBackup(final Long vmId, Object job) throws ResourceAllocationException;
208176

209177
/**
210178
* List existing backups for a VM

api/src/main/java/org/apache/cloudstack/backup/BackupSchedule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ public interface BackupSchedule extends InternalIdentity {
3030
String getTimezone();
3131
Date getScheduledTimestamp();
3232
Long getAsyncJobId();
33-
Integer getMaxBackups();
33+
int getMaxBackups();
3434
String getUuid();
3535
}

engine/schema/src/main/java/org/apache/cloudstack/backup/BackupScheduleVO.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ public class BackupScheduleVO implements BackupSchedule {
6363
Long asyncJobId;
6464

6565
@Column(name = "max_backups")
66-
Integer maxBackups = 0;
66+
private int maxBackups = 0;
6767

6868
public BackupScheduleVO() {
6969
}
7070

71-
public BackupScheduleVO(Long vmId, DateUtil.IntervalType scheduleType, String schedule, String timezone, Date scheduledTimestamp, Integer maxBackups) {
71+
public BackupScheduleVO(Long vmId, DateUtil.IntervalType scheduleType, String schedule, String timezone, Date scheduledTimestamp, int maxBackups) {
7272
this.vmId = vmId;
7373
this.scheduleType = (short) scheduleType.ordinal();
7474
this.schedule = schedule;
@@ -142,11 +142,11 @@ public void setAsyncJobId(Long asyncJobId) {
142142
this.asyncJobId = asyncJobId;
143143
}
144144

145-
public Integer getMaxBackups() {
145+
public int getMaxBackups() {
146146
return maxBackups;
147147
}
148148

149-
public void setMaxBackups(Integer maxBackups) {
149+
public void setMaxBackups(int maxBackups) {
150150
this.maxBackups = maxBackups;
151151
}
152152
}

engine/schema/src/main/java/org/apache/cloudstack/backup/BackupVO.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ public class BackupVO implements Backup {
8888
@Column(name = "zone_id")
8989
private long zoneId;
9090

91-
@Column(name = "backup_interval_type")
92-
private short backupIntervalType;
93-
9491
@Column(name = "backed_volumes", length = 65535)
9592
protected String backedUpVolumes;
9693

94+
@Column(name = "backup_schedule_id")
95+
private Long backupScheduleId;
96+
9797
public BackupVO() {
9898
this.uuid = UUID.randomUUID().toString();
9999
}
@@ -211,14 +211,6 @@ public void setZoneId(long zoneId) {
211211
this.zoneId = zoneId;
212212
}
213213

214-
public short getBackupIntervalType() {
215-
return backupIntervalType;
216-
}
217-
218-
public void setBackupIntervalType(short backupIntervalType) {
219-
this.backupIntervalType = backupIntervalType;
220-
}
221-
222214
@Override
223215
public Class<?> getEntityType() {
224216
return Backup.class;
@@ -247,4 +239,13 @@ public Date getRemoved() {
247239
public void setRemoved(Date removed) {
248240
this.removed = removed;
249241
}
242+
243+
@Override
244+
public Long getBackupScheduleId() {
245+
return backupScheduleId;
246+
}
247+
248+
public void setBackupScheduleId(Long backupScheduleId) {
249+
this.backupScheduleId = backupScheduleId;
250+
}
250251
}

engine/schema/src/main/java/org/apache/cloudstack/backup/dao/BackupDao.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ public interface BackupDao extends GenericDao<BackupVO, Long> {
3636
BackupVO getBackupVO(Backup backup);
3737
List<Backup> listByOfferingId(Long backupOfferingId);
3838

39-
List<BackupVO> listBackupsByVMandIntervalType(Long vmId, Backup.Type backupType);
40-
4139
BackupResponse newBackupResponse(Backup backup);
4240
public Long countBackupsForAccount(long accountId);
4341
public Long calculateBackupStorageForAccount(long accountId);
42+
List<BackupVO> listBySchedule(Long backupScheduleId);
4443
}

engine/schema/src/main/java/org/apache/cloudstack/backup/dao/BackupDaoImpl.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import javax.annotation.PostConstruct;
2525
import javax.inject.Inject;
2626

27+
import com.cloud.utils.db.Filter;
2728
import com.cloud.utils.db.GenericSearchBuilder;
2829
import org.apache.cloudstack.api.response.BackupResponse;
2930
import org.apache.cloudstack.backup.Backup;
@@ -63,7 +64,7 @@ public class BackupDaoImpl extends GenericDaoBase<BackupVO, Long> implements Bac
6364
private SearchBuilder<BackupVO> backupSearch;
6465
private GenericSearchBuilder<BackupVO, Long> CountBackupsByAccount;
6566
private GenericSearchBuilder<BackupVO, SumCount> CalculateBackupStorageByAccount;
66-
private SearchBuilder<BackupVO> ListBackupsByVMandIntervalType;
67+
private SearchBuilder<BackupVO> listBackupsBySchedule;
6768

6869
public BackupDaoImpl() {
6970
}
@@ -91,12 +92,11 @@ protected void init() {
9192
CalculateBackupStorageByAccount.and("removed", CalculateBackupStorageByAccount.entity().getRemoved(), SearchCriteria.Op.NULL);
9293
CalculateBackupStorageByAccount.done();
9394

94-
ListBackupsByVMandIntervalType = createSearchBuilder();
95-
ListBackupsByVMandIntervalType.and("vmId", ListBackupsByVMandIntervalType.entity().getVmId(), SearchCriteria.Op.EQ);
96-
ListBackupsByVMandIntervalType.and("intervalType", ListBackupsByVMandIntervalType.entity().getBackupIntervalType(), SearchCriteria.Op.EQ);
97-
ListBackupsByVMandIntervalType.and("status", ListBackupsByVMandIntervalType.entity().getStatus(), SearchCriteria.Op.EQ);
98-
ListBackupsByVMandIntervalType.and("removed", ListBackupsByVMandIntervalType.entity().getRemoved(), SearchCriteria.Op.NULL);
99-
ListBackupsByVMandIntervalType.done();
95+
listBackupsBySchedule = createSearchBuilder();
96+
listBackupsBySchedule.and("backup_schedule_id", listBackupsBySchedule.entity().getBackupScheduleId(), SearchCriteria.Op.EQ);
97+
listBackupsBySchedule.and("status", listBackupsBySchedule.entity().getStatus(), SearchCriteria.Op.EQ);
98+
listBackupsBySchedule.and("removed", listBackupsBySchedule.entity().getRemoved(), SearchCriteria.Op.NULL);
99+
listBackupsBySchedule.done();
100100
}
101101

102102
@Override
@@ -184,12 +184,11 @@ public Long calculateBackupStorageForAccount(long accountId) {
184184
}
185185

186186
@Override
187-
public List<BackupVO> listBackupsByVMandIntervalType(Long vmId, Backup.Type backupType) {
188-
SearchCriteria<BackupVO> sc = ListBackupsByVMandIntervalType.create();
189-
sc.setParameters("vmId", vmId);
190-
sc.setParameters("type", backupType.ordinal());
187+
public List<BackupVO> listBySchedule(Long backupScheduleId) {
188+
SearchCriteria<BackupVO> sc = listBackupsBySchedule.create();
189+
sc.setParameters("backup_schedule_id", backupScheduleId);
191190
sc.setParameters("status", Backup.Status.BackedUp);
192-
return listBy(sc, null);
191+
return listBy(sc, new Filter(BackupVO.class, "date", true));
193192
}
194193

195194
@Override

0 commit comments

Comments
 (0)