Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tikv: add size limit to batch get cache #21015

Merged
merged 6 commits into from
Nov 19, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions store/tikv/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ type tikvSnapshot struct {
// It's OK as long as there are no zero-byte values in the protocol.
mu struct {
sync.RWMutex
hitCnt int64
cached map[string][]byte
stats *SnapshotRuntimeStats
hitCnt int64
cached map[string][]byte
cachedSize int
stats *SnapshotRuntimeStats
}
sampleStep uint32
}
Expand Down Expand Up @@ -174,7 +175,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could change it to a const for

if c.Performance.TxnTotalSizeLimit > 10<<30 {
and this one in another PR.

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