Skip to content

Commit 46c00a6

Browse files
authored
complete memory track module work. (#7)
1 parent 09ca406 commit 46c00a6

File tree

12 files changed

+378
-183
lines changed

12 files changed

+378
-183
lines changed

ddl/ddl.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/pingcap/errors"
3131
"github.com/pingcap/failpoint"
3232
"github.com/pingcap/tidb/config"
33-
lit "github.com/pingcap/tidb/ddl/lightning"
3433
"github.com/pingcap/tidb/ddl/util"
3534
"github.com/pingcap/tidb/infoschema"
3635
"github.com/pingcap/tidb/kv"
@@ -432,12 +431,6 @@ func (d *ddl) Start(ctxPool *pools.ResourcePool) error {
432431
// Start some background routine to manage TiFlash replica.
433432
d.wg.Run(d.PollTiFlashRoutine)
434433

435-
// Init Lighting Global environment. Once met error then the
436-
err := lit.InitGolbalLightningBackendEnv()
437-
if err != nil {
438-
logutil.BgLogger().Warn("Lightning initialize failed ", zap.String("Error", err.Error()))
439-
}
440-
441434
return nil
442435
}
443436

ddl/index_lightning.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Copyright 2022 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
114
package ddl
215

316
import (
@@ -50,8 +63,7 @@ func prepareLightningContext(workId int, keyEngine string, kvCount int) (*lit.Wo
5063
wctx, err := lit.GlobalLightningEnv.LitMemRoot.RegistWorkerContext(workId, keyEngine, kvCount)
5164
if err != nil {
5265
err = errors.New(lit.LERR_CREATE_CONTEX_FAILED)
53-
lit.GlobalLightningEnv.LitMemRoot.ReleaseKVCache(keyEngine + strconv.FormatUint(uint64(workId), 10))
54-
return wctx, err
66+
return nil, err
5567
}
5668
return wctx, err
5769
}
@@ -62,7 +74,7 @@ func prepareBackend(ctx context.Context, unique bool, job *model.Job) (err error
6274
err = lit.GlobalLightningEnv.LitMemRoot.RegistBackendContext(ctx, unique, bcKey)
6375
if err != nil {
6476
err = errors.New(lit.LERR_CREATE_BACKEND_FAILED)
65-
lit.GlobalLightningEnv.LitMemRoot.DeleteBackendContext(bcKey, false)
77+
lit.GlobalLightningEnv.LitMemRoot.DeleteBackendContext(bcKey)
6678
return err
6779
}
6880

@@ -72,10 +84,10 @@ func prepareBackend(ctx context.Context, unique bool, job *model.Job) (err error
7284
func prepareLightningEngine(job *model.Job, t *meta.Meta, workerId int64, tbl *model.TableInfo) (err error) {
7385
bcKey := genBackendContextKey(job.ID)
7486
enginKey := genEngineInfoKey(job.ID, workerId)
75-
_, err = lit.GlobalLightningEnv.LitMemRoot.RegistEngineInfo(job, t, bcKey, enginKey, tbl)
87+
err = lit.GlobalLightningEnv.LitMemRoot.RegistEngineInfo(job, t, bcKey, enginKey, tbl)
7688
if err != nil {
7789
err = errors.New(lit.LERR_CREATE_ENGINE_FAILED)
78-
lit.GlobalLightningEnv.LitMemRoot.DeleteBackendContext(bcKey, true)
90+
lit.GlobalLightningEnv.LitMemRoot.DeleteBackendContext(bcKey)
7991
}
8092
return err
8193
}
@@ -104,8 +116,8 @@ func importIndexDataToStore(ctx context.Context, reorg *reorgInfo, unique bool,
104116
func cleanUpLightningEnv(reorg *reorgInfo) {
105117
if reorg.IsLightningEnabled {
106118
// Close backend
107-
keyBackend := genBackendContextKey(reorg.ID)
108-
lit.GlobalLightningEnv.LitMemRoot.DeleteBackendContext(keyBackend, false)
119+
bcKey := genBackendContextKey(reorg.ID)
120+
lit.GlobalLightningEnv.LitMemRoot.DeleteBackendContext(bcKey)
109121
// at last
110122
reorg.IsLightningEnabled = false
111123
}

ddl/lightning/backend.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Copyright 2022 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
114
package lightning
215

316
import (
@@ -13,16 +26,6 @@ import (
1326
"github.com/pingcap/tidb/util/logutil"
1427
)
1528

16-
type BackendCache struct {
17-
bcCache map[string]*BackendContext
18-
backendNum int
19-
}
20-
21-
func (bca *BackendCache) init() {
22-
bca.bcCache = make(map[string]*BackendContext)
23-
bca.backendNum = 0
24-
}
25-
2629
type BackendContext struct {
2730
Key string // Currently, backend key used ddl job id string
2831
Backend *backend.Backend
@@ -73,7 +76,7 @@ func createLocalBackend(ctx context.Context, unique bool) (backend.Backend, erro
7376
return local.NewLocalBackend(ctx, tls, cfg, nil, int(GlobalLightningEnv.limit), nil)
7477
}
7578

76-
func CloseBackend(key string) error {
77-
err := GlobalLightningEnv.LitMemRoot.DeleteBackendContext(key, false)
78-
return err
79+
func CloseBackend(bcKey string) {
80+
GlobalLightningEnv.LitMemRoot.DeleteBackendContext(bcKey)
81+
return
7982
}

ddl/lightning/context.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

ddl/lightning/engine.go

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Copyright 2022 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
114
package lightning
215

316
import (
@@ -10,7 +23,6 @@ import (
1023
"github.com/pingcap/tidb/br/pkg/lightning/backend"
1124
"github.com/pingcap/tidb/br/pkg/lightning/backend/kv"
1225
"github.com/pingcap/tidb/br/pkg/lightning/checkpoints"
13-
"github.com/pingcap/tidb/br/pkg/lightning/common"
1426
"github.com/pingcap/tidb/br/pkg/lightning/config"
1527
"github.com/pingcap/tidb/meta"
1628
"github.com/pingcap/tidb/parser/model"
@@ -22,31 +34,23 @@ import (
2234
"github.com/twmb/murmur3"
2335
"go.uber.org/zap"
2436
)
25-
2637
type engineInfo struct {
2738
Id int32
2839
key string
29-
backend *backend.Backend
40+
BackCtx *BackendContext
3041
OpenedEngine *backend.OpenedEngine
3142
writer *backend.LocalEngineWriter
3243
cfg *backend.EngineConfig
3344
// TODO: use channel later;
3445
ref int32
35-
kvs []common.KvPair
3646
tbl *model.TableInfo
3747
isOpened bool
38-
//exec *sessionPool
39-
}
40-
41-
func (ei *engineInfo) ResetCache() {
42-
ei.kvs = ei.kvs[:0]
43-
// ei.size = 0
4448
}
4549

46-
func (ei *engineInfo) Init(key string, cfg *backend.EngineConfig, be *backend.Backend, en *backend.OpenedEngine, tbl *model.TableInfo) {
50+
func (ei *engineInfo) Init(key string, cfg *backend.EngineConfig, bCtx *BackendContext, en *backend.OpenedEngine, tbl *model.TableInfo) {
4751
ei.key = key
4852
ei.cfg = cfg
49-
ei.backend = be
53+
ei.BackCtx = bCtx
5054
ei.OpenedEngine = en
5155
ei.tbl = tbl
5256
ei.isOpened = false
@@ -79,7 +83,7 @@ func (ei *engineInfo) unsafeImportAndReset(ctx context.Context) error {
7983
return fmt.Errorf("FinishIndexOp err:%w", err)
8084
}
8185

82-
if err = ei.backend.FlushAll(ctx); err != nil {
86+
if err = ei.BackCtx.Backend.FlushAll(ctx); err != nil {
8387
//LogError("flush engine for disk quota failed, check again later : %v", err)
8488
return err
8589
}
@@ -89,7 +93,7 @@ func (ei *engineInfo) unsafeImportAndReset(ctx context.Context) error {
8993
}
9094
ctx = context.WithValue(ctx, RegionSizeStats, ret)
9195
_, uuid := backend.MakeUUID(ei.tbl.Name.String(), ei.Id)
92-
return ei.backend.UnsafeImportAndReset(ctx, uuid, int64(config.SplitRegionSize)*int64(config.MaxSplitRegionSizeRatio))
96+
return ei.BackCtx.Backend.UnsafeImportAndReset(ctx, uuid, int64(config.SplitRegionSize)*int64(config.MaxSplitRegionSizeRatio))
9397
}
9498

9599
func GenEngineKey(schemaId int64, tableId int64, indexId int64) string {
@@ -124,14 +128,14 @@ func CreateEngine(ctx context.Context, job *model.Job, t *meta.Meta, backendKey
124128
h.Write(b[:])
125129
eid := int32(h.Sum32())
126130

127-
bc := GlobalLightningEnv.BackendCache.bcCache[backendKey]
131+
bc := GlobalLightningEnv.BackendCache[backendKey]
128132
be := bc.Backend
129133

130134
en, err := be.OpenEngine(ctx, &cfg, job.TableName, eid)
131135
if err != nil {
132136
return errors.Errorf("PrepareIndexOp.OpenEngine err:%v", err)
133137
}
134-
ei.Init(engineKey, &cfg, be, en, tblInfo)
138+
ei.Init(engineKey, &cfg, bc, en, tblInfo)
135139
GlobalLightningEnv.EngineManager.engineCache[engineKey] = ei
136140
bc.EngineCache[engineKey] = ei
137141

@@ -156,7 +160,7 @@ func FlushKeyValSync(ctx context.Context, keyEngineInfo string, cache *WorkerKVC
156160
err = ei.unsafeImportAndReset(ctx)
157161
if err != nil {
158162
// LogError("unsafeImportAndReset %s cost %v", size2str(sn.szInc.encodeSize), time.Now().Sub(start))
159-
// 仅仅是导入失败,下次还是可以继续导入,不影响.
163+
// Only import failed, next time can import continue.
160164
return nil
161165
}
162166
sn.importAndReset(start)
@@ -229,7 +233,7 @@ func fetchTableRegionSizeStats(tblId int64, exec sqlexec.RestrictedSQLExecutor)
229233
return ret, nil
230234
}
231235

232-
// TODO: 如果多个 线程 同时调用该函数,是否存在这样情况? 如果存在,该怎么处理?
236+
// TODO: If multi thread call this function, how to handle this logic?
233237
func FinishIndexOp(ctx context.Context, keyEngineInfo string, tbl table.Table, unique bool) (err error) {
234238
ei, err := GlobalLightningEnv.EngineManager.GetEngineInfo(keyEngineInfo)
235239
if err != nil {
@@ -275,7 +279,7 @@ func FinishIndexOp(ctx context.Context, keyEngineInfo string, tbl table.Table, u
275279
return errors.New("engine.Cleanup err")
276280
}
277281
if unique {
278-
hasDupe, err := ei.backend.CollectRemoteDuplicateRows(ctx, tbl, ei.tbl.Name.O, &kv.SessionOptions{
282+
hasDupe, err := ei.BackCtx.Backend.CollectRemoteDuplicateRows(ctx, tbl, ei.tbl.Name.O, &kv.SessionOptions{
279283
SQLMode: mysql.ModeStrictAllTables,
280284
SysVars: defaultImportantVariables,
281285
})
@@ -291,13 +295,3 @@ func FinishIndexOp(ctx context.Context, keyEngineInfo string, tbl table.Table, u
291295
GlobalLightningEnv.EngineManager.ReleaseEngine(keyEngineInfo)
292296
return nil
293297
}
294-
295-
var defaultImportantVariables = map[string]string{
296-
"max_allowed_packet": "67108864",
297-
"div_precision_increment": "4",
298-
"time_zone": "SYSTEM",
299-
"lc_time_names": "en_US",
300-
"default_week_format": "0",
301-
"block_encryption_mode": "aes-128-ecb",
302-
"group_concat_max_len": "1024",
303-
}

ddl/lightning/engine_mgr.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Copyright 2022 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
114
package lightning
215

316
import (
@@ -45,5 +58,9 @@ func (em *EngineManager) GetWriter(key string) (*backend.LocalEngineWriter, erro
4558
}
4659

4760
func (em *EngineManager) ReleaseEngine(key string) {
48-
delete(em.engineCache, key)
61+
ei, exist := em.engineCache[key]
62+
if !exist {
63+
return
64+
}
65+
ei.isOpened = false
4966
}

0 commit comments

Comments
 (0)