Skip to content

Commit

Permalink
Merge feb470d into bb61092
Browse files Browse the repository at this point in the history
  • Loading branch information
nolouch committed May 16, 2017
2 parents bb61092 + feb470d commit 9e37f75
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 25 deletions.
9 changes: 9 additions & 0 deletions server/api/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ func newClusterHandler(svr *server.Server, rd *render.Render) *clusterHandler {
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) {
data, err := h.svr.GetRaftClusterBootstrapTime()
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
h.rd.JSON(w, http.StatusOK, data)
}
13 changes: 13 additions & 0 deletions server/api/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package api

import (
"fmt"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
Expand Down Expand Up @@ -57,3 +58,15 @@ func (s *testClusterInfo) TestCluster(c *C) {
c1.MaxPeerCount = 6
c.Assert(c1, DeepEquals, c2)
}

func (s *testClusterInfo) TestGetBootstrapTime(c *C) {
url := fmt.Sprintf("%s/raft/bootstrap/time", s.urlPrefix)
var t, now time.Time
err := readJSONWithURL(url, t)
c.Assert(err, NotNil)
now = time.Now()
mustBootstrapCluster(c, s.svr)
err = readJSONWithURL(url, &t)
c.Assert(err, IsNil)
c.Assert(t.After(now), IsTrue)
}
4 changes: 2 additions & 2 deletions server/api/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func newFeedHandler(svr *server.Server, rd *render.Render) *feedHandler {
func (h *feedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
cluster := h.svr.GetRaftCluster()
if cluster == nil {
h.rd.JSON(w, http.StatusInternalServerError, errNotBootstrapped.Error())
h.rd.JSON(w, http.StatusInternalServerError, server.ErrNotBootstrapped.Error())
return
}

Expand Down Expand Up @@ -71,7 +71,7 @@ func newEventsHandler(svr *server.Server, rd *render.Render) *eventsHandler {
func (h *eventsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
cluster := h.svr.GetRaftCluster()
if cluster == nil {
h.rd.JSON(w, http.StatusInternalServerError, errNotBootstrapped.Error())
h.rd.JSON(w, http.StatusInternalServerError, server.ErrNotBootstrapped.Error())
return
}

Expand Down
4 changes: 2 additions & 2 deletions server/api/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func newLabelsHandler(svr *server.Server, rd *render.Render) *labelsHandler {
func (h *labelsHandler) Get(w http.ResponseWriter, r *http.Request) {
cluster := h.svr.GetRaftCluster()
if cluster == nil {
h.rd.JSON(w, http.StatusInternalServerError, errNotBootstrapped.Error())
h.rd.JSON(w, http.StatusInternalServerError, server.ErrNotBootstrapped.Error())
return
}
var labels []*metapb.StoreLabel
Expand All @@ -59,7 +59,7 @@ func (h *labelsHandler) Get(w http.ResponseWriter, r *http.Request) {
func (h *labelsHandler) GetStores(w http.ResponseWriter, r *http.Request) {
cluster := h.svr.GetRaftCluster()
if cluster == nil {
h.rd.JSON(w, http.StatusInternalServerError, errNotBootstrapped.Error())
h.rd.JSON(w, http.StatusInternalServerError, server.ErrNotBootstrapped.Error())
return
}

Expand Down
6 changes: 3 additions & 3 deletions server/api/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func newRegionHandler(svr *server.Server, rd *render.Render) *regionHandler {
func (h *regionHandler) GetRegionByID(w http.ResponseWriter, r *http.Request) {
cluster := h.svr.GetRaftCluster()
if cluster == nil {
h.rd.JSON(w, http.StatusInternalServerError, errNotBootstrapped.Error())
h.rd.JSON(w, http.StatusInternalServerError, server.ErrNotBootstrapped.Error())
return
}

Expand All @@ -67,7 +67,7 @@ func (h *regionHandler) GetRegionByID(w http.ResponseWriter, r *http.Request) {
func (h *regionHandler) GetRegionByKey(w http.ResponseWriter, r *http.Request) {
cluster := h.svr.GetRaftCluster()
if cluster == nil {
h.rd.JSON(w, http.StatusInternalServerError, errNotBootstrapped.Error())
h.rd.JSON(w, http.StatusInternalServerError, server.ErrNotBootstrapped.Error())
return
}
vars := mux.Vars(r)
Expand All @@ -91,7 +91,7 @@ func newRegionsHandler(svr *server.Server, rd *render.Render) *regionsHandler {
func (h *regionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
cluster := h.svr.GetRaftCluster()
if cluster == nil {
h.rd.JSON(w, http.StatusInternalServerError, errNotBootstrapped.Error())
h.rd.JSON(w, http.StatusInternalServerError, server.ErrNotBootstrapped.Error())
return
}

Expand Down
1 change: 1 addition & 0 deletions server/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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/raft/bootstrap/time", newClusterHandler(svr, rd).GetRaftClusterBootstrapTime).Methods("GET")

confHandler := newConfHandler(svr, rd)
router.HandleFunc("/api/v1/config", confHandler.Get).Methods("GET")
Expand Down
5 changes: 0 additions & 5 deletions server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@ import (
"net/http"

"github.com/gorilla/mux"
"github.com/juju/errors"
"github.com/pingcap/pd/server"
"github.com/urfave/negroni"
)

const apiPrefix = "/pd"

var (
errNotBootstrapped = errors.New("TiKV cluster is not bootstrapped, please start TiKV first")
)

// NewHandler creates a HTTP handler for API.
func NewHandler(svr *server.Server) http.Handler {
engine := negroni.New()
Expand Down
6 changes: 3 additions & 3 deletions server/api/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func newStoreHandler(svr *server.Server, rd *render.Render) *storeHandler {
func (h *storeHandler) Get(w http.ResponseWriter, r *http.Request) {
cluster := h.svr.GetRaftCluster()
if cluster == nil {
h.rd.JSON(w, http.StatusInternalServerError, errNotBootstrapped.Error())
h.rd.JSON(w, http.StatusInternalServerError, server.ErrNotBootstrapped.Error())
return
}

Expand All @@ -127,7 +127,7 @@ func (h *storeHandler) Get(w http.ResponseWriter, r *http.Request) {
func (h *storeHandler) Delete(w http.ResponseWriter, r *http.Request) {
cluster := h.svr.GetRaftCluster()
if cluster == nil {
h.rd.JSON(w, http.StatusInternalServerError, errNotBootstrapped.Error())
h.rd.JSON(w, http.StatusInternalServerError, server.ErrNotBootstrapped.Error())
return
}

Expand Down Expand Up @@ -169,7 +169,7 @@ func newStoresHandler(svr *server.Server, rd *render.Render) *storesHandler {
func (h *storesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
cluster := h.svr.GetRaftCluster()
if cluster == nil {
h.rd.JSON(w, http.StatusInternalServerError, errNotBootstrapped.Error())
h.rd.JSON(w, http.StatusInternalServerError, server.ErrNotBootstrapped.Error())
return
}

Expand Down
25 changes: 25 additions & 0 deletions server/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ const (
backgroundJobInterval = time.Minute
)

// Error instances
var (
ErrNotBootstrapped = errors.New("TiKV cluster is not bootstrapped, please start TiKV first")
)

// RaftCluster is used for cluster config management.
// Raft cluster key format:
// cluster 1 -> /1/raft, value is metapb.Cluster
Expand Down Expand Up @@ -182,6 +187,15 @@ 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)
}
return parseTimestamp(data)
}

func (s *Server) createRaftCluster() error {
if s.cluster.isRunning() {
return nil
Expand All @@ -206,6 +220,10 @@ func makeStoreKeyPrefix(clusterRootPath string) string {
return strings.Join([]string{clusterRootPath, "s", ""}, "/")
}

func makeBootstrapTimeKey(clusterRootPath string) string {
return strings.Join([]string{clusterRootPath, "bootstrap_time"}, "/")
}

func checkBootstrapRequest(clusterID uint64, req *pdpb.BootstrapRequest) error {
// TODO: do more check for request fields validation.

Expand Down Expand Up @@ -266,6 +284,13 @@ func (s *Server) bootstrapCluster(req *pdpb.BootstrapRequest) (*pdpb.BootstrapRe
var ops []clientv3.Op
ops = append(ops, clientv3.OpPut(clusterRootPath, string(clusterValue)))

// Set bootstrap time
bootstrapKey := makeBootstrapTimeKey(clusterRootPath)
nano := time.Now().UnixNano()

timeData := uint64ToBytes(uint64(nano))
ops = append(ops, clientv3.OpPut(bootstrapKey, string(timeData)))

// Set store meta
storeMeta := req.GetStore()
storePath := makeStoreKey(clusterRootPath, storeMeta.GetId())
Expand Down
11 changes: 1 addition & 10 deletions server/tso.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,7 @@ func (s *Server) loadTimestamp() (time.Time, error) {
if err != nil {
return zeroTime, errors.Trace(err)
}
if data == nil {
return zeroTime, nil
}

nano, err := bytesToUint64(data)
if err != nil {
return zeroTime, errors.Trace(err)
}

return time.Unix(0, int64(nano)), nil
return parseTimestamp(data)
}

// save timestamp, if lastTs is 0, we think the timestamp doesn't exist, so create it,
Expand Down
13 changes: 13 additions & 0 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,16 @@ func diffRegionKeyInfo(origin *RegionInfo, other *RegionInfo) string {

return strings.Join(ret, ",")
}

func parseTimestamp(data []byte) (time.Time, error) {
if data == nil {
return zeroTime, nil
}

nano, err := bytesToUint64(data)
if err != nil {
return zeroTime, errors.Trace(err)
}

return time.Unix(0, int64(nano)), nil
}

0 comments on commit 9e37f75

Please sign in to comment.