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: fix health status with tls enabled #969

Merged
merged 8 commits into from
Mar 5, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/pd-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func main() {
log.Fatalf("create server failed: %v", errors.ErrorStack(err))
}

if err = api.InitHTTPClient(svr); err != nil {
log.Fatalf("initial http client for api handler failed: %v", errors.ErrorStack(err))
}

sc := make(chan os.Signal, 1)
signal.Notify(sc,
syscall.SIGHUP,
Expand Down
35 changes: 14 additions & 21 deletions server/api/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
package api

import (
"bytes"
"encoding/json"
"math/rand"
"net/http"
"time"

. "github.com/pingcap/check"
Expand All @@ -27,11 +25,6 @@ import (
var _ = Suite(&testConfigSuite{})

type testConfigSuite struct {
hc *http.Client
}

func (s *testConfigSuite) SetUpSuite(c *C) {
s.hc = newHTTPClient()
}

func checkConfigResponse(c *C, body []byte, cfgs []*server.Config) {
Expand All @@ -47,7 +40,7 @@ func (s *testConfigSuite) TestConfigAll(c *C) {
defer clean()

addr := cfgs[rand.Intn(len(cfgs))].ClientUrls + apiPrefix + "/api/v1/config"
resp, err := s.hc.Get(addr)
resp, err := doGet(addr)
c.Assert(err, IsNil)
cfg := &server.Config{}
err = readJSON(resp.Body, cfg)
Expand All @@ -56,18 +49,18 @@ func (s *testConfigSuite) TestConfigAll(c *C) {
r := map[string]int{"max-replicas": 5}
postData, err := json.Marshal(r)
c.Assert(err, IsNil)
err = postJSON(s.hc, addr, postData)
err = postJSON(addr, postData)
c.Assert(err, IsNil)
l := map[string]interface{}{
"location-labels": "zone,rack",
"region-schedule-limit": 10,
}
postData, err = json.Marshal(l)
c.Assert(err, IsNil)
err = postJSON(s.hc, addr, postData)
err = postJSON(addr, postData)
c.Assert(err, IsNil)

resp, err = s.hc.Get(addr)
resp, err = doGet(addr)
newCfg := &server.Config{}
err = readJSON(resp.Body, newCfg)
c.Assert(err, IsNil)
Expand All @@ -85,18 +78,18 @@ func (s *testConfigSuite) TestConfigSchedule(c *C) {
defer clean()

addr := cfgs[rand.Intn(len(cfgs))].ClientUrls + apiPrefix + "/api/v1/config/schedule"
resp, err := s.hc.Get(addr)
resp, err := doGet(addr)
c.Assert(err, IsNil)
sc := &server.ScheduleConfig{}
readJSON(resp.Body, sc)

sc.MaxStoreDownTime.Duration = time.Second
postData, err := json.Marshal(sc)
postAddr := cfgs[rand.Intn(len(cfgs))].ClientUrls + apiPrefix + "/api/v1/config/schedule"
err = postJSON(s.hc, postAddr, postData)
err = postJSON(postAddr, postData)
c.Assert(err, IsNil)

resp, err = s.hc.Get(addr)
resp, err = doGet(addr)
c.Assert(err, IsNil)
sc1 := &server.ScheduleConfig{}
readJSON(resp.Body, sc1)
Expand All @@ -112,7 +105,7 @@ func (s *testConfigSuite) TestConfigReplication(c *C) {
defer clean()

addr := cfgs[rand.Intn(len(cfgs))].ClientUrls + apiPrefix + "/api/v1/config/replicate"
resp, err := s.hc.Get(addr)
resp, err := doGet(addr)
c.Assert(err, IsNil)

rc := &server.ReplicationConfig{}
Expand All @@ -124,15 +117,15 @@ func (s *testConfigSuite) TestConfigReplication(c *C) {
rc1 := map[string]int{"max-replicas": 5}
postData, err := json.Marshal(rc1)
postAddr := cfgs[rand.Intn(len(cfgs))].ClientUrls + apiPrefix + "/api/v1/config/replicate"
err = postJSON(s.hc, postAddr, postData)
err = postJSON(postAddr, postData)
c.Assert(err, IsNil)
rc.LocationLabels = []string{"zone", "rack"}

rc2 := map[string]string{"location-labels": "zone,rack"}
postData, err = json.Marshal(rc2)
err = postJSON(s.hc, postAddr, postData)
err = postJSON(postAddr, postData)

resp, err = s.hc.Get(addr)
resp, err = doGet(addr)
c.Assert(err, IsNil)
rc3 := &server.ReplicationConfig{}

Expand All @@ -149,7 +142,7 @@ func (s *testConfigSuite) TestConfigLabelProperty(c *C) {
addr := svr.GetAddr() + apiPrefix + "/api/v1/config/label-property"

loadProperties := func() server.LabelPropertyConfig {
res, err := s.hc.Get(addr)
res, err := doGet(addr)
c.Assert(err, IsNil)
var cfg server.LabelPropertyConfig
err = readJSON(res.Body, &cfg)
Expand All @@ -166,7 +159,7 @@ func (s *testConfigSuite) TestConfigLabelProperty(c *C) {
`{"type": "bar", "action": "set", "label-key": "host", "label-value": "h1"}`,
}
for _, cmd := range cmds {
_, err := s.hc.Post(addr, "application/json", bytes.NewBufferString(cmd))
err := postJSON(addr, []byte(cmd))
c.Assert(err, IsNil)
}
cfg = loadProperties()
Expand All @@ -182,7 +175,7 @@ func (s *testConfigSuite) TestConfigLabelProperty(c *C) {
`{"type": "bar", "action": "delete", "label-key": "host", "label-value": "h1"}`,
}
for _, cmd := range cmds {
_, err := s.hc.Post(addr, "application/json", bytes.NewBufferString(cmd))
err := postJSON(addr, []byte(cmd))
c.Assert(err, IsNil)
}
cfg = loadProperties()
Expand Down
4 changes: 3 additions & 1 deletion server/api/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ func (h *healthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Health: true,
}
for _, cURL := range member.ClientUrls {
if err := doGet(fmt.Sprintf("%s%s%s", cURL, apiPrefix, pingAPI)); err != nil {
resp, err := doGet(fmt.Sprintf("%s%s%s", cURL, apiPrefix, pingAPI))
if err != nil {
h.Health = false
}
resp.Body.Close()
}
healths = append(healths, h)
}
Expand Down
4 changes: 2 additions & 2 deletions server/api/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ func (s *testOperatorSuite) TestAddRemovePeer(c *C) {
c.Assert(strings.Contains(operator, "operator not found"), IsTrue)

mustPutStore(c, s.svr, 3, metapb.StoreState_Up, nil)
err := postJSON(&http.Client{}, fmt.Sprintf("%s/operators", s.urlPrefix), []byte(`{"name":"add-peer", "region_id": 1, "store_id": 3}`))
err := postJSON(fmt.Sprintf("%s/operators", s.urlPrefix), []byte(`{"name":"add-peer", "region_id": 1, "store_id": 3}`))
c.Assert(err, IsNil)
operator = mustReadURL(c, regionURL)
c.Assert(strings.Contains(operator, "add peer 1 on store 3"), IsTrue)

err = doDelete(regionURL)
c.Assert(err, IsNil)

err = postJSON(&http.Client{}, fmt.Sprintf("%s/operators", s.urlPrefix), []byte(`{"name":"remove-peer", "region_id": 1, "store_id": 2}`))
err = postJSON(fmt.Sprintf("%s/operators", s.urlPrefix), []byte(`{"name":"remove-peer", "region_id": 1, "store_id": 2}`))
c.Assert(err, IsNil)
operator = mustReadURL(c, regionURL)
c.Log(operator)
Expand Down
16 changes: 3 additions & 13 deletions server/api/redirector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package api

import (
"crypto/tls"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -68,26 +67,17 @@ func (h *redirector) ServeHTTP(w http.ResponseWriter, r *http.Request, next http
return
}

tlsConfig, err := h.s.GetSecurityConfig().ToTLSConfig()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
newCustomReverseProxies(urls, tlsConfig).ServeHTTP(w, r)
newCustomReverseProxies(urls).ServeHTTP(w, r)
}

type customReverseProxies struct {
urls []url.URL
client *http.Client
}

func newCustomReverseProxies(urls []url.URL, tlsConfig *tls.Config) *customReverseProxies {
func newCustomReverseProxies(urls []url.URL) *customReverseProxies {
p := &customReverseProxies{
client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
},
client: dialClient,
}

p.urls = append(p.urls, urls...)
Expand Down
3 changes: 1 addition & 2 deletions server/api/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package api
import (
"encoding/json"
"fmt"
"net/http"

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
Expand Down Expand Up @@ -90,7 +89,7 @@ func (s *testScheduleSuite) testAddAndRemoveScheduler(name, createdName string,
if createdName == "" {
createdName = name
}
err := postJSON(&http.Client{}, s.urlPrefix, body)
err := postJSON(s.urlPrefix, body)
c.Assert(err, IsNil)
handler := s.svr.GetHandler()
sches, err := handler.GetSchedulers()
Expand Down
21 changes: 21 additions & 0 deletions server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ import (
"net/http"

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

const apiPrefix = "/pd"

var dialClient = &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
},
}

// NewHandler creates a HTTP handler for API.
func NewHandler(svr *server.Server) http.Handler {
engine := negroni.New()
Expand All @@ -40,3 +47,17 @@ func NewHandler(svr *server.Server) http.Handler {

return engine
}

// InitHTTPClient initials a http client for api handler.
func InitHTTPClient(svr *server.Server) error {
tlsConfig, err := svr.GetSecurityConfig().ToTLSConfig()
if err != nil {
return errors.Trace(err)
}

dialClient = &http.Client{Transport: &http.Transport{
TLSClientConfig: tlsConfig,
DisableKeepAlives: true,
}}
return nil
}
3 changes: 1 addition & 2 deletions server/api/store_ns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package api
import (
"encoding/json"
"fmt"
"net/http"

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
Expand Down Expand Up @@ -90,6 +89,6 @@ func (s *testStoreNsSuite) TestCreateNamespace(c *C) {
body := map[string]string{"namespace": "test"}
b, err := json.Marshal(body)
c.Assert(err, IsNil)
err = postJSON(&http.Client{}, fmt.Sprintf("%s/classifier/table/namespaces", s.urlPrefix), b)
err = postJSON(fmt.Sprintf("%s/classifier/table/namespaces", s.urlPrefix), b)
c.Assert(err, IsNil)
}
10 changes: 5 additions & 5 deletions server/api/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (s *testStoreSuite) TestStoreLabel(c *C) {
labels := map[string]string{"zone": "cn", "host": "local"}
b, err := json.Marshal(labels)
c.Assert(err, IsNil)
err = postJSON(&http.Client{}, url+"/label", b)
err = postJSON(url+"/label", b)
c.Assert(err, IsNil)

err = readJSONWithURL(url, &info)
Expand All @@ -148,7 +148,7 @@ func (s *testStoreSuite) TestStoreLabel(c *C) {
labels = map[string]string{"zack": "zack1", "Host": "host1"}
b, err = json.Marshal(labels)
c.Assert(err, IsNil)
err = postJSON(&http.Client{}, url+"/label", b)
err = postJSON(url+"/label", b)
c.Assert(err, IsNil)

expectLabel := map[string]string{"zone": "cn", "zack": "zack1", "host": "host1"}
Expand Down Expand Up @@ -197,23 +197,23 @@ func (s *testStoreSuite) TestStoreSetState(c *C) {

// Set to Offline.
info = StoreInfo{}
err = postJSON(&http.Client{}, url+"/state?state=Offline", nil)
err = postJSON(url+"/state?state=Offline", nil)
c.Assert(err, IsNil)
err = readJSONWithURL(url, &info)
c.Assert(err, IsNil)
c.Assert(info.Store.State, Equals, metapb.StoreState_Offline)

// Invalid state.
info = StoreInfo{}
err = postJSON(&http.Client{}, url+"/state?state=Foo", nil)
err = postJSON(url+"/state?state=Foo", nil)
c.Assert(err, NotNil)
err = readJSONWithURL(url, &info)
c.Assert(err, IsNil)
c.Assert(info.Store.State, Equals, metapb.StoreState_Offline)

// Set back to Up.
info = StoreInfo{}
err = postJSON(&http.Client{}, url+"/state?state=Up", nil)
err = postJSON(url+"/state?state=Up", nil)
c.Assert(err, IsNil)
err = readJSONWithURL(url, &info)
c.Assert(err, IsNil)
Expand Down
20 changes: 12 additions & 8 deletions server/api/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func readJSON(r io.ReadCloser, data interface{}) error {
return nil
}

func postJSON(cli *http.Client, url string, data []byte) error {
resp, err := cli.Post(url, "application/json", bytes.NewBuffer(data))
func postJSON(url string, data []byte) error {
resp, err := dialClient.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
return errors.Trace(err)
}
Expand All @@ -59,21 +59,25 @@ func doDelete(url string) error {
if err != nil {
return err
}
res, err := http.DefaultClient.Do(req)
res, err := dialClient.Do(req)
if err != nil {
return err
}
res.Body.Close()
return nil
}

func doGet(url string) error {
resp, err := http.Get(url)
func doGet(url string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return errors.Trace(err)
return nil, errors.Trace(err)
}
resp, err := dialClient.Do(req)
if err != nil {
return nil, errors.Trace(err)
}
if resp.StatusCode != http.StatusOK {
return errors.Errorf("http get url %s return code %d", url, resp.StatusCode)
return nil, errors.Errorf("http get url %s return code %d", url, resp.StatusCode)
}
return nil
return resp, nil
}