store.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cache
  15. import (
  16. "sync"
  17. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  18. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  19. "github.com/golang/groupcache/lru"
  20. )
  21. var (
  22. DefaultMaxEntries = 2048
  23. ErrCompacted = rpctypes.ErrGRPCCompacted
  24. )
  25. type Cache interface {
  26. Add(req *pb.RangeRequest, resp *pb.RangeResponse)
  27. Get(req *pb.RangeRequest) (*pb.RangeResponse, error)
  28. Compact(revision int64)
  29. }
  30. // keyFunc returns the key of an request, which is used to look up in the cache for it's caching response.
  31. func keyFunc(req *pb.RangeRequest) string {
  32. // TODO: use marshalTo to reduce allocation
  33. b, err := req.Marshal()
  34. if err != nil {
  35. panic(err)
  36. }
  37. return string(b)
  38. }
  39. func NewCache(maxCacheEntries int) Cache {
  40. return &cache{
  41. lru: lru.New(maxCacheEntries),
  42. }
  43. }
  44. // cache implements Cache
  45. type cache struct {
  46. mu sync.RWMutex
  47. lru *lru.Cache
  48. compactedRev int64
  49. }
  50. // Add adds the response of a request to the cache if its revision is larger than the compacted revision of the cache.
  51. func (c *cache) Add(req *pb.RangeRequest, resp *pb.RangeResponse) {
  52. key := keyFunc(req)
  53. c.mu.Lock()
  54. defer c.mu.Unlock()
  55. if req.Revision > c.compactedRev {
  56. c.lru.Add(key, resp)
  57. }
  58. }
  59. // Get looks up the caching response for a given request.
  60. // Get is also responsible for lazy eviction when accessing compacted entries.
  61. func (c *cache) Get(req *pb.RangeRequest) (*pb.RangeResponse, error) {
  62. key := keyFunc(req)
  63. c.mu.Lock()
  64. defer c.mu.Unlock()
  65. if req.Revision > c.compactedRev {
  66. c.lru.Remove(key)
  67. return nil, ErrCompacted
  68. }
  69. if resp, ok := c.lru.Get(key); ok {
  70. return resp.(*pb.RangeResponse), nil
  71. }
  72. return nil, nil
  73. }
  74. // Compact invalidate all caching response before the given rev.
  75. // Replace with the invalidation is lazy. The actual removal happens when the entries is accessed.
  76. func (c *cache) Compact(revision int64) {
  77. c.mu.Lock()
  78. defer c.mu.Unlock()
  79. if revision > c.compactedRev {
  80. c.compactedRev = revision
  81. }
  82. }