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: support range for random selection #1783

Merged
merged 10 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 6 additions & 6 deletions server/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (c *RaftCluster) processRegionHeartbeat(region *core.RegionInfo) error {
for _, item := range c.core.GetOverlaps(region) {
if region.GetRegionEpoch().GetVersion() < item.GetRegionEpoch().GetVersion() {
c.RUnlock()
return ErrRegionIsStale(region.GetMeta(), item)
return ErrRegionIsStale(region.GetMeta(), item.GetMeta())
}
}
}
Expand Down Expand Up @@ -441,19 +441,19 @@ func (c *RaftCluster) processRegionHeartbeat(region *core.RegionInfo) error {
overlaps := c.core.PutRegion(region)
if c.storage != nil {
for _, item := range overlaps {
if err := c.storage.DeleteRegion(item); err != nil {
if err := c.storage.DeleteRegion(item.GetMeta()); err != nil {
log.Error("failed to delete region from storage",
zap.Uint64("region-id", item.GetId()),
zap.Stringer("region-meta", core.RegionToHexMeta(item)),
zap.Uint64("region-id", item.GetID()),
zap.Stringer("region-meta", core.RegionToHexMeta(item.GetMeta())),
zap.Error(err))
}
}
}
for _, item := range overlaps {
if c.regionStats != nil {
c.regionStats.ClearDefunctRegion(item.GetId())
c.regionStats.ClearDefunctRegion(item.GetID())
}
c.labelLevelStats.ClearDefunctRegion(item.GetId(), c.GetLocationLabels())
c.labelLevelStats.ClearDefunctRegion(item.GetID(), c.GetLocationLabels())
}

// Update related stores.
Expand Down
49 changes: 19 additions & 30 deletions server/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,10 +905,10 @@ func checkRegions(c *C, cache *core.RegionsInfo, regions []*core.RegionInfo) {
regionCount[peer.StoreId]++
if peer.Id == region.GetLeader().Id {
leaderCount[peer.StoreId]++
checkRegion(c, cache.GetLeader(peer.StoreId, region.GetID()), region)
checkRegion(c, cache.GetLeader(peer.StoreId, region), region)
} else {
followerCount[peer.StoreId]++
checkRegion(c, cache.GetFollower(peer.StoreId, region.GetID()), region)
checkRegion(c, cache.GetFollower(peer.StoreId, region), region)
}
}
}
Expand Down Expand Up @@ -1245,11 +1245,9 @@ func (s *testClusterInfoSuite) TestRegionHeartbeat(c *C) {
}
}

func heartbeatRegions(c *C, cluster *RaftCluster, regions []*metapb.Region) {
func heartbeatRegions(c *C, cluster *RaftCluster, regions []*core.RegionInfo) {
// Heartbeat and check region one by one.
for _, region := range regions {
r := core.NewRegionInfo(region, nil)

for _, r := range regions {
c.Assert(cluster.processRegionHeartbeat(r), IsNil)

checkRegion(c, cluster.GetRegion(r.GetID()), r)
Expand All @@ -1262,9 +1260,7 @@ func heartbeatRegions(c *C, cluster *RaftCluster, regions []*metapb.Region) {
}

// Check all regions after handling all heartbeats.
for _, region := range regions {
r := core.NewRegionInfo(region, nil)

for _, r := range regions {
checkRegion(c, cluster.GetRegion(r.GetID()), r)
checkRegion(c, cluster.GetRegionInfoByKey(r.GetStartKey()), r)

Expand Down Expand Up @@ -1321,14 +1317,7 @@ func (s *testClusterInfoSuite) TestRegionSplitAndMerge(c *C) {
c.Assert(err, IsNil)
cluster := createTestRaftCluster(mockid.NewIDAllocator(), opt, core.NewStorage(kv.NewMemoryKV()))

regions := []*metapb.Region{
{
Id: 1,
StartKey: []byte{},
EndKey: []byte{},
RegionEpoch: &metapb.RegionEpoch{},
},
}
regions := []*core.RegionInfo{core.NewTestRegionInfo([]byte{}, []byte{})}

// Byte will underflow/overflow if n > 7.
n := 7
Expand Down Expand Up @@ -1403,25 +1392,25 @@ type testClusterUtilSuite struct{}

func (s *testClusterUtilSuite) TestCheckStaleRegion(c *C) {
// (0, 0) v.s. (0, 0)
region := core.NewRegion([]byte{}, []byte{})
origin := core.NewRegion([]byte{}, []byte{})
c.Assert(checkStaleRegion(region, origin), IsNil)
c.Assert(checkStaleRegion(origin, region), IsNil)
region := core.NewTestRegionInfo([]byte{}, []byte{})
origin := core.NewTestRegionInfo([]byte{}, []byte{})
c.Assert(checkStaleRegion(region.GetMeta(), origin.GetMeta()), IsNil)
c.Assert(checkStaleRegion(origin.GetMeta(), region.GetMeta()), IsNil)

// (1, 0) v.s. (0, 0)
region.RegionEpoch.Version++
c.Assert(checkStaleRegion(origin, region), IsNil)
c.Assert(checkStaleRegion(region, origin), NotNil)
region.GetRegionEpoch().Version++
c.Assert(checkStaleRegion(origin.GetMeta(), region.GetMeta()), IsNil)
c.Assert(checkStaleRegion(region.GetMeta(), origin.GetMeta()), NotNil)

// (1, 1) v.s. (0, 0)
region.RegionEpoch.ConfVer++
c.Assert(checkStaleRegion(origin, region), IsNil)
c.Assert(checkStaleRegion(region, origin), NotNil)
region.GetRegionEpoch().ConfVer++
c.Assert(checkStaleRegion(origin.GetMeta(), region.GetMeta()), IsNil)
c.Assert(checkStaleRegion(region.GetMeta(), origin.GetMeta()), NotNil)

// (0, 1) v.s. (0, 0)
region.RegionEpoch.Version--
c.Assert(checkStaleRegion(origin, region), IsNil)
c.Assert(checkStaleRegion(region, origin), NotNil)
region.GetRegionEpoch().Version--
c.Assert(checkStaleRegion(origin.GetMeta(), region.GetMeta()), IsNil)
c.Assert(checkStaleRegion(region.GetMeta(), origin.GetMeta()), NotNil)
}

func mustSaveStores(c *C, s *core.Storage, n int) []*metapb.Store {
Expand Down
4 changes: 2 additions & 2 deletions server/core/basic_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (bc *BasicCluster) TakeStore(storeID uint64) *StoreInfo {
}

// PutRegion put a region.
func (bc *BasicCluster) PutRegion(region *RegionInfo) []*metapb.Region {
func (bc *BasicCluster) PutRegion(region *RegionInfo) []*RegionInfo {
bc.Lock()
defer bc.Unlock()
return bc.Regions.SetRegion(region)
Expand Down Expand Up @@ -293,7 +293,7 @@ func (bc *BasicCluster) ScanRange(startKey, endKey []byte, limit int) []*RegionI
}

// GetOverlaps returns the regions which are overlapped with the specified region range.
func (bc *BasicCluster) GetOverlaps(region *RegionInfo) []*metapb.Region {
func (bc *BasicCluster) GetOverlaps(region *RegionInfo) []*RegionInfo {
bc.RLock()
defer bc.RUnlock()
return bc.Regions.GetOverlaps(region)
Expand Down
Loading