Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: use hex format for region meta key (#1627) #1639

Merged
merged 2 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions server/cluster_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func (c *clusterInfo) handleRegionHeartbeat(region *core.RegionInfo) error {
if origin == nil {
log.Debug("insert new region",
zap.Uint64("region-id", region.GetID()),
zap.Reflect("meta-region", core.HexRegionMeta(region.GetMeta())),
zap.Stringer("meta-region", core.RegionToHexMeta(region.GetMeta())),
)
saveKV, saveCache, isNew = true, true, true
} else {
Expand Down Expand Up @@ -595,7 +595,7 @@ func (c *clusterInfo) handleRegionHeartbeat(region *core.RegionInfo) error {
// after restart. Here we only log the error then go on updating cache.
log.Error("fail to save region to kv",
zap.Uint64("region-id", region.GetID()),
zap.Reflect("region-meta", core.HexRegionMeta(region.GetMeta())),
zap.Stringer("region-meta", core.RegionToHexMeta(region.GetMeta())),
zap.Error(err))
}
select {
Expand All @@ -620,7 +620,7 @@ func (c *clusterInfo) handleRegionHeartbeat(region *core.RegionInfo) error {
if err := c.kv.DeleteRegion(item); err != nil {
log.Error("fail to delete region from kv",
zap.Uint64("region-id", item.GetId()),
zap.Reflect("region-meta", core.HexRegionMeta(item)),
zap.Stringer("region-meta", core.RegionToHexMeta(item)),
zap.Error(err))
}
}
Expand Down
22 changes: 10 additions & 12 deletions server/cluster_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (c *RaftCluster) HandleRegionHeartbeat(region *core.RegionInfo) error {

// If the region peer count is 0, then we should not handle this.
if len(region.GetPeers()) == 0 {
log.Warn("invalid region, zero region peer count", zap.Reflect("region-meta", core.HexRegionMeta(region.GetMeta())))
return errors.Errorf("invalid region, zero region peer count: %v", core.HexRegionMeta(region.GetMeta()))
log.Warn("invalid region, zero region peer count", zap.Stringer("region-meta", core.RegionToHexMeta(region.GetMeta())))
return errors.Errorf("invalid region, zero region peer count: %v", core.RegionToHexMeta(region.GetMeta()))
}

c.coordinator.opController.Dispatch(region, schedule.DispatchFromHeartBeat)
Expand Down Expand Up @@ -81,7 +81,7 @@ func (c *RaftCluster) validRequestRegion(reqRegion *metapb.Region) error {
startKey := reqRegion.GetStartKey()
region, _ := c.GetRegionByKey(startKey)
if region == nil {
return errors.Errorf("region not found, request region: %v", core.HexRegionMeta(reqRegion))
return errors.Errorf("region not found, request region: %v", core.RegionToHexMeta(reqRegion))
}
// If the request epoch is less than current region epoch, then returns an error.
reqRegionEpoch := reqRegion.GetRegionEpoch()
Expand Down Expand Up @@ -172,8 +172,8 @@ func (c *RaftCluster) handleReportSplit(request *pdpb.ReportSplitRequest) (*pdpb
err := c.checkSplitRegion(left, right)
if err != nil {
log.Warn("report split region is invalid",
zap.Reflect("left-region", core.HexRegionMeta(left)),
zap.Reflect("right-region", core.HexRegionMeta(right)),
zap.Stringer("left-region", core.RegionToHexMeta(left)),
zap.Stringer("right-region", core.RegionToHexMeta(right)),
zap.Error(err))
return nil, err
}
Expand All @@ -184,29 +184,27 @@ func (c *RaftCluster) handleReportSplit(request *pdpb.ReportSplitRequest) (*pdpb
originRegion.StartKey = left.GetStartKey()
log.Info("region split, generate new region",
zap.Uint64("region-id", originRegion.GetId()),
zap.Reflect("region-meta", core.HexRegionMeta(left)))
zap.Stringer("region-meta", core.RegionToHexMeta(left)))
return &pdpb.ReportSplitResponse{}, nil
}

func (c *RaftCluster) handleBatchReportSplit(request *pdpb.ReportBatchSplitRequest) (*pdpb.ReportBatchSplitResponse, error) {
regions := request.GetRegions()
hexRegionMetas := make([]*metapb.Region, len(regions))
for i, region := range regions {
hexRegionMetas[i] = core.HexRegionMeta(region)
}

hrm := core.RegionsToHexMeta(regions)
err := c.checkSplitRegions(regions)
if err != nil {
log.Warn("report batch split region is invalid",
zap.Reflect("region-meta", hexRegionMetas),
zap.Stringer("region-meta", hrm),
zap.Error(err))
return nil, err
}
last := len(regions) - 1
originRegion := proto.Clone(regions[last]).(*metapb.Region)
hrm = core.RegionsToHexMeta(regions[:last])
log.Info("region batch split, generate new regions",
zap.Uint64("region-id", originRegion.GetId()),
zap.Reflect("origin", hexRegionMetas[:last]),
zap.Stringer("origin", hrm),
zap.Int("total", last))
return &pdpb.ReportBatchSplitResponse{}, nil
}
43 changes: 39 additions & 4 deletions server/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,14 +820,49 @@ func HexRegionKey(key []byte) []byte {
return []byte(strings.ToUpper(hex.EncodeToString(key)))
}

// HexRegionMeta converts a region meta's keys to hex format. Used for formating
// RegionToHexMeta converts a region meta's keys to hex format. Used for formating
// region in logs.
func HexRegionMeta(meta *metapb.Region) *metapb.Region {
func RegionToHexMeta(meta *metapb.Region) HexRegionMeta {
if meta == nil {
return nil
return HexRegionMeta{}
}
meta = proto.Clone(meta).(*metapb.Region)
meta.StartKey = HexRegionKey(meta.StartKey)
meta.EndKey = HexRegionKey(meta.EndKey)
return meta
return HexRegionMeta{meta}
}

// HexRegionMeta is a region meta in the hex format. Used for formating region in logs.
type HexRegionMeta struct {
*metapb.Region
}

func (h HexRegionMeta) String() string {
return strings.TrimSpace(proto.CompactTextString(h.Region))
}

// RegionsToHexMeta converts regions' meta keys to hex format. Used for formating
// region in logs.
func RegionsToHexMeta(regions []*metapb.Region) HexRegionsMeta {
hexRegionMetas := make([]*metapb.Region, len(regions))
for i, region := range regions {
meta := proto.Clone(region).(*metapb.Region)
meta.StartKey = HexRegionKey(meta.StartKey)
meta.EndKey = HexRegionKey(meta.EndKey)

hexRegionMetas[i] = meta
}
return HexRegionsMeta(hexRegionMetas)
}

// HexRegionsMeta is a slice of regions' meta in the hex format. Used for formating
// region in logs.
type HexRegionsMeta []*metapb.Region

func (h HexRegionsMeta) String() string {
var b strings.Builder
for _, r := range h {
b.WriteString(proto.CompactTextString(r))
}
return strings.TrimSpace(b.String())
}
2 changes: 1 addition & 1 deletion server/core/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (*testRegionKey) TestRegionKey(c *C) {
for _, t := range testCase {
got, err := strconv.Unquote(t.key)
c.Assert(err, IsNil)
s := fmt.Sprintln(HexRegionMeta(&metapb.Region{StartKey: []byte(got)}))
s := fmt.Sprintln(RegionToHexMeta(&metapb.Region{StartKey: []byte(got)}))
c.Assert(strings.Contains(s, t.expect), IsTrue)

// start key changed
Expand Down
4 changes: 2 additions & 2 deletions server/core/region_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func (t *regionTree) update(region *metapb.Region) []*metapb.Region {
for _, item := range overlaps {
log.Debug("overlapping region",
zap.Uint64("region-id", item.GetId()),
zap.Reflect("delete-region", HexRegionMeta(item)),
zap.Reflect("update-region", HexRegionMeta(region)))
zap.Stringer("delete-region", RegionToHexMeta(item)),
zap.Stringer("update-region", RegionToHexMeta(region)))
t.tree.Delete(&regionItem{item})
}

Expand Down
2 changes: 1 addition & 1 deletion server/schedule/merge_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (m *MergeChecker) Check(region *core.RegionInfo) []*Operator {
return nil
}

log.Debug("try to merge region", zap.Reflect("from", core.HexRegionMeta(region.GetMeta())), zap.Reflect("to", core.HexRegionMeta(target.GetMeta())))
log.Debug("try to merge region", zap.Stringer("from", core.RegionToHexMeta(region.GetMeta())), zap.Stringer("to", core.RegionToHexMeta(target.GetMeta())))
ops, err := CreateMergeRegionOperator("merge-region", m.cluster, region, target, OpMerge)
if err != nil {
return nil
Expand Down