forked from kubewharf/katalyst-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metacache.go
488 lines (394 loc) · 14.2 KB
/
metacache.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
/*
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 metacache
import (
"fmt"
"reflect"
"sync"
"time"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
"github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/types"
"github.com/kubewharf/katalyst-core/pkg/config"
"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/metric"
"github.com/kubewharf/katalyst-core/pkg/util/machine"
)
// [notice]
// to compatible with checkpoint checksum calculation,
// we should make guarantees below in checkpoint properties assignment
// 1. resource.Quantity use resource.MustParse("0") to initialize, not to use resource.Quantity{}
// 2. CPUSet use NewCPUSet(...) to initialize, not to use CPUSet{}
// 3. not use omitempty in map property and must make new map to do initialization
const (
stateFileName string = "sys_advisor_state"
storeStateWarningDuration = 2 * time.Second
)
// MetaReader provides a standard interface to refer to metadata type
type MetaReader interface {
// GetContainerEntries returns a ContainerEntry copy keyed by pod uid
GetContainerEntries(podUID string) (types.ContainerEntries, bool)
// GetContainerInfo returns a ContainerInfo copy keyed by pod uid and container name
GetContainerInfo(podUID string, containerName string) (*types.ContainerInfo, bool)
// GetContainerMetric returns the metric value of a container
GetContainerMetric(podUID string, containerName string, metricName string) (float64, error)
// RangeContainer applies a function to every podUID, containerName, containerInfo set
RangeContainer(f func(podUID string, containerName string, containerInfo *types.ContainerInfo) bool)
// GetPoolInfo returns a PoolInfo copy by pool name
GetPoolInfo(poolName string) (*types.PoolInfo, bool)
// GetPoolSize returns the size of pool as integer
GetPoolSize(poolName string) (int, bool)
// GetRegionInfo returns a RegionInfo copy by region name
GetRegionInfo(regionName string) (*types.RegionInfo, bool)
// RangeRegionInfo applies a function to every regionName, regionInfo set.
// If f returns false, range stops the iteration.
RangeRegionInfo(f func(regionName string, regionInfo *types.RegionInfo) bool)
}
// RawMetaWriter provides a standard interface to modify raw metadata (generated by other agents) in local cache
type RawMetaWriter interface {
// AddContainer adds a container keyed by pod uid and container name. For repeatedly added
// container, only mutable metadata will be updated, i.e. request quantity changed by vpa
AddContainer(podUID string, containerName string, containerInfo *types.ContainerInfo) error
// SetContainerInfo updates ContainerInfo keyed by pod uid and container name
SetContainerInfo(podUID string, containerName string, containerInfo *types.ContainerInfo) error
// RangeAndUpdateContainer applies a function to every podUID, containerName, containerInfo set.
// Not recommended using if RangeContainer satisfies the requirement.
// If f returns false, range stops the iteration.
RangeAndUpdateContainer(f func(podUID string, containerName string, containerInfo *types.ContainerInfo) bool) error
// DeleteContainer deletes a ContainerInfo keyed by pod uid and container name
DeleteContainer(podUID string, containerName string) error
// RangeAndDeleteContainer applies a function to every podUID, containerName, containerInfo set.
// If f returns true, the containerInfo will be deleted.
RangeAndDeleteContainer(f func(containerInfo *types.ContainerInfo) bool) error
// RemovePod deletes a PodInfo keyed by pod uid. Repeatedly remove will be ignored.
RemovePod(podUID string) error
// SetPoolInfo stores a PoolInfo by pool name
SetPoolInfo(poolName string, poolInfo *types.PoolInfo) error
// DeletePool deletes a PoolInfo keyed by pool name
DeletePool(poolName string) error
// GCPoolEntries deletes GCPoolEntries not existing on node
GCPoolEntries(livingPoolNameSet sets.String) error
}
// AdvisorMetaWriter provides a standard interface to modify advised metadata (generated by sysadvisor)
type AdvisorMetaWriter interface {
// SetRegionEntries overwrites the whole region entries
SetRegionEntries(entries types.RegionEntries) error
// SetRegionInfo stores a RegionInfo by region name
SetRegionInfo(regionName string, regionInfo *types.RegionInfo) error
}
type AdvisorNotifier struct{}
type MetaCache interface {
MetaReader
RawMetaWriter
AdvisorMetaWriter
}
// MetaCacheImp stores metadata and info of pod, node, pool, subnuma etc. as a cache,
// and synchronizes data to sysadvisor state file. It is thread-safe to read and write.
// Deep copy logic is performed during accessing metacache entries instead of directly
// return pointer of each struct to avoid mis-overwrite.
type MetaCacheImp struct {
podEntries types.PodEntries
podMutex sync.RWMutex
poolEntries types.PoolEntries
poolMutex sync.RWMutex
regionEntries types.RegionEntries
regionMutex sync.RWMutex
checkpointManager checkpointmanager.CheckpointManager
checkpointName string
metricsFetcher metric.MetricsFetcher
}
var _ MetaCache = &MetaCacheImp{}
// NewMetaCacheImp returns the single instance of MetaCacheImp
func NewMetaCacheImp(conf *config.Configuration, metricsFetcher metric.MetricsFetcher) (*MetaCacheImp, error) {
stateFileDir := conf.GenericSysAdvisorConfiguration.StateFileDirectory
checkpointManager, err := checkpointmanager.NewCheckpointManager(stateFileDir)
if err != nil {
return nil, fmt.Errorf("failed to initialize checkpoint manager: %v", err)
}
mc := &MetaCacheImp{
podEntries: make(types.PodEntries),
poolEntries: make(types.PoolEntries),
regionEntries: make(types.RegionEntries),
checkpointManager: checkpointManager,
checkpointName: stateFileName,
metricsFetcher: metricsFetcher,
}
// Restore from checkpoint before any function call to metacache api
if err := mc.restoreState(); err != nil {
return mc, err
}
return mc, nil
}
/*
standard implementation for metaReader
*/
func (mc *MetaCacheImp) GetContainerEntries(podUID string) (types.ContainerEntries, bool) {
mc.podMutex.RLock()
defer mc.podMutex.RUnlock()
v, ok := mc.podEntries[podUID]
return v.Clone(), ok
}
func (mc *MetaCacheImp) GetContainerInfo(podUID string, containerName string) (*types.ContainerInfo, bool) {
mc.podMutex.RLock()
defer mc.podMutex.RUnlock()
podInfo, ok := mc.podEntries[podUID]
if !ok {
return nil, false
}
containerInfo, ok := podInfo[containerName]
return containerInfo.Clone(), ok
}
// RangeContainer should deepcopy so that pod and container entries will not be overwritten.
func (mc *MetaCacheImp) RangeContainer(f func(podUID string, containerName string, containerInfo *types.ContainerInfo) bool) {
mc.podMutex.RLock()
defer mc.podMutex.RUnlock()
for podUID, podInfo := range mc.podEntries.Clone() {
for containerName, containerInfo := range podInfo {
if !f(podUID, containerName, containerInfo) {
break
}
}
}
}
func (mc *MetaCacheImp) GetContainerMetric(podUID string, containerName string, metricName string) (float64, error) {
m, err := mc.metricsFetcher.GetContainerMetric(podUID, containerName, metricName)
if err != nil {
return 0, err
}
return m.Value, nil
}
func (mc *MetaCacheImp) GetPoolInfo(poolName string) (*types.PoolInfo, bool) {
mc.poolMutex.RLock()
defer mc.poolMutex.RUnlock()
poolInfo, ok := mc.poolEntries[poolName]
return poolInfo.Clone(), ok
}
func (mc *MetaCacheImp) GetPoolSize(poolName string) (int, bool) {
mc.poolMutex.RLock()
defer mc.poolMutex.RUnlock()
pi, ok := mc.poolEntries[poolName]
if !ok {
return 0, false
}
return machine.CountCPUAssignmentCPUs(pi.TopologyAwareAssignments), true
}
func (mc *MetaCacheImp) GetRegionInfo(regionName string) (*types.RegionInfo, bool) {
mc.regionMutex.RLock()
defer mc.regionMutex.RUnlock()
regionInfo, ok := mc.regionEntries[regionName]
return regionInfo.Clone(), ok
}
func (mc *MetaCacheImp) RangeRegionInfo(f func(regionName string, regionInfo *types.RegionInfo) bool) {
mc.regionMutex.RLock()
defer mc.regionMutex.RUnlock()
for regionName, regionInfo := range mc.regionEntries.Clone() {
if !f(regionName, regionInfo) {
break
}
}
}
/*
standard implementation for RawMetaWriter
*/
func (mc *MetaCacheImp) AddContainer(podUID string, containerName string, containerInfo *types.ContainerInfo) error {
mc.podMutex.Lock()
defer mc.podMutex.Unlock()
if podInfo, ok := mc.podEntries[podUID]; ok {
if ci, ok := podInfo[containerName]; ok {
ci.UpdateMeta(containerInfo)
return nil
}
}
if mc.setContainerInfo(podUID, containerName, containerInfo) {
return mc.storeState()
}
return nil
}
func (mc *MetaCacheImp) SetContainerInfo(podUID string, containerName string, containerInfo *types.ContainerInfo) error {
mc.podMutex.Lock()
defer mc.podMutex.Unlock()
if mc.setContainerInfo(podUID, containerName, containerInfo) {
return mc.storeState()
}
return nil
}
func (mc *MetaCacheImp) setContainerInfo(podUID string, containerName string, containerInfo *types.ContainerInfo) bool {
podInfo, ok := mc.podEntries[podUID]
if !ok {
mc.podEntries[podUID] = make(types.ContainerEntries)
podInfo = mc.podEntries[podUID]
}
if reflect.DeepEqual(podInfo[containerName], containerInfo) {
return false
} else {
podInfo[containerName] = containerInfo
return true
}
}
func (mc *MetaCacheImp) RangeAndUpdateContainer(f func(podUID string, containerName string, containerInfo *types.ContainerInfo) bool) error {
mc.podMutex.Lock()
defer mc.podMutex.Unlock()
oldPodEntries := mc.podEntries.Clone()
for podUID, podInfo := range mc.podEntries {
for containerName, containerInfo := range podInfo {
if !f(podUID, containerName, containerInfo) {
break
}
}
}
if !reflect.DeepEqual(oldPodEntries, mc.podEntries) {
return mc.storeState()
}
return nil
}
func (mc *MetaCacheImp) DeleteContainer(podUID string, containerName string) error {
mc.podMutex.Lock()
defer mc.podMutex.Unlock()
if mc.deleteContainer(podUID, containerName) {
return mc.storeState()
}
return nil
}
func (mc *MetaCacheImp) RangeAndDeleteContainer(f func(containerInfo *types.ContainerInfo) bool) error {
mc.podMutex.Lock()
defer mc.podMutex.Unlock()
needStoreState := false
for _, podInfo := range mc.podEntries {
for _, containerInfo := range podInfo {
if f(containerInfo) {
if mc.deleteContainer(containerInfo.PodUID, containerInfo.ContainerName) {
needStoreState = true
}
}
}
}
if needStoreState {
return mc.storeState()
}
return nil
}
func (mc *MetaCacheImp) deleteContainer(podUID string, containerName string) bool {
podInfo, ok := mc.podEntries[podUID]
if !ok {
return false
}
_, ok = podInfo[containerName]
if !ok {
return false
}
delete(podInfo, containerName)
if len(podInfo) <= 0 {
delete(mc.podEntries, podUID)
}
return true
}
func (mc *MetaCacheImp) RemovePod(podUID string) error {
mc.podMutex.Lock()
defer mc.podMutex.Unlock()
_, ok := mc.podEntries[podUID]
if !ok {
return nil
}
delete(mc.podEntries, podUID)
return mc.storeState()
}
/*
standard implementation for AdvisorMetaWriter
*/
func (mc *MetaCacheImp) SetPoolInfo(poolName string, poolInfo *types.PoolInfo) error {
mc.poolMutex.Lock()
defer mc.poolMutex.Unlock()
if reflect.DeepEqual(mc.poolEntries[poolName], poolInfo) {
return nil
}
mc.poolEntries[poolName] = poolInfo
return mc.storeState()
}
func (mc *MetaCacheImp) DeletePool(poolName string) error {
mc.poolMutex.Lock()
defer mc.poolMutex.Unlock()
if _, ok := mc.poolEntries[poolName]; !ok {
return nil
}
delete(mc.poolEntries, poolName)
return mc.storeState()
}
func (mc *MetaCacheImp) GCPoolEntries(livingPoolNameSet sets.String) error {
mc.poolMutex.Lock()
defer mc.poolMutex.Unlock()
needStoreState := false
for poolName := range mc.poolEntries {
if _, ok := livingPoolNameSet[poolName]; !ok {
delete(mc.poolEntries, poolName)
needStoreState = true
}
}
if needStoreState {
return mc.storeState()
}
return nil
}
func (mc *MetaCacheImp) SetRegionEntries(entries types.RegionEntries) error {
mc.regionMutex.Lock()
defer mc.regionMutex.Unlock()
oldRegionEntries := mc.regionEntries.Clone()
mc.regionEntries = entries.Clone()
if !reflect.DeepEqual(oldRegionEntries, mc.regionEntries) {
return mc.storeState()
}
return nil
}
func (mc *MetaCacheImp) SetRegionInfo(regionName string, regionInfo *types.RegionInfo) error {
mc.regionMutex.Lock()
defer mc.regionMutex.Unlock()
if reflect.DeepEqual(mc.regionEntries[regionName], regionInfo) {
return nil
} else {
mc.regionEntries[regionName] = regionInfo
return mc.storeState()
}
}
/*
other helper functions
*/
func (mc *MetaCacheImp) storeState() error {
checkpoint := NewMetaCacheCheckpoint()
checkpoint.PodEntries = mc.podEntries
checkpoint.PoolEntries = mc.poolEntries
checkpoint.RegionEntries = mc.regionEntries
begin := time.Now()
defer func() {
duration := time.Since(begin)
if duration > storeStateWarningDuration {
klog.ErrorS(fmt.Errorf("storeState took too long"), "storeState took longer than expected", "expected", storeStateWarningDuration, "actual", duration.Round(time.Millisecond))
}
}()
if err := mc.checkpointManager.CreateCheckpoint(mc.checkpointName, checkpoint); err != nil {
klog.Errorf("[metacache] store state failed: %v", err)
return err
}
klog.Infof("[metacache] store state succeeded")
return nil
}
func (mc *MetaCacheImp) restoreState() error {
checkpoint := NewMetaCacheCheckpoint()
if err := mc.checkpointManager.GetCheckpoint(mc.checkpointName, checkpoint); err != nil {
klog.Infof("[metacache] checkpoint %v err %v, create it", mc.checkpointName, err)
return mc.storeState()
}
mc.podEntries = checkpoint.PodEntries
mc.poolEntries = checkpoint.PoolEntries
mc.regionEntries = checkpoint.RegionEntries
klog.Infof("[metacache] restore state succeeded")
return nil
}