-
Notifications
You must be signed in to change notification settings - Fork 291
/
infocache.go
509 lines (439 loc) · 13.7 KB
/
infocache.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
//
// (C) Copyright 2020-2024 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
package main
import (
"context"
"fmt"
"net"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"github.com/daos-stack/daos/src/control/build"
"github.com/daos-stack/daos/src/control/common"
"github.com/daos-stack/daos/src/control/lib/atm"
"github.com/daos-stack/daos/src/control/lib/cache"
"github.com/daos-stack/daos/src/control/lib/control"
"github.com/daos-stack/daos/src/control/lib/hardware"
"github.com/daos-stack/daos/src/control/lib/hardware/hwprov"
"github.com/daos-stack/daos/src/control/lib/telemetry"
"github.com/daos-stack/daos/src/control/logging"
)
const (
attachInfoKey = "GetAttachInfo"
fabricKey = "NUMAFabric"
)
type getAttachInfoFn func(ctx context.Context, rpcClient control.UnaryInvoker, req *control.GetAttachInfoReq) (*control.GetAttachInfoResp, error)
type fabricScanFn func(ctx context.Context, providers ...string) (*NUMAFabric, error)
// NewInfoCache creates a new InfoCache with appropriate parameters set.
func NewInfoCache(ctx context.Context, log logging.Logger, client control.UnaryInvoker, cfg *Config) *InfoCache {
ic := &InfoCache{
log: log,
ignoreIfaces: cfg.ExcludeFabricIfaces,
client: client,
cache: cache.NewItemCache(log),
getAttachInfoCb: control.GetAttachInfo,
fabricScan: getFabricScanFn(log, cfg, hwprov.DefaultFabricScanner(log)),
netIfaces: net.Interfaces,
devClassGetter: hwprov.DefaultNetDevClassProvider(log),
devStateGetter: hwprov.DefaultNetDevStateProvider(log),
}
ic.clientTelemetryEnabled.Store(cfg.TelemetryEnabled)
ic.clientTelemetryRetain.Store(cfg.TelemetryRetain > 0)
if cfg.DisableCache {
ic.DisableAttachInfoCache()
ic.DisableFabricCache()
return ic
}
ic.EnableAttachInfoCache(time.Duration(cfg.CacheExpiration))
if len(cfg.FabricInterfaces) > 0 {
nf := NUMAFabricFromConfig(log, cfg.FabricInterfaces)
ic.EnableStaticFabricCache(ctx, nf)
} else {
ic.EnableFabricCache()
}
return ic
}
func getFabricScanFn(log logging.Logger, cfg *Config, scanner *hardware.FabricScanner) fabricScanFn {
return func(ctx context.Context, provs ...string) (*NUMAFabric, error) {
fis, err := scanner.Scan(ctx, provs...)
if err != nil {
return nil, err
}
return NUMAFabricFromScan(ctx, log, fis).WithIgnoredDevices(cfg.ExcludeFabricIfaces), nil
}
}
type cacheItem struct {
sync.Mutex
lastCached time.Time
refreshInterval time.Duration
}
func (ci *cacheItem) isStale() bool {
if ci.refreshInterval == 0 {
return false
}
return ci.lastCached.Add(ci.refreshInterval).Before(time.Now())
}
func (ci *cacheItem) isCached() bool {
return !ci.lastCached.Equal(time.Time{})
}
type cachedAttachInfo struct {
cacheItem
fetch getAttachInfoFn
system string
rpcClient control.UnaryInvoker
lastResponse *control.GetAttachInfoResp
}
func newCachedAttachInfo(refreshInterval time.Duration, system string, rpcClient control.UnaryInvoker, fetchFn getAttachInfoFn) *cachedAttachInfo {
return &cachedAttachInfo{
cacheItem: cacheItem{
refreshInterval: refreshInterval,
},
fetch: fetchFn,
system: system,
rpcClient: rpcClient,
}
}
func sysAttachInfoKey(sys string) string {
return attachInfoKey + "-" + sys
}
// Key returns the key for this system-specific instance of GetAttachInfo.
func (ci *cachedAttachInfo) Key() string {
if ci == nil {
return ""
}
if ci.system == "" {
return attachInfoKey
}
return sysAttachInfoKey(ci.system)
}
// NeedsRefresh checks whether the cached data needs to be refreshed.
func (ci *cachedAttachInfo) NeedsRefresh() bool {
if ci == nil {
return false
}
return !ci.isCached() || ci.isStale()
}
// Refresh contacts the remote management server and refreshes the GetAttachInfo cache.
func (ci *cachedAttachInfo) Refresh(ctx context.Context) error {
if ci == nil {
return errors.New("cachedAttachInfo is nil")
}
req := &control.GetAttachInfoReq{System: ci.system, AllRanks: true}
resp, err := ci.fetch(ctx, ci.rpcClient, req)
if err != nil {
return errors.Wrap(err, "refreshing cached attach info failed")
}
ci.lastResponse = resp
ci.lastCached = time.Now()
return nil
}
type cachedFabricInfo struct {
cacheItem
fetch fabricScanFn
lastResults *NUMAFabric
}
func newCachedFabricInfo(log logging.Logger, fetchFn fabricScanFn) *cachedFabricInfo {
return &cachedFabricInfo{
fetch: fetchFn,
}
}
// Key returns the cache key for the fabric information.
func (cfi *cachedFabricInfo) Key() string {
return fabricKey
}
// NeedsRefresh indicates that the fabric information does not need to be refreshed unless it has
// never been populated.
func (cfi *cachedFabricInfo) NeedsRefresh() bool {
if cfi == nil {
return false
}
return !cfi.isCached()
}
// Refresh scans the hardware for information about the fabric devices and caches the result.
func (cfi *cachedFabricInfo) Refresh(ctx context.Context) error {
if cfi == nil {
return errors.New("cachedFabricInfo is nil")
}
results, err := cfi.fetch(ctx)
if err != nil {
return errors.Wrap(err, "refreshing cached fabric info")
}
cfi.lastResults = results
cfi.lastCached = time.Now()
return nil
}
// InfoCache is a cache for the results of expensive operations needed by the agent.
type InfoCache struct {
log logging.Logger
cache *cache.ItemCache
fabricCacheDisabled atm.Bool
attachInfoCacheDisabled atm.Bool
clientTelemetryEnabled atm.Bool
clientTelemetryRetain atm.Bool
getAttachInfoCb getAttachInfoFn
fabricScan fabricScanFn
netIfaces func() ([]net.Interface, error)
devClassGetter hardware.NetDevClassProvider
devStateGetter hardware.NetDevStateProvider
client control.UnaryInvoker
attachInfoRefresh time.Duration
providers common.StringSet
ignoreIfaces common.StringSet
}
// AddProvider adds a fabric provider to the scan list.
func (c *InfoCache) AddProvider(prov string) {
if c == nil || prov == "" {
return
}
if c.providers == nil {
c.providers = common.NewStringSet()
}
c.providers.Add(prov)
}
// IsAttachInfoEnabled checks whether the GetAttachInfo cache is enabled.
func (c *InfoCache) IsAttachInfoCacheEnabled() bool {
if c == nil {
return false
}
return !c.attachInfoCacheDisabled.Load()
}
// DisableAttachInfoCache fully disables the attach info cache.
func (c *InfoCache) DisableAttachInfoCache() {
if c == nil {
return
}
c.attachInfoCacheDisabled.Store(true)
}
// EnableAttachInfoCache enables a refreshable GetAttachInfo cache.
func (c *InfoCache) EnableAttachInfoCache(interval time.Duration) {
if c == nil {
return
}
c.attachInfoRefresh = interval
c.attachInfoCacheDisabled.Store(false)
}
// IsFabricCacheEnabled checks whether the NUMAFabric cache is enabled.
func (c *InfoCache) IsFabricCacheEnabled() bool {
if c == nil {
return false
}
return !c.fabricCacheDisabled.Load()
}
// DisableFabricCache fully disables the fabric device cache.
func (c *InfoCache) DisableFabricCache() {
if c == nil {
return
}
c.fabricCacheDisabled.Store(true)
}
// EnableFabricCache enables a refreshable local fabric cache.
func (c *InfoCache) EnableFabricCache() {
if c == nil {
return
}
c.fabricCacheDisabled.Store(false)
}
// EnableStaticFabricCache sets up a fabric cache based on a static value that cannot be refreshed.
func (c *InfoCache) EnableStaticFabricCache(ctx context.Context, nf *NUMAFabric) {
if c == nil {
return
}
item := &cachedFabricInfo{
cacheItem: cacheItem{
lastCached: time.Now(),
},
fetch: func(context.Context, ...string) (*NUMAFabric, error) {
return nf, nil
},
lastResults: nf,
}
if err := c.cache.Set(item); err != nil {
c.log.Errorf("error setting static fabric cache: %v", err)
}
c.EnableFabricCache()
}
func (c *InfoCache) getAttachInfo(ctx context.Context, rpcClient control.UnaryInvoker, req *control.GetAttachInfoReq) (*control.GetAttachInfoResp, error) {
if c == nil {
return nil, errors.New("InfoCache is nil")
}
if c.getAttachInfoCb == nil {
return nil, errors.New("getAttachInfoFn is nil")
}
resp, err := c.getAttachInfoCb(ctx, rpcClient, req)
if err != nil {
return nil, err
}
c.addTelemetrySettings(resp)
return resp, nil
}
// addTelemetrySettings modifies the response by adding telemetry settings
// before returning it.
func (c *InfoCache) addTelemetrySettings(resp *control.GetAttachInfoResp) {
if c == nil || resp == nil {
return
}
if c.clientTelemetryEnabled.IsTrue() {
resp.ClientNetHint.EnvVars = append(resp.ClientNetHint.EnvVars,
fmt.Sprintf("%s=1", telemetry.ClientMetricsEnabledEnv),
)
if c.clientTelemetryRetain.IsTrue() {
resp.ClientNetHint.EnvVars = append(resp.ClientNetHint.EnvVars,
fmt.Sprintf("%s=1", telemetry.ClientMetricsRetainEnv),
)
}
}
}
// GetAttachInfo fetches the attach info from the cache, and refreshes if necessary.
func (c *InfoCache) GetAttachInfo(ctx context.Context, sys string) (*control.GetAttachInfoResp, error) {
if c == nil {
return nil, errors.New("InfoCache is nil")
}
if !c.IsAttachInfoCacheEnabled() {
return c.getAttachInfoRemote(ctx, sys)
}
// Use the default system if none is specified.
if sys == "" {
sys = build.DefaultSystemName
}
createItem := func() (cache.Item, error) {
c.log.Debugf("cache miss for %s", sysAttachInfoKey(sys))
cai := newCachedAttachInfo(c.attachInfoRefresh, sys, c.client, c.getAttachInfo)
return cai, nil
}
item, release, err := c.cache.GetOrCreate(ctx, sysAttachInfoKey(sys), createItem)
defer release()
if err != nil {
return nil, errors.Wrap(err, "getting attach info from cache")
}
cai, ok := item.(*cachedAttachInfo)
if !ok {
return nil, errors.Errorf("unexpected attach info data type %T", item)
}
return copyGetAttachInfoResp(cai.lastResponse), nil
}
func copyGetAttachInfoResp(orig *control.GetAttachInfoResp) *control.GetAttachInfoResp {
if orig == nil {
return nil
}
cp := new(control.GetAttachInfoResp)
*cp = *orig
// Copy slices instead of using original pointers
cp.MSRanks = make([]uint32, len(orig.MSRanks))
_ = copy(cp.MSRanks, orig.MSRanks)
cp.ServiceRanks = make([]*control.PrimaryServiceRank, len(orig.ServiceRanks))
_ = copy(cp.ServiceRanks, orig.ServiceRanks)
if orig.ClientNetHint.EnvVars != nil {
cp.ClientNetHint.EnvVars = make([]string, len(orig.ClientNetHint.EnvVars))
_ = copy(cp.ClientNetHint.EnvVars, orig.ClientNetHint.EnvVars)
}
return cp
}
func (c *InfoCache) getAttachInfoRemote(ctx context.Context, sys string) (*control.GetAttachInfoResp, error) {
c.log.Debug("GetAttachInfo not cached, fetching directly from MS")
// Ask the MS for _all_ info, regardless of pbReq.AllRanks, so that the
// cache can serve future "pbReq.AllRanks == true" requests.
req := new(control.GetAttachInfoReq)
req.SetSystem(sys)
req.AllRanks = true
resp, err := c.getAttachInfo(ctx, c.client, req)
if err != nil {
return nil, errors.Wrapf(err, "GetAttachInfo %+v", req)
}
if resp.ClientNetHint.Provider == "" {
return nil, errors.New("GetAttachInfo response contained no provider")
}
return resp, nil
}
// GetFabricDevice returns an appropriate fabric device from the cache based on the requested parameters,
// and refreshes the cache if necessary.
func (c *InfoCache) GetFabricDevice(ctx context.Context, params *FabricIfaceParams) (*FabricInterface, error) {
if c == nil {
return nil, errors.New("InfoCache is nil")
}
nf, err := c.getNUMAFabric(ctx, params.DevClass, params.Provider)
if err != nil {
return nil, err
}
if params.Interface != "" {
fi, err := nf.FindDevice(params)
if err != nil {
return nil, err
}
return fi[0], nil
}
return nf.GetDevice(params)
}
func (c *InfoCache) getNUMAFabric(ctx context.Context, netDevClass hardware.NetDevClass, providers ...string) (*NUMAFabric, error) {
if !c.IsFabricCacheEnabled() {
c.log.Debug("NUMAFabric not cached, rescanning")
if err := c.waitFabricReady(ctx, netDevClass); err != nil {
return nil, err
}
return c.fabricScan(ctx, providers...)
}
createItem := func() (cache.Item, error) {
c.log.Debug("NUMAFabric cache miss")
if err := c.waitFabricReady(ctx, netDevClass); err != nil {
return nil, err
}
return newCachedFabricInfo(c.log, c.fabricScan), nil
}
item, release, err := c.cache.GetOrCreate(ctx, fabricKey, createItem)
defer release()
if err != nil {
return nil, errors.Wrap(err, "getting fabric scan from cache")
}
cfi, ok := item.(*cachedFabricInfo)
if !ok {
return nil, errors.Errorf("unexpected fabric data type %T", item)
}
return cfi.lastResults, nil
}
func (c *InfoCache) waitFabricReady(ctx context.Context, netDevClass hardware.NetDevClass) error {
ifaces, err := c.netIfaces()
if err != nil {
return errors.Wrap(err, "getting net interfaces")
}
var needIfaces []string
for _, iface := range ifaces {
devClass, err := c.devClassGetter.GetNetDevClass(iface.Name)
if err != nil {
return errors.Wrapf(err, "getting device class for %s", iface.Name)
}
if devClass == netDevClass {
needIfaces = append(needIfaces, iface.Name)
}
}
return hardware.WaitFabricReady(ctx, c.log, hardware.WaitFabricReadyParams{
StateProvider: c.devStateGetter,
FabricIfaces: needIfaces,
IgnoreUnusable: true,
IterationSleep: time.Second,
})
}
// Refresh forces any enabled, refreshable caches to re-fetch their content immediately.
func (c *InfoCache) Refresh(ctx context.Context) error {
if c == nil {
return errors.New("InfoCache is nil")
}
if !c.IsAttachInfoCacheEnabled() && !c.IsFabricCacheEnabled() {
return errors.New("all caches are disabled")
}
keys := []string{}
if c.IsFabricCacheEnabled() && c.cache.Has(fabricKey) {
keys = append(keys, fabricKey)
}
if c.IsAttachInfoCacheEnabled() {
for _, k := range c.cache.Keys() {
if strings.HasPrefix(k, attachInfoKey) {
keys = append(keys, k)
}
}
}
c.log.Debugf("refreshing cache keys: %+v", keys)
return c.cache.Refresh(ctx, keys...)
}