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

*: refactor storeInfo. #712

Merged
merged 3 commits into from
Aug 28, 2017
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
4 changes: 2 additions & 2 deletions server/api/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ func (h *labelsHandler) GetStores(w http.ResponseWriter, r *http.Request) {

stores = filter.filter(stores)
for _, s := range stores {
store, status, err := cluster.GetStore(s.GetId())
store, err := cluster.GetStore(s.GetId())
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}

storeInfo := newStoreInfo(store, status)
storeInfo := newStoreInfo(store)
storesInfo.Stores = append(storesInfo.Stores, storeInfo)
}
storesInfo.Count = len(storesInfo.Stores)
Expand Down
71 changes: 39 additions & 32 deletions server/api/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,17 @@ type metaStore struct {
}

type storeStatus struct {
StoreID uint64 `json:"store_id"`
Capacity typeutil.ByteSize `json:"capacity"`
Available typeutil.ByteSize `json:"available"`
LeaderCount int `json:"leader_count"`
RegionCount int `json:"region_count"`
SendingSnapCount uint32 `json:"sending_snap_count"`
ReceivingSnapCount uint32 `json:"receiving_snap_count"`
ApplyingSnapCount uint32 `json:"applying_snap_count"`
IsBusy bool `json:"is_busy"`

StartTS time.Time `json:"start_ts"`
LastHeartbeatTS time.Time `json:"last_heartbeat_ts"`
Uptime typeutil.Duration `json:"uptime"`
Capacity typeutil.ByteSize `json:"capacity,omitempty"`
Available typeutil.ByteSize `json:"available,omitempty"`
LeaderCount int `json:"leader_count,omitempty"`
RegionCount int `json:"region_count,omitempty"`
SendingSnapCount uint32 `json:"sending_snap_count,omitempty"`
ReceivingSnapCount uint32 `json:"receiving_snap_count,omitempty"`
ApplyingSnapCount uint32 `json:"applying_snap_count,omitempty"`
IsBusy bool `json:"is_busy,omitempty"`
StartTS *time.Time `json:"start_ts,omitempty"`
LastHeartbeatTS *time.Time `json:"last_heartbeat_ts,omitempty"`
Uptime *typeutil.Duration `json:"uptime,omitempty"`
}

