Skip to content

Commit

Permalink
make original HTTP API compatible with config manager
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx committed Jan 19, 2020
1 parent 6a28790 commit e76bd48
Show file tree
Hide file tree
Showing 24 changed files with 274 additions and 246 deletions.
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ require (
github.com/gorilla/mux v1.7.3
github.com/gorilla/websocket v1.2.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway v1.12.1
github.com/juju/ratelimit v1.0.1
github.com/mattn/go-shellwords v1.0.3
github.com/montanaflynn/stats v0.0.0-20151014174947-eeaced052adb
Expand Down Expand Up @@ -52,5 +51,3 @@ require (
google.golang.org/grpc v1.25.1
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)

replace github.com/pingcap/kvproto => github.com/rleungx/kvproto v0.0.0-20200108095827-b74c3984daa5
17 changes: 6 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrU
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/antihax/optional v0.0.0-20180407024304-ca021399b1a6/go.mod h1:V8iCPQYkqmusNa815XgQio277wI47sdRh1dUOLdyC6Q=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0=
Expand Down Expand Up @@ -105,6 +104,7 @@ github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4er
github.com/golang/groupcache v0.0.0-20181024230925-c65c006176ff h1:kOkM9whyQYodu09SJ6W3NCsHG7crFaJILQ22Gozp3lg=
github.com/golang/groupcache v0.0.0-20181024230925-c65c006176ff/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v0.0.0-20180814211427-aa810b61a9c7/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand All @@ -131,8 +131,6 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92Bcuy
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.5 h1:UImYN5qQ8tuGpGE16ZmjvcTtTw24zw1QAp/SlnNrZhI=
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.12.1 h1:zCy2xE9ablevUOrUZc3Dl72Dt+ya2FNAvC2yLYMHzi4=
github.com/grpc-ecosystem/grpc-gateway v1.12.1/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
Expand Down Expand Up @@ -216,6 +214,9 @@ github.com/pingcap/errors v0.11.5-0.20190809092503-95897b64e011 h1:58naV4XMEqm0h
github.com/pingcap/errors v0.11.5-0.20190809092503-95897b64e011/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
github.com/pingcap/failpoint v0.0.0-20191029060244-12f4ac2fd11d h1:F8vp38kTAckN+v8Jlc98uMBvKIzr1a+UhnLyVYn8Q5Q=
github.com/pingcap/failpoint v0.0.0-20191029060244-12f4ac2fd11d/go.mod h1:DNS3Qg7bEDhU6EXNHF+XSv/PGznQaMJ5FWvctpm6pQI=
github.com/pingcap/kvproto v0.0.0-20191211054548-3c6b38ea5107/go.mod h1:WWLmULLO7l8IOcQG+t+ItJ3fEcrL5FxF0Wu+HrMy26w=
github.com/pingcap/kvproto v0.0.0-20191213111810-93cb7c623c8b h1:gYXdnlC+ipZzLt9FrGc4iDlCtWvWCncJBVULiJQHzYg=
github.com/pingcap/kvproto v0.0.0-20191213111810-93cb7c623c8b/go.mod h1:WWLmULLO7l8IOcQG+t+ItJ3fEcrL5FxF0Wu+HrMy26w=
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9 h1:AJD9pZYm72vMgPcQDww9rkZ1DnWfl0pXV3BOWlkYIjA=
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/sysutil v0.0.0-20191216090214-5f9620d22b3b h1:EEyo/SCRswLGuSk+7SB86Ak1p8bS6HL1Mi4Dhyuv6zg=
Expand All @@ -238,10 +239,7 @@ github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.2 h1:6LJUbpNm42llc4HRCuvApCSWB/WfhuNo9K98Q9sNGfs=
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/rleungx/kvproto v0.0.0-20200108095827-b74c3984daa5 h1:PyVrXq/3zjxA30YPoK8W14y8TI/M/SlU4YoTL9DTmvw=
github.com/rleungx/kvproto v0.0.0-20200108095827-b74c3984daa5/go.mod h1:74hTtf0KOCW1kGA7+XpuHDpbZxmlPM8z3VQTpTalS0E=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/sergi/go-diff v1.0.1-0.20180205163309-da645544ed44/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shirou/gopsutil v2.19.10+incompatible h1:lA4Pi29JEVIQIgATSeftHSY0rMGI9CLrl2ZvDLiahto=
Expand Down Expand Up @@ -349,7 +347,6 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 h1:fHDIZ2oxGnUZRN6WgWFCbYBjH9uqVPRCUVUDhs0wnbA=
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191002035440-2ec189313ef0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -407,15 +404,14 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20181004005441-af9cb2a35e7f/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE=
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/genproto v0.0.0-20190927181202-20e1ac93f88c h1:hrpEMCZ2O7DR5gC1n2AJGVhrwiEjOi35+jxtIuZpTMo=
google.golang.org/genproto v0.0.0-20190927181202-20e1ac93f88c/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8=
google.golang.org/grpc v0.0.0-20180607172857-7a6a684ca69e/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.23.1 h1:q4XQuHFC6I28BKZpo6IYyb3mNO+l7lSOxRuYTCiDfXk=
google.golang.org/grpc v1.23.1/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.24.0/go.mod h1:XDChyiUovWa60DnaeDeZmSW86xtLtjtZbwvSiRnRtcA=
google.golang.org/grpc v1.25.1 h1:wdKvqQk7IttEw92GoRyKG2IDrUIpgpj6H6m81yfeMW0=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
gopkg.in/alecthomas/gometalinter.v2 v2.0.12/go.mod h1:NDRytsqEZyolNuAgTzJkZMkSQM7FIKyzVzGhjB/qfYo=
Expand Down Expand Up @@ -444,7 +440,6 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
4 changes: 2 additions & 2 deletions pkg/keyvisual/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type keyvisualService struct {
}

// NewKeyvisualService creates a HTTP handler for heatmap service.
func NewKeyvisualService(ctx context.Context, svr *server.Server) (http.Handler, server.APIGroup, func()) {
func NewKeyvisualService(ctx context.Context, svr *server.Server) (http.Handler, server.APIGroup) {
mux := http.NewServeMux()
k := &keyvisualService{
ServeMux: mux,
Expand All @@ -62,7 +62,7 @@ func NewKeyvisualService(ctx context.Context, svr *server.Server) (http.Handler,
negroni.Wrap(k),
)
go k.run()
return handler, defaultRegisterAPIGroupInfo, nil
return handler, defaultRegisterAPIGroupInfo
}

// Heatmap returns the heatmap data.
Expand Down
135 changes: 128 additions & 7 deletions server/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/http"
"reflect"

"github.com/BurntSushi/toml"
"github.com/pingcap/errcode"
"github.com/pingcap/kvproto/pkg/configpb"
"github.com/pingcap/log"
Expand Down Expand Up @@ -48,6 +49,27 @@ func (h *confHandler) Get(w http.ResponseWriter, r *http.Request) {
}

func (h *confHandler) Post(w http.ResponseWriter, r *http.Request) {
if h.svr.GetConfig().EnableConfigManager {
client := h.svr.GetConfigClient()
if client == nil {
h.rd.JSON(w, http.StatusServiceUnavailable, "no leader")
}
cm := h.svr.GetConfigManager()
m := make(map[string]interface{})
json.NewDecoder(r.Body).Decode(&m)
entries, err := transToEntries(m)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
err = redirectUpdateReq(h.svr.Context(), client, cm, entries)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
h.rd.JSON(w, http.StatusOK, nil)
return
}
config := h.svr.GetConfig()
data, err := ioutil.ReadAll(r.Body)
r.Body.Close()
Expand Down Expand Up @@ -137,6 +159,27 @@ func (h *confHandler) GetSchedule(w http.ResponseWriter, r *http.Request) {
}

func (h *confHandler) SetSchedule(w http.ResponseWriter, r *http.Request) {
if h.svr.GetConfig().EnableConfigManager {
client := h.svr.GetConfigClient()
if client == nil {
h.rd.JSON(w, http.StatusServiceUnavailable, "no leader")
}
cm := h.svr.GetConfigManager()
m := make(map[string]interface{})
json.NewDecoder(r.Body).Decode(&m)
entries, err := transToEntries(m)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
err = redirectUpdateReq(h.svr.Context(), client, cm, entries)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
h.rd.JSON(w, http.StatusOK, nil)
return
}
config := h.svr.GetScheduleConfig()
if err := apiutil.ReadJSONRespondError(h.rd, w, r.Body, &config); err != nil {
return
Expand All @@ -154,6 +197,27 @@ func (h *confHandler) GetReplication(w http.ResponseWriter, r *http.Request) {
}

func (h *confHandler) SetReplication(w http.ResponseWriter, r *http.Request) {
if h.svr.GetConfig().EnableConfigManager {
client := h.svr.GetConfigClient()
if client == nil {
h.rd.JSON(w, http.StatusServiceUnavailable, "no leader")
}
cm := h.svr.GetConfigManager()
m := make(map[string]interface{})
json.NewDecoder(r.Body).Decode(&m)
entries, err := transToEntries(m)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
err = redirectUpdateReq(h.svr.Context(), client, cm, entries)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
h.rd.JSON(w, http.StatusOK, nil)
return
}
config := h.svr.GetReplicationConfig()
if err := apiutil.ReadJSONRespondError(h.rd, w, r.Body, &config); err != nil {
return
Expand All @@ -175,6 +239,53 @@ func (h *confHandler) SetLabelProperty(w http.ResponseWriter, r *http.Request) {
if err := apiutil.ReadJSONRespondError(h.rd, w, r.Body, &input); err != nil {
return
}

if h.svr.GetConfig().EnableConfigManager {
client := h.svr.GetConfigClient()
if client == nil {
h.rd.JSON(w, http.StatusServiceUnavailable, "no leader")
}
cm := h.svr.GetConfigManager()
typ := input["type"]
labelKey, labelValue := input["label-key"], input["label-value"]
cfg := h.svr.GetScheduleOption().LoadLabelPropertyConfig().Clone()
switch input["action"] {
case "set":
for _, l := range cfg[typ] {
if l.Key == labelKey && l.Value == labelValue {
return
}
}
cfg[typ] = append(cfg[typ], config.StoreLabel{Key: labelKey, Value: labelValue})
case "delete":
oldLabels := cfg[typ]
cfg[typ] = []config.StoreLabel{}
for _, l := range oldLabels {
if l.Key == labelKey && l.Value == labelValue {
continue
}
cfg[typ] = append(cfg[typ], l)
}
if len(cfg[typ]) == 0 {
delete(cfg, typ)
}
default:
err := errors.Errorf("unknown action %v", input["action"])
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
var buf bytes.Buffer
toml.NewEncoder(&buf).Encode(cfg)
entries := []*entry{{key: "label-property", value: buf.String()}}
err := redirectUpdateReq(h.svr.Context(), client, cm, entries)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
h.rd.JSON(w, http.StatusOK, nil)
return
}

var err error
switch input["action"] {
case "set":
Expand Down Expand Up @@ -205,17 +316,27 @@ func (h *confHandler) SetClusterVersion(w http.ResponseWriter, r *http.Request)
apiutil.ErrorResp(h.rd, w, errcode.NewInvalidInputErr(errors.New("not set cluster-version")))
return
}

if h.svr.GetConfig().EnableConfigManager {
kind := &configpb.ConfigKind{Kind: &configpb.ConfigKind_Global{Global: &configpb.Global{Component: server.Component}}}
v := &configpb.Version{Global: h.svr.GetConfigManager().GlobalCfgs[server.Component].GetVersion()}
entry := &configpb.ConfigEntry{Name: "cluster-version", Value: version}
client := h.svr.GetConfigClient()
if client == nil {
h.rd.JSON(w, http.StatusServiceUnavailable, "no leader")
}
_, _, err := h.svr.GetConfigClient().Update(h.svr.Context(), v, kind, []*configpb.ConfigEntry{entry})
if err != nil {
log.Error("update cluster version meet error", zap.Error(err))
}
h.rd.JSON(w, http.StatusOK, nil)
return
}

err := h.svr.SetClusterVersion(version)
if err != nil {
apiutil.ErrorResp(h.rd, w, errcode.NewInternalErr(err))
return
}
kind := &configpb.ConfigKind{Kind: &configpb.ConfigKind_Global{Global: &configpb.Global{Component: server.Component}}}
v := &configpb.Version{Global: h.svr.GetConfigManager().GlobalCfgs[server.Component].GetVersion()}
entry := &configpb.ConfigEntry{Name: "cluster-version", Value: version}
_, _, err = h.svr.GetConfigClient().Update(h.svr.Context(), v, kind, []*configpb.ConfigEntry{entry})
if err != nil {
log.Error("update cluster version meet error", zap.Error(err))
}
h.rd.JSON(w, http.StatusOK, nil)
}
13 changes: 10 additions & 3 deletions server/api/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ type testConfigSuite struct {
}

func (s *testConfigSuite) SetUpSuite(c *C) {
server.ConfigCheckInterval = 10 * time.Millisecond
s.svr, s.cleanup = mustNewServer(c, func(cfg *config.Config) { cfg.EnableConfigManager = true })
mustWaitLeader(c, []*server.Server{s.svr})

addr := s.svr.GetAddr()
s.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix)
// make sure the config client is initialized
time.Sleep(20 * time.Millisecond)
}

func (s *testConfigSuite) TearDownSuite(c *C) {
Expand Down Expand Up @@ -71,7 +74,7 @@ func (s *testConfigSuite) TestConfigAll(c *C) {
err = postJSON(addr, postData)
c.Assert(err, IsNil)

time.Sleep(2 * time.Second)
time.Sleep(20 * time.Millisecond)
newCfg := &config.Config{}
err = readJSON(addr, newCfg)
c.Assert(err, IsNil)
Expand All @@ -93,7 +96,7 @@ func (s *testConfigSuite) TestConfigSchedule(c *C) {
err = postJSON(addr, postData)
c.Assert(err, IsNil)

time.Sleep(2 * time.Second)
time.Sleep(20 * time.Millisecond)
sc1 := &config.ScheduleConfig{}
c.Assert(readJSON(addr, sc1), IsNil)
c.Assert(*sc, DeepEquals, *sc1)
Expand All @@ -120,7 +123,7 @@ func (s *testConfigSuite) TestConfigReplication(c *C) {
err = postJSON(addr, postData)
c.Assert(err, IsNil)

time.Sleep(2 * time.Second)
time.Sleep(20 * time.Millisecond)
rc3 := &config.ReplicationConfig{}
err = readJSON(addr, rc3)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -149,7 +152,9 @@ func (s *testConfigSuite) TestConfigLabelProperty(c *C) {
for _, cmd := range cmds {
err := postJSON(addr, []byte(cmd))
c.Assert(err, IsNil)
time.Sleep(20 * time.Millisecond)
}

cfg = loadProperties()
c.Assert(cfg, HasLen, 2)
c.Assert(cfg["foo"], DeepEquals, []config.StoreLabel{
Expand All @@ -165,7 +170,9 @@ func (s *testConfigSuite) TestConfigLabelProperty(c *C) {
for _, cmd := range cmds {
err := postJSON(addr, []byte(cmd))
c.Assert(err, IsNil)
time.Sleep(20 * time.Millisecond)
}

cfg = loadProperties()
c.Assert(cfg, HasLen, 1)
c.Assert(cfg["foo"], DeepEquals, []config.StoreLabel{{Key: "zone", Value: "cn2"}})
Expand Down
8 changes: 7 additions & 1 deletion server/api/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (s *testLabelsStoreSuite) SetUpSuite(c *C) {
},
}

server.ConfigCheckInterval = 10 * time.Millisecond
s.svr, s.cleanup = mustNewServer(c, func(cfg *config.Config) {
cfg.Replication.StrictlyMatchLabel = false
cfg.EnableConfigManager = true
Expand All @@ -121,6 +122,8 @@ func (s *testLabelsStoreSuite) SetUpSuite(c *C) {
for _, store := range s.stores {
mustPutStore(c, s.svr, store.Id, store.State, store.Labels)
}
// make sure the config client is initialized
time.Sleep(20 * time.Millisecond)
}

func (s *testLabelsStoreSuite) TearDownSuite(c *C) {
Expand Down Expand Up @@ -187,6 +190,7 @@ type testStrictlyLabelsStoreSuite struct {
}

func (s *testStrictlyLabelsStoreSuite) SetUpSuite(c *C) {
server.ConfigCheckInterval = 10 * time.Millisecond
s.svr, s.cleanup = mustNewServer(c, func(cfg *config.Config) {
cfg.Replication.LocationLabels = []string{"zone", "disk"}
cfg.Replication.StrictlyMatchLabel = true
Expand All @@ -198,6 +202,8 @@ func (s *testStrictlyLabelsStoreSuite) SetUpSuite(c *C) {
s.urlPrefix = fmt.Sprintf("%s%s/api/v1", addr, apiPrefix)

mustBootstrapCluster(c, s.svr)
// make sure the config client is initialized
time.Sleep(20 * time.Millisecond)
}

func (s *testStrictlyLabelsStoreSuite) TestStoreMatch(c *C) {
Expand Down Expand Up @@ -282,7 +288,7 @@ func (s *testStrictlyLabelsStoreSuite) TestStoreMatch(c *C) {

// enable placement rules. Report no error any more.
c.Assert(postJSON(fmt.Sprintf("%s/config", s.urlPrefix), []byte(`{"enable-placement-rules":"true"}`)), IsNil)
time.Sleep(2 * time.Second)
time.Sleep(20 * time.Millisecond)
for _, t := range cases {
_, err := s.svr.PutStore(context.Background(), &pdpb.PutStoreRequest{
Header: &pdpb.RequestHeader{ClusterId: s.svr.ClusterID()},
Expand Down
Loading

0 comments on commit e76bd48

Please sign in to comment.