@@ -3,12 +3,17 @@ package cache
33
44import (
55 "context"
6+ "errors"
7+
8+ "github.com/onflow/flow-go/storage"
9+ "github.com/onflow/flow-go/storage/operation/badgerimpl"
10+ "github.com/rs/zerolog/log"
611 "go.opentelemetry.io/otel/attribute"
712 "go.opentelemetry.io/otel/metric"
813
914 "github.com/dgraph-io/badger/v3"
1015 "github.com/golang/protobuf/proto"
11- "github.com/onflow/rosetta/log"
16+ rosettalog "github.com/onflow/rosetta/log"
1217 "github.com/onflow/rosetta/process"
1318 "github.com/onflow/rosetta/trace"
1419 "google.golang.org/grpc"
@@ -43,12 +48,13 @@ var nonIdempotent = map[string]bool{
4348// The key for each entry is made up by hashing together the request method and
4449// message using BLAKE3. And the value is the protobuf-encoded response value.
4550type Store struct {
46- db * badger.DB
51+ db storage.DB
52+ badger * badger.DB // the underlying badger DB is retained only for the DropAll method.
4753}
4854
4955// DropAll drops all data stored in the underlying cache database.
5056func (s * Store ) DropAll () error {
51- return s .db .DropAll ()
57+ return s .badger .DropAll ()
5258}
5359
5460// InterceptUnary implements the gRPC middleware for caching certain Access API
@@ -80,13 +86,13 @@ func (s *Store) InterceptUnary(ctx context.Context, method string, req, res inte
8086 // different binary versions.
8187 enc , err := proto .Marshal (req .(proto.Message ))
8288 if err != nil {
83- log .Errorf ("Failed to encode the gRPC request for caching: %s" , err )
89+ log .Error (). Msgf ("Failed to encode the gRPC request for caching: %s" , err )
8490 cacheMiss .Add (ctx , 1 , mOpt )
8591 return invoker (ctx , method , req , res , cc , opts ... )
8692 }
8793 hash , err := getHash (method , enc )
8894 if err != nil {
89- log .Errorf ("Failed to hash the gRPC request for caching: %s" , err )
95+ log .Error (). Msgf ("Failed to hash the gRPC request for caching: %s" , err )
9096 cacheMiss .Add (ctx , 1 , mOpt )
9197 return invoker (ctx , method , req , res , cc , opts ... )
9298 }
@@ -96,22 +102,16 @@ func (s *Store) InterceptUnary(ctx context.Context, method string, req, res inte
96102 default :
97103 }
98104 _ , span := trace .NewSpan (ctx , "flow.access_api.cache.Lookup" )
99- err = s .db .View (func (txn * badger.Txn ) error {
100- item , err := txn .Get (hash )
101- if err != nil {
102- return err
103- }
104- return item .Value (func (val []byte ) error {
105- return proto .Unmarshal (val , res .(proto.Message ))
106- })
107- })
105+ item , closer , err := s .db .Reader ().Get (hash )
108106 if err == nil {
107+ err = proto .Unmarshal (item , res .(proto.Message ))
108+ closer .Close ()
109109 trace .EndSpanOk (span )
110110 if debug {
111111 if callerID == "" {
112- log .Infof ("+ Using cached Access API response for %s" , method )
112+ log .Info (). Msgf ("+ Using cached Access API response for %s" , method )
113113 } else {
114- log .Infof (
114+ log .Info (). Msgf (
115115 "+ Using cached Access API response for %s (%s)" ,
116116 method , callerID ,
117117 )
@@ -120,8 +120,8 @@ func (s *Store) InterceptUnary(ctx context.Context, method string, req, res inte
120120 cacheHit .Add (ctx , 1 , mOpt )
121121 return nil
122122 }
123- if err != badger . ErrKeyNotFound {
124- log .Errorf ("Got unexpected error when decoding gRPC response for caching: %s" , err )
123+ if err != storage . ErrNotFound {
124+ log .Error (). Msgf ("Got unexpected error when decoding gRPC response for caching: %s" , err )
125125 trace .EndSpanErr (span , err )
126126 } else {
127127 span .End ()
@@ -138,19 +138,19 @@ func (s *Store) InterceptUnary(ctx context.Context, method string, req, res inte
138138 trace .EndSpanOk (span )
139139 val , err := proto .Marshal (res .(proto.Message ))
140140 if err != nil {
141- log .Fatalf ("Failed to encode gRPC response for caching: %s" , err )
141+ log .Fatal (). Msgf ("Failed to encode gRPC response for caching: %s" , err )
142142 }
143143 select {
144144 case <- ctx .Done ():
145145 return ctx .Err ()
146146 default :
147147 }
148148 _ , span = trace .NewSpan (ctx , "flow.access_api.cache.Store" )
149- err = s .db .Update (func (txn * badger. Txn ) error {
150- return txn .Set (hash , val )
149+ err = s .db .WithReaderBatchWriter (func (rbw storage. ReaderBatchWriter ) error {
150+ return rbw . Writer () .Set (hash , val )
151151 })
152152 if err != nil {
153- log .Errorf ("Got unexpected error when persisting gRPC response for caching: %s" , err )
153+ log .Error (). Msgf ("Got unexpected error when persisting gRPC response for caching: %s" , err )
154154 trace .EndSpanErr (span , err )
155155 } else {
156156 trace .EndSpanOk (span )
@@ -170,19 +170,20 @@ func Context(parent context.Context, callerID string) context.Context {
170170// New opens the database at the given directory and returns the corresponding
171171// Store.
172172func New (dir string ) * Store {
173- opts := badger .DefaultOptions (dir ).WithLogger (log .Badger {Prefix : "cache" })
173+ opts := badger .DefaultOptions (dir ).WithLogger (rosettalog .Badger {Prefix : "cache" })
174174 db , err := badger .Open (opts )
175175 if err != nil {
176- log .Fatalf ("Failed to open the cache database at %s: %s" , dir , err )
176+ log .Fatal (). Msgf ("Failed to open the cache database at %s: %s" , dir , err )
177177 }
178178 process .SetExitHandler (func () {
179- log .Infof ("Closing the cache database" )
179+ log .Info (). Msgf ("Closing the cache database" )
180180 if err := db .Close (); err != nil {
181- log .Errorf ("Got error closing the cache database: %s" , err )
181+ log .Error (). Msgf ("Got error closing the cache database: %s" , err )
182182 }
183183 })
184184 return & Store {
185- db : db ,
185+ db : badgerimpl .ToDB (db ),
186+ badger : db ,
186187 }
187188}
188189
0 commit comments