-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
compactor.go
216 lines (197 loc) · 4.84 KB
/
compactor.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
// Copyright 2015 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package localstore
import (
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/juju/errors"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/localstore/engine"
"github.com/pingcap/tidb/terror"
)
const (
deleteWorkerCnt = 3
)
// compactPolicy defines gc policy of MVCC storage.
type compactPolicy struct {
// SafePoint specifies
SafePoint int
// TriggerInterval specifies how often should the compactor
// scans outdated data.
TriggerInterval time.Duration
// BatchDeleteCnt specifies the batch size for
// deleting outdated data transaction.
BatchDeleteCnt int
}
var localCompactDefaultPolicy = compactPolicy{
SafePoint: 20 * 1000, // in ms
TriggerInterval: 10 * time.Second,
BatchDeleteCnt: 100,
}
type localstoreCompactor struct {
mu sync.Mutex
recentKeys map[string]struct{}
stopCh chan struct{}
delCh chan kv.EncodedKey
workerWaitGroup *sync.WaitGroup
ticker *time.Ticker
db engine.DB
policy compactPolicy
}
func (gc *localstoreCompactor) OnSet(k kv.Key) {
gc.mu.Lock()
defer gc.mu.Unlock()
gc.recentKeys[string(k)] = struct{}{}
}
func (gc *localstoreCompactor) OnDelete(k kv.Key) {
gc.mu.Lock()
defer gc.mu.Unlock()
gc.recentKeys[string(k)] = struct{}{}
}
func (gc *localstoreCompactor) getAllVersions(key kv.Key) ([]kv.EncodedKey, error) {
var keys []kv.EncodedKey
k := key
for ver := kv.MaxVersion; ver.Ver > 0; ver.Ver-- {
mvccK, _, err := gc.db.Seek(MvccEncodeVersionKey(key, ver))
if terror.ErrorEqual(err, engine.ErrNotFound) {
break
}
if err != nil {
return nil, errors.Trace(err)
}
k, ver, err = MvccDecode(mvccK)
if k.Cmp(key) != 0 {
break
}
if err != nil {
return nil, errors.Trace(err)
}
keys = append(keys, mvccK)
}
return keys, nil
}
func (gc *localstoreCompactor) deleteWorker() {
defer gc.workerWaitGroup.Done()
cnt := 0
batch := gc.db.NewBatch()
for {
select {
case <-gc.stopCh:
return
case key := <-gc.delCh:
cnt++
batch.Delete(key)
// Batch delete.
if cnt == gc.policy.BatchDeleteCnt {
log.Debugf("[kv] GC delete commit %d keys", batch.Len())
err := gc.db.Commit(batch)
if err != nil {
log.Error(err)
}
batch = gc.db.NewBatch()
cnt = 0
}
}
}
}
func (gc *localstoreCompactor) checkExpiredKeysWorker() {
defer gc.workerWaitGroup.Done()
for {
select {
case <-gc.stopCh:
log.Debug("[kv] GC stopped")
return
case <-gc.ticker.C:
gc.mu.Lock()
m := gc.recentKeys
if len(m) == 0 {
gc.mu.Unlock()
continue
}
gc.recentKeys = make(map[string]struct{})
gc.mu.Unlock()
for k := range m {
err := gc.Compact([]byte(k))
if err != nil {
log.Error(err)
}
}
}
}
}
func (gc *localstoreCompactor) filterExpiredKeys(keys []kv.EncodedKey) []kv.EncodedKey {
var ret []kv.EncodedKey
first := true
currentTS := time.Now().UnixNano() / int64(time.Millisecond)
// keys are always in descending order.
for _, k := range keys {
_, ver, err := MvccDecode(k)
if err != nil {
// Should not happen.
panic(err)
}
ts := localVersionToTimestamp(ver)
// Check timeout keys.
if currentTS-int64(ts) >= int64(gc.policy.SafePoint) {
// Skip first version.
if first {
first = false
continue
}
ret = append(ret, k)
}
}
return ret
}
func (gc *localstoreCompactor) Compact(k kv.Key) error {
keys, err := gc.getAllVersions(k)
if err != nil {
return errors.Trace(err)
}
filteredKeys := gc.filterExpiredKeys(keys)
for _, key := range filteredKeys {
select {
case <-gc.stopCh:
return nil
case gc.delCh <- key:
}
}
return nil
}
func (gc *localstoreCompactor) Start() {
// Start workers.
gc.workerWaitGroup.Add(deleteWorkerCnt)
for i := 0; i < deleteWorkerCnt; i++ {
go gc.deleteWorker()
}
gc.workerWaitGroup.Add(1)
go gc.checkExpiredKeysWorker()
}
func (gc *localstoreCompactor) Stop() {
gc.ticker.Stop()
close(gc.stopCh)
// Wait for all workers to finish.
gc.workerWaitGroup.Wait()
}
func newLocalCompactor(policy compactPolicy, db engine.DB) *localstoreCompactor {
return &localstoreCompactor{
recentKeys: make(map[string]struct{}),
stopCh: make(chan struct{}),
delCh: make(chan kv.EncodedKey, 100),
ticker: time.NewTicker(policy.TriggerInterval),
policy: policy,
db: db,
workerWaitGroup: &sync.WaitGroup{},
}
}