@@ -32,34 +32,37 @@ 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 ) {
52+ func (i * indexer ) Add (hashes []BlockHash , pod ServerID ) error {
5353 if pod .Name == "" {
54- return
54+ return fmt . Errorf ( "pod name is empty" )
5555 }
5656 i .mu .Lock ()
5757 // Check if the LRU pod exist
58- podName := pod .String ()
59- lruForPod , exists := i .podToLRU [podName ]
58+ lruForPod , exists := i .podToLRU [pod ]
6059 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
6366 lruForPod = newLRU
6467 }
6568 i .mu .Unlock ()
@@ -80,7 +83,7 @@ func (i *indexer) Add(hashes []BlockHash, pod ServerID) {
8083 i .hashToPods [hash ] = pods
8184 }
8285 i .mu .Unlock ()
83-
86+ return nil
8487}
8588
8689// Get returns a set of servers that have the given prefix hash cached.
@@ -100,21 +103,15 @@ func (i *indexer) Get(hash BlockHash) podSet {
100103// makeEvictionFn returns a per-pod LRU eviction callback that removes the pod from hashToPods on eviction.
101104func (i * indexer ) makeEvictionFn (pod ServerID ) func (BlockHash , struct {}) {
102105 return func (hash BlockHash , _ struct {}) {
103- fmt .Printf ("Evicted hash %v from pod %s\n " , hash , pod )
104-
105106 i .mu .Lock ()
106107 defer i .mu .Unlock ()
107- print ("enter eviction" )
108108 // Remove the pod from the hash→pods map
109109 if podSet , ok := i .hashToPods [hash ]; ok {
110110 delete (podSet , pod )
111111 if len (podSet ) == 0 {
112112 delete (i .hashToPods , hash )
113- } else {
114- i .hashToPods [hash ] = podSet
115113 }
116114 }
117- print ("After eviction" )
118115 }
119116}
120117
@@ -126,14 +123,14 @@ func (i *indexer) ReportLRUSize(interval time.Duration) {
126123 i .mu .RLock ()
127124 totalEntries := 0
128125 maxPodEntries := 0
129- maxPodName := ""
126+ maxPodName := ServerID {}
130127
131- for podName , lruCache := range i .podToLRU {
128+ for pod , lruCache := range i .podToLRU {
132129 size := lruCache .Len ()
133130 totalEntries += size
134131 if size > maxPodEntries {
135132 maxPodEntries = size
136- maxPodName = podName
133+ maxPodName = pod
137134 }
138135 }
139136
0 commit comments