type storeInfo struct {
Expand All @@ -55,28 +53,37 @@ type storeInfo struct {

const downStateName = "Down"

func newStoreInfo(store *metapb.Store, status *server.StoreStatus) *storeInfo {
func newStoreInfo(store *server.StoreInfo) *storeInfo {
s := &storeInfo{
Store: &metaStore{
Store: store,
Store: store.Store,
StateName: store.State.String(),
},
Status: &storeStatus{
StoreID: status.StoreId,
Capacity: typeutil.ByteSize(status.Capacity),
Available: typeutil.ByteSize(status.Available),
LeaderCount: status.LeaderCount,
RegionCount: status.RegionCount,
SendingSnapCount: status.SendingSnapCount,
ReceivingSnapCount: status.ReceivingSnapCount,
ApplyingSnapCount: status.ApplyingSnapCount,
IsBusy: status.IsBusy,
StartTS: status.GetStartTS(),
LastHeartbeatTS: status.LastHeartbeatTS,
Uptime: typeutil.NewDuration(status.GetUptime()),
Capacity: typeutil.ByteSize(store.Stats.GetCapacity()),
Available: typeutil.ByteSize(store.Stats.GetAvailable()),
LeaderCount: store.LeaderCount,
RegionCount: store.RegionCount,
SendingSnapCount: store.Stats.GetSendingSnapCount(),
ReceivingSnapCount: store.Stats.GetReceivingSnapCount(),
ApplyingSnapCount: store.Stats.GetApplyingSnapCount(),
IsBusy: store.Stats.GetIsBusy(),
},
}
if store.State == metapb.StoreState_Up && status.IsDown() {

if store.Stats != nil {
startTS := store.GetStartTS()
s.Status.StartTS = &startTS
}
if lastHeartbeat := store.LastHeartbeatTS; !lastHeartbeat.IsZero() {
s.Status.LastHeartbeatTS = &lastHeartbeat
}
if upTime := store.GetUptime(); upTime > 0 {
duration := typeutil.NewDuration(upTime)
s.Status.Uptime = &duration
}

if store.State == metapb.StoreState_Up && store.IsDown() {
s.Store.StateName = downStateName
}
return s
Expand Down Expand Up @@ -114,13 +121,13 @@ func (h *storeHandler) Get(w http.ResponseWriter, r *http.Request) {
return
}

store, status, err := cluster.GetStore(storeID)
store, err := cluster.GetStore(storeID)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}

storeInfo := newStoreInfo(store, status)
storeInfo := newStoreInfo(store)
h.rd.JSON(w, http.StatusOK, storeInfo)
}

Expand Down Expand Up @@ -222,13 +229,13 @@ func (h *storesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

stores = urlFilter.filter(cluster.GetStores())
for _, s := range stores {
store, status, err := cluster.GetStore(s.GetId())
store, err := cluster.GetStore(s.GetId())
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}

storeInfo := newStoreInfo(store, status)
storeInfo := newStoreInfo(store)
storesInfo.Stores = append(storesInfo.Stores, storeInfo)
}
storesInfo.Count = len(storesInfo.Stores)
Expand Down
18 changes: 9 additions & 9 deletions server/api/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,17 @@ func (s *testStoreSuite) TestUrlStoreFilter(c *C) {
}

func (s *testStoreSuite) TestDownState(c *C) {
status := &server.StoreStatus{
StoreStats: &pdpb.StoreStats{},
}
store := &metapb.Store{
State: metapb.StoreState_Up,
store := &server.StoreInfo{
Store: &metapb.Store{
State: metapb.StoreState_Up,
},
Stats: &pdpb.StoreStats{},
LastHeartbeatTS: time.Now(),
}
status.LastHeartbeatTS = time.Now()
storeInfo := newStoreInfo(store, status)
storeInfo := newStoreInfo(store)
c.Assert(storeInfo.Store.StateName, Equals, metapb.StoreState_Up.String())

status.LastHeartbeatTS = time.Now().Add(-time.Minute * 2)
storeInfo = newStoreInfo(store, status)
store.LastHeartbeatTS = time.Now().Add(-time.Minute * 2)
storeInfo = newStoreInfo(store)
c.Assert(storeInfo.Store.StateName, Equals, downStateName)
}
6 changes: 3 additions & 3 deletions server/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func minBalanceDiff(count uint64) float64 {
// shouldBalance returns true if we should balance the source and target store.
// The min balance diff provides a buffer to make the cluster stable, so that we
// don't need to schedule very frequently.
func shouldBalance(source, target *storeInfo, kind ResourceKind) bool {
func shouldBalance(source, target *StoreInfo, kind ResourceKind) bool {
sourceCount := source.resourceCount(kind)
sourceScore := source.resourceScore(kind)
targetScore := target.resourceScore(kind)
Expand Down Expand Up @@ -289,7 +289,7 @@ func (r *replicaChecker) SelectBestStoreToAddReplica(region *RegionInfo, filters
filters = append(filters, newFilters...)

var (
bestStore *storeInfo
bestStore *StoreInfo
bestScore float64
)

Expand Down Expand Up @@ -317,7 +317,7 @@ func (r *replicaChecker) SelectBestStoreToAddReplica(region *RegionInfo, filters
// selectWorstPeer returns the worst peer in the region.
func (r *replicaChecker) selectWorstPeer(region *RegionInfo, filters ...Filter) (*metapb.Peer, float64) {
var (
worstStore *storeInfo
worstStore *StoreInfo
worstScore float64
)

Expand Down
36 changes: 19 additions & 17 deletions server/balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func newTestClusterInfo(cluster *clusterInfo) *testClusterInfo {
func (c *testClusterInfo) setStoreUp(storeID uint64) {
store := c.getStore(storeID)
store.State = metapb.StoreState_Up
store.status.LastHeartbeatTS = time.Now()
store.LastHeartbeatTS = time.Now()
c.putStore(store)
}

func (c *testClusterInfo) setStoreDown(storeID uint64) {
store := c.getStore(storeID)
store.State = metapb.StoreState_Up
store.status.LastHeartbeatTS = time.Time{}
store.LastHeartbeatTS = time.Time{}
c.putStore(store)
}

Expand All @@ -52,24 +52,26 @@ func (c *testClusterInfo) setStoreOffline(storeID uint64) {

func (c *testClusterInfo) setStoreBusy(storeID uint64, busy bool) {
store := c.getStore(storeID)
store.status.IsBusy = busy
store.status.LastHeartbeatTS = time.Now()
store.Stats.IsBusy = busy
store.LastHeartbeatTS = time.Now()
c.putStore(store)
}

func (c *testClusterInfo) addLeaderStore(storeID uint64, leaderCount int) {
store := newStoreInfo(&metapb.Store{Id: storeID})
store.status.LastHeartbeatTS = time.Now()
store.status.LeaderCount = leaderCount
store.Stats = &pdpb.StoreStats{}
store.LastHeartbeatTS = time.Now()
store.LeaderCount = leaderCount
c.putStore(store)
}

func (c *testClusterInfo) addRegionStore(storeID uint64, regionCount int) {
store := newStoreInfo(&metapb.Store{Id: storeID})
store.status.LastHeartbeatTS = time.Now()
store.status.RegionCount = regionCount
store.status.Capacity = uint64(1024)
store.status.Available = store.status.Capacity
store.Stats = &pdpb.StoreStats{}
store.LastHeartbeatTS = time.Now()
store.RegionCount = regionCount
store.Stats.Capacity = uint64(1024)
store.Stats.Available = store.Stats.Capacity
c.putStore(store)
}

Expand Down Expand Up @@ -120,33 +122,33 @@ func (c *testClusterInfo) addLeaderRegionWithWriteInfo(regionID uint64, leaderID

func (c *testClusterInfo) updateLeaderCount(storeID uint64, leaderCount int) {
store := c.getStore(storeID)
store.status.LeaderCount = leaderCount
store.LeaderCount = leaderCount
c.putStore(store)
}

func (c *testClusterInfo) updateRegionCount(storeID uint64, regionCount int) {
store := c.getStore(storeID)
store.status.RegionCount = regionCount
store.RegionCount = regionCount
c.putStore(store)
}

func (c *testClusterInfo) updateSnapshotCount(storeID uint64, snapshotCount int) {
store := c.getStore(storeID)
store.status.ApplyingSnapCount = uint32(snapshotCount)
store.Stats.ApplyingSnapCount = uint32(snapshotCount)
c.putStore(store)
}

func (c *testClusterInfo) updateStorageRatio(storeID uint64, usedRatio, availableRatio float64) {
store := c.getStore(storeID)
store.status.Capacity = uint64(1024)
store.status.UsedSize = uint64(float64(store.status.Capacity) * usedRatio)
store.status.Available = uint64(float64(store.status.Capacity) * availableRatio)
store.Stats.Capacity = uint64(1024)
store.Stats.UsedSize = uint64(float64(store.Stats.Capacity) * usedRatio)
store.Stats.Available = uint64(float64(store.Stats.Capacity) * availableRatio)
c.putStore(store)
}

func (c *testClusterInfo) updateStorageWrittenBytes(storeID uint64, BytesWritten uint64) {
store := c.getStore(storeID)
store.status.BytesWritten = BytesWritten
store.Stats.BytesWritten = BytesWritten
c.putStore(store)
}

Expand Down
Loading