MCPcopy Create free account
hub / github.com/cortexproject/cortex / syncKVStoreToLocalMap

Method syncKVStoreToLocalMap

pkg/ha/ha_tracker.go:326–401  ·  view source on GitHub ↗

syncKVStoreToLocalMap warms up the local cache by fetching all active entries from the KV store.

(ctx context.Context)

Source from the content-addressed store, hash-verified

324
325// syncKVStoreToLocalMap warms up the local cache by fetching all active entries from the KV store.
326func (c *HATracker) syncKVStoreToLocalMap(ctx context.Context) error {
327 if !c.cfg.EnableHATracker {
328 return nil
329 }
330
331 if !c.cfg.EnableStartupSync {
332 return nil
333 }
334
335 start := time.Now()
336 level.Info(c.logger).Log("msg", "starting HA tracker cache warmup")
337
338 keys, err := c.client.List(ctx, "")
339 if err != nil {
340 level.Error(c.logger).Log("msg", "failed to list keys during HA tracker cache warmup", "err", err)
341 return err
342 }
343
344 if len(keys) == 0 {
345 level.Info(c.logger).Log("msg", "HA tracker cache warmup finished", "reason", "no keys found in KV store")
346 return nil
347 }
348
349 // create temporarily map
350 tempElected := make(map[string]ReplicaDesc, len(keys))
351 tempReplicaGroups := make(map[string]map[string]struct{})
352 successCount := 0
353
354 for _, key := range keys {
355 if ctx.Err() != nil {
356 return ctx.Err()
357 }
358
359 val, err := c.client.Get(ctx, key)
360 if err != nil {
361 level.Warn(c.logger).Log("msg", "failed to fetch key during cache warmup", "key", key, "err", err)
362 continue
363 }
364
365 desc, ok := val.(*ReplicaDesc)
366 if !ok || desc == nil || desc.DeletedAt > 0 {
367 continue
368 }
369
370 user, cluster, keyHasSeparator := strings.Cut(key, "/")
371 if !keyHasSeparator {
372 continue
373 }
374
375 tempElected[key] = *desc
376 if tempReplicaGroups[user] == nil {
377 tempReplicaGroups[user] = make(map[string]struct{})
378 }
379 tempReplicaGroups[user][cluster] = struct{}{}
380 successCount++
381 }
382
383 c.electedLock.Lock()

Calls 7

ListMethod · 0.65
ErrMethod · 0.65
GetMethod · 0.65
LogMethod · 0.45
ErrorMethod · 0.45
CopyMethod · 0.45

Tested by 1