Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
nolouch committed May 16, 2017
1 parent d13a80e commit 1d4e19b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 31 deletions.
19 changes: 6 additions & 13 deletions server/api/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package api
import (
"net/http"

"github.com/gorilla/mux"
"github.com/juju/errors"
"github.com/pingcap/pd/server"
"github.com/unrolled/render"
Expand All @@ -40,17 +39,11 @@ func (h *clusterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.rd.JSON(w, http.StatusOK, h.svr.GetCluster())
}

func (h *clusterHandler) GetRaftClusterBootstrapTime(w http.ResponseWriter, r *http.Request) {
option := mux.Vars(r)["bootstrap_time"]
switch option {
case option:
data, err := h.svr.GetRaftClusterBootstrapTime()
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
h.rd.JSON(w, http.StatusOK, data)
default:
h.rd.JSON(w, http.StatusInternalServerError, errUnknownStatusOption.Error())
func (h *clusterHandler) GetRaftClusterStatus(w http.ResponseWriter, r *http.Request) {
status, err := h.svr.GetRaftClusterStatus()
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
h.rd.JSON(w, http.StatusOK, status)
}
14 changes: 7 additions & 7 deletions server/api/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ func (s *testClusterInfo) TestCluster(c *C) {
c.Assert(c1, DeepEquals, c2)
}

func (s *testClusterInfo) TestGetBootstrapTime(c *C) {
url := fmt.Sprintf("%s/cluster/raft/status/bootstrap_time", s.urlPrefix)
var t, now time.Time
err := readJSONWithURL(url, t)
func (s *testClusterInfo) TestGetClusterStatus(c *C) {
url := fmt.Sprintf("%s/cluster/raft/status", s.urlPrefix)
status := server.RaftClusterStatus{}
err := readJSONWithURL(url, &status)
c.Assert(err, NotNil)
now = time.Now()
now := time.Now()
mustBootstrapCluster(c, s.svr)
err = readJSONWithURL(url, &t)
err = readJSONWithURL(url, &status)
c.Assert(err, IsNil)
c.Assert(t.After(now), IsTrue)
c.Assert(status.BootstrapTime.After(now), IsTrue)
}
3 changes: 1 addition & 2 deletions server/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func createRouter(prefix string, svr *server.Server) *mux.Router {
})

router := mux.NewRouter().PathPrefix(prefix).Subrouter()

handler := svr.GetHandler()

historyHanlder := newHistoryHandler(handler, rd)
Expand All @@ -46,7 +45,7 @@ func createRouter(prefix string, svr *server.Server) *mux.Router {
router.HandleFunc("/api/v1/schedulers/{name}", schedulerHandler.Delete).Methods("DELETE")

router.Handle("/api/v1/cluster", newClusterHandler(svr, rd)).Methods("GET")
router.HandleFunc("/api/v1/cluster/raft/status/{option}", newClusterHandler(svr, rd).GetRaftClusterBootstrapTime).Methods("GET")
router.HandleFunc("/api/v1/cluster/raft/status", newClusterHandler(svr, rd).GetRaftClusterStatus).Methods("GET")

confHandler := newConfHandler(svr, rd)
router.HandleFunc("/api/v1/config", confHandler.Get).Methods("GET")
Expand Down
38 changes: 29 additions & 9 deletions server/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ type RaftCluster struct {

wg sync.WaitGroup
quit chan struct{}

status *RaftClusterStatus
}

// RaftClusterStatus saves some state information
type RaftClusterStatus struct {
BootstrapTime time.Time `json:"bootstrap_time"`
}

func newRaftCluster(s *Server, clusterID uint64) *RaftCluster {
Expand All @@ -72,6 +79,16 @@ func newRaftCluster(s *Server, clusterID uint64) *RaftCluster {
}
}

func (c *RaftCluster) loadClusterStatus() error {
status := &RaftClusterStatus{}
time, err := c.s.kv.getClusterBootstrapTime()
if err != nil {
return errors.Trace(err)
}
status.BootstrapTime = time
c.status = status
return nil
}
func (c *RaftCluster) start() error {
c.Lock()
defer c.Unlock()
Expand Down Expand Up @@ -187,16 +204,19 @@ func (s *Server) GetCluster() *metapb.Cluster {
}
}

// GetRaftClusterBootstrapTime gets raft cluster bootstrap time
func (s *Server) GetRaftClusterBootstrapTime() (time.Time, error) {
data, err := getValue(s.client, makeBootstrapTimeKey(s.getClusterRootPath()))
if err != nil {
return zeroTime, errors.Trace(err)
}
if len(data) == 0 {
return zeroTime, ErrNotBootstrapped
// GetRaftClusterStatus gets cluster status
func (s *Server) GetRaftClusterStatus() (*RaftClusterStatus, error) {
s.cluster.Lock()
defer s.cluster.Unlock()
if s.cluster.status == nil {
err := s.cluster.loadClusterStatus()
if err != nil {
return nil, errors.Trace(err)
}
}
return parseTimestamp(data)
clone := &RaftClusterStatus{}
*clone = *(s.cluster.status)
return clone, nil
}

func (s *Server) createRaftCluster() error {
Expand Down
15 changes: 15 additions & 0 deletions server/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ func (kv *kv) regionPath(regionID uint64) string {
return path.Join(kv.clusterPath, "r", fmt.Sprintf("%020d", regionID))
}

func (kv *kv) clusterStatePath(option string) string {
return path.Join(kv.clusterPath, "status", option)
}

func (kv *kv) getClusterBootstrapTime() (time.Time, error) {
data, err := kv.load(kv.clusterStatePath("bootstrap_time"))
if err != nil {
return zeroTime, errors.Trace(err)
}
if len(data) == 0 {
return zeroTime, ErrNotBootstrapped
}
return parseTimestamp(data)
}

func (kv *kv) loadMeta(meta *metapb.Cluster) (bool, error) {
return kv.loadProto(kv.clusterPath, meta)
}
Expand Down

0 comments on commit 1d4e19b

Please sign in to comment.