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

api: add initialized flag in cluster status #1555

Merged
merged 3 commits into from Jun 3, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions server/api/api.raml
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
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
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
rleungx marked this conversation as resolved.
Show resolved Hide resolved
}

// 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