-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ddl/ingest: add the management infra for lightning struct #37521
Changes from 13 commits
3f8886c
1402e50
2b237aa
693c433
64bf089
d5444f0
b7b7103
5aaa6fb
b1bfdba
5b7817c
c58d2ad
e1a67a9
d65c0b5
37eef0c
3a5cd49
393a165
aca6609
9d02ffb
c6c195e
b6f810e
51f1095
ee5b7f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// 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 lightning | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pingcap/errors" | ||
"github.com/pingcap/tidb/br/pkg/lightning/backend" | ||
"github.com/pingcap/tidb/br/pkg/lightning/backend/kv" | ||
"github.com/pingcap/tidb/br/pkg/lightning/config" | ||
tikv "github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/parser/mysql" | ||
"github.com/pingcap/tidb/table" | ||
"github.com/pingcap/tidb/util/logutil" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// BackendContext store a backend info for add index reorg task. | ||
type BackendContext struct { | ||
jobID int64 | ||
backend *backend.Backend | ||
ctx context.Context | ||
cfg *config.Config | ||
EngMgr engineManager | ||
sysVars map[string]string | ||
diskRoot DiskRoot | ||
done bool | ||
} | ||
|
||
// FinishImport imports all the key-values in engine into the storage, collects the duplicate errors if any, and | ||
// removes the engine from the backend context. | ||
func (bc *BackendContext) FinishImport(indexID int64, unique bool, tbl table.Table) error { | ||
ei, exist := bc.EngMgr.Load(indexID) | ||
if !exist { | ||
return errors.New(LitErrGetEngineFail) | ||
} | ||
|
||
err := ei.ImportAndClean() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Check remote duplicate value for the index. | ||
if unique { | ||
hasDupe, err := bc.backend.CollectRemoteDuplicateRows(bc.ctx, tbl, tbl.Meta().Name.L, &kv.SessionOptions{ | ||
SQLMode: mysql.ModeStrictAllTables, | ||
SysVars: bc.sysVars, | ||
IndexID: ei.indexID, | ||
}) | ||
if err != nil { | ||
logutil.BgLogger().Error(LitInfoRemoteDupCheck, zap.Error(err), | ||
zap.String("table", tbl.Meta().Name.O), zap.Int64("index ID", indexID)) | ||
return errors.New(LitInfoRemoteDupCheck) | ||
} else if hasDupe { | ||
logutil.BgLogger().Error(LitErrRemoteDupExistErr, | ||
zap.String("table", tbl.Meta().Name.O), zap.Int64("index ID", indexID)) | ||
return tikv.ErrKeyExists | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
const importThreshold = 0.85 | ||
|
||
// Flush checks the disk quota and imports the current key-values in engine to the storage. | ||
func (bc *BackendContext) Flush(indexID int64) error { | ||
ei, exist := bc.EngMgr.Load(indexID) | ||
if !exist { | ||
logutil.BgLogger().Error(LitErrGetEngineFail, zap.Int64("index ID", indexID)) | ||
return errors.New(LitErrGetEngineFail) | ||
} | ||
|
||
err := bc.diskRoot.UpdateUsageAndQuota() | ||
if err != nil { | ||
logutil.BgLogger().Error(LitErrUpdateDiskStats, zap.Int64("index ID", indexID)) | ||
return err | ||
} | ||
|
||
if bc.diskRoot.CurrentUsage() >= uint64(importThreshold*float64(bc.diskRoot.MaxQuota())) { | ||
// TODO: it should be changed according checkpoint solution. | ||
// Flush writer cached data into local disk for engine first. | ||
err := ei.Flush() | ||
if err != nil { | ||
return err | ||
} | ||
logutil.BgLogger().Info(LitInfoUnsafeImport, zap.Int64("index ID", indexID), | ||
zap.Uint64("current disk usage", bc.diskRoot.CurrentUsage()), | ||
zap.Uint64("max disk quota", bc.diskRoot.MaxQuota())) | ||
err = bc.backend.UnsafeImportAndReset(bc.ctx, ei.uuid, int64(config.SplitRegionSize)*int64(config.MaxSplitRegionSizeRatio), int64(config.SplitRegionKeys)) | ||
if err != nil { | ||
logutil.BgLogger().Error(LitErrIngestDataErr, zap.Int64("index ID", indexID), | ||
zap.Error(err), zap.Uint64("current disk usage", bc.diskRoot.CurrentUsage()), | ||
zap.Uint64("max disk quota", bc.diskRoot.MaxQuota())) | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Done returns true if the lightning backfill is done. | ||
func (bc *BackendContext) Done() bool { | ||
return bc.done | ||
} | ||
|
||
// SetDone sets the done flag. | ||
func (bc *BackendContext) SetDone() { | ||
bc.done = true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
// 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 lightning | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"math" | ||
|
||
"github.com/pingcap/tidb/br/pkg/lightning/backend" | ||
"github.com/pingcap/tidb/br/pkg/lightning/backend/local" | ||
"github.com/pingcap/tidb/br/pkg/lightning/checkpoints" | ||
"github.com/pingcap/tidb/br/pkg/lightning/config" | ||
"github.com/pingcap/tidb/br/pkg/lightning/errormanager" | ||
"github.com/pingcap/tidb/br/pkg/lightning/glue" | ||
"github.com/pingcap/tidb/br/pkg/lightning/log" | ||
"github.com/pingcap/tidb/parser" | ||
"github.com/pingcap/tidb/parser/model" | ||
"github.com/pingcap/tidb/parser/mysql" | ||
"github.com/pingcap/tidb/util/generic" | ||
"github.com/pingcap/tidb/util/logutil" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type backendCtxManager struct { | ||
generic.SyncMap[int64, *BackendContext] | ||
memRoot MemRoot | ||
diskRoot DiskRoot | ||
} | ||
|
||
func (m *backendCtxManager) init(memRoot MemRoot, diskRoot DiskRoot) { | ||
m.SyncMap = generic.NewSyncMap[int64, *BackendContext](10) | ||
m.memRoot = memRoot | ||
m.diskRoot = diskRoot | ||
} | ||
|
||
// Register creates a new backend and registers it to the backend context. | ||
func (m *backendCtxManager) Register(ctx context.Context, unique bool, jobID int64, _ mysql.SQLMode) (*BackendContext, error) { | ||
bc, exist := m.Load(jobID) | ||
if !exist { | ||
m.memRoot.RefreshConsumption() | ||
ok := m.memRoot.CheckConsume(StructSizeBackendCtx) | ||
if !ok { | ||
return nil, genBackendAllocMemFailedErr(m.memRoot, jobID) | ||
} | ||
cfg, err := generateLightningConfig(m.memRoot, jobID, unique) | ||
if err != nil { | ||
logutil.BgLogger().Warn(LitWarnConfigError, zap.Int64("job ID", jobID), zap.Error(err)) | ||
return nil, err | ||
} | ||
bd, err := createLocalBackend(ctx, cfg, glueLit{}) | ||
if err != nil { | ||
logutil.BgLogger().Error(LitErrCreateBackendFail, zap.Int64("job ID", jobID), zap.Error(err)) | ||
return nil, err | ||
} | ||
|
||
bcCtx := newBackendContext(ctx, jobID, &bd, cfg, defaultImportantVariables, m.memRoot, m.diskRoot) | ||
m.Store(jobID, bcCtx) | ||
|
||
m.memRoot.Consume(StructSizeBackendCtx) | ||
logutil.BgLogger().Info(LitInfoCreateBackend, zap.Int64("job ID", jobID), | ||
zap.Int64("current memory usage", m.memRoot.CurrentUsage()), | ||
zap.Int64("max memory quota", m.memRoot.MaxMemoryQuota()), | ||
zap.Bool("is unique index", unique)) | ||
return bcCtx, nil | ||
} | ||
return bc, nil | ||
} | ||
|
||
func createLocalBackend(ctx context.Context, cfg *config.Config, glue glue.Glue) (backend.Backend, error) { | ||
tls, err := cfg.ToTLS() | ||
if err != nil { | ||
logutil.BgLogger().Error(LitErrCreateBackendFail, zap.Error(err)) | ||
return backend.Backend{}, err | ||
} | ||
|
||
errorMgr := errormanager.New(nil, cfg, log.Logger{Logger: logutil.BgLogger()}) | ||
return local.NewLocalBackend(ctx, tls, cfg, glue, int(LitRLimit), errorMgr) | ||
} | ||
|
||
func newBackendContext(ctx context.Context, jobID int64, be *backend.Backend, | ||
cfg *config.Config, vars map[string]string, memRoot MemRoot, diskRoot DiskRoot) *BackendContext { | ||
bc := &BackendContext{ | ||
jobID: jobID, | ||
backend: be, | ||
ctx: ctx, | ||
cfg: cfg, | ||
sysVars: vars, | ||
diskRoot: diskRoot, | ||
} | ||
bc.EngMgr.init(memRoot, diskRoot) | ||
return bc | ||
} | ||
|
||
// Unregister removes a backend context from the backend context manager. | ||
func (m *backendCtxManager) Unregister(jobID int64) { | ||
bc, exist := m.Load(jobID) | ||
if !exist { | ||
return | ||
} | ||
bc.EngMgr.UnregisterAll(jobID) | ||
bc.backend.Close() | ||
m.memRoot.Release(StructSizeBackendCtx) | ||
m.Delete(jobID) | ||
m.memRoot.ReleaseWithTag(encodeBackendTag(jobID)) | ||
logutil.BgLogger().Info(LitInfoCloseBackend, zap.Int64("job ID", jobID), | ||
zap.Int64("current memory usage", m.memRoot.CurrentUsage()), | ||
zap.Int64("max memory quota", m.memRoot.MaxMemoryQuota())) | ||
} | ||
|
||
// TotalDiskUsage returns the total disk usage of all backends. | ||
func (m *backendCtxManager) TotalDiskUsage() uint64 { | ||
var totalDiskUsed uint64 | ||
for _, key := range m.Keys() { | ||
bc, exists := m.Load(key) | ||
if exists { | ||
_, _, bcDiskUsed, _ := bc.backend.CheckDiskQuota(math.MaxInt64) | ||
totalDiskUsed += uint64(bcDiskUsed) | ||
} | ||
} | ||
return totalDiskUsed | ||
} | ||
|
||
// UpdateMemoryUsage collects the memory usages from all the backend and updates it to the memRoot. | ||
func (m *backendCtxManager) UpdateMemoryUsage() { | ||
for _, key := range m.Keys() { | ||
bc, exists := m.Load(key) | ||
if exists { | ||
curSize := bc.backend.TotalMemoryConsume() | ||
m.memRoot.ReleaseWithTag(encodeBackendTag(bc.jobID)) | ||
m.memRoot.ConsumeWithTag(encodeBackendTag(bc.jobID), curSize) | ||
} | ||
} | ||
} | ||
|
||
type glueLit struct{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add comments for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, it is a noop struct. |
||
|
||
// OwnsSQLExecutor Implement interface OwnsSQLExecutor. | ||
func (glueLit) OwnsSQLExecutor() bool { | ||
return false | ||
} | ||
|
||
// GetSQLExecutor Implement interface GetSQLExecutor. | ||
func (glueLit) GetSQLExecutor() glue.SQLExecutor { | ||
return nil | ||
} | ||
|
||
// GetDB Implement interface GetDB. | ||
func (glueLit) GetDB() (*sql.DB, error) { | ||
return nil, nil | ||
} | ||
|
||
// GetParser Implement interface GetParser. | ||
func (glueLit) GetParser() *parser.Parser { | ||
return nil | ||
} | ||
|
||
// GetTables Implement interface GetTables. | ||
func (glueLit) GetTables(context.Context, string) ([]*model.TableInfo, error) { | ||
return nil, nil | ||
} | ||
|
||
// GetSession Implement interface GetSession. | ||
func (glueLit) GetSession(context.Context) (checkpoints.Session, error) { | ||
return nil, nil | ||
} | ||
|
||
// OpenCheckpointsDB Implement interface OpenCheckpointsDB. | ||
func (glueLit) OpenCheckpointsDB(context.Context, *config.Config) (checkpoints.DB, error) { | ||
return nil, nil | ||
} | ||
|
||
// Record is used to report some information (key, value) to host TiDB, including progress, stage currently. | ||
func (glueLit) Record(string, uint64) { | ||
} | ||
|
||
func encodeBackendTag(jobID int64) string { | ||
return fmt.Sprintf("%d", jobID) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see where
EngMgr.Register
is called. Has it not been called yet or am I reading it wrong?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
engineInfo
is initialized inwritePhysicalTableRecord
. It will be used in the next PR:https://github.com/pingcap/tidb/pull/36767/files#diff-1bbc7833321782b3a22a842b6237974b6a60cb07b49b246b85552ab5e915ed6eR757