Skip to content

Commit

Permalink
feat(qrm): make adaption for change of qos level and enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
csfldf committed Dec 12, 2023
1 parent 454bc85 commit a957d90
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ func (p *DynamicPolicy) sharedCoresAllocationHandler(_ context.Context,

needSet := true
allocationInfo := p.state.GetAllocationInfo(req.PodUid, req.ContainerName)
err = updateAllocationInfoByReq(req, allocationInfo)
if err != nil {
general.Errorf("pod: %s/%s, container: %s updateAllocationInfoByReq failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
return nil, fmt.Errorf("updateAllocationInfoByReq failed with error: %v", err)
}

if allocationInfo == nil {
general.Infof("pod: %s/%s, container: %s is met firstly, do ramp up with pooled cpus: %s",
Expand Down Expand Up @@ -168,6 +174,13 @@ func (p *DynamicPolicy) reclaimedCoresAllocationHandler(_ context.Context,
}

allocationInfo := p.state.GetAllocationInfo(req.PodUid, req.ContainerName)
err = updateAllocationInfoByReq(req, allocationInfo)
if err != nil {
general.Errorf("pod: %s/%s, container: %s updateAllocationInfoByReq failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
return nil, fmt.Errorf("updateAllocationInfoByReq failed with error: %v", err)
}

reclaimedAllocationInfo := p.state.GetAllocationInfo(state.PoolNameReclaim, advisorapi.FakedContainerName)
if reclaimedAllocationInfo == nil {
general.Errorf("allocation for pod: %s/%s, container: %s is failed, because pool: %s is not ready",
Expand Down Expand Up @@ -206,6 +219,7 @@ func (p *DynamicPolicy) reclaimedCoresAllocationHandler(_ context.Context,
}
}

allocationInfo.OwnerPoolName = state.PoolNameReclaim
allocationInfo.AllocationResult = reclaimedAllocationInfo.AllocationResult.Clone()
allocationInfo.OriginalAllocationResult = reclaimedAllocationInfo.OriginalAllocationResult.Clone()
allocationInfo.TopologyAwareAssignments = machine.DeepcopyCPUAssignment(reclaimedAllocationInfo.TopologyAwareAssignments)
Expand Down
26 changes: 26 additions & 0 deletions pkg/agent/qrm-plugins/cpu/dynamicpolicy/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ limitations under the License.
package dynamicpolicy

import (
"fmt"

pluginapi "k8s.io/kubelet/pkg/apis/resourceplugin/v1alpha1"

apiconsts "github.com/kubewharf/katalyst-api/pkg/consts"
cpuconsts "github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/consts"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/dynamicpolicy/state"
"github.com/kubewharf/katalyst-core/pkg/util/general"
"github.com/kubewharf/katalyst-core/pkg/util/machine"
)

Expand All @@ -29,3 +35,23 @@ func getProportionalSize(oldPoolSize, oldTotalSize, newTotalSize int) int {
func generateMachineStateFromPodEntries(topology *machine.CPUTopology, podEntries state.PodEntries) (state.NUMANodeMap, error) {
return state.GenerateMachineStateFromPodEntries(topology, podEntries, cpuconsts.CPUResourcePluginPolicyNameDynamic)
}

// updateAllocationInfoByReq updates allocationInfo by latest req when admitting active pod,
// because qos level and annotations will change after we support customized updater of enhancements and qos level
func updateAllocationInfoByReq(req *pluginapi.ResourceRequest, allocationInfo *state.AllocationInfo) error {
if req == nil {
return fmt.Errorf("updateAllocationInfoByReq got ni l req")
} else if allocationInfo == nil {
return nil
}

if req.Annotations[apiconsts.PodAnnotationQoSLevelKey] != "" &&
req.Annotations[apiconsts.PodAnnotationQoSLevelKey] != allocationInfo.QoSLevel {
general.Infof("update allocationInfo QoS level from %s to %s",
allocationInfo.QoSLevel, req.Annotations[apiconsts.PodAnnotationQoSLevelKey])
allocationInfo.QoSLevel = req.Annotations[apiconsts.PodAnnotationQoSLevelKey]
}

allocationInfo.Annotations = general.DeepCopyMap(req.Annotations)
return nil
}
65 changes: 65 additions & 0 deletions pkg/agent/qrm-plugins/cpu/dynamicpolicy/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2022 The Katalyst Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package dynamicpolicy

import (
"testing"

pluginapi "k8s.io/kubelet/pkg/apis/resourceplugin/v1alpha1"

apiconsts "github.com/kubewharf/katalyst-api/pkg/consts"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/dynamicpolicy/state"
)

func Test_updateAllocationInfoByReq(t *testing.T) {
type args struct {
req *pluginapi.ResourceRequest
allocationInfo *state.AllocationInfo
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "req == nil",
args: args{
allocationInfo: &state.AllocationInfo{},
},
wantErr: true,
},
{
name: "update qos level",
args: args{
req: &pluginapi.ResourceRequest{
Annotations: map[string]string{apiconsts.PodAnnotationQoSLevelKey: apiconsts.PodAnnotationQoSLevelSharedCores},
},
allocationInfo: &state.AllocationInfo{
QoSLevel: apiconsts.PodAnnotationQoSLevelReclaimedCores,
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := updateAllocationInfoByReq(tt.args.req, tt.args.allocationInfo); (err != nil) != tt.wantErr {
t.Errorf("updateAllocationInfoByReq() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit a957d90

Please sign in to comment.