Skip to content

Commit

Permalink
tikv: add size limit to batch get cache (#21015) (#21129)
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
ti-srebot authored Nov 23, 2020
1 parent cc61a9f commit bf4ba97
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions store/tikv/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ type tikvSnapshot struct {
// It's OK as long as there are no zero-byte values in the protocol.
mu struct {
sync.RWMutex
cached map[string][]byte
stats *SnapshotRuntimeStats
cached map[string][]byte
cachedSize int
stats *SnapshotRuntimeStats
}
}

Expand Down Expand Up @@ -169,7 +170,23 @@ func (s *tikvSnapshot) BatchGet(ctx context.Context, keys []kv.Key) (map[string]
s.mu.cached = make(map[string][]byte, len(m))
}
for _, key := range keys {
s.mu.cached[string(key)] = m[string(key)]
val := m[string(key)]
s.mu.cachedSize += len(key) + len(val)
s.mu.cached[string(key)] = val
}

const cachedSizeLimit = 10 << 30
if s.mu.cachedSize >= cachedSizeLimit {
for k, v := range s.mu.cached {
if _, needed := m[k]; needed {
continue
}
delete(s.mu.cached, k)
s.mu.cachedSize -= len(k) + len(v)
if s.mu.cachedSize < cachedSizeLimit {
break
}
}
}
s.mu.Unlock()

Expand Down

0 comments on commit bf4ba97

Please sign in to comment.