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

scheduler: refactor clusterInfo to extract a BasicCluster, and add mockCluster to easy testing #782

Merged
merged 7 commits into from Oct 17, 2017

Conversation

qqsun8819
Copy link
Contributor

This pr resolves issue issue 752
1 add mockCluster in schedulers , and implements basic interface in schedule.Cluster.
2 move balance_test.go into schedulers
3 remove balance.go in server for duplicate code of schedulers/util.go

On the other hand, this pr imports some new bad smell code:
1 mockCluster has some duplicate code of clusterInfo in server/cache.go, due to dependency problem.
2 coordinator_test.go also some duplicate code of mockCluster, this is a remaining problem after we move balance_test.go out of server.
I plan to propose a new pr to resolve these two problems after this pr is merged

@sre-bot
Copy link
Contributor

sre-bot commented Oct 1, 2017

Hi contributor, thanks for your PR.

This patch needs to be approved by someone of admins. They should reply with "/ok-to-test" to accept this PR for running test automatically.


// NewMockCluster creates a new mockCluster
func newMockCluster(id *core.MockIDAllocator) *mockCluster {
return &mockCluster{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can let clusterinfo be public and use it directly

@qqsun8819
Copy link
Contributor Author

@disksing @nolouch We need to discuss about how we deal with clusterInfo in cache.go, as @nolouch proposed in his comment.
As I mentioned in pr description, now mockCluster has many duplicate code as clusterInfo, so we should extract clusterInfo along into a separate package; but as clusterInfo depends on many data structure in server package ( struct in replication.go etc.) , these module also has to be moved into separate package like core. This change is somewhat big...
According to these problems , I have some proposals we can discuss so we can proceed for further refactor:
1 move clusterInfo into "schedules" package, or into a new package like "cluster".
2 resolve all the dependency problems caused by the movement of clusterInfo in 1.
3 whether 1 and 2 should be done in a new pr after this pr is merged, or do it just in this pr.
As 1 and 2 caused many changes, what I prefer is open a new pr to do this, and I need your suggestion.

"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/pd/server/cache"
"github.com/pingcap/pd/server/core"
"github.com/pingcap/pd/server/schedule"
_ "github.com/pingcap/pd/server/schedulers" // Register schedulers for tests.
"math"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put it before 3rd-part libraries.

@disksing
Copy link
Contributor

disksing commented Oct 9, 2017

How about extract a cluster struct in core or scheduler contains following four fields:

stores          *core.StoresInfo
regions         *core.RegionsInfo
writeStatistics cache.Cache
readStatistics  cache.Cache

Then embed it into clusterInfo and mockClusterInfo.

@qqsun8819

@siddontang
Copy link
Contributor

Any update @qqsun8819

@siddontang siddontang changed the title scheduler: add mockCluster to ease scheduler testing [WIP] scheduler: add mockCluster to ease scheduler testing Oct 13, 2017
@qqsun8819
Copy link
Contributor Author

@siddontang I'm working on it , plan to complete it this weekend according to @disksing 's suggestion

@qqsun8819 qqsun8819 changed the title [WIP] scheduler: add mockCluster to ease scheduler testing scheduler: add mockCluster to ease scheduler testing Oct 14, 2017
@qqsun8819 qqsun8819 changed the title scheduler: add mockCluster to ease scheduler testing scheduler: refactor clusterInfo to extract a BasicCluster, and add mockCluster to easy testing Oct 14, 2017
@qqsun8819
Copy link
Contributor Author

@disksing @nolouch @siddontang PR is updated according to review comments, PTAL if you have time

@@ -337,22 +337,22 @@ func (s *testClusterInfoSuite) testRegionHeartbeat(c *C, cache *clusterInfo) {
n, np := uint64(3), uint64(3)

stores := newTestStores(3)
regions := newTestRegions(n, np)
Regions := newTestRegions(n, np)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't change local variable names...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -84,10 +84,10 @@ var _ = Suite(&testRegionsInfoSuite{})

type testRegionsInfoSuite struct{}

// Create n regions (0..n) of n stores (0..n).
// Create n Regions (0..n) of n stores (0..n).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need to change the comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return &MockIDAllocator{base: 0}
}

// Alloc return a new id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alloc returns a new id.

"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/pd/server/cache"
"github.com/pingcap/pd/server/core"
"github.com/pingcap/pd/server/namespace"
"github.com/pingcap/pd/server/schedule"
_ "github.com/pingcap/pd/server/schedulers" // Register schedulers for tests.
"math"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually put builtin packages before 3rd-part ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

server/cache.go Outdated
@@ -36,25 +36,19 @@ var (
)

type clusterInfo struct {
*schedule.BasicCluster
sync.RWMutex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RWMutex should also wrap the basic cluster, if yes, move the RWMutex to the first line of the clusterInfo.

server/cache.go Outdated
return nil, errors.Trace(err)
}
log.Infof("load %v stores cost %v", c.stores.GetStoreCount(), time.Since(start))
log.Infof("load %v Stores cost %v", c.Stores.GetStoreCount(), time.Since(start))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using stores and regions in the log and comment is ok, no need to change them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
}

func (s *testClusterInfoSuite) testStoreHeartbeat(c *C, cache *clusterInfo) {
n, np := uint64(3), uint64(3)
stores := newTestStores(n)
regions := newTestRegions(n, np)
Regions := newTestRegions(n, np)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regions := 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@disksing
Copy link
Contributor

LGTM.

Copy link
Contributor

@nolouch nolouch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@disksing
Copy link
Contributor

/run-all-tests

@disksing disksing merged commit efc52b5 into master Oct 17, 2017
@disksing disksing deleted the qqsun8819/mockcluster branch October 17, 2017 05:34
@sre-bot sre-bot added the contribution Indicates that the PR was contributed by an external member. label Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution Indicates that the PR was contributed by an external member.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants