@@ -32,34 +32,37 @@ import (
32
32
// prefix cached.
33
33
type indexer struct {
34
34
mu sync.RWMutex
35
- hashToPods map [BlockHash ]podSet // the lookup data structure to find pods that have the BlockHash cached
36
- podToLRU map [string ]* lru.Cache [BlockHash , struct {}] // key is pod namespacedName, value is an LRU cache
35
+ hashToPods map [BlockHash ]podSet // the lookup data structure to find pods that have the BlockHash cached
36
+ podToLRU map [ServerID ]* lru.Cache [BlockHash , struct {}] // key is pod namespacedName, value is an LRU cache
37
37
maxLRUSize int
38
38
}
39
39
40
40
// newIndexer initializes an indexer with size limits and starts cache size reporting.
41
41
func newIndexer (maxLRUSize int ) * indexer {
42
42
ix := & indexer {
43
43
hashToPods : make (map [BlockHash ]podSet ),
44
- podToLRU : make (map [string ]* lru.Cache [BlockHash , struct {}]),
44
+ podToLRU : make (map [ServerID ]* lru.Cache [BlockHash , struct {}]),
45
45
maxLRUSize : maxLRUSize ,
46
46
}
47
47
go ix .ReportLRUSize (time .Second )
48
48
return ix
49
49
}
50
50
51
51
// Add adds a list of prefix hashes to the cache, tied to the server.
52
- func (i * indexer ) Add (hashes []BlockHash , pod ServerID ) {
52
+ func (i * indexer ) Add (hashes []BlockHash , pod ServerID ) error {
53
53
if pod .Name == "" {
54
- return
54
+ return fmt . Errorf ( "pod name is empty" )
55
55
}
56
56
i .mu .Lock ()
57
57
// Check if the LRU pod exist
58
- podName := pod .String ()
59
- lruForPod , exists := i .podToLRU [podName ]
58
+ lruForPod , exists := i .podToLRU [pod ]
60
59
if ! exists {
61
- newLRU , _ := lru .NewWithEvict [BlockHash , struct {}](i .maxLRUSize , i .makeEvictionFn (pod ))
62
- i .podToLRU [podName ] = newLRU
60
+ newLRU , err := lru .NewWithEvict [BlockHash , struct {}](i .maxLRUSize , i .makeEvictionFn (pod ))
61
+ if err != nil {
62
+ i .mu .Unlock ()
63
+ return fmt .Errorf ("failed to create LRU for pod %s: %w" , pod , err )
64
+ }
65
+ i .podToLRU [pod ] = newLRU
63
66
lruForPod = newLRU
64
67
}
65
68
i .mu .Unlock ()
@@ -80,7 +83,7 @@ func (i *indexer) Add(hashes []BlockHash, pod ServerID) {
80
83
i .hashToPods [hash ] = pods
81
84
}
82
85
i .mu .Unlock ()
83
-
86
+ return nil
84
87
}
85
88
86
89
// Get returns a set of servers that have the given prefix hash cached.
@@ -100,21 +103,15 @@ func (i *indexer) Get(hash BlockHash) podSet {
100
103
// makeEvictionFn returns a per-pod LRU eviction callback that removes the pod from hashToPods on eviction.
101
104
func (i * indexer ) makeEvictionFn (pod ServerID ) func (BlockHash , struct {}) {
102
105
return func (hash BlockHash , _ struct {}) {
103
- fmt .Printf ("Evicted hash %v from pod %s\n " , hash , pod )
104
-
105
106
i .mu .Lock ()
106
107
defer i .mu .Unlock ()
107
- print ("enter eviction" )
108
108
// Remove the pod from the hash→pods map
109
109
if podSet , ok := i .hashToPods [hash ]; ok {
110
110
delete (podSet , pod )
111
111
if len (podSet ) == 0 {
112
112
delete (i .hashToPods , hash )
113
- } else {
114
- i .hashToPods [hash ] = podSet
115
113
}
116
114
}
117
- print ("After eviction" )
118
115
}
119
116
}
120
117
@@ -126,14 +123,14 @@ func (i *indexer) ReportLRUSize(interval time.Duration) {
126
123
i .mu .RLock ()
127
124
totalEntries := 0
128
125
maxPodEntries := 0
129
- maxPodName := ""
126
+ maxPodName := ServerID {}
130
127
131
- for podName , lruCache := range i .podToLRU {
128
+ for pod , lruCache := range i .podToLRU {
132
129
size := lruCache .Len ()
133
130
totalEntries += size
134
131
if size > maxPodEntries {
135
132
maxPodEntries = size
136
- maxPodName = podName
133
+ maxPodName = pod
137
134
}
138
135
}
139
136
0 commit comments