Project: Longhorn (Runtime / Cloud Native Storage)
File: 20200821-rebuild-replica-with-existing-data.md

Longhorn could reuse the existing data of failed replicas to speed up rebuild progress as well as save bandwidth. https://github.com/longhorn/longhorn/issues/1304 The (data of) failed replicas can be reused during the replica rebuild. The rebuild won't be blocked when the data of failed replicas are completely corrupted, or there is no existing replica. With the existing data, some of the data transferring can be skipped, and replica rebuild may speed up. Add a new setting `ReplicaReplenishmentWaitInterval` to delay the replica rebuild. If the failed replica currently is unavailable but it may be able to be reused later(we call it potential reusable failed replica), Longhorn may need to delay the new replica replenishment so that there is a chance to reuse this kind of replica. For eviction/data locality/new volume cases, a new replica should be recreated immediately hence this setting won't be applied. In order to reuse the existing data, Longhorn can directly reuse the failed replica objects for the rebuild. Add max retry count for the replica rebuild with failed replicas. Otherwise, the rebuild will get stuck of the reusing the failed replicas there if the data of failed replicas are completely corrupted. Add backoff interval for the retry of the failed replica reuse. Before the enhancement, there is no chance to reuse the failed replicas on the node, and the rebuild can take a long time with heavy bandwidth usage. After the enhancement, the replica rebuild won't start until the new worker nodes with old disks are up. Then the failed replicas will be reused during the rebuild, and the rebuild can be pretty fast. Users don't need to do anything except for setting `ReplicaReplenishmentWaitInterval` No API change is required. Add a setting `ReplicaReplenishmentWaitInterval`. This will block the rebuilding when there is a failed replica that is temporarily unavailable in the volume. Add a field `volume.Status.LastDegradedAt` so that we can determine if `ReplicaReplenishmentWaitInterval` is passed. Add field `Replica.Spec.RebuildRetryCount` to indicate how many times Longhorn tries to reuse this failed replica for the rebuild. In Volume Controller && Replica Scheduler: Check if there is a reusable failed replica and if the replica reuse is not in the backoff window. If YES, directly try to reuse the failed replica. Otherwise, replenish a new replica is required for one of the following cases: the volume is a new volume (volume.Status.Robustness is Empty) data locality is required (hardNodeAffinity is not Empty and volume.Status.Robustness is Healthy) replica eviction happens (volume.Status.Robustness is Healthy) there is no potential reusable replica there is a potential reusable replica but the replica replenishment wait interval is passed. Reuse the failed replica by cleaning up `ReplicaSpec.HealthyAt` and `ReplicaSpec.FailedAt`. And `Replica.Spec.RebuildRetryCount` will be increased by 1. Clean up the related record in `Replica.Spec.RebuildRetryCount` when the rebuilding replica becomes mode `RW`. Guarantee the reused failed replica will be stopped before re-launching it. Set `ReplicaReplenishmentWaitInterval`. Make sure it's longer than the node recovery interval. Create and attach a large volume. Set a short `staleReplicaTimeout` for the volume, e.g., 1 minute. Write a large amount of data then take a snapshot. Repeat step 3 several times. Reboot/Temporarily disconnect a node contains replica only. According to the `ReplicaReplenishmentWaitInterval` and the node recovery interval: Verify the failed replica is reused and there is no new replica for the rebuild after the node recovery. Verify the replica rebuild only takes a relatively short time. Create and attach a large volume. Write data then take

Hack into one replica directory and make the directory and files read-only. Crash the related replica process and wait for the replica failure. Wait and check if Longhorn tries to reuse the corrupted replica but always fail. Since there is backoff mechanism, this will take a long time(8 ~ 10min). Check if Longhorn will create a new replica and succeeds to finish the rebuild when the max retry count is reached. Verify the data content. And check if the volume still works fine. Set `ReplicaReplenishmentWaitInterval` to 60s. Create and attach a large volume. Write data then take snapshots. Shut down a node containing replica only for 60s. Wait and check if Longhorn tries to reuse the failed replica for 2~3 times but always fail. Check if Longhorn will create a new replica once the replenishment wait interval is passed. Verify the data content. And check if the volume still works fine. Deploy Longhorn v1.0.2. Create and attach a volume. Write data to the volume. Disable scheduling for 1 node. Crash the replica on the node. Upgrade Longhorn to the latest. Verify the volume robustness `Degraded`. Enable scheduling for the node. Verify the failed replica of the existing degraded volume will be reused. Verify the data content, and the volume r/w still works fine. Deploy the latest Longhorn. Create and attach a volume. Write data to the volume. Update `Replica Replenishment Wait Interval` to 60s. Crash a replica: removing the volume head file and creating a directory with the volume head file name. Then the replica reuse will continuously fail. e.g., `rm volume-head-001.img && mkdir volume-head-001.img` Verify: There is a backoff interval for the failed replica reuse. A new replica will be created after (around) 60s despite the failed replica reuse is in backoff. the data content. the volume r/w still works fine. Set a long wait interval for setting `replica-replenishment-wait-interval`. Disable the setting soft node anti-affinity. Create and attach a volume. Then write data to the volume. Disable the scheduling for a node. Mess up the data of a random snapshot or the volume head for a replica. Then crash the replica on the node. --> Verify Longhorn won't create a new replica on the node for the volume. Update setting `replica-replenishment-wait-interval` to a small value. Verify Longhorn starts to create a new replica for the volume. Notice that the new replica scheduling will fail. Update setting `replica-replenishment-wait-interval` to a large value. Delete the newly created replica. --> Verify Longhorn won't create a new replica on the node for the volume. Enable the scheduling for the node. Verify the failed replica (in step 5) will be reused. Verify the volume r/w still works fine. Set a long wait interval for setting `replica-replenishment-wait-interval`. Disable the setting soft node anti-affinity. Add tags for all nodes and disks. Create and attach a volume with node and disk selectors. Then write data to the volume. Disable the scheduling for the 2 nodes (node1 and node2). Crash the replicas on the node1 and node2. --> Verify Longhorn won't create new replicas on the nodes. Remove tags for node1 and the related disks. Enable the scheduling for node1 and node2. Verify the only failed replica on node2 is reused. Add the tags back for node1 and the related disks. Verify the failed replica on node1 is reused. Verify the volume r/w still works fine. Need to update `volume.Status.LastDegradedAt` for existing degraded volumes during live upgrade.