-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
check_info.go
161 lines (139 loc) · 5.19 KB
/
check_info.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
// Copyright 2020 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 importer
import (
"context"
"strings"
"github.com/docker/go-units"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/br/pkg/storage"
"github.com/pingcap/tidb/lightning/pkg/precheck"
"github.com/pingcap/tidb/pkg/lightning/config"
)
const (
defaultCSVSize = 10 * units.GiB
maxSampleDataSize = 10 * 1024 * 1024
maxSampleRowCount = 10 * 1024
warnEmptyRegionCntPerStore = 500
errorEmptyRegionCntPerStore = 1000
warnRegionCntMinMaxRatio = 0.75
errorRegionCntMinMaxRatio = 0.5
// We only check RegionCntMaxMinRatio when the maximum region count of all stores is larger than this threshold.
checkRegionCntRatioThreshold = 1000
)
func (rc *Controller) isSourceInLocal() bool {
return strings.HasPrefix(rc.store.URI(), storage.LocalURIPrefix)
}
func (rc *Controller) doPreCheckOnItem(ctx context.Context, checkItemID precheck.CheckItemID) error {
theChecker, err := rc.precheckItemBuilder.BuildPrecheckItem(checkItemID)
if err != nil {
return errors.Trace(err)
}
result, err := theChecker.Check(ctx)
if err != nil {
return errors.Trace(err)
}
if result != nil {
rc.checkTemplate.Collect(result.Severity, result.Passed, result.Message)
}
return nil
}
// clusterResource check cluster has enough resource to import data. this test can be skipped.
func (rc *Controller) clusterResource(ctx context.Context) error {
checkCtx := ctx
if rc.taskMgr != nil {
checkCtx = WithPrecheckKey(ctx, taskManagerKey, rc.taskMgr)
}
return rc.doPreCheckOnItem(checkCtx, precheck.CheckTargetClusterSize)
}
// ClusterIsAvailable check cluster is available to import data. this test can be skipped.
func (rc *Controller) ClusterIsAvailable(ctx context.Context) error {
return rc.doPreCheckOnItem(ctx, precheck.CheckTargetClusterVersion)
}
func (rc *Controller) checkEmptyRegion(ctx context.Context) error {
return rc.doPreCheckOnItem(ctx, precheck.CheckTargetClusterEmptyRegion)
}
// checkRegionDistribution checks if regions distribution is unbalanced.
func (rc *Controller) checkRegionDistribution(ctx context.Context) error {
return rc.doPreCheckOnItem(ctx, precheck.CheckTargetClusterRegionDist)
}
// checkClusterRegion checks cluster if there are too many empty regions or region distribution is unbalanced.
func (rc *Controller) checkClusterRegion(ctx context.Context) error {
err := rc.taskMgr.CheckTasksExclusively(ctx, func(tasks []taskMeta) ([]taskMeta, error) {
restoreStarted := false
for _, task := range tasks {
if task.status > taskMetaStatusInitial {
restoreStarted = true
break
}
}
if restoreStarted {
return nil, nil
}
if err := rc.checkEmptyRegion(ctx); err != nil {
return nil, errors.Trace(err)
}
if err := rc.checkRegionDistribution(ctx); err != nil {
return nil, errors.Trace(err)
}
return nil, nil
})
return errors.Trace(err)
}
// StoragePermission checks whether Lightning has enough permission to storage.
func (rc *Controller) StoragePermission(ctx context.Context) error {
return rc.doPreCheckOnItem(ctx, precheck.CheckSourcePermission)
}
// HasLargeCSV checks whether input csvs is fit for Lightning import.
// If strictFormat is false, and csv file is large. Lightning will have performance issue.
// this test cannot be skipped.
func (rc *Controller) HasLargeCSV(ctx context.Context) error {
return rc.doPreCheckOnItem(ctx, precheck.CheckLargeDataFile)
}
// localResource checks the local node has enough resources for this import when local backend enabled;
func (rc *Controller) localResource(ctx context.Context) error {
if rc.isSourceInLocal() {
if err := rc.doPreCheckOnItem(ctx, precheck.CheckLocalDiskPlacement); err != nil {
return errors.Trace(err)
}
}
return rc.doPreCheckOnItem(ctx, precheck.CheckLocalTempKVDir)
}
func (rc *Controller) checkCSVHeader(ctx context.Context) error {
return rc.doPreCheckOnItem(ctx, precheck.CheckCSVHeader)
}
func (rc *Controller) checkTableEmpty(ctx context.Context) error {
if rc.cfg.TikvImporter.Backend == config.BackendTiDB || rc.cfg.TikvImporter.ParallelImport {
return nil
}
return rc.doPreCheckOnItem(ctx, precheck.CheckTargetTableEmpty)
}
func (rc *Controller) checkCheckpoints(ctx context.Context) error {
if !rc.cfg.Checkpoint.Enable {
return nil
}
return rc.doPreCheckOnItem(ctx, precheck.CheckCheckpoints)
}
func (rc *Controller) checkSourceSchema(ctx context.Context) error {
if rc.cfg.TikvImporter.Backend == config.BackendTiDB {
return nil
}
return rc.doPreCheckOnItem(ctx, precheck.CheckSourceSchemaValid)
}
func (rc *Controller) checkCDCPiTR(ctx context.Context) error {
if rc.cfg.TikvImporter.Backend == config.BackendTiDB {
return nil
}
return rc.doPreCheckOnItem(ctx, precheck.CheckTargetUsingCDCPITR)
}