@@ -32,34 +32,34 @@ import (
3232// prefix cached.
3333type indexer struct {
3434 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
3737 maxLRUSize int
3838}
3939
4040// newIndexer initializes an indexer with size limits and starts cache size reporting.
4141func newIndexer (maxLRUSize int ) * indexer {
4242 ix := & indexer {
4343 hashToPods : make (map [BlockHash ]podSet ),
44- podToLRU : make (map [string ]* lru.Cache [BlockHash , struct {}]),
44+ podToLRU : make (map [ServerID ]* lru.Cache [BlockHash , struct {}]),
4545 maxLRUSize : maxLRUSize ,
4646 }
4747 go ix .ReportLRUSize (time .Second )
4848 return ix
4949}
5050
5151// Add adds a list of prefix hashes to the cache, tied to the server.
52- func (i * indexer ) Add (hashes []BlockHash , pod ServerID ) {
53- if pod .Name == "" {
54- return
55- }
52+ func (i * indexer ) Add (hashes []BlockHash , pod ServerID ) error {
5653 i .mu .Lock ()
5754 // Check if the LRU pod exist
58- podName := pod .String ()
59- lruForPod , exists := i .podToLRU [podName ]
55+ lruForPod , exists := i .podToLRU [pod ]
6056 if ! exists {
61- newLRU , _ := lru .NewWithEvict [BlockHash , struct {}](i .maxLRUSize , i .makeEvictionFn (pod ))
62- i .podToLRU [podName ] = newLRU
57+ newLRU , err := lru .NewWithEvict [BlockHash , struct {}](i .maxLRUSize , i .makeEvictionFn (pod ))
58+ if err != nil {
59+ i .mu .Unlock ()
60+ return fmt .Errorf ("failed to create LRU for pod %s: %w" , pod , err )
61+ }
62+ i .podToLRU [pod ] = newLRU
6363 lruForPod = newLRU
6464 }
6565 i .mu .Unlock ()
@@ -80,7 +80,7 @@ func (i *indexer) Add(hashes []BlockHash, pod ServerID) {
8080 i .hashToPods [hash ] = pods
8181 }
8282 i .mu .Unlock ()
83-
83+ return nil
8484}
8585
8686// Get returns a set of servers that have the given prefix hash cached.
@@ -100,21 +100,15 @@ func (i *indexer) Get(hash BlockHash) podSet {
100100// makeEvictionFn returns a per-pod LRU eviction callback that removes the pod from hashToPods on eviction.
101101func (i * indexer ) makeEvictionFn (pod ServerID ) func (BlockHash , struct {}) {
102102 return func (hash BlockHash , _ struct {}) {
103- fmt .Printf ("Evicted hash %v from pod %s\n " , hash , pod )
104-
105103 i .mu .Lock ()
106104 defer i .mu .Unlock ()
107- print ("enter eviction" )
108105 // Remove the pod from the hash→pods map
109106 if podSet , ok := i .hashToPods [hash ]; ok {
110107 delete (podSet , pod )
111108 if len (podSet ) == 0 {
112109 delete (i .hashToPods , hash )
113- } else {
114- i .hashToPods [hash ] = podSet
115110 }
116111 }
117- print ("After eviction" )
118112 }
119113}
120114
@@ -126,14 +120,14 @@ func (i *indexer) ReportLRUSize(interval time.Duration) {
126120 i .mu .RLock ()
127121 totalEntries := 0
128122 maxPodEntries := 0
129- maxPodName := ""
123+ maxPodName := ServerID {}
130124
131- for podName , lruCache := range i .podToLRU {
125+ for pod , lruCache := range i .podToLRU {
132126 size := lruCache .Len ()
133127 totalEntries += size
134128 if size > maxPodEntries {
135129 maxPodEntries = size
136- maxPodName = podName
130+ maxPodName = pod
137131 }
138132 }
139133
0 commit comments