Project: Longhorn (Runtime / Cloud Native Storage)
File: 20200721-refactor-restore-for-rebuild-enabling.md

This enhancement will refactor the restore implementation and enable rebuild for restore/DR volumes. https://github.com/longhorn/longhorn/issues/1279 The goal of this enhancement is to simplify the restore flow so that it can work for rebuilding replicas of restore/DR volumes without breaking the live upgrade feature. This enhancement won't guarantee that the restore/DR volume activation won't be blocked by replica rebuilding. When there are replicas crashing among restore/DR volumes, new rebuilding replicas will be created as usual. But instead of following the normal replica rebuilding workflow (syncing data/files from other running replicas), the rebuilding replicas of restore/DR volumes will directly restore data from backup. The normal rebuilding (file syncing) workflow implicitly considers that all existing snapshots won't change and newer data during the rebuilding will be written into volume head. But for restore/DR volumes, new data writing is directly handled by replica (sync agent server) and it will be written to underlying snapshots rather than volume heads. As a result, the normal rebuilding logic doesn't fit restore/DR volumes. In order to skip the file syncing and snapshotting and directly do restore, the rebuilding related API should be updated, which will lead to API version bumps. As long as there is a replica having not restored the newest/latest backup, longhorn manager will directly call restore command. Then rebuilding replicas will be able to start the restore even if all other replicas are up-to-date. Previously, in order to maintain the consistency of DR volume replicas, Longhorn manager will guarantee that all replicas have restored the same backup before starting the next backup restore. But considering the case that the newly rebuilt replicas are empty whereas the existing replicas have restored some backups, This restriction makes replica rebuilding become impossible in some cases. Hence we need to break the restriction. This restriction break degrades the consistency of DR volume replicas. But it's acceptable as long as all replicas can finish the latest backup restore and the DR volume can be activated in the end. This modification means engines and replicas should be intelligent enough to decide if they need to do restore and which kind of restore they need to launch. Actually replica processes have all information about the restore status and they can decide if they need incremental restore or full restore by themself. Specifying the last backup in the restore command is redundant. Longhorn manager only needs to tell the replicas what is the latest backup they should restore. Longhorn manager still need to know what is the last restored backup of all replicas, since it relies on it to determine if the restore/DR volume is available/can be activated. Longhorn should wait for rebuild complete and check restore status before auto detachment. Otherwise, the restore volume will be automatically detached when the rebuild is in progress then the rebuild is meaningless in this case. Before, the restore volume keeps state `Degraded` if there is replica crashing. And the volume will finally become `Faulted` if all replicas are crashed one by one during restoring. After, the restore volume will start replica rebuilding automatically then be back to state `Healthy` if there is replica crashing. The volume is available as long as all replicas are not crashed at the same time. And volume will finish activation/auto-detachment after the rebuild is done. Users create a restore volume and wait for restore complete. When the restore is in progress, some replicas somehow get

