Skip to content

Commit

Permalink
api: add initialized flag in cluster status (#1555) (#1581)
Browse files Browse the repository at this point in the history
* api: add initialized flag in cluster status

Signed-off-by: disksing <[email protected]>
  • Loading branch information
disksing authored and nolouch committed Jun 14, 2019
1 parent 9ae18a3 commit 42e6e08
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions server/api/api.raml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ types:
type: object
properties:
raft_bootstrap_time?: string
is_initialized: boolean
Version:
type: object
properties:
Expand Down
7 changes: 7 additions & 0 deletions server/api/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,16 @@ func (s *testClusterInfo) TestGetClusterStatus(c *C) {
err := readJSONWithURL(url, &status)
c.Assert(err, IsNil)
c.Assert(status.RaftBootstrapTime.IsZero(), IsTrue)
c.Assert(status.IsInitialized, IsFalse)
now := time.Now()
mustBootstrapCluster(c, s.svr)
err = readJSONWithURL(url, &status)
c.Assert(err, IsNil)
c.Assert(status.RaftBootstrapTime.After(now), IsTrue)
c.Assert(status.IsInitialized, IsFalse)
s.svr.SetReplicationConfig(server.ReplicationConfig{MaxReplicas: 1})
err = readJSONWithURL(url, &status)
c.Assert(err, IsNil)
c.Assert(status.RaftBootstrapTime.After(now), IsTrue)
c.Assert(status.IsInitialized, IsTrue)
}
38 changes: 32 additions & 6 deletions server/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type RaftCluster struct {
// ClusterStatus saves some state information
type ClusterStatus struct {
RaftBootstrapTime time.Time `json:"raft_bootstrap_time,omitempty"`
IsInitialized bool `json:"is_initialized"`
}

func newRaftCluster(s *Server, clusterID uint64) *RaftCluster {
Expand All @@ -78,18 +79,43 @@ func newRaftCluster(s *Server, clusterID uint64) *RaftCluster {
}

func (c *RaftCluster) loadClusterStatus() (*ClusterStatus, error) {
data, err := c.s.kv.Load((c.s.kv.ClusterStatePath("raft_bootstrap_time")))
bootstrapTime, err := c.loadBootstrapTime()
if err != nil {
return nil, err
}
if len(data) == 0 {
return &ClusterStatus{}, nil
var isInitialized bool
if bootstrapTime != zeroTime {
isInitialized = c.isInitialized()
}
t, err := parseTimestamp([]byte(data))
return &ClusterStatus{
RaftBootstrapTime: bootstrapTime,
IsInitialized: isInitialized,
}, nil
}

func (c *RaftCluster) isInitialized() bool {
if c.cachedCluster.getRegionCount() > 1 {
return true
}
region := c.cachedCluster.searchRegion(nil)
return region != nil &&
len(region.GetVoters()) >= int(c.s.GetReplicationConfig().MaxReplicas) &&
len(region.GetPendingPeers()) == 0
}

// loadBootstrapTime loads the saved bootstrap time from etcd. It returns zero
// value of time.Time when there is error or the cluster is not bootstrapped
// yet.
func (c *RaftCluster) loadBootstrapTime() (time.Time, error) {
var t time.Time
data, err := c.s.kv.Load(c.s.kv.ClusterStatePath("raft_bootstrap_time"))
if err != nil {
return nil, err
return t, err
}
if data == "" {
return t, nil
}
return &ClusterStatus{RaftBootstrapTime: t}, nil
return parseTimestamp([]byte(data))
}

func (c *RaftCluster) start() error {
Expand Down

0 comments on commit 42e6e08

Please sign in to comment.