Files
tidb/pkg/ddl/backfilling_import_cloud.go

167 lines
4.4 KiB
Go

// Copyright 2023 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 ddl
import (
"context"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/pkg/ddl/ingest"
"github.com/pingcap/tidb/pkg/disttask/framework/proto"
"github.com/pingcap/tidb/pkg/disttask/framework/taskexecutor"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/lightning/backend"
"github.com/pingcap/tidb/pkg/lightning/backend/external"
"github.com/pingcap/tidb/pkg/lightning/common"
"github.com/pingcap/tidb/pkg/lightning/config"
"github.com/pingcap/tidb/pkg/meta/model"
"github.com/pingcap/tidb/pkg/parser/terror"
"github.com/pingcap/tidb/pkg/table"
"github.com/pingcap/tidb/pkg/util/logutil"
)
type cloudImportExecutor struct {
taskexecutor.EmptyStepExecutor
job *model.Job
indexes []*model.IndexInfo
ptbl table.PhysicalTable
bc ingest.BackendCtx
cloudStoreURI string
}
func newCloudImportExecutor(
job *model.Job,
indexes []*model.IndexInfo,
ptbl table.PhysicalTable,
bcGetter func() (ingest.BackendCtx, error),
cloudStoreURI string,
) (*cloudImportExecutor, error) {
bc, err := bcGetter()
if err != nil {
return nil, err
}
return &cloudImportExecutor{
job: job,
indexes: indexes,
ptbl: ptbl,
bc: bc,
cloudStoreURI: cloudStoreURI,
}, nil
}
func (*cloudImportExecutor) Init(ctx context.Context) error {
logutil.Logger(ctx).Info("cloud import executor init subtask exec env")
return nil
}
func (m *cloudImportExecutor) RunSubtask(ctx context.Context, subtask *proto.Subtask) error {
logutil.Logger(ctx).Info("cloud import executor run subtask")
sm, err := decodeBackfillSubTaskMeta(subtask.Meta)
if err != nil {
return err
}
local := m.bc.GetLocalBackend()
if local == nil {
return errors.Errorf("local backend not found")
}
var (
currentIdx *model.IndexInfo
idxID int64
)
switch len(sm.EleIDs) {
case 1:
for _, idx := range m.indexes {
if idx.ID == sm.EleIDs[0] {
currentIdx = idx
idxID = idx.ID
break
}
}
case 0:
// maybe this subtask is generated from an old version TiDB
if len(m.indexes) == 1 {
currentIdx = m.indexes[0]
}
idxID = m.indexes[0].ID
default:
return errors.Errorf("unexpected EleIDs count %v", sm.EleIDs)
}
_, engineUUID := backend.MakeUUID(m.ptbl.Meta().Name.L, idxID)
all := external.SortedKVMeta{}
for _, g := range sm.MetaGroups {
all.Merge(g)
}
// compatible with old version task meta
jobKeys := sm.RangeJobKeys
if jobKeys == nil {
jobKeys = sm.RangeSplitKeys
}
err = local.CloseEngine(ctx, &backend.EngineConfig{
External: &backend.ExternalEngineConfig{
StorageURI: m.cloudStoreURI,
DataFiles: sm.DataFiles,
StatFiles: sm.StatFiles,
StartKey: all.StartKey,
EndKey: all.EndKey,
JobKeys: jobKeys,
SplitKeys: sm.RangeSplitKeys,
TotalFileSize: int64(all.TotalKVSize),
TotalKVCount: 0,
CheckHotspot: true,
},
TS: sm.TS,
}, engineUUID)
if err != nil {
return err
}
local.WorkerConcurrency = subtask.Concurrency * 2
err = local.ImportEngine(ctx, engineUUID, int64(config.SplitRegionSize), int64(config.SplitRegionKeys))
if err == nil {
return nil
}
if currentIdx != nil {
return ingest.TryConvertToKeyExistsErr(err, currentIdx, m.ptbl.Meta())
}
// cannot fill the index name for subtask generated from an old version TiDB
tErr, ok := errors.Cause(err).(*terror.Error)
if !ok {
return err
}
if tErr.ID() != common.ErrFoundDuplicateKeys.ID() {
return err
}
return kv.ErrKeyExists
}
func (m *cloudImportExecutor) Cleanup(ctx context.Context) error {
logutil.Logger(ctx).Info("cloud import executor clean up subtask env")
// cleanup backend context
ingest.LitBackCtxMgr.Unregister(m.job.ID)
return nil
}
func (*cloudImportExecutor) OnFinished(ctx context.Context, _ *proto.Subtask) error {
logutil.Logger(ctx).Info("cloud import executor finish subtask")
return nil
}