Then the volume rebuilds new replicas immediately, and it will become `Healthy` once the new replicas start rebuilding. The volume will be detached automatically once the restore and the rebuild complete. Users create a DR volume. Some replicas get crashed. Then the DR volume automatically rebuilds new replicas and restores the latest backup for the rebuilt replicas. Users try to activate the DR volume. The DR volume will wait for the rebuild of all replicas and successful restoration of the latest backup before detachment. Add a new flag `--restore` for command `add-replica`, which indicates skipping file syncing and snapshotting. Deprecate the arg `lastRestoreBackup` and the flag `--incrementally` for command `backup restore`. Add a new command `verify-rebuild-replica`, which can mark the rebuilding replicas as available (mode `RW`) for restore/DR volumes after the initial restore is done. Create a separate message/struct for `ReplicaCreate` request then add the two new fields `Mode` and `SnapshotRequired` to the request. Modify command `add-replica` related APIs: Use a new flag `--restore` in command `add-replica` to indicate that file syncing and snapshotting should be skipped for restore/DR volumes. The current controller gRPC call `ReplicaCreate` used in the command will directly create a snapshot before the rebuilding. But considering the (snapshot) consistency of of restore/DR volumes, snapshots creation/deletion is fully controlled by the restore command (and the expansion command during the restore). Hence, the snapshotting here needs to be skipped by updating the gRPC call `ReplicaCreate`. Add command `verify-rebuild-replica`: It just calls the existing controller gRPC function `ReplicaVerifyRebuild`. It's mainly used to mark the rebuilding replica of restore/DR volumes as mode `RW` with some verifications and a replica reload. Modify command `backup restore`: Deprecate/Ignore the arg `lastRestoreBackup` in the restore command and the following sync agent gRPC function. Instead, the sync agent server will directly do a full restore or a incremental restore based on its current restore status. Deprecate/Ignore the flag `--incrementally` for command `backup restore`. By checking the disk list of all existing replicas, the command function knows if it needs to generate a new snapshot name. The caller of the gRPC call `BackupRestore` only needs to tell the name of the final snapshot file that stores restored data. For new restore volume, there is no existing snapshot among all replicas hence we will generate a random snapshot name. For replicas of DR volumes or rebuilding replicas of restore volumes, the caller will find the replica containing the most snapshots then use the latest snapshot of the replica in the following restore. As for the delta file used in the incremental restore, it will be generated by the sync agent server rather than by the caller. Since the caller has no idea about the last restored backup now and the delta file naming format is `volume-delta-<last restored backup name>.img`. To avoid disk/snapshot chain inconsistency between rebuilt replicas and old replicas of a DR volume, snapshot purge is required if there are more than 1 snapshots in one replica. And the (incremental) restore will be blocked before the snapshot purge complete. Make the sync agent gRPC call `BackupRestore` more intelligent: The function will check the restore status first. If there is no restore record in the sync agent server or the last restored backup is invalid, a full restore will be applied. This means we can remove the gRPC call `BackupRestoreIncrementally`. Remove the expansion before the restore call. The expansion of DR volumes should be guaranteed by longhorn

Coalesce the incremental restore related functions to normal restore functions if possible. Allow replica replenishment for restore/DR volumes. Add the new flag `--restore` when using command `add-replica` to rebuild replicas of restore/DR volumes. Modify the pre-restore check and restore status sync logic: Previously, the restore command will be invoked only if there is no restoring replica. Right now the command will be called as long as there is a replica having not restored the latest backup. Do not apply the consensual check as the prerequisite of the restore command invocation. The consensual check will be used for `engine.Status.LastRestoredBackup` update only. Invoke `verify-rebuild-replica` when there is a complete restore for a rebuilding replica (mode `WO`). Modify the way to invoke restore command: Retain the old implementation for compatibility. For the engine using the new engine image, call restore command directly as long as the pre-restore check gets passed. Need to ignore some errors. e.g.: replicas are restoring, the requested backup restore is the same as the last backup restore, or replicas need to complete the snapshot purge before the restore. Mark the rebuilding replicas as mode `ERR` and disable the replica replenishment during the expansion. Modify the prerequisites of restore volume auto detachment or DR volume activation: Wait for the rebuild complete and the volume becoming `Healthy`. Check and wait for the snapshot purge. This prerequisite check works only for new restore/DR volumes. Create a restore volume with 2 replicas. Run command `backup restore` for the DR volume. Delete one replica of the restore volume. Initialize a new replica, and add the replica to the restore volume. Run command `backup restore`. Verify the restored data is correct, and all replicas work fine. Create a DR volume with 2 replicas. Run command `backup restore` for the DR volume. Wait for restore complete. Expand the DR volume and wait for the expansion complete. Delete one replica of the DR volume. Initialize a new replica, and add the replica to the DR volume. Run command `backup restore`. The old replica should start snapshot purge and the restore is actually not launched. Wait for the snapshot purge complete. Re-run command `backup restore`. Then wait for the restore complete. Check if the restored data is correct, and all replicas work fine. And verify all replicas contain only 1 snapshot. Launch a pod with Longhorn volume. Write data to the volume and take a backup. Create a restore volume from the backup and wait for the restore start. Crash one random replicas. Then check if the replicas will be rebuilt and the restore volume can be `Healthy` after the rebuilding. Wait for the restore complete and auto detachment. Launch a pod for the restored volume. Verify all replicas work fine with the correct data. Launch a pod with Longhorn volume. Write data to the volume and take the 1st backup. Wait for the 1st backup creation complete then write more data to the volume (which is the data of the 2nd backup). Create a DR volume from the 1st backup and wait for the restore start. Crash one random replica. Take the 2nd backup for the original volume. Then trigger DR volume last backup update immediately (by calling backup list API) after the 2nd backup creation complete. Check if the replicas will be rebuilt and the restore volume can be `Healthy` after the rebuilding. Wait for the restore complete then activate the volume. Launch a pod for the activated DR

