store.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "errors"
  17. "sync"
  18. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "github.com/golang/groupcache/lru"
  21. )
  22. var (
  23. DefaultMaxEntries = 2048
  24. ErrCompacted = rpctypes.ErrGRPCCompacted
  25. )
  26. type Cache interface {
  27. Add(req *pb.RangeRequest, resp *pb.RangeResponse)
  28. Get(req *pb.RangeRequest) (*pb.RangeResponse, error)
  29. Compact(revision int64)
  30. }
  31. // keyFunc returns the key of an request, which is used to look up in the cache for it's caching response.
  32. func keyFunc(req *pb.RangeRequest) string {
  33. // TODO: use marshalTo to reduce allocation
  34. b, err := req.Marshal()
  35. if err != nil {
  36. panic(err)
  37. }
  38. return string(b)
  39. }
  40. func NewCache(maxCacheEntries int) Cache {
  41. return &cache{
  42. lru: lru.New(maxCacheEntries),
  43. compactedRev: -1,
  44. }
  45. }
  46. // cache implements Cache
  47. type cache struct {
  48. mu sync.RWMutex
  49. lru *lru.Cache
  50. compactedRev int64
  51. }
  52. // Add adds the response of a request to the cache if its revision is larger than the compacted revision of the cache.
  53. func (c *cache) Add(req *pb.RangeRequest, resp *pb.RangeResponse) {
  54. key := keyFunc(req)
  55. c.mu.Lock()
  56. defer c.mu.Unlock()
  57. if req.Revision > c.compactedRev {
  58. c.lru.Add(key, resp)
  59. }
  60. }
  61. // Get looks up the caching response for a given request.
  62. // Get is also responsible for lazy eviction when accessing compacted entries.
  63. func (c *cache) Get(req *pb.RangeRequest) (*pb.RangeResponse, error) {
  64. key := keyFunc(req)
  65. c.mu.Lock()
  66. defer c.mu.Unlock()
  67. if req.Revision < c.compactedRev {
  68. c.lru.Remove(key)
  69. return nil, ErrCompacted
  70. }
  71. if resp, ok := c.lru.Get(key); ok {
  72. return resp.(*pb.RangeResponse), nil
  73. }
  74. return nil, errors.New("not exist")
  75. }
  76. // Compact invalidate all caching response before the given rev.
  77. // Replace with the invalidation is lazy. The actual removal happens when the entries is accessed.
  78. func (c *cache) Compact(revision int64) {
  79. c.mu.Lock()
  80. defer c.mu.Unlock()
  81. if revision > c.compactedRev {
  82. c.compactedRev = revision
  83. }
  84. }