Skip to content

Commit

Permalink
pref:分表路由规则优化 TencentBlueKing#11406
Browse files Browse the repository at this point in the history
  • Loading branch information
carlyin0801 committed Jan 14, 2025
1 parent 34f1e7e commit 761fa70
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ data class TableShardingConfig(
@field:BkField(minLength = 1, maxLength = 128)
val tableName: String,
@get:Schema(title = "分表数量")
val shardingNum: Int
val shardingNum: Int,
@get:Schema(title = "表范围")
val tableScope: List<String>
)
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
package com.tencent.devops.project.dao

import com.tencent.devops.common.api.enums.SystemModuleEnum
import com.tencent.devops.common.api.pojo.ShardingRuleTypeEnum
import com.tencent.devops.common.api.util.JsonUtil
import com.tencent.devops.common.api.util.UUIDUtil
import com.tencent.devops.model.project.tables.TTableShardingConfig
import com.tencent.devops.model.project.tables.records.TTableShardingConfigRecord
Expand All @@ -50,6 +52,7 @@ class TableShardingConfigDao {
MODULE_CODE,
TABLE_NAME,
SHARDING_NUM,
TABLE_SCOPE,
CREATOR,
MODIFIER
)
Expand All @@ -59,6 +62,7 @@ class TableShardingConfigDao {
tableShardingConfig.moduleCode.name,
tableShardingConfig.tableName,
tableShardingConfig.shardingNum,
JsonUtil.toJson(tableShardingConfig.tableScope),
userId,
userId
).onDuplicateKeyUpdate()
Expand Down Expand Up @@ -106,14 +110,16 @@ class TableShardingConfigDao {
dslContext: DSLContext,
clusterName: String,
moduleCode: SystemModuleEnum,
tableName: String
tableName: String,
tableRuleType: ShardingRuleTypeEnum = ShardingRuleTypeEnum.TABLE
): TTableShardingConfigRecord? {
return with(TTableShardingConfig.T_TABLE_SHARDING_CONFIG) {
dslContext.selectFrom(this)
.where(
CLUSTER_NAME.eq(clusterName)
.and(MODULE_CODE.eq(moduleCode.name))
.and(TABLE_NAME.eq(tableName))
.and(TABLE_SCOPE.contains(tableRuleType.name))
)
.limit(1)
.fetchOne()
Expand All @@ -123,12 +129,14 @@ class TableShardingConfigDao {
fun listByModule(
dslContext: DSLContext,
clusterName: String,
moduleCode: SystemModuleEnum
moduleCode: SystemModuleEnum,
tableRuleType: ShardingRuleTypeEnum = ShardingRuleTypeEnum.TABLE
): Result<TTableShardingConfigRecord>? {
return with(TTableShardingConfig.T_TABLE_SHARDING_CONFIG) {
val conditions = mutableListOf<Condition>()
conditions.add(CLUSTER_NAME.eq(clusterName))
conditions.add(MODULE_CODE.eq(moduleCode.name))
conditions.add(TABLE_SCOPE.contains(tableRuleType.name))
dslContext.selectFrom(this).where(conditions).fetch()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package com.tencent.devops.project.service

import com.tencent.devops.common.api.enums.SystemModuleEnum
import com.tencent.devops.common.api.pojo.ShardingRuleTypeEnum
import com.tencent.devops.project.pojo.TableShardingConfig
import org.jooq.DSLContext

Expand All @@ -44,12 +45,14 @@ interface TableShardingConfigService {
fun getTableShardingConfigByName(
clusterName: String,
moduleCode: SystemModuleEnum,
tableName: String
tableName: String,
ruleType: ShardingRuleTypeEnum = ShardingRuleTypeEnum.TABLE
): TableShardingConfig?

fun listByModule(
dslContext: DSLContext,
clusterName: String,
moduleCode: SystemModuleEnum
moduleCode: SystemModuleEnum,
ruleType: ShardingRuleTypeEnum = ShardingRuleTypeEnum.TABLE
): List<TableShardingConfig>?
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ class ShardingRoutingRuleAssignServiceImpl @Autowired constructor(
val tableShardingConfigs = tableShardingConfigService.listByModule(
dslContext = dslContext,
clusterName = clusterName,
moduleCode = moduleCode
moduleCode = moduleCode,
ruleType = ShardingRuleTypeEnum.TABLE
)
tableShardingConfigs?.forEach { tableShardingConfig ->
assignTableShardingRoutingRule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class ShardingRoutingRuleFacadeServiceImpl @Autowired constructor(
val tableShardingConfig = tableShardingConfigService.getTableShardingConfigByName(
clusterName = clusterName,
moduleCode = moduleCode,
tableName = tableName
tableName = tableName,
ruleType = ruleType
)
tableShardingConfig?.let {
// 查找该分片规则对应的数据源
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@

package com.tencent.devops.project.service.impl

import com.fasterxml.jackson.core.type.TypeReference
import com.tencent.devops.common.api.constant.CommonMessageCode
import com.tencent.devops.common.api.enums.SystemModuleEnum
import com.tencent.devops.common.api.exception.ErrorCodeException
import com.tencent.devops.common.api.pojo.ShardingRuleTypeEnum
import com.tencent.devops.common.api.util.JsonUtil
import com.tencent.devops.model.project.tables.records.TTableShardingConfigRecord
import com.tencent.devops.project.dao.TableShardingConfigDao
import com.tencent.devops.project.pojo.TableShardingConfig
import com.tencent.devops.project.service.TableShardingConfigService
Expand Down Expand Up @@ -104,48 +108,53 @@ class TableShardingConfigServiceImpl @Autowired constructor(

override fun getTableShardingConfigById(id: String): TableShardingConfig? {
val record = tableShardingConfigDao.getById(dslContext, id)
return if (record != null) {
TableShardingConfig(
clusterName = record.clusterName,
moduleCode = SystemModuleEnum.valueOf(record.moduleCode),
tableName = record.tableName,
shardingNum = record.shardingNum
)
} else {
null
}
return convertTableShardingConfig(record)
}

override fun getTableShardingConfigByName(
clusterName: String,
moduleCode: SystemModuleEnum,
tableName: String
tableName: String,
ruleType: ShardingRuleTypeEnum
): TableShardingConfig? {
val record = tableShardingConfigDao.getByName(
dslContext = dslContext,
clusterName = clusterName,
moduleCode = moduleCode,
tableName = tableName
tableName = tableName,
tableRuleType = ruleType
)
return if (record != null) {
return convertTableShardingConfig(record)
}

private fun convertTableShardingConfig(record: TTableShardingConfigRecord?): TableShardingConfig? {
val tableShardingConfig = if (record != null) {
TableShardingConfig(
clusterName = record.clusterName,
moduleCode = SystemModuleEnum.valueOf(record.moduleCode),
tableName = record.tableName,
shardingNum = record.shardingNum
shardingNum = record.shardingNum,
tableScope = JsonUtil.to(record.tableScope, object : TypeReference<List<String>>() {})
)
} else {
null
}
return tableShardingConfig
}

override fun listByModule(
dslContext: DSLContext,
clusterName: String,
moduleCode: SystemModuleEnum
moduleCode: SystemModuleEnum,
ruleType: ShardingRuleTypeEnum
): List<TableShardingConfig>? {
var tableShardingConfigs: MutableList<TableShardingConfig>? = null
val records = tableShardingConfigDao.listByModule(dslContext, clusterName, moduleCode)
val records = tableShardingConfigDao.listByModule(
dslContext = dslContext,
clusterName = clusterName,
moduleCode = moduleCode,
tableRuleType = ruleType
)
records?.forEach { record ->
if (tableShardingConfigs == null) {
tableShardingConfigs = mutableListOf()
Expand All @@ -155,7 +164,8 @@ class TableShardingConfigServiceImpl @Autowired constructor(
clusterName = record.clusterName,
moduleCode = SystemModuleEnum.valueOf(record.moduleCode),
tableName = record.tableName,
shardingNum = record.shardingNum
shardingNum = record.shardingNum,
tableScope = JsonUtil.to(record.tableScope, object : TypeReference<List<String>>() {})
)
)
}
Expand Down

0 comments on commit 761fa70

Please sign in to comment.