Verify all replicas work fine with the correct data. Launch a pod with Longhorn volume. Write data to the volume and take the 1st backup. Create a DR volume from the 1st backup. Shutdown the pod and wait for the original volume detached. Expand the original volume and wait for the expansion complete. Re-launch a pod for the original volume. Write data to the original volume and take the 2nd backup. (Make sure the total data size is larger than the original volume size so that there is date written to the expanded part.) Wait for the 2nd backup creation complete. Trigger DR volume and crash one random replica of the DR volume. Check if the replicas will be rebuilt, and the restore volume can be `Healthy` after the rebuilding. Wait for the expansion, restore, and rebuild complete. Verify the DR volume size and snapshots count after the restore. Write data to the original volume and take the 3rd backup. Wait for the 3rd backup creation complete then trigger the incremental restore for the DR volume. Activate the DR volume and wait for the DR volume activated. Launch a pod for the activated DR volume. Verify the restored data of the activated DR volume. Write more data to the activated DR volume. Then verify all replicas are still running. Crash one random replica of the activated DR volume. Wait for the rebuild complete then verify the activated volume still works fine. Launch Longhorn v1.0.1. Launch a pod with Longhorn volume. Write data to the volume and take the 1st backup. Create 2 DR volumes from the 1st backup. Shutdown the pod and wait for the original volume detached. Expand the original volume and wait for the expansion complete. Write data to the original volume and take the 2nd backup. (Make sure the total data size is larger than the original volume size so that there is date written to the expanded part.) Trigger incremental restore for the DR volumes by listing the backup volumes, and wait for restore complete. Upgrade Longhorn to the latest version. Crash one random replica for the 1st DR volume . Verify the 1st DR volume won't rebuild replicas and keep state `Degraded`. Write data to the original volume and take the 3rd backup. Trigger incremental restore for the DR volumes, and wait for restore complete. Do live upgrade for the 1st DR volume. This live upgrade call should fail and nothing gets changed. Activate the 1st DR volume. Launch a pod for the 1st activated volume, and verify the restored data is correct. Do live upgrade for the original volume and the 2nd DR volumes. Crash one random replica for the 2nd DR volume. Wait for the restore & rebuild complete. Delete one replica for the 2nd DR volume, then activate the DR volume before the rebuild complete. Verify the DR volume will be auto detached after the rebuild complete. Launch a pod for the 2nd activated volume, and verify the restored data is correct. Crash one replica for the 2nd activated volume. Wait for the rebuild complete, then verify the volume still works fine by reading/writing more data. Live upgrade is supported. It's possible that the restore/DR volume rebuilding somehow gets stuck, or users have no time to wait for the restore/DR volume rebuilding done. We need to provide a way that users can use the volume as soon as possible. This enhancement is tracked in https://github.com/longhorn/longhorn/issues/1512.