Skip to content

Commit 4c72027

Browse files
committed
refactor(dbm-services): 增大遍重载模拟执行任务的次数 #8834
1 parent 23b359d commit 4c72027

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

dbm-services/mysql/db-simulation/app/service/simulation_task.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,12 @@ func init() {
172172
})
173173
go func() {
174174
// 等待一个周期的原因,避免重载执行正常的任务
175-
time.Sleep(time.Duration(HeartbeatInterval) * time.Second)
176-
reloadRunningTaskFromdb()
175+
// 因为http svr 是graceful shutdown, 可能旧的服务会接受请求
176+
for i := 0; i < 5; i++ {
177+
logger.Info("the %d times reload running task", i)
178+
time.Sleep(time.Duration(HeartbeatInterval) * time.Second)
179+
reloadRunningTaskFromdb(i + HeartbeatInterval)
180+
}
177181
rdb.Close()
178182
}()
179183
}
@@ -185,7 +189,8 @@ type ReloadParam struct {
185189
}
186190

187191
// reloadRunningTaskFromdb 重载重启服务前运行的任务 加锁是避免多个服务同时启动,避免相同任务被重载
188-
func reloadRunningTaskFromdb() {
192+
// nolint
193+
func reloadRunningTaskFromdb(heartbeatInterval int) {
189194
key := "simulation:reload:lock"
190195
locker := redislock.New(rdb)
191196
ctx := context.Background()
@@ -200,9 +205,8 @@ func reloadRunningTaskFromdb() {
200205
}()
201206
var tks []model.TbSimulationTask
202207
if err := model.DB.Model(model.TbSimulationTask{}).Where(
203-
//nolint
204-
"phase not in (?) and create_time > DATE_SUB(NOW(),INTERVAL 2 HOUR) and time_to_sec(timediff(now(),heartbeat_time)) > ? ",
205-
[]string{model.PhaseDone, model.PhaseReloading}, HeartbeatInterval).Scan(&tks).Error; err != nil {
208+
"phase not in (?) and create_time > DATE_SUB(NOW(),INTERVAL 2 HOUR) and time_to_sec(timediff(now(),heartbeat_time)) > ?",
209+
[]string{model.PhaseDone, model.PhaseReloading}, heartbeatInterval).Scan(&tks).Error; err != nil {
206210
logger.Error("get running task failed %s", err.Error())
207211
return
208212
}

dbm-services/mysql/db-simulation/app/syntax/syntax.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (tf *TmysqlParseFile) Do(dbtype string, versions []string) (result map[stri
142142
}
143143

144144
func (tf *TmysqlParseFile) doSingleVersion(dbtype string, mysqlVersion string) (err error) {
145-
errChan := make(chan error, len(tf.Param.FileNames))
145+
errChan := make(chan error)
146146
alreadExecutedSqlfileChan := make(chan string, len(tf.Param.FileNames))
147147
signalChan := make(chan struct{})
148148

@@ -257,7 +257,7 @@ func (t *TmysqlParse) delTempDir() {
257257
// Downloadfile download sqlfile
258258
func (tf *TmysqlParseFile) Downloadfile() (err error) {
259259
wg := &sync.WaitGroup{}
260-
errCh := make(chan error, 10)
260+
errCh := make(chan error)
261261
c := make(chan struct{}, 5)
262262
for _, fileName := range tf.Param.FileNames {
263263
wg.Add(1)
@@ -335,7 +335,7 @@ func (tf *TmysqlParseFile) Execute(alreadExecutedSqlfileCh chan string, version
335335
var wg sync.WaitGroup
336336
var errs []error
337337
c := make(chan struct{}, 10) // Semaphore to limit concurrent goroutines
338-
errChan := make(chan error, len(tf.Param.FileNames))
338+
errChan := make(chan error)
339339

340340
// Iterate through all SQL files
341341
for _, fileName := range tf.Param.FileNames {
@@ -383,7 +383,7 @@ func (t *TmysqlParse) AnalyzeParseResult(alreadExecutedSqlfileCh chan string, my
383383
dbtype string) (err error) {
384384
var errs []error
385385
c := make(chan struct{}, 10)
386-
errChan := make(chan error, 5)
386+
errChan := make(chan error)
387387
wg := &sync.WaitGroup{}
388388

389389
for sqlfile := range alreadExecutedSqlfileCh {

dbm-services/mysql/db-simulation/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package main TODO
12
/*
23
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
34
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
@@ -7,7 +8,7 @@
78
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
89
* specific language governing permissions and limitations under the License.
910
*/
10-
11+
// main
1112
package main
1213

1314
import (
@@ -26,7 +27,6 @@ import (
2627
"github.com/gin-gonic/gin"
2728
"github.com/samber/lo"
2829
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
29-
3030
_ "go.uber.org/automaxprocs"
3131

3232
"dbm-services/common/go-pubpkg/apm/metric"

dbm-services/mysql/db-simulation/pkg/util/spider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func ParseGetShardKeyForSpider(tableComment string) (string, error) {
4949
if end-pos <= 0 {
5050
return "", errors.New("parse error")
5151
}
52-
52+
//nolint
5353
len := uint(end - pos)
5454
keyBuf := make([]byte, len)
5555
copy(keyBuf, tableComment[pos:end])

0 commit comments

Comments
 (0)