|
@@ -20,11 +20,17 @@ type TableModel interface {
|
|
|
|
|
|
// ThresholdConfig 阈值配置
|
|
|
type ThresholdConfig struct {
|
|
|
- Enabled bool
|
|
|
- MaxRows int64
|
|
|
- CheckInterval time.Duration
|
|
|
- // 不同表的阈值配置
|
|
|
- TableThresholds map[string]int64
|
|
|
+ Enabled bool `mapstructure:"enabled"`
|
|
|
+ MaxRows int64 `mapstructure:"max_rows"`
|
|
|
+ CheckInterval time.Duration `mapstructure:"check_interval"`
|
|
|
+ Tables []TableConfig `mapstructure:"tables"`
|
|
|
+}
|
|
|
+
|
|
|
+// TableConfig 单表配置
|
|
|
+type TableConfig struct {
|
|
|
+ Name string `mapstructure:"name"`
|
|
|
+ Enabled bool `mapstructure:"enabled"`
|
|
|
+ MaxRows int64 `mapstructure:"max_rows"`
|
|
|
}
|
|
|
|
|
|
// ShardingManager 分表管理器
|
|
@@ -34,21 +40,17 @@ type ShardingManager struct {
|
|
|
thresholdConfig *ThresholdConfig
|
|
|
}
|
|
|
|
|
|
-func NewShardingManager(strategy ShardingStrategy, logger *log.Logger) *ShardingManager {
|
|
|
- return &ShardingManager{
|
|
|
- strategy: strategy,
|
|
|
- logger: logger,
|
|
|
- }
|
|
|
-}
|
|
|
|
|
|
-func NewShardingManagerWithThreshold(strategy ShardingStrategy, logger *log.Logger, thresholdConfig *ThresholdConfig) *ShardingManager {
|
|
|
+// NewShardingManager 从配置创建ShardingManager
|
|
|
+func NewShardingManager(strategy ShardingStrategy, logger *log.Logger, config *ThresholdConfig) *ShardingManager {
|
|
|
return &ShardingManager{
|
|
|
strategy: strategy,
|
|
|
logger: logger,
|
|
|
- thresholdConfig: thresholdConfig,
|
|
|
+ thresholdConfig: config,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
// GetWriteTableName 获取写入表名(基于记录的创建时间)
|
|
|
func (sm *ShardingManager) GetWriteTableName(model TableModel) string {
|
|
|
baseTableName := model.GetBaseTableName()
|
|
@@ -188,8 +190,8 @@ func (sm *ShardingManager) findTablesByPattern(db *gorm.DB, pattern string) []st
|
|
|
return tables
|
|
|
}
|
|
|
|
|
|
-// GetOptimalWriteTable 获取最优的写入表(考虑数据量阈值)
|
|
|
-func (sm *ShardingManager) GetOptimalWriteTable(ctx context.Context, db *gorm.DB, model TableModel, maxRows int64) (string, error) {
|
|
|
+// GetOptimalWriteTable 获取最优的写入表(根据model自动获取阈值)
|
|
|
+func (sm *ShardingManager) GetOptimalWriteTable(ctx context.Context, db *gorm.DB, model TableModel) (string, error) {
|
|
|
baseTableName := model.GetBaseTableName()
|
|
|
createdAt := model.GetCreatedAt()
|
|
|
|
|
@@ -205,9 +207,12 @@ func (sm *ShardingManager) GetOptimalWriteTable(ctx context.Context, db *gorm.DB
|
|
|
return baseShardTableName, nil
|
|
|
}
|
|
|
|
|
|
- // 使用配置的maxRows,如果没有则使用默认值
|
|
|
- if maxRows <= 0 {
|
|
|
- maxRows = sm.thresholdConfig.MaxRows
|
|
|
+ // 根据表名自动获取阈值
|
|
|
+ maxRows := sm.GetMaxRowsForTable(baseTableName)
|
|
|
+
|
|
|
+ // 如果返回-1,表示该表禁用了阈值检查,直接返回基础表名
|
|
|
+ if maxRows == -1 {
|
|
|
+ return baseShardTableName, nil
|
|
|
}
|
|
|
|
|
|
// 检查当前表是否已达到阈值
|
|
@@ -270,18 +275,30 @@ func (sm *ShardingManager) CheckAndCreateNewTable(ctx context.Context, db *gorm.
|
|
|
|
|
|
// GetMaxRowsForTable 获取指定表的最大行数配置
|
|
|
func (sm *ShardingManager) GetMaxRowsForTable(tableName string) int64 {
|
|
|
- // 优先使用表级配置
|
|
|
- if sm.thresholdConfig != nil && sm.thresholdConfig.TableThresholds != nil {
|
|
|
- if maxRows, exists := sm.thresholdConfig.TableThresholds[tableName]; exists {
|
|
|
- return maxRows
|
|
|
+ // 检查表级配置
|
|
|
+ if sm.thresholdConfig != nil && sm.thresholdConfig.Tables != nil {
|
|
|
+ for _, tableConfig := range sm.thresholdConfig.Tables {
|
|
|
+ if tableConfig.Name == tableName {
|
|
|
+ if !tableConfig.Enabled {
|
|
|
+ // 表级别禁用分表,返回-1表示无限制
|
|
|
+ return -1
|
|
|
+ }
|
|
|
+ return tableConfig.MaxRows
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 使用默认配置
|
|
|
- if sm.thresholdConfig != nil {
|
|
|
+ // 检查全局配置是否启用
|
|
|
+ if sm.thresholdConfig != nil && !sm.thresholdConfig.Enabled {
|
|
|
+ // 全局禁用阈值检查,返回-1表示无限制
|
|
|
+ return -1
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用全局默认配置
|
|
|
+ if sm.thresholdConfig != nil && sm.thresholdConfig.MaxRows > 0 {
|
|
|
return sm.thresholdConfig.MaxRows
|
|
|
}
|
|
|
|
|
|
- // 最终默认值
|
|
|
- return 3000000
|
|
|
+ // 配置缺失,返回错误而不是默认值
|
|
|
+ panic(fmt.Sprintf("表 '%s' 的阈值配置缺失,请在配置文件中添加相应配置", tableName))
|
|
|
}
|