-
Notifications
You must be signed in to change notification settings - Fork 89
/
policy.go
1078 lines (923 loc) · 39.5 KB
/
policy.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 (
"context"
"fmt"
"sync"
"time"
"google.golang.org/grpc"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
pluginapi "k8s.io/kubelet/pkg/apis/resourceplugin/v1alpha1"
maputil "k8s.io/kubernetes/pkg/util/maps"
"k8s.io/utils/clock"
"github.com/kubewharf/katalyst-api/pkg/consts"
"github.com/kubewharf/katalyst-api/pkg/plugins/skeleton"
"github.com/kubewharf/katalyst-core/cmd/katalyst-agent/app/agent"
"github.com/kubewharf/katalyst-core/cmd/katalyst-agent/app/agent/qrm"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/advisorsvc"
cpuconsts "github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/consts"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/dynamicpolicy/calculator"
advisorapi "github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/dynamicpolicy/cpuadvisor"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/dynamicpolicy/cpueviction"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/dynamicpolicy/state"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/dynamicpolicy/validator"
cpuutil "github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/cpu/util"
"github.com/kubewharf/katalyst-core/pkg/agent/qrm-plugins/util"
"github.com/kubewharf/katalyst-core/pkg/agent/utilcomponent/periodicalhandler"
"github.com/kubewharf/katalyst-core/pkg/config"
dynamicconfig "github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic"
"github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic/crd"
"github.com/kubewharf/katalyst-core/pkg/config/generic"
"github.com/kubewharf/katalyst-core/pkg/metaserver"
"github.com/kubewharf/katalyst-core/pkg/metrics"
"github.com/kubewharf/katalyst-core/pkg/util/general"
"github.com/kubewharf/katalyst-core/pkg/util/machine"
"github.com/kubewharf/katalyst-core/pkg/util/native"
"github.com/kubewharf/katalyst-core/pkg/util/process"
"github.com/kubewharf/katalyst-core/pkg/util/timemonitor"
)
const (
cpuPluginStateFileName = "cpu_plugin_state"
reservedReclaimedCPUsSize = 4
cpusetCheckPeriod = 10 * time.Second
stateCheckPeriod = 30 * time.Second
maxResidualTime = 5 * time.Minute
syncCPUIdlePeriod = 30 * time.Second
healthCheckTolerationTimes = 3
)
var (
readonlyStateLock sync.RWMutex
readonlyState state.ReadonlyState
)
// GetReadonlyState returns state.ReadonlyState to provides a way
// to obtain the running states of the plugin
func GetReadonlyState() (state.ReadonlyState, error) {
readonlyStateLock.RLock()
defer readonlyStateLock.RUnlock()
if readonlyState == nil {
return nil, fmt.Errorf("readonlyState isn't setted")
}
return readonlyState, nil
}
// DynamicPolicy is the policy that's used by default;
// it will consider the dynamic running information to calculate
// and adjust resource requirements and configurations
type DynamicPolicy struct {
sync.RWMutex
name string
stopCh chan struct{}
started bool
emitter metrics.MetricEmitter
metaServer *metaserver.MetaServer
machineInfo *machine.KatalystMachineInfo
advisorClient advisorapi.CPUAdvisorClient
advisorConn *grpc.ClientConn
advisorValidator *validator.CPUAdvisorValidator
advisorapi.UnimplementedCPUPluginServer
lwRecvTimeMonitor *timemonitor.TimeMonitor
state state.State
residualHitMap map[string]int64
allocationHandlers map[string]util.AllocationHandler
hintHandlers map[string]util.HintHandler
cpuPressureEviction agent.Component
cpuPressureEvictionCancel context.CancelFunc
// those are parsed from configurations
// todo if we want to use dynamic configuration, we'd better not use self-defined conf
enableCPUAdvisor bool
reservedCPUs machine.CPUSet
cpuAdvisorSocketAbsPath string
cpuPluginSocketAbsPath string
extraStateFileAbsPath string
enableCPUIdle bool
enableSyncingCPUIdle bool
reclaimRelativeRootCgroupPath string
qosConfig *generic.QoSConfiguration
dynamicConfig *dynamicconfig.DynamicAgentConfiguration
podDebugAnnoKeys []string
transitionPeriod time.Duration
}
func NewDynamicPolicy(agentCtx *agent.GenericContext, conf *config.Configuration,
_ interface{}, agentName string,
) (bool, agent.Component, error) {
reservedCPUs, reserveErr := cpuutil.GetCoresReservedForSystem(conf, agentCtx.MetaServer, agentCtx.KatalystMachineInfo, agentCtx.CPUDetails.CPUs().Clone())
if reserveErr != nil {
return false, agent.ComponentStub{}, fmt.Errorf("GetCoresReservedForSystem for reservedCPUsNum: %d failed with error: %v",
conf.ReservedCPUCores, reserveErr)
}
stateImpl, stateErr := state.NewCheckpointState(conf.GenericQRMPluginConfiguration.StateFileDirectory, cpuPluginStateFileName,
cpuconsts.CPUResourcePluginPolicyNameDynamic, agentCtx.CPUTopology, conf.SkipCPUStateCorruption)
if stateErr != nil {
return false, agent.ComponentStub{}, fmt.Errorf("NewCheckpointState failed with error: %v", stateErr)
}
readonlyStateLock.Lock()
readonlyState = stateImpl
readonlyStateLock.Unlock()
wrappedEmitter := agentCtx.EmitterPool.GetDefaultMetricsEmitter().WithTags(agentName, metrics.MetricTag{
Key: util.QRMPluginPolicyTagName,
Val: cpuconsts.CPUResourcePluginPolicyNameDynamic,
})
var (
cpuPressureEviction agent.Component
err error
)
if conf.EnableCPUPressureEviction {
cpuPressureEviction, err = cpueviction.NewCPUPressureEviction(
agentCtx.EmitterPool.GetDefaultMetricsEmitter(), agentCtx.MetaServer, conf, stateImpl)
if err != nil {
return false, agent.ComponentStub{}, err
}
}
// since the reservedCPUs won't influence stateImpl directly.
// so we don't modify stateImpl with reservedCPUs here.
// for those pods have already been allocated reservedCPUs,
// we won't touch them and wait them to be deleted the next update.
policyImplement := &DynamicPolicy{
name: fmt.Sprintf("%s_%s", agentName, cpuconsts.CPUResourcePluginPolicyNameDynamic),
stopCh: make(chan struct{}),
machineInfo: agentCtx.KatalystMachineInfo,
emitter: wrappedEmitter,
metaServer: agentCtx.MetaServer,
state: stateImpl,
residualHitMap: make(map[string]int64),
advisorValidator: validator.NewCPUAdvisorValidator(stateImpl, agentCtx.KatalystMachineInfo),
cpuPressureEviction: cpuPressureEviction,
qosConfig: conf.QoSConfiguration,
dynamicConfig: conf.DynamicAgentConfiguration,
cpuAdvisorSocketAbsPath: conf.CPUAdvisorSocketAbsPath,
cpuPluginSocketAbsPath: conf.CPUPluginSocketAbsPath,
enableCPUAdvisor: conf.CPUQRMPluginConfig.EnableCPUAdvisor,
reservedCPUs: reservedCPUs,
extraStateFileAbsPath: conf.ExtraStateFileAbsPath,
enableSyncingCPUIdle: conf.CPUQRMPluginConfig.EnableSyncingCPUIdle,
enableCPUIdle: conf.CPUQRMPluginConfig.EnableCPUIdle,
reclaimRelativeRootCgroupPath: conf.ReclaimRelativeRootCgroupPath,
podDebugAnnoKeys: conf.PodDebugAnnoKeys,
transitionPeriod: 30 * time.Second,
}
// register allocation behaviors for pods with different QoS level
policyImplement.allocationHandlers = map[string]util.AllocationHandler{
consts.PodAnnotationQoSLevelSharedCores: policyImplement.sharedCoresAllocationHandler,
consts.PodAnnotationQoSLevelDedicatedCores: policyImplement.dedicatedCoresAllocationHandler,
consts.PodAnnotationQoSLevelReclaimedCores: policyImplement.reclaimedCoresAllocationHandler,
}
// register hint providers for pods with different QoS level
policyImplement.hintHandlers = map[string]util.HintHandler{
consts.PodAnnotationQoSLevelSharedCores: policyImplement.sharedCoresHintHandler,
consts.PodAnnotationQoSLevelDedicatedCores: policyImplement.dedicatedCoresHintHandler,
consts.PodAnnotationQoSLevelReclaimedCores: policyImplement.reclaimedCoresHintHandler,
}
state.SetContainerRequestedCores(policyImplement.getContainerRequestedCores)
if err := policyImplement.cleanPools(); err != nil {
return false, agent.ComponentStub{}, fmt.Errorf("cleanPools failed with error: %v", err)
}
if err := policyImplement.initReservePool(); err != nil {
return false, agent.ComponentStub{}, fmt.Errorf("dynamic policy initReservePool failed with error: %v", err)
}
if err := policyImplement.initReclaimPool(); err != nil {
return false, agent.ComponentStub{}, fmt.Errorf("dynamic policy initReclaimPool failed with error: %v", err)
}
err = agentCtx.MetaServer.ConfigurationManager.AddConfigWatcher(crd.AdminQoSConfigurationGVR)
if err != nil {
return false, nil, err
}
pluginWrapper, err := skeleton.NewRegistrationPluginWrapper(policyImplement, conf.QRMPluginSocketDirs, func(key string, value int64) {
_ = wrappedEmitter.StoreInt64(key, value, metrics.MetricTypeNameRaw)
})
if err != nil {
return false, agent.ComponentStub{}, fmt.Errorf("dynamic policy new plugin wrapper failed with error: %v", err)
}
return true, &agent.PluginWrapper{GenericPlugin: pluginWrapper}, nil
}
func (p *DynamicPolicy) Name() string {
return p.name
}
func (p *DynamicPolicy) ResourceName() string {
return string(v1.ResourceCPU)
}
func (p *DynamicPolicy) Start() (err error) {
general.Infof("called")
p.Lock()
defer func() {
if !p.started {
if err == nil {
p.started = true
} else {
close(p.stopCh)
}
}
p.Unlock()
}()
if p.started {
general.Infof("is already started")
return nil
}
p.stopCh = make(chan struct{})
go wait.Until(func() {
_ = p.emitter.StoreInt64(util.MetricNameHeartBeat, 1, metrics.MetricTypeNameRaw)
}, time.Second*30, p.stopCh)
err = periodicalhandler.RegisterPeriodicalHandlerWithHealthz(cpuconsts.ClearResidualState, general.HealthzCheckStateNotReady,
qrm.QRMCPUPluginPeriodicalHandlerGroupName, p.clearResidualState, stateCheckPeriod, healthCheckTolerationTimes)
if err != nil {
general.Errorf("start %v failed,err:%v", cpuconsts.ClearResidualState, err)
}
err = periodicalhandler.RegisterPeriodicalHandlerWithHealthz(cpuconsts.CheckCPUSet, general.HealthzCheckStateNotReady,
qrm.QRMCPUPluginPeriodicalHandlerGroupName, p.checkCPUSet, cpusetCheckPeriod, healthCheckTolerationTimes)
if err != nil {
general.Errorf("start %v failed,err:%v", cpuconsts.CheckCPUSet, err)
}
// start cpu-idle syncing if needed
if p.enableSyncingCPUIdle {
general.Infof("syncCPUIdle enabled")
if p.reclaimRelativeRootCgroupPath == "" {
return fmt.Errorf("enable syncing cpu idle but not set reclaiemd relative root cgroup path in configuration")
}
err = periodicalhandler.RegisterPeriodicalHandlerWithHealthz(cpuconsts.SyncCPUIdle, general.HealthzCheckStateNotReady,
qrm.QRMCPUPluginPeriodicalHandlerGroupName, p.syncCPUIdle, syncCPUIdlePeriod, healthCheckTolerationTimes)
if err != nil {
general.Errorf("start %v failed,err:%v", cpuconsts.SyncCPUIdle, err)
}
}
// start cpu-pressure eviction plugin if needed
if p.cpuPressureEviction != nil {
var ctx context.Context
ctx, p.cpuPressureEvictionCancel = context.WithCancel(context.Background())
go p.cpuPressureEviction.Run(ctx)
}
go wait.Until(func() {
periodicalhandler.ReadyToStartHandlersByGroup(qrm.QRMCPUPluginPeriodicalHandlerGroupName)
}, 5*time.Second, p.stopCh)
// pre-check necessary dirs if sys-advisor is enabled
if !p.enableCPUAdvisor {
general.Infof("start dynamic policy cpu plugin without sys-advisor")
return nil
} else if p.cpuAdvisorSocketAbsPath == "" || p.cpuPluginSocketAbsPath == "" {
return fmt.Errorf("invalid cpuAdvisorSocketAbsPath: %s or cpuPluginSocketAbsPath: %s",
p.cpuAdvisorSocketAbsPath, p.cpuPluginSocketAbsPath)
}
general.Infof("start dynamic policy cpu plugin with sys-advisor")
err = p.initAdvisorClientConn()
if err != nil {
general.Errorf("initAdvisorClientConn failed with error: %v", err)
return
}
go wait.BackoffUntil(func() { p.serveForAdvisor(p.stopCh) }, wait.NewExponentialBackoffManager(
800*time.Millisecond, 30*time.Second, 2*time.Minute, 2.0, 0, &clock.RealClock{}), true, p.stopCh)
communicateWithCPUAdvisorServer := func() {
general.Infof("waiting cpu plugin checkpoint server serving confirmation")
if conn, err := process.Dial(p.cpuPluginSocketAbsPath, 5*time.Second); err != nil {
general.Errorf("dial check at socket: %s failed with err: %v", p.cpuPluginSocketAbsPath, err)
return
} else {
_ = conn.Close()
}
general.Infof("cpu plugin checkpoint server serving confirmed")
err = p.pushCPUAdvisor()
if err != nil {
general.Errorf("sync existing containers to cpu advisor failed with error: %v", err)
return
}
general.Infof("sync existing containers to cpu advisor successfully")
// call lw of CPUAdvisorServer and do allocation
if err = p.lwCPUAdvisorServer(p.stopCh); err != nil {
general.Errorf("lwCPUAdvisorServer failed with error: %v", err)
} else {
general.Infof("lwCPUAdvisorServer finished")
}
}
general.RegisterHeartbeatCheck(cpuconsts.CommunicateWithAdvisor, 2*time.Minute, general.HealthzCheckStateNotReady, 2*time.Minute)
go wait.BackoffUntil(communicateWithCPUAdvisorServer, wait.NewExponentialBackoffManager(800*time.Millisecond,
30*time.Second, 2*time.Minute, 2.0, 0, &clock.RealClock{}), true, p.stopCh)
p.lwRecvTimeMonitor = timemonitor.NewTimeMonitor(cpuAdvisorLWRecvTimeMonitorName,
cpuAdvisorLWRecvTimeMonitorDurationThreshold, cpuAdvisorLWRecvTimeMonitorInterval,
util.MetricNameLWRecvStuck, p.emitter)
go p.lwRecvTimeMonitor.Run(p.stopCh)
return nil
}
func (p *DynamicPolicy) Stop() error {
p.Lock()
defer func() {
p.started = false
p.Unlock()
general.Infof("stopped")
}()
if !p.started {
general.Warningf("already stopped")
return nil
}
close(p.stopCh)
if p.cpuPressureEvictionCancel != nil {
p.cpuPressureEvictionCancel()
}
periodicalhandler.StopHandlersByGroup(qrm.QRMCPUPluginPeriodicalHandlerGroupName)
if p.advisorConn != nil {
return p.advisorConn.Close()
}
return nil
}
// GetResourcesAllocation returns allocation results of corresponding resources
func (p *DynamicPolicy) GetResourcesAllocation(_ context.Context,
req *pluginapi.GetResourcesAllocationRequest,
) (*pluginapi.GetResourcesAllocationResponse, error) {
if req == nil {
return nil, fmt.Errorf("GetResourcesAllocation got nil req")
}
general.Infof("called")
p.Lock()
defer p.Unlock()
podEntries := p.state.GetPodEntries()
machineState := p.state.GetMachineState()
// pooledCPUs is the total available cpu cores minus those that are reserved
pooledCPUs := machineState.GetFilteredAvailableCPUSet(p.reservedCPUs,
state.CheckDedicated, state.CheckDedicatedNUMABinding)
pooledCPUsTopologyAwareAssignments, err := machine.GetNumaAwareAssignments(p.machineInfo.CPUTopology, pooledCPUs)
if err != nil {
return nil, fmt.Errorf("GetNumaAwareAssignments err: %v", err)
}
podResources := make(map[string]*pluginapi.ContainerResources)
var allocationInfosJustFinishRampUp []*state.AllocationInfo
for podUID, containerEntries := range podEntries {
// if it's a pool, not returning to QRM
if containerEntries.IsPoolEntry() {
continue
}
if podResources[podUID] == nil {
podResources[podUID] = &pluginapi.ContainerResources{}
}
for containerName, allocationInfo := range containerEntries {
if allocationInfo == nil {
continue
}
allocationInfo = allocationInfo.Clone()
initTs, tsErr := time.Parse(util.QRMTimeFormat, allocationInfo.InitTimestamp)
if tsErr != nil {
if state.CheckShared(allocationInfo) {
general.Errorf("pod: %s/%s, container: %s init timestamp parsed failed with error: %v, re-ramp-up it",
allocationInfo.PodNamespace, allocationInfo.PodName, allocationInfo.ContainerName, tsErr)
clonedPooledCPUs := pooledCPUs.Clone()
clonedPooledCPUsTopologyAwareAssignments := machine.DeepcopyCPUAssignment(pooledCPUsTopologyAwareAssignments)
allocationInfo.AllocationResult = clonedPooledCPUs
allocationInfo.OriginalAllocationResult = clonedPooledCPUs
allocationInfo.TopologyAwareAssignments = clonedPooledCPUsTopologyAwareAssignments
allocationInfo.OriginalTopologyAwareAssignments = clonedPooledCPUsTopologyAwareAssignments
// fill OwnerPoolName with empty string when ramping up
allocationInfo.OwnerPoolName = advisorapi.EmptyOwnerPoolName
allocationInfo.RampUp = true
}
allocationInfo.InitTimestamp = time.Now().Format(util.QRMTimeFormat)
p.state.SetAllocationInfo(podUID, containerName, allocationInfo)
} else if allocationInfo.RampUp && time.Now().After(initTs.Add(p.transitionPeriod)) {
general.Infof("pod: %s/%s, container: %s ramp up finished", allocationInfo.PodNamespace, allocationInfo.PodName, allocationInfo.ContainerName)
allocationInfo.RampUp = false
p.state.SetAllocationInfo(podUID, containerName, allocationInfo)
if state.CheckShared(allocationInfo) {
allocationInfosJustFinishRampUp = append(allocationInfosJustFinishRampUp, allocationInfo)
}
}
if podResources[podUID].ContainerResources == nil {
podResources[podUID].ContainerResources = make(map[string]*pluginapi.ResourceAllocation)
}
podResources[podUID].ContainerResources[containerName] = &pluginapi.ResourceAllocation{
ResourceAllocation: map[string]*pluginapi.ResourceAllocationInfo{
string(v1.ResourceCPU): {
OciPropertyName: util.OCIPropertyNameCPUSetCPUs,
IsNodeResource: false,
IsScalarResource: true,
AllocatedQuantity: float64(allocationInfo.AllocationResult.Size()),
AllocationResult: allocationInfo.AllocationResult.String(),
},
},
}
}
}
if len(allocationInfosJustFinishRampUp) > 0 {
if err = p.putAllocationsAndAdjustAllocationEntries(allocationInfosJustFinishRampUp, true); err != nil {
// not influencing return response to kubelet when putAllocationsAndAdjustAllocationEntries failed
general.Errorf("putAllocationsAndAdjustAllocationEntries failed with error: %v", err)
}
}
return &pluginapi.GetResourcesAllocationResponse{
PodResources: podResources,
}, nil
}
// GetTopologyAwareResources returns allocation results of corresponding resources as machineInfo aware format
func (p *DynamicPolicy) GetTopologyAwareResources(_ context.Context,
req *pluginapi.GetTopologyAwareResourcesRequest,
) (*pluginapi.GetTopologyAwareResourcesResponse, error) {
if req == nil {
return nil, fmt.Errorf("GetTopologyAwareResources got nil req")
}
general.Infof("called")
p.RLock()
defer p.RUnlock()
allocationInfo := p.state.GetAllocationInfo(req.PodUid, req.ContainerName)
if allocationInfo == nil {
return nil, fmt.Errorf("pod: %s, container: %s is not show up in cpu plugin state", req.PodUid, req.ContainerName)
}
resp := &pluginapi.GetTopologyAwareResourcesResponse{
PodUid: allocationInfo.PodUid,
PodName: allocationInfo.PodName,
PodNamespace: allocationInfo.PodNamespace,
ContainerTopologyAwareResources: &pluginapi.ContainerTopologyAwareResources{
ContainerName: allocationInfo.ContainerName,
},
}
if allocationInfo.CheckSideCar() {
resp.ContainerTopologyAwareResources.AllocatedResources = map[string]*pluginapi.TopologyAwareResource{
string(v1.ResourceCPU): {
IsNodeResource: false,
IsScalarResource: true,
AggregatedQuantity: 0,
OriginalAggregatedQuantity: 0,
TopologyAwareQuantityList: nil,
OriginalTopologyAwareQuantityList: nil,
},
}
} else {
resp.ContainerTopologyAwareResources.AllocatedResources = map[string]*pluginapi.TopologyAwareResource{
string(v1.ResourceCPU): {
IsNodeResource: false,
IsScalarResource: true,
AggregatedQuantity: float64(allocationInfo.AllocationResult.Size()),
OriginalAggregatedQuantity: float64(allocationInfo.OriginalAllocationResult.Size()),
TopologyAwareQuantityList: util.GetTopologyAwareQuantityFromAssignments(allocationInfo.TopologyAwareAssignments),
OriginalTopologyAwareQuantityList: util.GetTopologyAwareQuantityFromAssignments(allocationInfo.OriginalTopologyAwareAssignments),
},
}
}
return resp, nil
}
// GetTopologyAwareAllocatableResources returns corresponding allocatable resources as machineInfo aware format
func (p *DynamicPolicy) GetTopologyAwareAllocatableResources(_ context.Context,
_ *pluginapi.GetTopologyAwareAllocatableResourcesRequest,
) (*pluginapi.GetTopologyAwareAllocatableResourcesResponse, error) {
general.Infof("is called")
numaNodes := p.machineInfo.CPUDetails.NUMANodes().ToSliceInt()
topologyAwareAllocatableQuantityList := make([]*pluginapi.TopologyAwareQuantity, 0, len(numaNodes))
topologyAwareCapacityQuantityList := make([]*pluginapi.TopologyAwareQuantity, 0, len(numaNodes))
for _, numaNode := range numaNodes {
numaNodeCPUs := p.machineInfo.CPUDetails.CPUsInNUMANodes(numaNode).Clone()
topologyAwareAllocatableQuantityList = append(topologyAwareAllocatableQuantityList, &pluginapi.TopologyAwareQuantity{
ResourceValue: float64(numaNodeCPUs.Difference(p.reservedCPUs).Size()),
Node: uint64(numaNode),
})
topologyAwareCapacityQuantityList = append(topologyAwareCapacityQuantityList, &pluginapi.TopologyAwareQuantity{
ResourceValue: float64(numaNodeCPUs.Size()),
Node: uint64(numaNode),
})
}
return &pluginapi.GetTopologyAwareAllocatableResourcesResponse{
AllocatableResources: map[string]*pluginapi.AllocatableTopologyAwareResource{
string(v1.ResourceCPU): {
IsNodeResource: false,
IsScalarResource: true,
AggregatedAllocatableQuantity: float64(p.machineInfo.NumCPUs - p.reservedCPUs.Size()),
TopologyAwareAllocatableQuantityList: topologyAwareAllocatableQuantityList,
AggregatedCapacityQuantity: float64(p.machineInfo.NumCPUs),
TopologyAwareCapacityQuantityList: topologyAwareCapacityQuantityList,
},
},
}, nil
}
// GetTopologyHints returns hints of corresponding resources
func (p *DynamicPolicy) GetTopologyHints(ctx context.Context,
req *pluginapi.ResourceRequest,
) (resp *pluginapi.ResourceHintsResponse, err error) {
if req == nil {
return nil, fmt.Errorf("GetTopologyHints got nil req")
}
// identify if the pod is a debug pod,
// if so, apply specific strategy to it.
// since GetKatalystQoSLevelFromResourceReq function will filter annotations,
// we should do it before GetKatalystQoSLevelFromResourceReq.
isDebugPod := util.IsDebugPod(req.Annotations, p.podDebugAnnoKeys)
qosLevel, err := util.GetKatalystQoSLevelFromResourceReq(p.qosConfig, req)
if err != nil {
err = fmt.Errorf("GetKatalystQoSLevelFromResourceReq for pod: %s/%s, container: %s failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
general.Errorf("%s", err.Error())
return nil, err
}
reqInt, reqFloat64, err := util.GetQuantityFromResourceReq(req)
if err != nil {
return nil, fmt.Errorf("getReqQuantityFromResourceReq failed with error: %v", err)
}
general.InfoS("called",
"podNamespace", req.PodNamespace,
"podName", req.PodName,
"containerName", req.ContainerName,
"podType", req.PodType,
"podRole", req.PodRole,
"containerType", req.ContainerType,
"qosLevel", qosLevel,
"numCPUsInt", reqInt,
"numCPUsFloat64", reqFloat64,
"isDebugPod", isDebugPod)
if req.ContainerType == pluginapi.ContainerType_INIT || isDebugPod {
general.Infof("there is no NUMA preference, return nil hint")
return util.PackResourceHintsResponse(req, string(v1.ResourceCPU),
map[string]*pluginapi.ListOfTopologyHints{
string(v1.ResourceCPU): nil, // indicates that there is no numa preference
})
}
p.RLock()
defer func() {
p.RUnlock()
if err != nil {
_ = p.emitter.StoreInt64(util.MetricNameGetTopologyHintsFailed, 1, metrics.MetricTypeNameRaw)
}
}()
if p.hintHandlers[qosLevel] == nil {
return nil, fmt.Errorf("katalyst QoS level: %s is not supported yet", qosLevel)
}
return p.hintHandlers[qosLevel](ctx, req)
}
// GetResourcePluginOptions returns options to be communicated with Resource Manager
func (p *DynamicPolicy) GetResourcePluginOptions(context.Context,
*pluginapi.Empty,
) (*pluginapi.ResourcePluginOptions, error) {
general.Infof("called")
return &pluginapi.ResourcePluginOptions{
PreStartRequired: false,
WithTopologyAlignment: true,
NeedReconcile: true,
}, nil
}
// Allocate is called during pod admit so that the resource
// plugin can allocate corresponding resource for the container
// according to resource request
func (p *DynamicPolicy) Allocate(ctx context.Context,
req *pluginapi.ResourceRequest,
) (resp *pluginapi.ResourceAllocationResponse, respErr error) {
if req == nil {
return nil, fmt.Errorf("allocate got nil req")
}
// identify if the pod is a debug pod,
// if so, apply specific strategy to it.
// since GetKatalystQoSLevelFromResourceReq function will filter annotations,
// we should do it before GetKatalystQoSLevelFromResourceReq.
isDebugPod := util.IsDebugPod(req.Annotations, p.podDebugAnnoKeys)
qosLevel, err := util.GetKatalystQoSLevelFromResourceReq(p.qosConfig, req)
if err != nil {
err = fmt.Errorf("GetKatalystQoSLevelFromResourceReq for pod: %s/%s, container: %s failed with error: %v",
req.PodNamespace, req.PodName, req.ContainerName, err)
general.Errorf("%s", err.Error())
return nil, err
}
reqInt, reqFloat64, err := util.GetQuantityFromResourceReq(req)
if err != nil {
return nil, fmt.Errorf("getReqQuantityFromResourceReq failed with error: %v", err)
}
general.InfoS("called",
"podNamespace", req.PodNamespace,
"podName", req.PodName,
"containerName", req.ContainerName,
"podType", req.PodType,
"podRole", req.PodRole,
"containerType", req.ContainerType,
"qosLevel", qosLevel,
"numCPUsInt", reqInt,
"numCPUsFloat64", reqFloat64,
"isDebugPod", isDebugPod)
if req.ContainerType == pluginapi.ContainerType_INIT {
return &pluginapi.ResourceAllocationResponse{
PodUid: req.PodUid,
PodNamespace: req.PodNamespace,
PodName: req.PodName,
ContainerName: req.ContainerName,
ContainerType: req.ContainerType,
ContainerIndex: req.ContainerIndex,
PodRole: req.PodRole,
PodType: req.PodType,
ResourceName: string(v1.ResourceCPU),
Labels: general.DeepCopyMap(req.Labels),
Annotations: general.DeepCopyMap(req.Annotations),
}, nil
} else if isDebugPod {
return &pluginapi.ResourceAllocationResponse{
PodUid: req.PodUid,
PodNamespace: req.PodNamespace,
PodName: req.PodName,
ContainerName: req.ContainerName,
ContainerType: req.ContainerType,
ContainerIndex: req.ContainerIndex,
PodRole: req.PodRole,
PodType: req.PodType,
ResourceName: string(v1.ResourceCPU),
AllocationResult: &pluginapi.ResourceAllocation{
ResourceAllocation: map[string]*pluginapi.ResourceAllocationInfo{
string(v1.ResourceCPU): {
// return ResourceAllocation with empty OciPropertyName, AllocatedQuantity, AllocationResult for containers in debug pod,
// it won't influence oci spec properties of the container
IsNodeResource: false,
IsScalarResource: true,
},
},
},
Labels: general.DeepCopyMap(req.Labels),
Annotations: general.DeepCopyMap(req.Annotations),
}, nil
}
p.Lock()
defer func() {
// calls sys-advisor to inform the latest container
if p.enableCPUAdvisor && respErr == nil && req.ContainerType != pluginapi.ContainerType_INIT {
_, err := p.advisorClient.AddContainer(ctx, &advisorsvc.ContainerMetadata{
PodUid: req.PodUid,
PodNamespace: req.PodNamespace,
PodName: req.PodName,
ContainerName: req.ContainerName,
ContainerType: req.ContainerType,
ContainerIndex: req.ContainerIndex,
Labels: maputil.CopySS(req.Labels),
Annotations: maputil.CopySS(req.Annotations),
QosLevel: qosLevel,
RequestQuantity: uint64(reqInt),
})
if err != nil {
resp = nil
respErr = fmt.Errorf("add container to qos aware server failed with error: %v", err)
_ = p.removeContainer(req.PodUid, req.ContainerName)
}
} else if respErr != nil {
_ = p.removeContainer(req.PodUid, req.ContainerName)
_ = p.emitter.StoreInt64(util.MetricNameAllocateFailed, 1, metrics.MetricTypeNameRaw)
}
p.Unlock()
return
}()
allocationInfo := p.state.GetAllocationInfo(req.PodUid, req.ContainerName)
if allocationInfo != nil && allocationInfo.OriginalAllocationResult.Size() >= reqInt {
general.InfoS("already allocated and meet requirement",
"podNamespace", req.PodNamespace,
"podName", req.PodName,
"containerName", req.ContainerName,
"numCPUs", reqInt,
"originalAllocationResult", allocationInfo.OriginalAllocationResult.String(),
"currentResult", allocationInfo.AllocationResult.String())
return &pluginapi.ResourceAllocationResponse{
PodUid: req.PodUid,
PodNamespace: req.PodNamespace,
PodName: req.PodName,
ContainerName: req.ContainerName,
ContainerType: req.ContainerType,
ContainerIndex: req.ContainerIndex,
PodRole: req.PodRole,
PodType: req.PodType,
ResourceName: string(v1.ResourceCPU),
AllocationResult: &pluginapi.ResourceAllocation{
ResourceAllocation: map[string]*pluginapi.ResourceAllocationInfo{
string(v1.ResourceCPU): {
OciPropertyName: util.OCIPropertyNameCPUSetCPUs,
IsNodeResource: false,
IsScalarResource: true,
AllocatedQuantity: float64(allocationInfo.AllocationResult.Size()),
AllocationResult: allocationInfo.AllocationResult.String(),
},
},
},
Labels: general.DeepCopyMap(req.Labels),
Annotations: general.DeepCopyMap(req.Annotations),
}, nil
}
if p.allocationHandlers[qosLevel] == nil {
return nil, fmt.Errorf("katalyst QoS level: %s is not supported yet", qosLevel)
}
return p.allocationHandlers[qosLevel](ctx, req)
}
// PreStartContainer is called, if indicated by resource plugin during registration phase,
// before each container start. Resource plugin can run resource specific operations
// such as resetting the resource before making resources available to the container
func (p *DynamicPolicy) PreStartContainer(context.Context,
*pluginapi.PreStartContainerRequest,
) (*pluginapi.PreStartContainerResponse, error) {
return nil, nil
}
func (p *DynamicPolicy) RemovePod(ctx context.Context,
req *pluginapi.RemovePodRequest,
) (resp *pluginapi.RemovePodResponse, err error) {
if req == nil {
return nil, fmt.Errorf("RemovePod got nil req")
}
general.InfoS("called", "podUID", req.PodUid)
p.Lock()
defer func() {
p.Unlock()
if err != nil {
general.ErrorS(err, "remove pod failed with error", "podUID", req.PodUid)
_ = p.emitter.StoreInt64(util.MetricNameRemovePodFailed, 1, metrics.MetricTypeNameRaw)
}
}()
if p.enableCPUAdvisor {
_, err = p.advisorClient.RemovePod(ctx, &advisorsvc.RemovePodRequest{PodUid: req.PodUid})
if err != nil {
return nil, fmt.Errorf("remove pod in QoS aware server failed with error: %v", err)
}
}
err = p.removePod(req.PodUid)
if err != nil {
general.ErrorS(err, "remove pod failed with error", "podUID", req.PodUid)
return nil, err
}
aErr := p.adjustAllocationEntries()
if aErr != nil {
general.ErrorS(aErr, "adjustAllocationEntries failed", "podUID", req.PodUid)
}
return &pluginapi.RemovePodResponse{}, nil
}
func (p *DynamicPolicy) removePod(podUID string) error {
podEntries := p.state.GetPodEntries()
if len(podEntries[podUID]) == 0 {
return nil
}
delete(podEntries, podUID)
updatedMachineState, err := generateMachineStateFromPodEntries(p.machineInfo.CPUTopology, podEntries)
if err != nil {
return fmt.Errorf("GenerateMachineStateFromPodEntries failed with error: %v", err)
}
p.state.SetPodEntries(podEntries)
p.state.SetMachineState(updatedMachineState)
return nil
}
func (p *DynamicPolicy) removeContainer(podUID, containerName string) error {
podEntries := p.state.GetPodEntries()
found := false
if podEntries[podUID][containerName] != nil {
found = true
}
delete(podEntries[podUID], containerName)
if !found {
return nil
}
updatedMachineState, err := generateMachineStateFromPodEntries(p.machineInfo.CPUTopology, podEntries)
if err != nil {
return fmt.Errorf("GenerateMachineStateFromPodEntries failed with error: %v", err)
}
p.state.SetPodEntries(podEntries)
p.state.SetMachineState(updatedMachineState)
return nil
}
// initAdvisorClientConn initializes cpu-advisor related connections
func (p *DynamicPolicy) initAdvisorClientConn() (err error) {
cpuAdvisorConn, err := process.Dial(p.cpuAdvisorSocketAbsPath, 5*time.Second)
if err != nil {
err = fmt.Errorf("get cpu advisor connection with socket: %s failed with error: %v", p.cpuAdvisorSocketAbsPath, err)
return
}
p.advisorClient = advisorapi.NewCPUAdvisorClient(cpuAdvisorConn)
p.advisorConn = cpuAdvisorConn
return nil
}
// cleanPools is used to clean pools-related data in local state
func (p *DynamicPolicy) cleanPools() error {
remainPools := make(map[string]bool)
// walk through pod entries to put them into specified pool maps
podEntries := p.state.GetPodEntries()
for _, entries := range podEntries {
if entries.IsPoolEntry() {
continue
}
for _, allocationInfo := range entries {
ownerPool := allocationInfo.GetOwnerPoolName()
if ownerPool != advisorapi.EmptyOwnerPoolName {
remainPools[ownerPool] = true
}
}
}
// if pool exists in entries, but has no corresponding container, we need to delete it
poolsToDelete := sets.NewString()
for poolName, entries := range podEntries {
if entries.IsPoolEntry() {
if !remainPools[poolName] && !state.ResidentPools.Has(poolName) {
poolsToDelete.Insert(poolName)
}
}
}
if poolsToDelete.Len() > 0 {
general.Infof("pools to delete: %v", poolsToDelete.UnsortedList())
for _, poolName := range poolsToDelete.UnsortedList() {
delete(podEntries, poolName)
}
machineState, err := generateMachineStateFromPodEntries(p.machineInfo.CPUTopology, podEntries)
if err != nil {
return fmt.Errorf("calculate machineState by podEntries failed with error: %v", err)
}
p.state.SetPodEntries(podEntries)
p.state.SetMachineState(machineState)
} else {
general.Infof("there is no pool to delete")
}
return nil
}
// initReservePool initializes reserve pool for system cores workload
func (p *DynamicPolicy) initReservePool() error {
reserveAllocationInfo := p.state.GetAllocationInfo(state.PoolNameReserve, advisorapi.FakedContainerName)
if reserveAllocationInfo != nil && !reserveAllocationInfo.AllocationResult.IsEmpty() {
general.Infof("pool: %s allocation result transform from %s to %s",
state.PoolNameReserve, reserveAllocationInfo.AllocationResult.String(), p.reservedCPUs)
}
general.Infof("initReservePool %s: %s", state.PoolNameReserve, p.reservedCPUs)
topologyAwareAssignments, err := machine.GetNumaAwareAssignments(p.machineInfo.CPUTopology, p.reservedCPUs)
if err != nil {
return fmt.Errorf("unable to calculate topologyAwareAssignments for pool: %s, result cpuset: %s, error: %v",
state.PoolNameReserve, p.reservedCPUs.String(), err)
}
curReserveAllocationInfo := &state.AllocationInfo{
PodUid: state.PoolNameReserve,
OwnerPoolName: state.PoolNameReserve,
AllocationResult: p.reservedCPUs.Clone(),
OriginalAllocationResult: p.reservedCPUs.Clone(),
TopologyAwareAssignments: topologyAwareAssignments,
OriginalTopologyAwareAssignments: machine.DeepcopyCPUAssignment(topologyAwareAssignments),
}
p.state.SetAllocationInfo(state.PoolNameReserve, advisorapi.FakedContainerName, curReserveAllocationInfo)
return nil
}
// initReclaimPool initializes pools for reclaimed-cores.
// if this info already exists in state-file, just use it, otherwise calculate right away
func (p *DynamicPolicy) initReclaimPool() error {
reclaimedAllocationInfo := p.state.GetAllocationInfo(state.PoolNameReclaim, advisorapi.FakedContainerName)
if reclaimedAllocationInfo == nil {
podEntries := p.state.GetPodEntries()
noneResidentCPUs := podEntries.GetFilteredPoolsCPUSet(state.ResidentPools)
machineState := p.state.GetMachineState()