mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
fix: support db checksum algorithm of SHA256 (#19681)
Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
@@ -14,6 +14,8 @@
|
||||
|
||||
package consts
|
||||
|
||||
import "yunion.io/x/log"
|
||||
|
||||
var (
|
||||
QueryOffsetOptimization = false
|
||||
|
||||
@@ -22,6 +24,8 @@ var (
|
||||
defaultDBDialect string
|
||||
|
||||
defaultDBConnectionString string
|
||||
|
||||
defaultDBChecksumHashAlgorithm string
|
||||
)
|
||||
|
||||
func SetDefaultDB(dialect, connStr string) {
|
||||
@@ -36,3 +40,15 @@ func DefaultDBDialect() string {
|
||||
func DefaultDBConnStr() string {
|
||||
return defaultDBConnectionString
|
||||
}
|
||||
|
||||
func SetDefaultDBChecksumHashAlgorithm(alg string) {
|
||||
log.Infof("Set default DB checksum hash algorithm: %s", alg)
|
||||
defaultDBChecksumHashAlgorithm = alg
|
||||
}
|
||||
|
||||
func DefaultDBChecksumHashAlgorithm() string {
|
||||
if len(defaultDBChecksumHashAlgorithm) > 0 {
|
||||
return defaultDBChecksumHashAlgorithm
|
||||
}
|
||||
return "sha256"
|
||||
}
|
||||
|
||||
@@ -143,6 +143,10 @@ func InitDB(options *common_options.DBOptions) {
|
||||
}
|
||||
// lm := lockman.NewNoopLockManager()
|
||||
|
||||
if options.EnableDBChecksumTables && len(options.DBChecksumHashAlgorithm) > 0 {
|
||||
consts.SetDefaultDBChecksumHashAlgorithm(options.DBChecksumHashAlgorithm)
|
||||
}
|
||||
|
||||
initDBNotifier()
|
||||
startInitInformer(options)
|
||||
}
|
||||
|
||||
@@ -16,9 +16,11 @@ package db
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
@@ -27,6 +29,7 @@ import (
|
||||
"yunion.io/x/pkg/utils"
|
||||
"yunion.io/x/sqlchemy"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
|
||||
"yunion.io/x/onecloud/pkg/util/splitable"
|
||||
)
|
||||
|
||||
@@ -126,13 +129,19 @@ func CheckRecordChecksumConsistent(model IModel) error {
|
||||
}
|
||||
|
||||
func calculateRecordChecksumByValues(vals []interface{}) string {
|
||||
ss := ""
|
||||
ss := strings.Builder{}
|
||||
for _, val := range vals {
|
||||
ss += fmt.Sprintf("\n%v", val)
|
||||
ss.WriteString(fmt.Sprintf("\n%v", val))
|
||||
}
|
||||
hStr := md5.Sum([]byte(ss))
|
||||
sum := fmt.Sprintf("%x", hStr)
|
||||
log.Debugf("calculate values string: %s checksum: %s", ss, sum)
|
||||
var sum string
|
||||
algStr := consts.DefaultDBChecksumHashAlgorithm()
|
||||
switch algStr {
|
||||
case "md5":
|
||||
sum = fmt.Sprintf("%x", md5.Sum([]byte(ss.String())))
|
||||
default:
|
||||
sum = fmt.Sprintf("%x", sha256.Sum256([]byte(ss.String())))
|
||||
}
|
||||
// log.Debugf("calculate values string: %s alg: %s, checksum: %s", ss, algStr, sum)
|
||||
return sum
|
||||
}
|
||||
|
||||
|
||||
@@ -164,6 +164,8 @@ type DBOptions struct {
|
||||
EnableDBChecksumTables bool `help:"Enable DB tables with record checksum for consistency"`
|
||||
DBChecksumSkipInit bool `help:"Skip DB tables with record checksum calculation when init" default:"false"`
|
||||
|
||||
DBChecksumHashAlgorithm string `help:"hash algorithm for db checksum hash" choices:"md5|sha256" default:"sha256"`
|
||||
|
||||
AutoSyncTable bool `help:"Automatically synchronize table changes if differences are detected"`
|
||||
ExitAfterDBInit bool `help:"Exit program after db initialization" default:"false"`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user