Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(qrm): modify memory control knob handler signature to add more utils convenient for handlers registered out of policy itself #199

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,23 @@ import (
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/advisorsvc"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/commonstate"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/memory/dynamicpolicy/state"
"github.com/kubewharf/katalyst-core/pkg/config"
dynamicconfig "github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic"
"github.com/kubewharf/katalyst-core/pkg/metaserver"
"github.com/kubewharf/katalyst-core/pkg/metrics"
)

// MemoryControlKnobHandler may trigger operations for corresponding control knobs
// or set control knob value to memory plugin checkpoint and take effect asynchronously
type MemoryControlKnobHandler func(entryName, subEntryName string,
calculationInfo *advisorsvc.CalculationInfo, podResourceEntries state.PodResourceEntries) error
type MemoryControlKnobHandler func(
coreConf *config.Configuration,
extraConf interface{},
dynamicConf *dynamicconfig.DynamicAgentConfiguration,
emitter metrics.MetricEmitter,
metaServer *metaserver.MetaServer,
entryName, subEntryName string,
calculationInfo *advisorsvc.CalculationInfo,
podResourceEntries state.PodResourceEntries) error

var memoryControlKnobHandlers sync.Map

Expand All @@ -48,8 +59,15 @@ func GetRegisteredControlKnobHandlers() map[MemoryControlKnobName]MemoryControlK
}

func ControlKnobHandlerWithChecker(handler MemoryControlKnobHandler) MemoryControlKnobHandler {
return func(entryName, subEntryName string,
calculationInfo *advisorsvc.CalculationInfo, podResourceEntries state.PodResourceEntries) error {
return func(
coreConf *config.Configuration,
extraConf interface{},
dynamicConf *dynamicconfig.DynamicAgentConfiguration,
emitter metrics.MetricEmitter,
metaServer *metaserver.MetaServer,
entryName, subEntryName string,
calculationInfo *advisorsvc.CalculationInfo,
podResourceEntries state.PodResourceEntries) error {

if calculationInfo == nil {
return fmt.Errorf("handler got nil calculationInfo")
Expand All @@ -58,13 +76,20 @@ func ControlKnobHandlerWithChecker(handler MemoryControlKnobHandler) MemoryContr
} else if calculationInfo.CgroupPath == "" &&
podResourceEntries[v1.ResourceMemory][entryName][subEntryName] == nil {
return fmt.Errorf("calculationInfo indicates no target")
} else if emitter == nil {
return fmt.Errorf("handler got nil emitter")
} else if metaServer == nil {
return fmt.Errorf("handler got nil metaServer")
}

if podResourceEntries[v1.ResourceMemory][entryName][subEntryName] != nil &&
podResourceEntries[v1.ResourceMemory][entryName][subEntryName].ExtraControlKnobInfo == nil {
podResourceEntries[v1.ResourceMemory][entryName][subEntryName].ExtraControlKnobInfo = make(map[string]commonstate.ControlKnobInfo)
}

return handler(entryName, subEntryName, calculationInfo, podResourceEntries)
return handler(coreConf, extraConf, dynamicConf,
emitter, metaServer,
entryName, subEntryName,
calculationInfo, podResourceEntries)
}
}
6 changes: 3 additions & 3 deletions pkg/agent/qrm-plugins/memory/dynamicpolicy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ func NewDynamicPolicy(agentCtx *agent.GenericContext, conf *config.Configuration
}

memoryadvisor.RegisterControlKnobHandler(memoryadvisor.ControlKnobKeyMemoryLimitInBytes,
memoryadvisor.ControlKnobHandlerWithChecker(policyImplement.handleAdvisorMemoryLimitInBytes))
memoryadvisor.ControlKnobHandlerWithChecker(handleAdvisorMemoryLimitInBytes))
memoryadvisor.RegisterControlKnobHandler(memoryadvisor.ControlKnobKeyCPUSetMems,
memoryadvisor.ControlKnobHandlerWithChecker(handleAdvisorCPUSetMems))
memoryadvisor.RegisterControlKnobHandler(memoryadvisor.ControlKnobKeyDropCache,
memoryadvisor.ControlKnobHandlerWithChecker(policyImplement.handleAdvisorDropCache))
memoryadvisor.RegisterControlKnobHandler(memoryadvisor.ControlKnobKeyCPUSetMems,
memoryadvisor.ControlKnobHandlerWithChecker(policyImplement.handleAdvisorCPUSetMems))

