forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*: workload repository init code (pingcap#1098)
* domain: add repository worker Signed-off-by: xhe <xw897002528@gmail.com> * sessionctx, domain: add function gate Signed-off-by: xhe <xw897002528@gmail.com> * repository: refine context usage Signed-off-by: xhe <xw897002528@gmail.com> * repository: add recover and session getter Signed-off-by: xhe <xw897002528@gmail.com> * sessionctx: hide variable Signed-off-by: xhe <xw897002528@gmail.com> * fix bazel Signed-off-by: xhe <xw897002528@gmail.com> * fix check Signed-off-by: xhe <xw897002528@gmail.com> * repository: refine owner management Signed-off-by: xhe <xw897002528@gmail.com> * repository: free memref Signed-off-by: xhe <xw897002528@gmail.com> * fix check Signed-off-by: xhe <xw897002528@gmail.com> --------- Signed-off-by: xhe <xw897002528@gmail.com>
- Loading branch information
Showing
10 changed files
with
311 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "repository", | ||
srcs = [ | ||
"const.go", | ||
"dest.go", | ||
"table.go", | ||
"worker.go", | ||
], | ||
importpath = "github.com/pingcap/tidb/pkg/domain/repository", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/owner", | ||
"//pkg/sessionctx", | ||
"//pkg/util", | ||
"//pkg/util/logutil", | ||
"@com_github_ngaut_pools//:pools", | ||
"@io_etcd_go_etcd_client_v3//:client", | ||
"@org_uber_go_zap//:zap", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2024 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 repository | ||
|
||
const ( | ||
ownerKey = "/tidb/repository/owner" | ||
promptKey = "repository" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2024 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 repository | ||
|
||
import "strings" | ||
|
||
// ValidateDest will validate destination url, and normalize it. | ||
func ValidateDest(orig string) (string, error) { | ||
// validate S3 URL, etc... | ||
return strings.ToLower(orig), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2024 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 repository | ||
|
||
import ( | ||
"github.com/pingcap/tidb/sessionctx" | ||
) | ||
|
||
func createTable(_ sessionctx.Context) { | ||
} | ||
|
||
func checkTableExists(_ sessionctx.Context) bool { | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// Copyright 2024 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 repository | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/ngaut/pools" | ||
"github.com/pingcap/tidb/owner" | ||
"github.com/pingcap/tidb/sessionctx" | ||
"github.com/pingcap/tidb/util" | ||
"github.com/pingcap/tidb/util/logutil" | ||
clientv3 "go.etcd.io/etcd/client/v3" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type sessionPool interface { | ||
Get() (pools.Resource, error) | ||
Put(resource pools.Resource) | ||
} | ||
|
||
// Worker is the main struct for repository. | ||
type Worker struct { | ||
etcdClient *clientv3.Client | ||
exit chan struct{} | ||
sesspool sessionPool | ||
cancel context.CancelFunc | ||
getGlobalVar func(string) (string, error) | ||
newOwner func(string, string) owner.Manager | ||
owner owner.Manager | ||
wg *util.WaitGroupEnhancedWrapper | ||
} | ||
|
||
// NewWorker creates a new repository worker. | ||
func NewWorker(etcdClient *clientv3.Client, | ||
getGlobalVar func(string) (string, error), | ||
newOwner func(string, string) owner.Manager, | ||
sesspool sessionPool, exit chan struct{}) *Worker { | ||
w := &Worker{ | ||
etcdClient: etcdClient, | ||
exit: exit, | ||
getGlobalVar: getGlobalVar, | ||
sesspool: sesspool, | ||
newOwner: newOwner, | ||
wg: util.NewWaitGroupEnhancedWrapper("repository", exit, false), | ||
} | ||
return w | ||
} | ||
|
||
func (w *Worker) startSnapshot(ctx context.Context) func() { | ||
return func() { | ||
ticker := time.NewTicker(time.Minute) | ||
for { | ||
select { | ||
case <-w.exit: | ||
return | ||
case <-ctx.Done(): | ||
return | ||
case <-ticker.C: | ||
// snapshot thread | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (w *Worker) startSample(ctx context.Context) func() { | ||
return func() { | ||
ticker := time.NewTicker(time.Minute) | ||
for { | ||
select { | ||
case <-w.exit: | ||
return | ||
case <-ctx.Done(): | ||
return | ||
case <-ticker.C: | ||
// sample thread | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (w *Worker) getSessionWithRetry() pools.Resource { | ||
for { | ||
_sessctx, err := w.sesspool.Get() | ||
if err != nil { | ||
logutil.BgLogger().Warn("can not init session for repository") | ||
time.Sleep(time.Second) | ||
continue | ||
} | ||
return _sessctx | ||
} | ||
} | ||
|
||
func (w *Worker) start(ctx context.Context) func() { | ||
return func() { | ||
w.owner = w.newOwner(ownerKey, promptKey) | ||
_sessctx := w.getSessionWithRetry() | ||
defer w.sesspool.Put(_sessctx) | ||
|
||
sess := _sessctx.(sessionctx.Context) | ||
ticker := time.NewTicker(time.Second) | ||
for { | ||
select { | ||
case <-w.exit: | ||
return | ||
case <-ctx.Done(): | ||
return | ||
case <-ticker.C: | ||
if w.owner.IsOwner() { | ||
// create table if not exist | ||
createTable(sess) | ||
} | ||
// check if table exists | ||
if checkTableExists(sess) { | ||
w.wg.RunWithRecover(w.startSample(ctx), func(err interface{}) { | ||
logutil.BgLogger().Info("sample panic", zap.Any("err", err), zap.Stack("stack")) | ||
}, "sample") | ||
w.wg.RunWithRecover(w.startSnapshot(ctx), func(err interface{}) { | ||
logutil.BgLogger().Info("snapshot panic", zap.Any("err", err), zap.Stack("stack")) | ||
}, "snapshot") | ||
return | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Start will start the worker. | ||
func (w *Worker) Start(ctx context.Context) { | ||
if w.cancel != nil { | ||
return | ||
} | ||
|
||
nctx, cancel := context.WithCancel(context.Background()) | ||
w.cancel = cancel | ||
w.wg.RunWithRecover(w.start(nctx), func(err interface{}) { | ||
logutil.BgLogger().Info("prestart panic", zap.Any("err", err), zap.Stack("stack")) | ||
}, "prestart") | ||
} | ||
|
||
// Stop will stop the worker. | ||
func (w *Worker) Stop() { | ||
if w.owner != nil { | ||
w.owner.Cancel() | ||
w.owner = nil | ||
} | ||
if w.cancel != nil { | ||
w.cancel() | ||
w.cancel = nil | ||
} | ||
w.wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters