Skip to content

Commit

Permalink
core: use hex format for region meta key (#1627)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>=
  • Loading branch information
rleungx authored Jul 15, 2019
1 parent 6d3725d commit d6b5378
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 24 deletions.
2 changes: 1 addition & 1 deletion server/checker/merge_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (m *MergeChecker) Check(region *core.RegionInfo) []*schedule.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 := schedule.CreateMergeRegionOperator("merge-region", m.cluster, region, target, schedule.OpMerge)
if err != nil {
return nil
Expand Down
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 storage",
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.storage.DeleteRegion(item); err != nil {
log.Error("fail to delete region from storage",
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 tools/regions-dump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func loadRegions(client *clientv3.Client, f *os.File) error {
return errors.WithStack(err)
}
nextID = region.GetId() + 1
fmt.Fprintln(w, core.HexRegionMeta(region))
fmt.Fprintln(w, core.RegionToHexMeta(region).Region)
}

if len(res) < rangeLimit {
Expand Down

0 comments on commit d6b5378

Please sign in to comment.