forked from pingcap/tidb
/
coprocessor_cache.go
219 lines (193 loc) · 5.58 KB
/
coprocessor_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package copr
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"time"
"unsafe"
"github.com/dgraph-io/ristretto"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/coprocessor"
"github.com/tikv/client-go/v2/config"
)
type coprCache struct {
cache *ristretto.Cache
admissionMaxRanges int
admissionMaxSize int
admissionMinProcessTime time.Duration
}
type coprCacheValue struct {
Key []byte
Data []byte
TimeStamp uint64
RegionID uint64
RegionDataVersion uint64
// Used in coprocessor paging protocol
PageStart []byte
PageEnd []byte
}
func (v *coprCacheValue) String() string {
return fmt.Sprintf("{ Ts = %d, RegionID = %d, RegionDataVersion = %d, len(Data) = %d }",
v.TimeStamp,
v.RegionID,
v.RegionDataVersion,
len(v.Data))
}
const coprCacheValueSize = int(unsafe.Sizeof(coprCacheValue{}))
func (v *coprCacheValue) Len() int {
return coprCacheValueSize + len(v.Key) + len(v.Data) + len(v.PageStart) + len(v.PageEnd)
}
func newCoprCache(config *config.CoprocessorCache) (*coprCache, error) {
if config == nil || config.CapacityMB == 0 {
return nil, nil
}
capacityInBytes := int64(config.CapacityMB * 1024.0 * 1024.0)
if capacityInBytes <= 0 {
return nil, errors.New("Capacity must be > 0 to enable the cache")
}
maxEntityInBytes := int64(config.AdmissionMaxResultMB * 1024.0 * 1024.0)
if maxEntityInBytes == 0 {
return nil, errors.New("AdmissionMaxResultMB must be > 0 to enable the cache")
}
estimatedEntities := capacityInBytes / maxEntityInBytes * 2
if estimatedEntities < 10 {
estimatedEntities = 10
}
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: estimatedEntities * 10,
MaxCost: capacityInBytes,
BufferItems: 64,
})
if err != nil {
return nil, errors.Trace(err)
}
c := coprCache{
cache: cache,
admissionMaxRanges: int(config.AdmissionMaxRanges),
admissionMaxSize: int(maxEntityInBytes),
admissionMinProcessTime: time.Duration(config.AdmissionMinProcessMs) * time.Millisecond,
}
return &c, nil
}
func coprCacheBuildKey(copReq *coprocessor.Request) ([]byte, error) {
// Calculate amount of space to allocate
if copReq.Tp > math.MaxUint8 {
return nil, errors.New("Request Tp too big")
}
if len(copReq.Data) > math.MaxUint32 {
return nil, errors.New("Cache data too big")
}
totalLength := 1 + 4 + len(copReq.Data)
for _, r := range copReq.Ranges {
if len(r.Start) > math.MaxUint16 {
return nil, errors.New("Cache start key too big")
}
if len(r.End) > math.MaxUint16 {
return nil, errors.New("Cache end key too big")
}
totalLength += 2 + len(r.Start) + 2 + len(r.End)
}
if copReq.PagingSize > 0 {
totalLength++
}
key := make([]byte, totalLength)
// 1 byte Tp
key[0] = uint8(copReq.Tp)
dest := 1
// 4 bytes Data len
binary.LittleEndian.PutUint32(key[dest:], uint32(len(copReq.Data)))
dest += 4
// N bytes Data
copy(key[dest:], copReq.Data)
dest += len(copReq.Data)
for _, r := range copReq.Ranges {
// 2 bytes Key len
binary.LittleEndian.PutUint16(key[dest:], uint16(len(r.Start)))
dest += 2
// N bytes Key
copy(key[dest:], r.Start)
dest += len(r.Start)
// 2 bytes Key len
binary.LittleEndian.PutUint16(key[dest:], uint16(len(r.End)))
dest += 2
// N bytes Key
copy(key[dest:], r.End)
dest += len(r.End)
}
// 1 byte when use paging protocol
if copReq.PagingSize > 0 {
key[dest] = 1
}
return key, nil
}
// Get gets a cache item according to cache key.
func (c *coprCache) Get(key []byte) *coprCacheValue {
if c == nil {
return nil
}
value, hit := c.cache.Get(key)
if !hit {
return nil
}
typedValue := value.(*coprCacheValue)
// ristretto does not handle hash collision, so check the key equality after getting a value.
if !bytes.Equal(typedValue.Key, key) {
return nil
}
return typedValue
}
// CheckRequestAdmission checks whether a response item is worth caching.
func (c *coprCache) CheckRequestAdmission(ranges int) bool {
if c == nil {
return false
}
if c.admissionMaxRanges != 0 && ranges > c.admissionMaxRanges {
return false
}
return true
}
// CheckResponseAdmission checks whether a response item is worth caching.
func (c *coprCache) CheckResponseAdmission(dataSize int, processTime time.Duration, pagingTaskIdx uint32) bool {
if c == nil {
return false
}
if dataSize == 0 || dataSize > c.admissionMaxSize {
return false
}
if pagingTaskIdx > 50 {
return false
}
admissionMinProcessTime := c.admissionMinProcessTime
if pagingTaskIdx > 0 {
admissionMinProcessTime = admissionMinProcessTime / 3
}
if processTime < admissionMinProcessTime {
return false
}
return true
}
// Set inserts an item to the cache.
// It is recommended to call `CheckRequestAdmission` and `CheckResponseAdmission` before inserting
// the item to the cache.
func (c *coprCache) Set(key []byte, value *coprCacheValue) bool {
if c == nil {
return false
}
// Always ensure that the `Key` in `value` is the `key` we received.
value.Key = key
return c.cache.Set(key, value, int64(value.Len()))
}