return true, &agent.PluginWrapper{GenericPlugin: pluginWrapper}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/memory/dynamicpolicy/memoryadvisor"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/memory/dynamicpolicy/state"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/util"
"github.com/kubewharf/katalyst-core/pkg/config"
dynamicconfig "github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic"
"github.com/kubewharf/katalyst-core/pkg/metaserver"
"github.com/kubewharf/katalyst-core/pkg/metrics"
"github.com/kubewharf/katalyst-core/pkg/util/asyncworker"
cgroupcommon "github.com/kubewharf/katalyst-core/pkg/util/cgroup/common"
Expand Down Expand Up @@ -137,7 +140,9 @@ func (p *DynamicPolicy) handleAdvisorResp(advisorResp *advisorsvc.ListAndWatchRe
"controlKnobValue", controlKnobValue)
handler := handlers[memoryadvisor.MemoryControlKnobName(controlKnobName)]
if handler != nil {
err := handler(entryName, subEntryName, calculationInfo, podResourceEntries)
err := handler(nil, nil, nil,
p.emitter, p.metaServer,
entryName, subEntryName, calculationInfo, podResourceEntries)

if err != nil {
general.ErrorS(err, "handle control knob failed",
Expand Down Expand Up @@ -182,7 +187,9 @@ func (p *DynamicPolicy) handleAdvisorResp(advisorResp *advisorsvc.ListAndWatchRe
"controlKnobValue", controlKnobValue)
handler := handlers[memoryadvisor.MemoryControlKnobName(controlKnobName)]
if handler != nil {
err := handler("", "", calculationInfo, podResourceEntries)
err := handler(nil, nil, nil,
p.emitter, p.metaServer,
"", "", calculationInfo, podResourceEntries)

if err != nil {
general.ErrorS(err, "handle control knob failed",
Expand Down Expand Up @@ -215,7 +222,13 @@ func (p *DynamicPolicy) handleAdvisorResp(advisorResp *advisorsvc.ListAndWatchRe
return nil
}

func (p *DynamicPolicy) handleAdvisorMemoryLimitInBytes(entryName, subEntryName string,
func handleAdvisorMemoryLimitInBytes(
_ *config.Configuration,
_ interface{},
_ *dynamicconfig.DynamicAgentConfiguration,
emitter metrics.MetricEmitter,
metaServer *metaserver.MetaServer,
entryName, subEntryName string,
calculationInfo *advisorsvc.CalculationInfo, podResourceEntries state.PodResourceEntries) error {

calculatedLimitInBytes := calculationInfo.CalculationResult.Values[string(memoryadvisor.ControlKnobKeyMemoryLimitInBytes)]
Expand All @@ -236,7 +249,7 @@ func (p *DynamicPolicy) handleAdvisorMemoryLimitInBytes(entryName, subEntryName
calculationInfo.CgroupPath, err)
}

_ = p.emitter.StoreInt64(util.MetricNameMemoryHandleAdvisorMemoryLimit, calculatedLimitInBytesInt64,
_ = emitter.StoreInt64(util.MetricNameMemoryHandleAdvisorMemoryLimit, calculatedLimitInBytesInt64,
metrics.MetricTypeNameRaw, metrics.ConvertMapToTags(map[string]string{
"cgroupPath": calculationInfo.CgroupPath,
})...)
Expand All @@ -258,7 +271,7 @@ func (p *DynamicPolicy) handleAdvisorMemoryLimitInBytes(entryName, subEntryName
OciPropertyName: util.OCIPropertyNameMemoryLimitInBytes,
}

_ = p.emitter.StoreInt64(util.MetricNameMemoryHandleAdvisorMemoryLimit, calculatedLimitInBytesInt64,
_ = emitter.StoreInt64(util.MetricNameMemoryHandleAdvisorMemoryLimit, calculatedLimitInBytesInt64,
metrics.MetricTypeNameRaw, metrics.ConvertMapToTags(map[string]string{
"entryName": entryName,
"subEntryName": subEntryName,
Expand All @@ -267,7 +280,13 @@ func (p *DynamicPolicy) handleAdvisorMemoryLimitInBytes(entryName, subEntryName
return nil
}

func (p *DynamicPolicy) handleAdvisorDropCache(entryName, subEntryName string,
func (p *DynamicPolicy) handleAdvisorDropCache(
_ *config.Configuration,
_ interface{},
_ *dynamicconfig.DynamicAgentConfiguration,
emitter metrics.MetricEmitter,
metaServer *metaserver.MetaServer,
entryName, subEntryName string,
calculationInfo *advisorsvc.CalculationInfo, podResourceEntries state.PodResourceEntries) error {

dropCache := calculationInfo.CalculationResult.Values[string(memoryadvisor.ControlKnobKeyDropCache)]
Expand All @@ -285,7 +304,7 @@ func (p *DynamicPolicy) handleAdvisorDropCache(entryName, subEntryName string,
return nil
}

containerID, err := p.metaServer.GetContainerID(entryName, subEntryName)
containerID, err := metaServer.GetContainerID(entryName, subEntryName)
if err != nil {
return fmt.Errorf("get container id of pod: %s container: %s failed with error: %v", entryName, subEntryName, err)
}
Expand All @@ -303,7 +322,7 @@ func (p *DynamicPolicy) handleAdvisorDropCache(entryName, subEntryName string,
return fmt.Errorf("add work: %s pod: %s container: %s failed with error: %v", dropCacheWorkName, entryName, subEntryName, err)
}

_ = p.emitter.StoreInt64(util.MetricNameMemoryHandleAdvisorDropCache, 1,
_ = emitter.StoreInt64(util.MetricNameMemoryHandleAdvisorDropCache, 1,
metrics.MetricTypeNameRaw, metrics.ConvertMapToTags(map[string]string{
"entryName": entryName,
"subEntryName": subEntryName,
Expand All @@ -312,7 +331,13 @@ func (p *DynamicPolicy) handleAdvisorDropCache(entryName, subEntryName string,
return nil
}

func (p *DynamicPolicy) handleAdvisorCPUSetMems(entryName, subEntryName string,
func handleAdvisorCPUSetMems(
_ *config.Configuration,
_ interface{},
_ *dynamicconfig.DynamicAgentConfiguration,
emitter metrics.MetricEmitter,
metaServer *metaserver.MetaServer,
entryName, subEntryName string,
calculationInfo *advisorsvc.CalculationInfo, podResourceEntries state.PodResourceEntries) error {

cpusetMemsStr := calculationInfo.CalculationResult.Values[string(memoryadvisor.ControlKnobKeyCPUSetMems)]
Expand All @@ -336,7 +361,7 @@ func (p *DynamicPolicy) handleAdvisorCPUSetMems(entryName, subEntryName string,
allocationInfo.TopologyAwareAllocations = nil
allocationInfo.AggregatedQuantity = 0

_ = p.emitter.StoreInt64(util.MetricNameMemoryHandleAdvisorCPUSetMems, 1,
_ = emitter.StoreInt64(util.MetricNameMemoryHandleAdvisorCPUSetMems, 1,
metrics.MetricTypeNameRaw, metrics.ConvertMapToTags(map[string]string{
"entryName": entryName,
"subEntryName": subEntryName,
Expand Down
6 changes: 3 additions & 3 deletions pkg/agent/qrm-plugins/memory/dynamicpolicy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2080,11 +2080,11 @@ func TestHandleAdvisorResp(t *testing.T) {
}

memoryadvisor.RegisterControlKnobHandler(memoryadvisor.ControlKnobKeyMemoryLimitInBytes,
memoryadvisor.ControlKnobHandlerWithChecker(dynamicPolicy.handleAdvisorMemoryLimitInBytes))
memoryadvisor.ControlKnobHandlerWithChecker(handleAdvisorMemoryLimitInBytes))
memoryadvisor.RegisterControlKnobHandler(memoryadvisor.ControlKnobKeyCPUSetMems,
memoryadvisor.ControlKnobHandlerWithChecker(handleAdvisorCPUSetMems))
memoryadvisor.RegisterControlKnobHandler(memoryadvisor.ControlKnobKeyDropCache,
memoryadvisor.ControlKnobHandlerWithChecker(dynamicPolicy.handleAdvisorDropCache))
memoryadvisor.RegisterControlKnobHandler(memoryadvisor.ControlKnobKeyCPUSetMems,
memoryadvisor.ControlKnobHandlerWithChecker(dynamicPolicy.handleAdvisorCPUSetMems))

machineState, err := state.GenerateMachineStateFromPodEntries(machineInfo, tc.podResourceEntries, resourcesReservedMemory)
as.Nil(err)
Expand Down