-
Notifications
You must be signed in to change notification settings - Fork 90
/
qos_aware.go
154 lines (127 loc) · 4.93 KB
/
qos_aware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
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 qosaware
import (
"context"
"sync"
"time"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/metacache"
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin"
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/qosaware/reporter"
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/qosaware/resource"
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/plugin/qosaware/server"
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/types"
"github.com/kubewharf/katalyst-core/pkg/config"
"github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic/crd"
"github.com/kubewharf/katalyst-core/pkg/metaserver"
"github.com/kubewharf/katalyst-core/pkg/metrics"
metricspool "github.com/kubewharf/katalyst-core/pkg/metrics/metrics-pool"
)
const (
// MetricsNamePlugQoSAwareHearBeat is the heartbeat metrics of qos aware plugin
MetricsNamePlugQoSAwareHearBeat = "plugin_qosaware_heartbeat"
)
// QoSAwarePlugin calculates node headroom and updates resource provision value suggestions
// using different algorithms configured by user. Resource headroom data will be reported by the
// reporter and provision result will be sync to QRM plugin by gRPC. To take different resource
// dimensions into consideration, implement calculators or algorithms for each resource and register
// them to qos aware plugin.
type QoSAwarePlugin struct {
name string
period time.Duration
resourceAdvisor resource.ResourceAdvisor
qrmServer server.QRMServer
reporters []reporter.Reporter
metaCache metacache.MetaCache
emitter metrics.MetricEmitter
}
// NewQoSAwarePlugin creates a qos aware plugin with the specified config
func NewQoSAwarePlugin(pluginName string, conf *config.Configuration, extraConf interface{}, emitterPool metricspool.MetricsEmitterPool,
metaServer *metaserver.MetaServer, metaCache metacache.MetaCache,
) (plugin.SysAdvisorPlugin, error) {
emitter := emitterPool.GetDefaultMetricsEmitter().WithTags("advisor-qosaware")
resourceAdvisor, err := resource.NewResourceAdvisor(conf, extraConf, metaCache, metaServer, emitter)
if err != nil {
return nil, err
}
qrmServer, err := server.NewQRMServer(resourceAdvisor, conf, metaCache, metaServer, emitter)
if err != nil {
return nil, err
}
reporters := make([]reporter.Reporter, 0)
for _, reporterName := range conf.Reporters {
switch reporterName {
case types.HeadroomReporter:
headroomReporter, err := reporter.NewHeadroomReporter(emitter, metaServer, conf, resourceAdvisor)
if err != nil {
return nil, err
}
reporters = append(reporters, headroomReporter)
case types.NodeMetricReporter:
nodeMetricsReporter, err := reporter.NewNodeMetricsReporter(emitter, metaServer, metaCache, conf)
if err != nil {
return nil, err
}
reporters = append(reporters, nodeMetricsReporter)
}
}
// add AdminQos dynamic config watcher
err = metaServer.ConfigurationManager.AddConfigWatcher(crd.AdminQoSConfigurationGVR)
if err != nil {
return nil, err
}
// add TransparentMemoryOffloading dynamic config watcher
err = metaServer.ConfigurationManager.AddConfigWatcher(crd.TransparentMemoryOffloadingConfigurationGVR)
if err != nil {
return nil, err
}
qap := &QoSAwarePlugin{
name: pluginName,
period: conf.SysAdvisorPluginsConfiguration.QoSAwarePluginConfiguration.SyncPeriod,
resourceAdvisor: resourceAdvisor,
reporters: reporters,
qrmServer: qrmServer,
metaCache: metaCache,
emitter: emitter,
}
return qap, nil
}
// Run starts the qos aware plugin, which periodically inspects cpu usage and takes measures.
func (qap *QoSAwarePlugin) Run(ctx context.Context) {
go wait.UntilWithContext(ctx, qap.periodicWork, qap.period)
go qap.qrmServer.Run(ctx)
go qap.resourceAdvisor.Run(ctx)
// reporters must run synchronously to be stopped gracefully
wg := sync.WaitGroup{}
for _, reporter := range qap.reporters {
wg.Add(1)
runnable := reporter
go func() {
defer wg.Done()
runnable.Run(ctx)
}()
}
wg.Wait()
}
// Name returns the name of qos aware plugin
func (qap *QoSAwarePlugin) Name() string {
return qap.name
}
// Init initializes the qos aware plugin
func (qap *QoSAwarePlugin) Init() error {
return nil
}
func (qap *QoSAwarePlugin) periodicWork(_ context.Context) {
_ = qap.emitter.StoreInt64(MetricsNamePlugQoSAwareHearBeat, int64(qap.period.Seconds()), metrics.MetricTypeNameCount)
}