mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(backup): resume unfinished jobs after restart (#5721)
* fix(backup): resume unfinished jobs after restart * fix(backup): address recovery review feedback Coordinate recovery-point deletion and completion, increment new-point counts, and validate vault creation identity during recovery. Local focused validation: 92 tests, four additional persistence/restart checks, and Checkstyle. Existing timestamp-granularity and legacy compatibility bounds remain. * fix(backup): simplify recovery identity and deadline tests Address maintainer feedback with the exact independently reviewed local candidate. Focused affected-area checks passed; remote CI and maintainer acceptance remain outstanding.
This commit is contained in:
1 parent
74124690b8
commit
6d96849d2a
7 files changed
+970
-89
No files matched your search
@@ -416,7 +416,7 @@ public class BackupController {
|
||||
@GET
|
||||
@Path("/backup-jobs/{backupJobId}")
|
||||
public Response describeBackupJob(@PathParam("backupJobId") String jobId) {
|
||||
return Response.ok(service.describeBackupJob(jobId)).build();
|
||||
return Response.ok(jobJson(service.describeBackupJob(jobId))).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@@ -435,10 +435,16 @@ public class BackupController {
|
||||
List<BackupJob> jobs = service.listBackupJobs(byVaultName, byState, byResourceArn, byResourceType);
|
||||
ObjectNode out = objectMapper.createObjectNode();
|
||||
ArrayNode list = out.putArray("BackupJobs");
|
||||
jobs.forEach(list::addPOJO);
|
||||
jobs.forEach(job -> list.add(jobJson(job)));
|
||||
return Response.ok(out).build();
|
||||
}
|
||||
|
||||
private ObjectNode jobJson(BackupJob job) {
|
||||
ObjectNode node = objectMapper.valueToTree(job);
|
||||
node.remove(BackupVault.VAULT_ID_FIELD);
|
||||
return node;
|
||||
}
|
||||
|
||||
// ── Recovery Point ─────────────────────────────────────────────────────────
|
||||
|
||||
@GET
|
||||
|
||||
@@ -8,11 +8,21 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.backup.model.*;
|
||||
import io.github.hectorvent.floci.services.backup.model.BackupJob;
|
||||
import io.github.hectorvent.floci.services.backup.model.BackupPlan;
|
||||
import io.github.hectorvent.floci.services.backup.model.BackupRule;
|
||||
import io.github.hectorvent.floci.services.backup.model.BackupSelection;
|
||||
import io.github.hectorvent.floci.services.backup.model.BackupVault;
|
||||
import io.github.hectorvent.floci.services.backup.model.BackupVaultNotifications;
|
||||
import io.github.hectorvent.floci.services.backup.model.Lifecycle;
|
||||
import io.github.hectorvent.floci.services.backup.model.RecoveryPoint;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;
|
||||
import io.quarkus.runtime.Startup;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -21,13 +31,19 @@ import org.jboss.logging.Logger;
|
||||
import java.time.Instant;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@ApplicationScoped
|
||||
@Startup
|
||||
public class BackupService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(BackupService.class);
|
||||
@@ -40,7 +56,7 @@ public class BackupService {
|
||||
private final StorageBackend<String, BackupVault> vaultStore;
|
||||
private final StorageBackend<String, BackupPlan> planStore;
|
||||
private final StorageBackend<String, BackupSelection> selectionStore;
|
||||
private final StorageBackend<String, BackupJob> jobStore;
|
||||
private final AccountAwareStorageBackend<BackupJob> jobStore;
|
||||
private final StorageBackend<String, RecoveryPoint> recoveryStore;
|
||||
private final StorageBackend<String, String> accessPolicyStore;
|
||||
private final StorageBackend<String, BackupVaultNotifications> notificationStore;
|
||||
@@ -86,6 +102,53 @@ public class BackupService {
|
||||
scheduler.shutdownNow();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
void resumeJobs() {
|
||||
for (AccountAwareStorageBackend.AccountEntry<BackupJob> entry : jobStore.scanAllAccountEntries(k -> true)) {
|
||||
BackupJob job = entry.value();
|
||||
if (!List.of("CREATED", "RUNNING", "ABORTING").contains(job.getState())) {
|
||||
continue;
|
||||
}
|
||||
String region = AwsArnUtils.regionOrDefault(job.getBackupVaultArn(), regionResolver.getDefaultRegion());
|
||||
if ("ABORTING".equals(job.getState())) {
|
||||
scheduler.execute(() -> RequestScopes.runAs(entry.accountId(), region, () -> abortJob(entry.key())));
|
||||
} else {
|
||||
scheduleJob(entry.key(), job, entry.accountId(), region);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void scheduleJob(String jobId, BackupJob job, String accountId, String region) {
|
||||
long now = Instant.now().getEpochSecond();
|
||||
if ("CREATED".equals(job.getState()) && job.getStartBy() != null && job.getStartBy() < now) {
|
||||
scheduler.execute(() -> RequestScopes.runAs(accountId, region, () -> {
|
||||
synchronized (lockFor(jobLockKey(jobId))) {
|
||||
jobStore.get(jobId).ifPresent(current -> {
|
||||
if ("CREATED".equals(current.getState())) {
|
||||
current.setState("EXPIRED");
|
||||
current.setStatusMessage("The job did not start before its start window expired");
|
||||
current.setCompletionDate(Instant.now().getEpochSecond());
|
||||
jobStore.put(jobId, current);
|
||||
}
|
||||
});
|
||||
}
|
||||
}));
|
||||
return;
|
||||
}
|
||||
long completion = job.getExpectedCompletionDate() == null
|
||||
? job.getCreationDate() + jobCompletionDelaySeconds : job.getExpectedCompletionDate();
|
||||
scheduler.schedule(() -> RequestScopes.runAs(accountId, region,
|
||||
() -> transitionJob(jobId, job.getBackupVaultName(), region)),
|
||||
Math.max(0, Math.min(job.getCreationDate() + 1, completion) - now), TimeUnit.SECONDS);
|
||||
scheduler.schedule(() -> RequestScopes.runAs(accountId, region,
|
||||
() -> completeJob(jobId, job.getBackupVaultName(), region)),
|
||||
Math.max(0, completion - now), TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
private String jobLockKey(String jobId) {
|
||||
return "job:" + regionResolver.getAccountId() + ":" + jobId;
|
||||
}
|
||||
|
||||
// ── Vault ──────────────────────────────────────────────────────────────────
|
||||
|
||||
public BackupVault createBackupVault(String vaultName, String encryptionKeyArn,
|
||||
@@ -653,6 +716,7 @@ public class BackupService {
|
||||
job.setBackupJobId(jobId);
|
||||
job.setBackupVaultName(vaultName);
|
||||
job.setBackupVaultArn(vault.getBackupVaultArn());
|
||||
job.setVaultId(vault.getVaultId());
|
||||
job.setResourceArn(resourceArn);
|
||||
job.setResourceType(inferResourceType(resourceArn));
|
||||
job.setIamRoleArn(iamRoleArn);
|
||||
@@ -664,16 +728,7 @@ public class BackupService {
|
||||
String accountId = regionResolver.getAccountId();
|
||||
job.setAccountId(accountId);
|
||||
jobStore.put(jobId, job);
|
||||
// Pinned now, not looked up by name at completion: if this vault is deleted and the name
|
||||
// reused while the job runs, the job must not notify the replacement vault's topic.
|
||||
String notificationKey = subResourceKey(region, vault);
|
||||
|
||||
// The scheduler thread has no request context; run in the caller's account
|
||||
// so the account-aware stores and buildArn resolve the right partition.
|
||||
scheduler.schedule(() -> RequestScopes.runAs(accountId,
|
||||
() -> transitionJob(jobId, vaultName, region)), 1, TimeUnit.SECONDS);
|
||||
scheduler.schedule(() -> RequestScopes.runAs(accountId,
|
||||
() -> completeJob(jobId, vaultName, region, notificationKey)), jobCompletionDelaySeconds, TimeUnit.SECONDS);
|
||||
scheduleJob(jobId, job, accountId, region);
|
||||
|
||||
return job;
|
||||
}
|
||||
@@ -684,17 +739,20 @@ public class BackupService {
|
||||
}
|
||||
|
||||
public void stopBackupJob(String jobId) {
|
||||
BackupJob job = describeBackupJob(jobId);
|
||||
String state = job.getState();
|
||||
if ("COMPLETED".equals(state) || "ABORTED".equals(state) || "FAILED".equals(state)) {
|
||||
throw new AwsException("InvalidRequestException",
|
||||
"Job cannot be stopped in state: " + state, 400);
|
||||
synchronized (lockFor(jobLockKey(jobId))) {
|
||||
BackupJob job = describeBackupJob(jobId);
|
||||
String state = job.getState();
|
||||
if (List.of("COMPLETED", "ABORTED", "FAILED", "EXPIRED", "PARTIAL").contains(state)) {
|
||||
throw new AwsException("InvalidRequestException",
|
||||
"Job cannot be stopped in state: " + state, 400);
|
||||
}
|
||||
job.setState("ABORTING");
|
||||
job.setStatusMessage("Job stop requested");
|
||||
jobStore.put(jobId, job);
|
||||
String accountId = regionResolver.getAccountId();
|
||||
String region = AwsArnUtils.regionOrDefault(job.getBackupVaultArn(), regionResolver.getDefaultRegion());
|
||||
scheduler.schedule(() -> RequestScopes.runAs(accountId, region, () -> abortJob(jobId)), 1, TimeUnit.SECONDS);
|
||||
}
|
||||
job.setState("ABORTING");
|
||||
job.setStatusMessage("Job stop requested");
|
||||
jobStore.put(jobId, job);
|
||||
String accountId = regionResolver.getAccountId();
|
||||
scheduler.schedule(() -> RequestScopes.runAs(accountId, () -> abortJob(jobId)), 1, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public List<BackupJob> listBackupJobs(String byVaultName, String byState,
|
||||
@@ -743,20 +801,22 @@ public class BackupService {
|
||||
* passes would be describing AWS behaviour that needs a specific permission to reproduce.
|
||||
*/
|
||||
public void deleteRecoveryPoint(String vaultName, String recoveryPointArn, String region) {
|
||||
BackupVault vault = describeBackupVault(vaultName, region);
|
||||
RecoveryPoint rp = describeRecoveryPoint(vaultName, recoveryPointArn, region);
|
||||
if (vault.isLocked() && vault.getMinRetentionDays() != null) {
|
||||
long ageDays = ChronoUnit.DAYS.between(
|
||||
Instant.ofEpochSecond(rp.getCreationDate()), Instant.now());
|
||||
if (ageDays < vault.getMinRetentionDays()) {
|
||||
throw new AwsException("InvalidRequestException",
|
||||
"Recovery point is protected by a vault lock until its minimum retention of "
|
||||
+ vault.getMinRetentionDays() + " day(s) has elapsed: " + recoveryPointArn,
|
||||
400);
|
||||
synchronized (lockFor(vaultKey(region, vaultName))) {
|
||||
BackupVault vault = describeBackupVault(vaultName, region);
|
||||
RecoveryPoint rp = describeRecoveryPoint(vaultName, recoveryPointArn, region);
|
||||
if (vault.isLocked() && vault.getMinRetentionDays() != null) {
|
||||
long ageDays = ChronoUnit.DAYS.between(
|
||||
Instant.ofEpochSecond(rp.getCreationDate()), Instant.now());
|
||||
if (ageDays < vault.getMinRetentionDays()) {
|
||||
throw new AwsException("InvalidRequestException",
|
||||
"Recovery point is protected by a vault lock until its minimum retention of "
|
||||
+ vault.getMinRetentionDays() + " day(s) has elapsed: " + recoveryPointArn,
|
||||
400);
|
||||
}
|
||||
}
|
||||
recoveryStore.delete(recoveryPointArn);
|
||||
decrementVaultCount(vaultName, region);
|
||||
}
|
||||
recoveryStore.delete(recoveryPointArn);
|
||||
decrementVaultCount(vaultName, region);
|
||||
}
|
||||
|
||||
// ── Tags ───────────────────────────────────────────────────────────────────
|
||||
@@ -782,24 +842,28 @@ public class BackupService {
|
||||
// ── Private helpers ────────────────────────────────────────────────────────
|
||||
|
||||
private void transitionJob(String jobId, String vaultName, String region) {
|
||||
jobStore.get(jobId).ifPresent(job -> {
|
||||
if ("CREATED".equals(job.getState())) {
|
||||
job.setState("RUNNING");
|
||||
job.setPercentDone("50.0");
|
||||
jobStore.put(jobId, job);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void completeJob(String jobId, String vaultName, String region, String notificationKey) {
|
||||
// Under the vault-name monitor, so the vault cannot be deleted or recreated between the
|
||||
// incarnation check and the recovery point landing in it.
|
||||
synchronized (lockFor(vaultKey(region, vaultName))) {
|
||||
completeJobLocked(jobId, vaultName, region, notificationKey);
|
||||
synchronized (lockFor(jobLockKey(jobId))) {
|
||||
jobStore.get(jobId).ifPresent(job -> {
|
||||
if ("CREATED".equals(job.getState())) {
|
||||
job.setState("RUNNING");
|
||||
job.setPercentDone("50.0");
|
||||
jobStore.put(jobId, job);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void completeJobLocked(String jobId, String vaultName, String region, String notificationKey) {
|
||||
private void completeJob(String jobId, String vaultName, String region) {
|
||||
// Under the vault-name monitor, so the vault cannot be deleted or recreated between the
|
||||
// incarnation check and the recovery point landing in it.
|
||||
synchronized (lockFor(jobLockKey(jobId))) {
|
||||
synchronized (lockFor(vaultKey(region, vaultName))) {
|
||||
completeJobLocked(jobId, vaultName, region);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void completeJobLocked(String jobId, String vaultName, String region) {
|
||||
jobStore.get(jobId).ifPresent(job -> {
|
||||
if ("RUNNING".equals(job.getState())) {
|
||||
long now = Instant.now().getEpochSecond();
|
||||
@@ -807,9 +871,10 @@ public class BackupService {
|
||||
// vault holds the name now. A vault deleted mid-job (allowed: it held no recovery
|
||||
// point yet) and recreated unlocked would otherwise receive a recovery point its
|
||||
// creator's lock was meant to protect, deletable inside that lock's retention.
|
||||
boolean sameVault = vaultStore.get(vaultKey(region, vaultName))
|
||||
.map(v -> subResourceKey(region, v).equals(notificationKey))
|
||||
.orElse(false);
|
||||
Optional<BackupVault> vault = vaultStore.get(vaultKey(region, vaultName));
|
||||
boolean sameVault = vault.filter(v -> v.getBackupVaultArn().equals(job.getBackupVaultArn())
|
||||
&& (job.getVaultId() != null ? job.getVaultId().equals(v.getVaultId())
|
||||
: v.getCreationDate() <= job.getCreationDate())).isPresent();
|
||||
if (!sameVault) {
|
||||
job.setState("FAILED");
|
||||
job.setStatusMessage("Backup vault was deleted before the job completed: " + vaultName);
|
||||
@@ -817,9 +882,31 @@ public class BackupService {
|
||||
jobStore.put(jobId, job);
|
||||
return;
|
||||
}
|
||||
String rpArn = regionResolver.buildArn("backup", region,
|
||||
"recovery-point:" + UUID.randomUUID());
|
||||
|
||||
BackupVault currentVault = vault.orElseThrow();
|
||||
String notificationKey = subResourceKey(region, currentVault);
|
||||
String rpArn = job.getRecoveryPointArn() != null ? job.getRecoveryPointArn()
|
||||
: regionResolver.buildArn("backup", region, "recovery-point:" + jobId);
|
||||
if (recoveryStore.get(rpArn).isEmpty()) {
|
||||
RecoveryPoint rp = new RecoveryPoint();
|
||||
rp.setRecoveryPointArn(rpArn);
|
||||
rp.setBackupVaultName(vaultName);
|
||||
rp.setBackupVaultArn(job.getBackupVaultArn());
|
||||
rp.setResourceArn(job.getResourceArn());
|
||||
rp.setResourceType(job.getResourceType());
|
||||
rp.setIamRoleArn(job.getIamRoleArn());
|
||||
rp.setStatus("COMPLETED");
|
||||
rp.setCreationDate(job.getCreationDate());
|
||||
rp.setCompletionDate(now);
|
||||
rp.setBackupSizeInBytes(0L);
|
||||
rp.setStorageClass("WARM");
|
||||
rp.setEncrypted(false);
|
||||
recoveryStore.put(rpArn, rp);
|
||||
currentVault.setNumberOfRecoveryPoints(currentVault.getNumberOfRecoveryPoints() + 1);
|
||||
} else {
|
||||
currentVault.setNumberOfRecoveryPoints((int) recoveryStore.scan(k -> true).stream()
|
||||
.filter(point -> currentVault.getBackupVaultArn().equals(point.getBackupVaultArn())).count());
|
||||
}
|
||||
vaultStore.put(vaultKey(region, vaultName), currentVault);
|
||||
job.setState("COMPLETED");
|
||||
job.setPercentDone("100.0");
|
||||
job.setCompletionDate(now);
|
||||
@@ -827,23 +914,6 @@ public class BackupService {
|
||||
job.setBackupSizeInBytes(0L);
|
||||
job.setBytesTransferred(0L);
|
||||
jobStore.put(jobId, job);
|
||||
|
||||
RecoveryPoint rp = new RecoveryPoint();
|
||||
rp.setRecoveryPointArn(rpArn);
|
||||
rp.setBackupVaultName(vaultName);
|
||||
rp.setBackupVaultArn(job.getBackupVaultArn());
|
||||
rp.setResourceArn(job.getResourceArn());
|
||||
rp.setResourceType(job.getResourceType());
|
||||
rp.setIamRoleArn(job.getIamRoleArn());
|
||||
rp.setStatus("COMPLETED");
|
||||
rp.setCreationDate(job.getCreationDate());
|
||||
rp.setCompletionDate(now);
|
||||
rp.setBackupSizeInBytes(0L);
|
||||
rp.setStorageClass("WARM");
|
||||
rp.setEncrypted(false);
|
||||
recoveryStore.put(rpArn, rp);
|
||||
|
||||
incrementVaultCount(vaultName, region);
|
||||
LOG.infov("Backup job {0} completed, recovery point: {1}", jobId, rpArn);
|
||||
notifyJobCompleted(job, vaultName, region, notificationKey);
|
||||
}
|
||||
@@ -885,20 +955,15 @@ public class BackupService {
|
||||
}
|
||||
|
||||
private void abortJob(String jobId) {
|
||||
jobStore.get(jobId).ifPresent(job -> {
|
||||
if ("ABORTING".equals(job.getState())) {
|
||||
job.setState("ABORTED");
|
||||
job.setCompletionDate(Instant.now().getEpochSecond());
|
||||
jobStore.put(jobId, job);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void incrementVaultCount(String vaultName, String region) {
|
||||
vaultStore.get(vaultKey(region, vaultName)).ifPresent(vault -> {
|
||||
vault.setNumberOfRecoveryPoints(vault.getNumberOfRecoveryPoints() + 1);
|
||||
vaultStore.put(vaultKey(region, vaultName), vault);
|
||||
});
|
||||
synchronized (lockFor(jobLockKey(jobId))) {
|
||||
jobStore.get(jobId).ifPresent(job -> {
|
||||
if ("ABORTING".equals(job.getState())) {
|
||||
job.setState("ABORTED");
|
||||
job.setCompletionDate(Instant.now().getEpochSecond());
|
||||
jobStore.put(jobId, job);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void decrementVaultCount(String vaultName, String region) {
|
||||
|
||||
@@ -19,6 +19,9 @@ public class BackupJob {
|
||||
@JsonProperty("BackupVaultArn")
|
||||
private String backupVaultArn;
|
||||
|
||||
@JsonProperty("FlociVaultId")
|
||||
private String vaultId;
|
||||
|
||||
@JsonProperty("RecoveryPointArn")
|
||||
private String recoveryPointArn;
|
||||
|
||||
@@ -72,6 +75,9 @@ public class BackupJob {
|
||||
public String getBackupVaultArn() { return backupVaultArn; }
|
||||
public void setBackupVaultArn(String backupVaultArn) { this.backupVaultArn = backupVaultArn; }
|
||||
|
||||
public String getVaultId() { return vaultId; }
|
||||
public void setVaultId(String vaultId) { this.vaultId = vaultId; }
|
||||
|
||||
public String getRecoveryPointArn() { return recoveryPointArn; }
|
||||
public void setRecoveryPointArn(String recoveryPointArn) { this.recoveryPointArn = recoveryPointArn; }
|
||||
|
||||
|
||||
Reference in new issue
Block a user