Project: Longhorn (Runtime / Cloud Native Storage)
File: 20210125-enhanced-cpu-reservation.md

To make sure Longhorn system stable enough, we can apply the node specific CPU resource reservation mechanism to avoid the engine/replica engine crash due to the CPU resource exhaustion. https://github.com/longhorn/longhorn/issues/2207 Reserve CPU resource separately for engine manager pods and replica manager pods. The reserved CPU count is node specific: The more allocable CPU resource a node has, the more CPUs be will reserved for the instance managers in general. Allow reserving CPU resource for a specific node. This setting will override the global setting. Guarantee that the CPU resource is always enough, or the CPU resource reservation is always reasonable based on the volume numbers of a node. Notify/Warn users CPU resource exhaustion on a node: https://github.com/longhorn/longhorn/issues/1930 Add new fields `node.Spec.EngineManagerCPURequest` and `node.Spec.ReplicaManagerCPURequest`. This allows reserving a different amount of CPU resource for a specific node. Add two settings `Guaranteed Engine Manager CPU` and `Guaranteed Replica Manager CPU`: It indicates how many percentages of CPU on a node will be reserved for one engine/replica manager pod. The old settings `Guaranteed Engine CPU` will be deprecated: This setting will be unset and readonly in the new version. For the old Longhorn system upgrade, Longhorn will automatically set the node fields based on the old setting then clean up the old setting, so that users don't need to do anything manually as well as not affect existing instance manager pods. Before the enhancement, users rely on the setting `Guaranteed Engine CPU` to reserve the same amount of CPU resource for all engine managers and all replica managers on all nodes. There is no way to reserve more CPUs for the instance managers on node having more allocable CPUs. After the enhancement, users can: Modify the global settings `Guaranteed Engine Manager CPU` and `Guaranteed Replica Manager CPU` to reserve how many percentage of CPUs for engine manager pods and replica manager pods, respectively. Set a different CPU value for the engine/replica manager pods on some particular nodes by `Node Edit`. Add a new field `EngineManagerCPURequest` and `ReplicaManagerCPURequest` for node objects. Add 2 new settings `Guaranteed Engine Manager CPU` and `Guaranteed Replica Manager CPU`. These 2 setting values are integers ranging from 0 to 40. The sum of the 2 setting values should be smaller than 40 (%) as well. In Node Controller, The requested CPU resource when creating an instance manager pod: Ignore the deprecated setting `Guaranteed Engine CPU`. If the newly introduced node field `node.Spec.EngineManagerCPURequest`/`node.Spec.ReplicaManagerCPURequest` is not empty, the engine/replica manager pod requested CPU is determined by the field value. Notice that the field value is a milli value. Else using the formula based on setting `Guaranteed Engine Manager CPU`/`Guaranteed Replica Manager CPU`: `The Reserved CPUs = The value of field "kubenode.status.allocatable.cpu" The setting values

In Setting Controller The current requested CPU of an instance manager pod should keep same as `node.Spec.EngineManagerCPURequest`/`node.Spec.ReplicaManagerCPURequest`, or the value calculated by the above formula. Otherwise, the pod will be killed then Node Controller will recreate it later. In upgrade Longhorn should update `node.Spec.EngineManagerCPURequest` and `node.Spec.ReplicaManagerCPURequest` based on setting `Guaranteed Engine CPU` then clean up `Guaranteed Engine CPU`. The fields 0 means Longhorn will use the setting values directly. The setting value 0 means removing the CPU requests for instance manager pods. Add 2 new arguments `Guaranteed Engine Manager CPU(Milli)` and `Guaranteed Replica Manager CPU(Milli)` in the node update page. Hide the deprecated setting `Guaranteed Engine CPU` which is type `Deprecated`. Type `Deprecated` is a newly introduced setting type. Update the existing test case `testsettingguaranteedenginecpu`: Validate the settings `Guaranteed Engine Manager CPU` controls the reserved CPUs of engine manager pods on each node. Validate the settings `Guaranteed Replica Manager CPU` controls the reserved CPUs of replica manager pods on each node. Validate that fields `node.Spec.EngineManagerCPURequest`/`node.Spec.ReplicaManagerCPURequest` can override the settings `Guaranteed Engine Manager CPU`/`Guaranteed Replica Manager CPU`. Deploy a cluster that each node has different CPUs. Launch Longhorn v1.1.0. Deploy some workloads using Longhorn volumes. Upgrade to the latest Longhorn version. Validate: all workloads work fine and no instance manager pod crash during the upgrade. The fields `node.Spec.EngineManagerCPURequest` and `node.Spec.ReplicaManagerCPURequest` of each node are the same as the setting `Guaranteed Engine CPU` value in the old version * 1000. The old setting `Guaranteed Engine CPU` is deprecated with an empty value. Modify new settings `Guaranteed Engine Manager CPU` and `Guaranteed Replica Manager CPU`. Validate all workloads work fine and no instance manager pod restart. Scale down all workloads and wait for the volume detachment. Set `node.Spec.EngineManagerCPURequest` and `node.Spec.ReplicaManagerCPURequest` to 0 for some node. Verify the new settings will be applied to those node and the related instance manager pods will be recreated with the CPU requests matching the new settings. Scale up all workloads and verify the data as well as the volume r/w. Do cleanup. Prepare 3 sets of longhorn-manager and longhorn-instance-manager images. Deploy Longhorn with the 1st set of images. Set `Guaranteed Engine Manager CPU` and `Guaranteed Replica Manager CPU` to 15 and 24, respectively. Then wait for the instance manager recreation. Create and attach a volume to a node (node1). Upgrade the Longhorn system with the 2nd set of images. Verify the CPU requests in the pods of both instance managers match the settings. Create and attach one more volume to node1. Upgrade the Longhorn system with the 3rd set of images. Verify the pods of the 3rd instance manager cannot be launched on node1 since there is no available CPU for the allocation. Detach the volume in the 1st instance manager pod. Verify the related instance manager pods will be cleaned up and the new instance manager pod can be launched on node1. N/A