Skip to content

Commit

Permalink
3.9修复主机与实例表唯一索引缺失问题
Browse files Browse the repository at this point in the history
  • Loading branch information
bd-xiaowang committed Aug 6, 2024
1 parent cd0cc08 commit 16e28b2
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/scene_server/admin_server/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,5 @@ import (
_ "configcenter/src/scene_server/admin_server/upgrader/y3.9.202112061431"
_ "configcenter/src/scene_server/admin_server/upgrader/y3.9.202112071130"
_ "configcenter/src/scene_server/admin_server/upgrader/y3.9.202112071431"
_ "configcenter/src/scene_server/admin_server/upgrader/y3.9.202408020955"
)
56 changes: 56 additions & 0 deletions src/scene_server/admin_server/migrate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

#configServer:
# addrs: 127.0.0.1:2181
# usr: cc
# pwd: cc
#registerServer:
# addrs: 127.0.0.1:2181
# usr: cc
# pwd: cc
#mongodb:
# host: 127.0.0.1
# port: 27017
# usr: cc
# pwd: cc
# database: cmdb
# maxOpenConns: 5
# maxIdleConns: 1
# mechanism: SCRAM-SHA-1
# rsName: rs0
#redis:
# host: 127.0.0.1:6379
# pwd: 123456
# database: "0"
# maxOpenConns: 5
# maxIDleConns: 1
#confs:
# dir: /data/cmdb/cmdb_adminserver/configures/
#errors:
# res: /data/cmdb/cmdb_adminserver/conf/errors
#language:
# res: /data/cmdb/cmdb_adminserver/conf/language
#auth:
# address: 127.0.0.1
# appCode: bk_cmdb
# appSecret: 123456

# 配置中心
configServer:
addrs: 127.0.0.1:2181
usr:
pwd:
# 注册中心
registerServer:
addrs: 127.0.0.1:2181
usr:
pwd:
# 指定configures的路径,通过这个路径找到其他的配置文件
confs:
dir: /root/.gvm/pkgsets/go1.17.2/global/src/configcenter/scripts/cmdb_adminserver/configures/
# 指定errors的路径
errors:
res: /root/.gvm/pkgsets/go1.17.2/global/src/configcenter/resources/errors
# 指定language的路径
language:
res: /root/.gvm/pkgsets/go1.17.2/global/src/configcenter/resources/language

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Tencent is pleased to support the open source community by making
* 蓝鲸智云 - 配置平台 (BlueKing - Configuration System) available.
* Copyright (C) 2017 THL A29 Limited,
* a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
* 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.
* We undertake not to change the open source license (MIT license) applicable
* to the current version of the project delivered to anyone in the future.
*/

package y3_9_202408020955

import (
"context"

"configcenter/src/common"
"configcenter/src/common/blog"
"configcenter/src/storage/dal"
"configcenter/src/storage/dal/types"
)

var (
newIndexMap = map[string]types.Index{
common.BKTableNameBaseHost: {
Name: "bkcc_unique_bkHostInnerIP_bkCloudID",
Keys: map[string]int32{
common.BKCloudIDField: 1, common.BKHostInnerIPField: 1,
},
Background: true,
Unique: true,
},
common.BKTableNameBaseInst: {
Name: "bkcc_unique_bkObjectID_bkInstName",
Keys: map[string]int32{
common.BKObjIDField: 1, common.BKInstNameField: 1,
},
Background: true,
Unique: true,
},
}

tableIndexField = map[string][]string{
common.BKTableNameBaseHost: {common.BKCloudIDField, common.BKHostInnerIPField},
common.BKTableNameBaseInst: {common.BKObjIDField, common.BKInstNameField},
}
)

// updateTableIndex update cc_HostBase and cc_ObjectBase table index
func updateTableIndex(ctx context.Context, db dal.RDB) error {
for table, index := range newIndexMap {
existIndexes, err := db.Table(table).Indexes(ctx)
if err != nil {
return err
}

for _, idx := range existIndexes {
if idx.Keys == nil {
continue
}
if len(idx.Keys) != 2 {
continue
}
if idx.Unique == true {
continue
}

_, ok1 := idx.Keys[tableIndexField[table][0]]
_, ok2 := idx.Keys[tableIndexField[table][1]]
if !ok1 || !ok2 {
continue
}

if err := db.Table(table).DropIndex(ctx, idx.Name); err != nil {
blog.Errorf("drop table index failed, table: %s, index: %s, err: %v", table, index.Name, err)
return err
}
}

if err := db.Table(table).CreateIndex(ctx, index); err != nil {
blog.Errorf("create table index failed, table: %s, index: %s, err: %v", table, index.Name, err)
return err
}
}

return nil
}
42 changes: 42 additions & 0 deletions src/scene_server/admin_server/upgrader/y3.9.202408020955/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Tencent is pleased to support the open source community by making
* 蓝鲸智云 - 配置平台 (BlueKing - Configuration System) available.
* Copyright (C) 2017 THL A29 Limited,
* a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://opensource.org/licenses/MIT
* 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.
* We undertake not to change the open source license (MIT license) applicable
* to the current version of the project delivered to anyone in the future.
*/

package y3_9_202408020955

import (
"context"

"configcenter/src/common/blog"
"configcenter/src/scene_server/admin_server/upgrader"
"configcenter/src/storage/dal"
)

func init() {
upgrader.RegistUpgrader("y3.9.202408020955", upgrade)
}

func upgrade(ctx context.Context, db dal.RDB, conf *upgrader.Config) (err error) {
blog.Infof("start execute y3.9.202408020955")

if err = updateTableIndex(ctx, db); err != nil {
blog.Errorf("upgrade y3.9.202408020955 update table index failed, err: %v", err)
return err
}

blog.Infof("upgrade y3.9.202408020955 update table index success")
return nil
}

0 comments on commit 16e28b2

Please sign in to comment.