-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathcompact_table.go
360 lines (322 loc) · 13 KB
/
compact_table.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2022 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 executor
import (
"bytes"
"context"
"encoding/hex"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/log"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/store/driver/backoff"
"github.com/pingcap/tidb/util/chunk"
tikverr "github.com/tikv/client-go/v2/error"
"github.com/tikv/client-go/v2/tikv"
"github.com/tikv/client-go/v2/tikvrpc"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var _ Executor = &CompactTableTiFlashExec{}
const (
compactRequestTimeout = time.Minute * 60 // A single compact request may take at most 1 hour.
compactMaxBackoffSleepMs = 5 * 1000 // Backoff at most 5 seconds for each request.
compactProgressReportInterval = time.Second * 10
)
// TODO: maybe we can cache it.
func getTiFlashStores(ctx sessionctx.Context) ([]infoschema.ServerInfo, error) {
// TODO: Don't use infoschema, to preserve StoreID information.
aliveTiFlashStores := make([]infoschema.ServerInfo, 0)
stores, err := infoschema.GetStoreServerInfo(ctx)
if err != nil {
return nil, err
}
for _, store := range stores {
if store.ServerType == kv.TiFlash.Name() {
aliveTiFlashStores = append(aliveTiFlashStores, store)
}
}
return aliveTiFlashStores, nil
}
// CompactTableTiFlashExec represents an executor for "ALTER TABLE [NAME] COMPACT TIFLASH REPLICA" statement.
type CompactTableTiFlashExec struct {
baseExecutor
tableInfo *model.TableInfo
partitionIDs []int64
done bool
tikvStore tikv.Storage
}
// Next implements the Executor Next interface.
func (e *CompactTableTiFlashExec) Next(ctx context.Context, chk *chunk.Chunk) error {
chk.Reset()
if e.done {
return nil
}
e.done = true
return e.doCompact(ctx)
}
func (e *CompactTableTiFlashExec) doCompact(execCtx context.Context) error {
vars := e.ctx.GetSessionVars()
if e.tableInfo.TiFlashReplica == nil || e.tableInfo.TiFlashReplica.Count == 0 {
vars.StmtCtx.AppendWarning(errors.Errorf("compact skipped: no tiflash replica in the table"))
return nil
}
// We will do a TiFlash compact in this way:
// For each TiFlash instance (in parallel): <--- This is called "storeCompactTask"
// For each partition (in series):
// Send a series of compact request for this partition. <--- Handled by "compactOnePhysicalTable"
tiFlashStores, err := getTiFlashStores(e.ctx)
if err != nil {
return err
}
g, ctx := errgroup.WithContext(execCtx)
// TODO: We may add concurrency control in future.
for _, store := range tiFlashStores {
task := &storeCompactTask{
ctx: ctx,
parentExec: e,
targetStore: store,
}
g.Go(task.work)
}
_ = g.Wait() // Errors have been turned into warnings, let's simply discard them.
return nil
}
// storeCompactTask compacts a logical table described by parentExec in a targetStore.
type storeCompactTask struct {
ctx context.Context // Maybe cancelled by other tasks, or parentExec is killed.
parentExec *CompactTableTiFlashExec
targetStore infoschema.ServerInfo
startAt time.Time
// Fields below are used to output the progress in the log.
allPhysicalTables int
compactedPhysicalTables int
lastProgressOutputAt time.Time
}
func (task *storeCompactTask) work() error {
// We will :
// For each partition (in series):
// Send a series of compact request for this partition. <--- Handled by "compactOnePhysicalTable"
var stopAllTasks bool
var err error
log.Info("Begin compacting table in a store",
zap.String("table", task.parentExec.tableInfo.Name.O),
zap.Int64("table-id", task.parentExec.tableInfo.ID),
zap.Int64s("partition-id", task.parentExec.partitionIDs),
zap.String("store-address", task.targetStore.Address),
)
task.startAt = time.Now()
task.lastProgressOutputAt = task.startAt
if task.parentExec.tableInfo.Partition != nil {
// There is no need for partition-level concurrency, as TiFlash will limit table compaction one at a time.
allPartitions := task.parentExec.partitionIDs
if len(allPartitions) == 0 {
// There are partitions, but user did not specify partitions.
for _, definition := range task.parentExec.tableInfo.Partition.Definitions {
allPartitions = append(allPartitions, definition.ID)
}
}
task.allPhysicalTables = len(allPartitions)
task.compactedPhysicalTables = 0
for _, partitionID := range allPartitions {
stopAllTasks, err = task.compactOnePhysicalTable(partitionID)
task.compactedPhysicalTables++
if err != nil {
// Stop remaining partitions when error happens.
break
}
}
} else {
task.allPhysicalTables = 1
task.compactedPhysicalTables = 0
stopAllTasks, err = task.compactOnePhysicalTable(task.parentExec.tableInfo.ID)
task.compactedPhysicalTables++
}
if err == nil {
log.Info("Compact table finished in a store",
zap.Duration("elapsed", time.Since(task.startAt)),
zap.String("table", task.parentExec.tableInfo.Name.O),
zap.Int64("table-id", task.parentExec.tableInfo.ID),
zap.Int64s("partition-id", task.parentExec.partitionIDs),
zap.String("store-address", task.targetStore.Address),
)
}
if err != nil && stopAllTasks {
// Propagate the error to the errgroup, to stop tasks for other stores.
return err
}
return nil
}
func (task *storeCompactTask) logFailure(otherFields ...zap.Field) {
allFields := []zap.Field{
zap.String("table", task.parentExec.tableInfo.Name.O),
zap.Int64("table-id", task.parentExec.tableInfo.ID),
zap.Int64s("partition-id", task.parentExec.partitionIDs),
zap.String("store-address", task.targetStore.Address),
}
log.Warn("Compact table failed", append(allFields, otherFields...)...)
}
func (task *storeCompactTask) logProgressOptionally() {
// Output progress every 10 seconds.
if time.Since(task.lastProgressOutputAt) > compactProgressReportInterval {
task.lastProgressOutputAt = time.Now()
log.Info("Compact table in progress",
zap.Float64("compacted-ratio", float64(task.compactedPhysicalTables)/float64(task.allPhysicalTables)),
zap.Duration("elapsed", time.Since(task.startAt)),
zap.String("table", task.parentExec.tableInfo.Name.O),
zap.Int64("table-id", task.parentExec.tableInfo.ID),
zap.Int64s("partition-id", task.parentExec.partitionIDs),
zap.String("store-address", task.targetStore.Address),
zap.Int("all-physical-tables", task.allPhysicalTables),
zap.Int("compacted-physical-tables", task.compactedPhysicalTables),
)
}
}
// compactOnePhysicalTable compacts one physical table in the TiFlash store, in an incremental way.
// Returns when compaction is finished. When there are network problems it will retry internally.
//
// There are two kind of errors may be returned:
// A. Error only cancel tasks related with this store, e.g. this store is down even after retry.
//
// The remaining partitions in this store should be cancelled.
//
// B. Error that should cancel tasks of other stores, e.g. CompactErrorCompactInProgress.
//
// The remaining partitions in this store should be cancelled, and tasks of other stores should also be cancelled.
//
// During this function, some "problems" will cause it to early return, e.g. physical table not exist in this
// store any more (maybe caused by DDL). No errors will be produced so that remaining partitions will continue
// being compacted.
//
// Returns: (stopAllTasks, err)
func (task *storeCompactTask) compactOnePhysicalTable(physicalTableID int64) (bool, error) {
var startKey []byte = nil
for { // This loop is to compact incrementally for all data. Each RPC request will only compact a partial of data.
if task.ctx.Err() != nil {
return true, task.ctx.Err()
}
task.logProgressOptionally()
resp, err := task.sendRequestWithRetry(&tikvrpc.Request{
Type: tikvrpc.CmdCompact,
StoreTp: tikvrpc.TiFlash,
Req: &kvrpcpb.CompactRequest{
LogicalTableId: task.parentExec.tableInfo.ID,
PhysicalTableId: physicalTableID,
StartKey: startKey,
},
})
if err != nil {
// Even after backoff, the request is still failed.., or the request is cancelled or timed out
// For example, the store is down. Let's simply don't compact other partitions.
warn := errors.Errorf("compact on store %s failed: %v", task.targetStore.Address, err)
task.parentExec.ctx.GetSessionVars().StmtCtx.AppendWarning(warn)
task.logFailure(
zap.Int64("physical-table-id", physicalTableID),
zap.Error(err))
return false, warn
}
if resp.GetError() != nil {
switch resp.GetError().GetError().(type) {
case *kvrpcpb.CompactError_ErrCompactInProgress:
warn := errors.Errorf("compact on store %s failed: table is compacting in progress", task.targetStore.Address)
task.parentExec.ctx.GetSessionVars().StmtCtx.AppendWarning(warn)
task.logFailure(
zap.Int64("physical-table-id", physicalTableID),
zap.Error(warn))
// TiFlash reported that there are existing compacting for the same table.
// We should stop the whole SQL execution, including compacting requests to other stores, as repeatedly
// compacting the same table is a waste of resource.
return true, warn
case *kvrpcpb.CompactError_ErrTooManyPendingTasks:
// The store is already very busy, don't retry and don't compact other partitions.
warn := errors.Errorf("compact on store %s failed: store is too busy", task.targetStore.Address)
task.parentExec.ctx.GetSessionVars().StmtCtx.AppendWarning(warn)
task.logFailure(
zap.Int64("physical-table-id", physicalTableID),
zap.Error(warn))
return false, warn
case *kvrpcpb.CompactError_ErrPhysicalTableNotExist:
// The physical table does not exist, don't retry this partition, but other partitions should still be compacted.
// This may happen when partition or table is dropped during the long compaction.
log.Info("Compact physical table skipped",
zap.String("table", task.parentExec.tableInfo.Name.O),
zap.Int64("table-id", task.parentExec.tableInfo.ID),
zap.String("store-address", task.targetStore.Address),
zap.Any("response-error", resp.GetError().GetError()))
// We don't need to produce any user warnings.
return false, nil
default:
// Others are unexpected errors, don't retry and don't compact other partitions.
warn := errors.Errorf("compact on store %s failed: internal error (check logs for details)", task.targetStore.Address)
task.parentExec.ctx.GetSessionVars().StmtCtx.AppendWarning(warn)
task.logFailure(
zap.Int64("physical-table-id", physicalTableID),
zap.Any("response-error", resp.GetError().GetError()))
return false, warn
}
}
if !resp.HasRemaining {
return false, nil
}
// Let's send more compact requests, as there are remaining data to compact.
lastEndKey := resp.GetCompactedEndKey()
if len(lastEndKey) == 0 || bytes.Compare(lastEndKey, startKey) <= 0 {
// The TiFlash server returned an invalid compacted end key.
// This is unexpected...
warn := errors.Errorf("compact on store %s failed: internal error (check logs for details)", task.targetStore.Address)
task.parentExec.ctx.GetSessionVars().StmtCtx.AppendWarning(warn)
task.logFailure(
zap.Int64("physical-table-id", physicalTableID),
zap.String("compacted-start-key", hex.EncodeToString(resp.GetCompactedStartKey())),
zap.String("compacted-end-key", hex.EncodeToString(resp.GetCompactedEndKey())),
)
return false, warn
}
startKey = lastEndKey
}
}
// sendRequestWithRetry sends one Compact request to the remote.
// It will backoff and retry when encountering network errors.
func (task *storeCompactTask) sendRequestWithRetry(req *tikvrpc.Request) (*kvrpcpb.CompactResponse, error) {
bo := backoff.NewBackoffer(task.ctx, compactMaxBackoffSleepMs)
for {
resp, err := task.parentExec.tikvStore.
GetTiKVClient().
SendRequest(task.ctx, task.targetStore.Address, req, compactRequestTimeout)
if err != nil {
if errors.Cause(err) == context.Canceled || errors.Cause(err) == context.DeadlineExceeded || status.Code(errors.Cause(err)) == codes.Canceled {
// The request is timed out, or cancelled because of Killed
// No need to retry.
return nil, err
}
if bo.Backoff(tikv.BoTiFlashRPC(), err) != nil {
// Exceeds max sleep time,
return nil, err
}
// Otherwise: let's loop again to retry.
continue
}
if resp.Resp == nil {
// The response is invalid.. This is unexpected, no need to retry.
return nil, tikverr.ErrBodyMissing
}
return resp.Resp.(*kvrpcpb.CompactResponse), nil
}
}