@@ -22,6 +22,8 @@ import (
2222 "container/list"
2323 "time"
2424
25+ "github.com/google/uuid"
26+ estats "google.golang.org/grpc/experimental/stats"
2527 "google.golang.org/grpc/internal/backoff"
2628 internalgrpclog "google.golang.org/grpc/internal/grpclog"
2729 "google.golang.org/grpc/internal/grpcsync"
@@ -163,22 +165,40 @@ func (l *lru) getLeastRecentlyUsed() cacheKey {
163165//
164166// It is not safe for concurrent access.
165167type dataCache struct {
166- maxSize int64 // Maximum allowed size.
167- currentSize int64 // Current size.
168- keys * lru // Cache keys maintained in lru order.
169- entries map [cacheKey ]* cacheEntry
170- logger * internalgrpclog.PrefixLogger
171- shutdown * grpcsync.Event
168+ maxSize int64 // Maximum allowed size.
169+ currentSize int64 // Current size.
170+ keys * lru // Cache keys maintained in lru order.
171+ entries map [cacheKey ]* cacheEntry
172+ logger * internalgrpclog.PrefixLogger
173+ shutdown * grpcsync.Event
174+ rlsServerTarget string
175+
176+ // Read only after initialization.
177+ grpcTarget string
178+ uuid string
179+ metricsRecorder estats.MetricsRecorder
172180}
173181
174- func newDataCache (size int64 , logger * internalgrpclog.PrefixLogger ) * dataCache {
175- return & dataCache {
176- maxSize : size ,
177- keys : newLRU (),
178- entries : make (map [cacheKey ]* cacheEntry ),
179- logger : logger ,
180- shutdown : grpcsync .NewEvent (),
182+ func newDataCache (size int64 , logger * internalgrpclog.PrefixLogger , metricsRecorder estats.MetricsRecorder , grpcTarget string ) * dataCache {
183+ dc := & dataCache {
184+ maxSize : size ,
185+ keys : newLRU (),
186+ entries : make (map [cacheKey ]* cacheEntry ),
187+ logger : logger ,
188+ shutdown : grpcsync .NewEvent (),
189+ grpcTarget : grpcTarget ,
190+ uuid : uuid .New ().String (),
191+ metricsRecorder : metricsRecorder ,
181192 }
193+ cacheSizeMetric .Record (dc .metricsRecorder , 0 , grpcTarget , "" , dc .uuid )
194+ cacheEntriesMetric .Record (dc .metricsRecorder , 0 , grpcTarget , "" , dc .uuid )
195+ return dc
196+ }
197+
198+ // updateRLSServerTarget updates the RLS Server Target the RLS Balancer is
199+ // configured with.
200+ func (dc * dataCache ) updateRLSServerTarget (rlsServerTarget string ) {
201+ dc .rlsServerTarget = rlsServerTarget
182202}
183203
184204// resize changes the maximum allowed size of the data cache.
@@ -310,6 +330,9 @@ func (dc *dataCache) addEntry(key cacheKey, entry *cacheEntry) (backoffCancelled
310330 if dc .currentSize > dc .maxSize {
311331 backoffCancelled = dc .resize (dc .maxSize )
312332 }
333+
334+ cacheSizeMetric .Record (dc .metricsRecorder , dc .currentSize , dc .grpcTarget , dc .rlsServerTarget , dc .uuid )
335+ cacheEntriesMetric .Record (dc .metricsRecorder , int64 (len (dc .entries )), dc .grpcTarget , dc .rlsServerTarget , dc .uuid )
313336 return backoffCancelled , true
314337}
315338
@@ -319,6 +342,7 @@ func (dc *dataCache) updateEntrySize(entry *cacheEntry, newSize int64) {
319342 dc .currentSize -= entry .size
320343 entry .size = newSize
321344 dc .currentSize += entry .size
345+ cacheSizeMetric .Record (dc .metricsRecorder , dc .currentSize , dc .grpcTarget , dc .rlsServerTarget , dc .uuid )
322346}
323347
324348func (dc * dataCache ) getEntry (key cacheKey ) * cacheEntry {
@@ -351,6 +375,8 @@ func (dc *dataCache) deleteAndcleanup(key cacheKey, entry *cacheEntry) {
351375 delete (dc .entries , key )
352376 dc .currentSize -= entry .size
353377 dc .keys .removeEntry (key )
378+ cacheSizeMetric .Record (dc .metricsRecorder , dc .currentSize , dc .grpcTarget , dc .rlsServerTarget , dc .uuid )
379+ cacheEntriesMetric .Record (dc .metricsRecorder , int64 (len (dc .entries )), dc .grpcTarget , dc .rlsServerTarget , dc .uuid )
354380}
355381
356382func (dc * dataCache ) stop () {
0 commit comments