-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#### What type of PR is this? /kind feature /area core #### What this PR does / why we need it: See https://github.com/JohnNiang/halo/blob/9921deb0768cb22fb4ba738e20fdc099f1678926/docs/backup-and-restore.md for more. <img width="1906" alt="image" src="https://github.com/halo-dev/halo/assets/21301288/41531186-d305-44fd-8bdc-30df9b71af43"> <img width="1909" alt="image" src="https://github.com/halo-dev/halo/assets/21301288/3d7af1b9-37ad-4a40-9b81-f15ed0f1f6e8"> #### Which issue(s) this PR fixes: Fixes #4059 Fixes #3274 #### Special notes for your reviewer: #### Does this PR introduce a user-facing change? ```release-note 支持备份和恢复功能。 ```
- Loading branch information
Showing
49 changed files
with
3,865 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
application/src/main/java/run/halo/app/migration/Backup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package run.halo.app.migration; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import java.time.Instant; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.ToString; | ||
import run.halo.app.extension.AbstractExtension; | ||
import run.halo.app.extension.GVK; | ||
|
||
@Data | ||
@ToString(callSuper = true) | ||
@EqualsAndHashCode(callSuper = true) | ||
@GVK(group = "migration.halo.run", version = "v1alpha1", kind = "Backup", | ||
plural = "backups", singular = "backup") | ||
public class Backup extends AbstractExtension { | ||
|
||
private Spec spec = new Spec(); | ||
|
||
private Status status = new Status(); | ||
|
||
@Data | ||
@Schema(name = "BackupSpec") | ||
public static class Spec { | ||
|
||
@Schema(description = "Backup file format. Currently, only zip format is supported.") | ||
private String format; | ||
|
||
private Instant expiresAt; | ||
|
||
} | ||
|
||
@Data | ||
@Schema(name = "BackupStatus") | ||
public static class Status { | ||
|
||
private Phase phase = Phase.PENDING; | ||
|
||
private Instant startTimestamp; | ||
|
||
private Instant completionTimestamp; | ||
|
||
private String failureReason; | ||
|
||
private String failureMessage; | ||
|
||
/** | ||
* Size of backup file. Data unit: byte | ||
*/ | ||
private Long size; | ||
|
||
/** | ||
* Name of backup file. | ||
*/ | ||
private String filename; | ||
} | ||
|
||
public enum Phase { | ||
PENDING, | ||
RUNNING, | ||
SUCCEEDED, | ||
FAILED, | ||
} | ||
|
||
} |
133 changes: 133 additions & 0 deletions
133
application/src/main/java/run/halo/app/migration/BackupReconciler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package run.halo.app.migration; | ||
|
||
import static run.halo.app.extension.ExtensionUtil.addFinalizers; | ||
import static run.halo.app.extension.ExtensionUtil.isDeleted; | ||
import static run.halo.app.extension.ExtensionUtil.removeFinalizers; | ||
import static run.halo.app.extension.controller.Reconciler.Result.doNotRetry; | ||
import static run.halo.app.migration.Constant.HOUSE_KEEPER_FINALIZER; | ||
|
||
import java.time.Clock; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.Set; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import reactor.core.Exceptions; | ||
import run.halo.app.extension.ExtensionClient; | ||
import run.halo.app.extension.controller.Controller; | ||
import run.halo.app.extension.controller.ControllerBuilder; | ||
import run.halo.app.extension.controller.Reconciler; | ||
import run.halo.app.extension.controller.Reconciler.Request; | ||
import run.halo.app.migration.Backup.Phase; | ||
|
||
@Slf4j | ||
@Component | ||
public class BackupReconciler implements Reconciler<Request> { | ||
|
||
private final ExtensionClient client; | ||
|
||
private final MigrationService migrationService; | ||
|
||
private Clock clock; | ||
|
||
public BackupReconciler(ExtensionClient client, MigrationService migrationService) { | ||
this.client = client; | ||
this.migrationService = migrationService; | ||
clock = Clock.systemDefaultZone(); | ||
} | ||
|
||
/** | ||
* Set clock. The method is only for unit test. | ||
* | ||
* @param clock is new clock | ||
*/ | ||
void setClock(Clock clock) { | ||
this.clock = clock; | ||
} | ||
|
||
@Override | ||
public Result reconcile(Request request) { | ||
return client.fetch(Backup.class, request.name()) | ||
.map(backup -> { | ||
var metadata = backup.getMetadata(); | ||
var status = backup.getStatus(); | ||
var spec = backup.getSpec(); | ||
if (isDeleted(backup)) { | ||
if (removeFinalizers(metadata, Set.of(HOUSE_KEEPER_FINALIZER))) { | ||
migrationService.cleanup(backup).block(); | ||
client.update(backup); | ||
} | ||
return doNotRetry(); | ||
} | ||
if (addFinalizers(metadata, Set.of(HOUSE_KEEPER_FINALIZER))) { | ||
client.update(backup); | ||
} | ||
|
||
if (Phase.PENDING.equals(status.getPhase())) { | ||
// Do backup | ||
try { | ||
status.setPhase(Phase.RUNNING); | ||
status.setStartTimestamp(Instant.now(clock)); | ||
updateStatus(request.name(), status); | ||
// Long period execution when backing up | ||
migrationService.backup(backup).block(); | ||
status.setPhase(Phase.SUCCEEDED); | ||
status.setCompletionTimestamp(Instant.now(clock)); | ||
updateStatus(request.name(), status); | ||
} catch (Throwable t) { | ||
var unwrapped = Exceptions.unwrap(t); | ||
log.error("Failed to backup", unwrapped); | ||
// Only happen when shutting down | ||
status.setPhase(Phase.FAILED); | ||
if (unwrapped instanceof InterruptedException) { | ||
status.setFailureReason("Interrupted"); | ||
status.setFailureMessage("The backup process was interrupted."); | ||
} else { | ||
status.setFailureReason("SystemError"); | ||
status.setFailureMessage( | ||
"Something went wrong! Error message: " + unwrapped.getMessage()); | ||
} | ||
updateStatus(request.name(), status); | ||
} | ||
} | ||
// Only happen when failing to update status when interrupted | ||
if (Phase.RUNNING.equals(status.getPhase())) { | ||
status.setPhase(Phase.FAILED); | ||
status.setFailureReason("UnexpectedExit"); | ||
status.setFailureMessage("The backup process may exit abnormally."); | ||
updateStatus(request.name(), status); | ||
} | ||
// Check the expires at and requeue if necessary | ||
if (isTerminal(status.getPhase())) { | ||
var expiresAt = spec.getExpiresAt(); | ||
if (expiresAt != null) { | ||
var now = Instant.now(clock); | ||
if (now.isBefore(expiresAt)) { | ||
return new Result(true, Duration.between(now, expiresAt)); | ||
} | ||
client.delete(backup); | ||
} | ||
} | ||
return doNotRetry(); | ||
}).orElseGet(Result::doNotRetry); | ||
} | ||
|
||
private void updateStatus(String name, Backup.Status status) { | ||
client.fetch(Backup.class, name) | ||
.ifPresent(backup -> { | ||
backup.setStatus(status); | ||
client.update(backup); | ||
}); | ||
} | ||
|
||
private static boolean isTerminal(Phase phase) { | ||
return Phase.FAILED.equals(phase) || Phase.SUCCEEDED.equals(phase); | ||
} | ||
|
||
@Override | ||
public Controller setupWith(ControllerBuilder builder) { | ||
return builder | ||
.extension(new Backup()) | ||
.build(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
application/src/main/java/run/halo/app/migration/Constant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package run.halo.app.migration; | ||
|
||
public enum Constant { | ||
; | ||
|
||
public static final String GROUP = "migration.halo.run"; | ||
|
||
public static final String VERSION = "v1alpha1"; | ||
|
||
public static final String HOUSE_KEEPER_FINALIZER = "housekeeper"; | ||
|
||
} |
Oops, something went wrong.