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

server/store: embed metapb.Store into storeInfo #352

Merged
merged 1 commit into from
Oct 22, 2016
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
14 changes: 7 additions & 7 deletions server/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func selectFromStore(stores []*storeInfo, excluded map[uint64]struct{}, filters
continue
}

if _, ok := excluded[store.store.GetId()]; ok {
if _, ok := excluded[store.GetId()]; ok {
continue
}

Expand Down Expand Up @@ -84,7 +84,7 @@ func selectToStore(stores []*storeInfo, excluded map[uint64]struct{}, filters []
continue
}

if _, ok := excluded[store.store.GetId()]; ok {
if _, ok := excluded[store.GetId()]; ok {
continue
}

Expand Down Expand Up @@ -133,7 +133,7 @@ func (cb *capacityBalancer) selectBalanceRegion(cluster *clusterInfo, stores []*
return nil, nil, nil
}

storeID := store.store.GetId()
storeID := store.GetId()
meta := cluster.getMeta()
if meta.GetMaxPeerCount() == 1 {
region := cluster.regions.randLeaderRegion(storeID)
Expand Down Expand Up @@ -162,7 +162,7 @@ func (cb *capacityBalancer) selectAddPeer(cluster *clusterInfo, stores []*storeI

peer := &metapb.Peer{
Id: peerID,
StoreId: store.store.GetId(),
StoreId: store.GetId(),
}

return peer, nil
Expand All @@ -179,7 +179,7 @@ func (cb *capacityBalancer) selectRemovePeer(cluster *clusterInfo, peers map[uin
return nil, nil
}

storeID := store.store.GetId()
storeID := store.GetId()
return peers[storeID], nil
}

Expand Down Expand Up @@ -244,7 +244,7 @@ func (lb *leaderBalancer) selectBalanceRegion(cluster *clusterInfo, stores []*st
}

// Random select one leader region from store.
storeID := store.store.GetId()
storeID := store.GetId()
region := cluster.regions.randLeaderRegion(storeID)
if region == nil {
return nil, nil, nil
Expand Down Expand Up @@ -275,7 +275,7 @@ func (lb *leaderBalancer) selectNewLeaderPeer(cluster *clusterInfo, peers map[ui
return nil
}

storeID := store.store.GetId()
storeID := store.GetId()
return peers[storeID]
}

Expand Down
8 changes: 4 additions & 4 deletions server/balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ func (s *testBalancerSuite) updateStore(c *C, clusterInfo *clusterInfo, storeID
}

func (s *testBalancerSuite) updateStoreState(c *C, clusterInfo *clusterInfo, storeID uint64, state metapb.StoreState) {
storeInfo := clusterInfo.getStore(storeID)
storeInfo.store.State = state
clusterInfo.addStore(storeInfo.store)
ok := clusterInfo.updateStoreStatus(storeInfo.stats.StoreStats)
store := clusterInfo.getStore(storeID)
store.State = state
clusterInfo.addStore(store.Store)
ok := clusterInfo.updateStoreStatus(store.stats.StoreStats)
c.Assert(ok, IsTrue)
}

Expand Down
2 changes: 1 addition & 1 deletion server/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func (c *clusterInfo) getMetaStores() []*metapb.Store {

stores := make([]*metapb.Store, 0, len(c.stores))
for _, store := range c.stores {
stores = append(stores, proto.Clone(store.store).(*metapb.Store))
stores = append(stores, proto.Clone(store.Store).(*metapb.Store))
}

return stores
Expand Down
6 changes: 3 additions & 3 deletions server/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s *testClusterCacheSuite) TestCache(c *C) {
c.Assert(cluster.cachedCluster.getMeta().GetMaxPeerCount(), Equals, uint32(3))

cacheStore := cluster.cachedCluster.getStore(store1.GetId())
c.Assert(cacheStore.store, DeepEquals, store1)
c.Assert(cacheStore.Store, DeepEquals, store1)
c.Assert(cluster.cachedCluster.regions.regions, HasLen, 1)
c.Assert(cluster.cachedCluster.regions.searchRegions.length(), Equals, 1)
c.Assert(cluster.cachedCluster.regions.leaders.storeRegions, HasLen, 0)
Expand All @@ -101,9 +101,9 @@ func (s *testClusterCacheSuite) TestCache(c *C) {
c.Assert(cluster.cachedCluster.getMeta().GetMaxPeerCount(), Equals, uint32(3))

cacheStore = cluster.cachedCluster.getStore(store1.GetId())
c.Assert(cacheStore.store, DeepEquals, store1)
c.Assert(cacheStore.Store, DeepEquals, store1)
cacheStore = cluster.cachedCluster.getStore(store2.GetId())
c.Assert(cacheStore.store, DeepEquals, store2)
c.Assert(cacheStore.Store, DeepEquals, store2)
cacheStores := cluster.cachedCluster.getStores()
c.Assert(cacheStores, HasLen, 2)
c.Assert(cluster.cachedCluster.regions.regions, HasLen, 1)
Expand Down
18 changes: 9 additions & 9 deletions server/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func (c *RaftCluster) GetStore(storeID uint64) (*metapb.Store, *StoreStatus, err
return nil, nil, errors.Errorf("invalid store ID %d, not found", storeID)
}

return store.store, store.stats, nil
return store.Store, store.stats, nil
}

func (c *RaftCluster) putStore(store *metapb.Store) error {
Expand All @@ -398,21 +398,21 @@ func (c *RaftCluster) putStore(store *metapb.Store) error {
// Case 1: store id exists with the same address - do nothing;
// Case 2: store id exists with different address - update address;
if s := c.cachedCluster.getStore(store.GetId()); s != nil {
if s.store.GetAddress() == store.GetAddress() {
if s.GetAddress() == store.GetAddress() {
return nil
}
s.store.Address = store.Address
return c.saveStore(s.store)
s.Address = store.Address
return c.saveStore(s.Store)
}

// Case 3: store id does not exist, check duplicated address.
for _, s := range c.cachedCluster.getStores() {
// It's OK to start a new store on the same address if the old store has been removed.
if s.store.GetState() == metapb.StoreState_Tombstone {
if s.isTombstone() {
continue
}
if s.store.GetAddress() == store.GetAddress() {
return errors.Errorf("duplicated store address: %v, already registered by %v", store, s.store)
if s.GetAddress() == store.GetAddress() {
return errors.Errorf("duplicated store address: %v, already registered by %v", store, s.Store)
}
}
return c.saveStore(store)
Expand Down Expand Up @@ -523,7 +523,7 @@ func (c *RaftCluster) collectMetrics() {

for _, s := range cluster.getStores() {
// Store state.
switch s.store.GetState() {
switch s.GetState() {
case metapb.StoreState_Up:
storeUpCount++
case metapb.StoreState_Offline:
Expand Down Expand Up @@ -699,7 +699,7 @@ func (c *RaftCluster) GetHistoryOperators() []Operator {
// GetScores gets store scores from balancer.
func (c *RaftCluster) GetScores(store *metapb.Store, status *StoreStatus) []int {
storeInfo := &storeInfo{
store: store,
Store: store,
stats: status,
}

Expand Down
8 changes: 4 additions & 4 deletions server/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,10 @@ func (s *testClusterSuite) testPutStore(c *C, conn net.Conn, clusterID uint64, s
func (s *testClusterSuite) resetStoreState(c *C, storeID uint64, state metapb.StoreState) {
cluster := s.svr.GetRaftCluster()
c.Assert(cluster, NotNil)
storeInfo := cluster.cachedCluster.getStore(storeID)
c.Assert(storeInfo, NotNil)
storeInfo.store.State = state
cluster.cachedCluster.addStore(storeInfo.store)
store := cluster.cachedCluster.getStore(storeID)
c.Assert(store, NotNil)
store.State = state
cluster.cachedCluster.addStore(store.Store)
}

func (s *testClusterSuite) testRemoveStore(c *C, conn net.Conn, clusterID uint64, store *metapb.Store) {
Expand Down
13 changes: 7 additions & 6 deletions server/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,36 @@ import (
)

// storeInfo contains information about a store.
// TODO: Export this to API directly.
type storeInfo struct {
store *metapb.Store
*metapb.Store
stats *StoreStatus
}

func newStoreInfo(store *metapb.Store) *storeInfo {
return &storeInfo{
store: store,
Store: store,
stats: newStoreStatus(),
}
}

func (s *storeInfo) clone() *storeInfo {
return &storeInfo{
store: proto.Clone(s.store).(*metapb.Store),
Store: proto.Clone(s.Store).(*metapb.Store),
stats: s.stats.clone(),
}
}

func (s *storeInfo) isUp() bool {
return s.store.GetState() == metapb.StoreState_Up
return s.GetState() == metapb.StoreState_Up
}

func (s *storeInfo) isOffline() bool {
return s.store.GetState() == metapb.StoreState_Offline
return s.GetState() == metapb.StoreState_Offline
}

func (s *storeInfo) isTombstone() bool {
return s.store.GetState() == metapb.StoreState_Tombstone
return s.GetState() == metapb.StoreState_Tombstone
}

func (s *storeInfo) downTime() time.Duration {
Expand Down