From 204ca74a33a46ac072573a58d12c69895d0d3720 Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Thu, 18 Jul 2024 11:24:12 +0800 Subject: [PATCH 1/3] fix redact log Signed-off-by: Ryan Leung --- pkg/keyspace/keyspace.go | 5 ++-- pkg/schedule/labeler/rules.go | 39 ++++++++++++++++++++++++++++++++ server/cluster/cluster_worker.go | 4 ++-- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/pkg/keyspace/keyspace.go b/pkg/keyspace/keyspace.go index 26fd4db10f0..bcee8e75dea 100644 --- a/pkg/keyspace/keyspace.go +++ b/pkg/keyspace/keyspace.go @@ -32,6 +32,7 @@ import ( "github.com/tikv/pd/pkg/storage/endpoint" "github.com/tikv/pd/pkg/storage/kv" "github.com/tikv/pd/pkg/utils/etcdutil" + "github.com/tikv/pd/pkg/utils/logutil" "github.com/tikv/pd/pkg/utils/syncutil" "go.uber.org/zap" ) @@ -333,7 +334,7 @@ func (manager *Manager) splitKeyspaceRegion(id uint32, waitRegionSplit bool) (er if waitRegionSplit { ranges := keyspaceRule.Data.([]*labeler.KeyRangeRule) if len(ranges) < 2 { - log.Warn("[keyspace] failed to split keyspace region with insufficient range", zap.Any("label-rule", keyspaceRule)) + log.Warn("[keyspace] failed to split keyspace region with insufficient range", logutil.ZapRedactString("label-rule", keyspaceRule.String())) return ErrRegionSplitFailed } rawLeftBound, rawRightBound := ranges[0].StartKey, ranges[0].EndKey @@ -382,7 +383,7 @@ func (manager *Manager) splitKeyspaceRegion(id uint32, waitRegionSplit bool) (er log.Info("[keyspace] added region label for keyspace", zap.Uint32("keyspace-id", id), - zap.Any("label-rule", keyspaceRule), + logutil.ZapRedactString("label-rule", keyspaceRule.String()), zap.Duration("takes", time.Since(start)), ) return diff --git a/pkg/schedule/labeler/rules.go b/pkg/schedule/labeler/rules.go index 3462cb7c459..44679886cd7 100644 --- a/pkg/schedule/labeler/rules.go +++ b/pkg/schedule/labeler/rules.go @@ -20,6 +20,7 @@ import ( "encoding/json" "fmt" "reflect" + "strings" "time" "github.com/pingcap/failpoint" @@ -38,6 +39,10 @@ type RegionLabel struct { expire *time.Time } +func (l *RegionLabel) String() string { + return fmt.Sprintf("key: %s, value: %s", l.Key, l.Value) +} + // LabelRule is the rule to assign labels to a region. // NOTE: This type is exported by HTTP API. Please pay more attention when modifying it. type LabelRule struct { @@ -49,6 +54,36 @@ type LabelRule struct { minExpire *time.Time } +func (rule *LabelRule) String() string { + var b strings.Builder + b.WriteString(fmt.Sprintf("id: %s, index: %d, type: %s", rule.ID, rule.Index, rule.RuleType)) + b.WriteString(", labels: ") + for i, l := range rule.Labels { + if i == 0 { + b.WriteString("[") + } + b.WriteString(l.String()) + if i == len(rule.Labels)-1 { + b.WriteString("]") + } + b.WriteString(", ") + } + b.WriteString("data: ") + ranges := rule.Data.([]*KeyRangeRule) + for i, r := range ranges { + if i == 0 { + b.WriteString("[") + } + b.WriteString(fmt.Sprintf("startKey: {%s}, endKey: {%s}", r.StartKeyHex, r.EndKeyHex)) + if i == len(ranges)-1 { + b.WriteString("]") + } else { + b.WriteString(", ") + } + } + return b.String() +} + // NewLabelRuleFromJSON creates a label rule from the JSON data. func NewLabelRuleFromJSON(data []byte) (*LabelRule, error) { lr := &LabelRule{} @@ -78,6 +113,10 @@ type KeyRangeRule struct { EndKeyHex string `json:"end_key"` // hex format end key, for marshal/unmarshal } +func (r *KeyRangeRule) String() string { + return fmt.Sprintf("startKey: %s, endKey: %s", r.StartKeyHex, r.EndKeyHex) +} + // LabelRulePatch is the patch to update the label rules. // NOTE: This type is exported by HTTP API. Please pay more attention when modifying it. type LabelRulePatch struct { diff --git a/server/cluster/cluster_worker.go b/server/cluster/cluster_worker.go index 93cf7bcbfad..2d9bb411995 100644 --- a/server/cluster/cluster_worker.go +++ b/server/cluster/cluster_worker.go @@ -238,7 +238,7 @@ func (*RaftCluster) HandleBatchReportSplit(request *pdpb.ReportBatchSplitRequest err := checkSplitRegions(regions) if err != nil { log.Warn("report batch split region is invalid", - zap.Stringer("region-meta", hrm), + logutil.ZapRedactStringer("region-meta", hrm), errs.ZapError(err)) return nil, err } @@ -247,7 +247,7 @@ func (*RaftCluster) HandleBatchReportSplit(request *pdpb.ReportBatchSplitRequest hrm = core.RegionsToHexMeta(regions[:last]) log.Info("region batch split, generate new regions", zap.Uint64("region-id", originRegion.GetId()), - zap.Stringer("origin", hrm), + logutil.ZapRedactStringer("origin", hrm), zap.Int("total", last)) return &pdpb.ReportBatchSplitResponse{}, nil } From e7fdb9398ec334cd689330cfa4e91611918b08c0 Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Fri, 19 Jul 2024 16:17:31 +0800 Subject: [PATCH 2/3] fix Signed-off-by: Ryan Leung --- pkg/schedule/labeler/rules.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/schedule/labeler/rules.go b/pkg/schedule/labeler/rules.go index 44679886cd7..897643cdd85 100644 --- a/pkg/schedule/labeler/rules.go +++ b/pkg/schedule/labeler/rules.go @@ -65,10 +65,11 @@ func (rule *LabelRule) String() string { b.WriteString(l.String()) if i == len(rule.Labels)-1 { b.WriteString("]") + } else { + b.WriteString(", ") } - b.WriteString(", ") } - b.WriteString("data: ") + b.WriteString(", data: ") ranges := rule.Data.([]*KeyRangeRule) for i, r := range ranges { if i == 0 { From 16c60dde8b396d4883f3d95b2bbbf1ec59bf3da3 Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Fri, 19 Jul 2024 16:52:57 +0800 Subject: [PATCH 3/3] remove unnecessary func Signed-off-by: Ryan Leung --- pkg/schedule/labeler/rules.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/schedule/labeler/rules.go b/pkg/schedule/labeler/rules.go index 897643cdd85..39a420032d8 100644 --- a/pkg/schedule/labeler/rules.go +++ b/pkg/schedule/labeler/rules.go @@ -114,10 +114,6 @@ type KeyRangeRule struct { EndKeyHex string `json:"end_key"` // hex format end key, for marshal/unmarshal } -func (r *KeyRangeRule) String() string { - return fmt.Sprintf("startKey: %s, endKey: %s", r.StartKeyHex, r.EndKeyHex) -} - // LabelRulePatch is the patch to update the label rules. // NOTE: This type is exported by HTTP API. Please pay more attention when modifying it. type LabelRulePatch struct {