client.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package client
  15. import (
  16. "errors"
  17. "fmt"
  18. "io/ioutil"
  19. "math/rand"
  20. "net"
  21. "net/http"
  22. "net/url"
  23. "reflect"
  24. "sort"
  25. "strconv"
  26. "sync"
  27. "time"
  28. "golang.org/x/net/context"
  29. )
  30. var (
  31. ErrNoEndpoints = errors.New("client: no endpoints available")
  32. ErrTooManyRedirects = errors.New("client: too many redirects")
  33. ErrClusterUnavailable = errors.New("client: etcd cluster is unavailable or misconfigured")
  34. ErrNoLeaderEndpoint = errors.New("client: no leader endpoint available")
  35. errTooManyRedirectChecks = errors.New("client: too many redirect checks")
  36. // oneShotCtxValue is set on a context using WithValue(&oneShotValue) so
  37. // that Do() will not retry a request
  38. oneShotCtxValue interface{}
  39. )
  40. var DefaultRequestTimeout = 5 * time.Second
  41. var DefaultTransport CancelableTransport = &http.Transport{
  42. Proxy: http.ProxyFromEnvironment,
  43. Dial: (&net.Dialer{
  44. Timeout: 30 * time.Second,
  45. KeepAlive: 30 * time.Second,
  46. }).Dial,
  47. TLSHandshakeTimeout: 10 * time.Second,
  48. }
  49. type EndpointSelectionMode int
  50. const (
  51. // EndpointSelectionRandom is the default value of the 'SelectionMode'.
  52. // As the name implies, the client object will pick a node from the members
  53. // of the cluster in a random fashion. If the cluster has three members, A, B,
  54. // and C, the client picks any node from its three members as its request
  55. // destination.
  56. EndpointSelectionRandom EndpointSelectionMode = iota
  57. // If 'SelectionMode' is set to 'EndpointSelectionPrioritizeLeader',
  58. // requests are sent directly to the cluster leader. This reduces
  59. // forwarding roundtrips compared to making requests to etcd followers
  60. // who then forward them to the cluster leader. In the event of a leader
  61. // failure, however, clients configured this way cannot prioritize among
  62. // the remaining etcd followers. Therefore, when a client sets 'SelectionMode'
  63. // to 'EndpointSelectionPrioritizeLeader', it must use 'client.AutoSync()' to
  64. // maintain its knowledge of current cluster state.
  65. //
  66. // This mode should be used with Client.AutoSync().
  67. EndpointSelectionPrioritizeLeader
  68. )
  69. type Config struct {
  70. // Endpoints defines a set of URLs (schemes, hosts and ports only)
  71. // that can be used to communicate with a logical etcd cluster. For
  72. // example, a three-node cluster could be provided like so:
  73. //
  74. // Endpoints: []string{
  75. // "http://node1.example.com:2379",
  76. // "http://node2.example.com:2379",
  77. // "http://node3.example.com:2379",
  78. // }
  79. //
  80. // If multiple endpoints are provided, the Client will attempt to
  81. // use them all in the event that one or more of them are unusable.
  82. //
  83. // If Client.Sync is ever called, the Client may cache an alternate
  84. // set of endpoints to continue operation.
  85. Endpoints []string
  86. // Transport is used by the Client to drive HTTP requests. If not
  87. // provided, DefaultTransport will be used.
  88. Transport CancelableTransport
  89. // CheckRedirect specifies the policy for handling HTTP redirects.
  90. // If CheckRedirect is not nil, the Client calls it before
  91. // following an HTTP redirect. The sole argument is the number of
  92. // requests that have already been made. If CheckRedirect returns
  93. // an error, Client.Do will not make any further requests and return
  94. // the error back it to the caller.
  95. //
  96. // If CheckRedirect is nil, the Client uses its default policy,
  97. // which is to stop after 10 consecutive requests.
  98. CheckRedirect CheckRedirectFunc
  99. // Username specifies the user credential to add as an authorization header
  100. Username string
  101. // Password is the password for the specified user to add as an authorization header
  102. // to the request.
  103. Password string
  104. // HeaderTimeoutPerRequest specifies the time limit to wait for response
  105. // header in a single request made by the Client. The timeout includes
  106. // connection time, any redirects, and header wait time.
  107. //
  108. // For non-watch GET request, server returns the response body immediately.
  109. // For PUT/POST/DELETE request, server will attempt to commit request
  110. // before responding, which is expected to take `100ms + 2 * RTT`.
  111. // For watch request, server returns the header immediately to notify Client
  112. // watch start. But if server is behind some kind of proxy, the response
  113. // header may be cached at proxy, and Client cannot rely on this behavior.
  114. //
  115. // Especially, wait request will ignore this timeout.
  116. //
  117. // One API call may send multiple requests to different etcd servers until it
  118. // succeeds. Use context of the API to specify the overall timeout.
  119. //
  120. // A HeaderTimeoutPerRequest of zero means no timeout.
  121. HeaderTimeoutPerRequest time.Duration
  122. // SelectionMode is an EndpointSelectionMode enum that specifies the
  123. // policy for choosing the etcd cluster node to which requests are sent.
  124. SelectionMode EndpointSelectionMode
  125. }
  126. func (cfg *Config) transport() CancelableTransport {
  127. if cfg.Transport == nil {
  128. return DefaultTransport
  129. }
  130. return cfg.Transport
  131. }
  132. func (cfg *Config) checkRedirect() CheckRedirectFunc {
  133. if cfg.CheckRedirect == nil {
  134. return DefaultCheckRedirect
  135. }
  136. return cfg.CheckRedirect
  137. }
  138. // CancelableTransport mimics net/http.Transport, but requires that
  139. // the object also support request cancellation.
  140. type CancelableTransport interface {
  141. http.RoundTripper
  142. CancelRequest(req *http.Request)
  143. }
  144. type CheckRedirectFunc func(via int) error
  145. // DefaultCheckRedirect follows up to 10 redirects, but no more.
  146. var DefaultCheckRedirect CheckRedirectFunc = func(via int) error {
  147. if via > 10 {
  148. return ErrTooManyRedirects
  149. }
  150. return nil
  151. }
  152. type Client interface {
  153. // Sync updates the internal cache of the etcd cluster's membership.
  154. Sync(context.Context) error
  155. // AutoSync periodically calls Sync() every given interval.
  156. // The recommended sync interval is 10 seconds to 1 minute, which does
  157. // not bring too much overhead to server and makes client catch up the
  158. // cluster change in time.
  159. //
  160. // The example to use it:
  161. //
  162. // for {
  163. // err := client.AutoSync(ctx, 10*time.Second)
  164. // if err == context.DeadlineExceeded || err == context.Canceled {
  165. // break
  166. // }
  167. // log.Print(err)
  168. // }
  169. AutoSync(context.Context, time.Duration) error
  170. // Endpoints returns a copy of the current set of API endpoints used
  171. // by Client to resolve HTTP requests. If Sync has ever been called,
  172. // this may differ from the initial Endpoints provided in the Config.
  173. Endpoints() []string
  174. // SetEndpoints sets the set of API endpoints used by Client to resolve
  175. // HTTP requests. If the given endpoints are not valid, an error will be
  176. // returned
  177. SetEndpoints(eps []string) error
  178. httpClient
  179. }
  180. func New(cfg Config) (Client, error) {
  181. c := &httpClusterClient{
  182. clientFactory: newHTTPClientFactory(cfg.transport(), cfg.checkRedirect(), cfg.HeaderTimeoutPerRequest),
  183. rand: rand.New(rand.NewSource(int64(time.Now().Nanosecond()))),
  184. selectionMode: cfg.SelectionMode,
  185. }
  186. if cfg.Username != "" {
  187. c.credentials = &credentials{
  188. username: cfg.Username,
  189. password: cfg.Password,
  190. }
  191. }
  192. if err := c.SetEndpoints(cfg.Endpoints); err != nil {
  193. return nil, err
  194. }
  195. return c, nil
  196. }
  197. type httpClient interface {
  198. Do(context.Context, httpAction) (*http.Response, []byte, error)
  199. }
  200. func newHTTPClientFactory(tr CancelableTransport, cr CheckRedirectFunc, headerTimeout time.Duration) httpClientFactory {
  201. return func(ep url.URL) httpClient {
  202. return &redirectFollowingHTTPClient{
  203. checkRedirect: cr,
  204. client: &simpleHTTPClient{
  205. transport: tr,
  206. endpoint: ep,
  207. headerTimeout: headerTimeout,
  208. },
  209. }
  210. }
  211. }
  212. type credentials struct {
  213. username string
  214. password string
  215. }
  216. type httpClientFactory func(url.URL) httpClient
  217. type httpAction interface {
  218. HTTPRequest(url.URL) *http.Request
  219. }
  220. type httpClusterClient struct {
  221. clientFactory httpClientFactory
  222. endpoints []url.URL
  223. pinned int
  224. credentials *credentials
  225. sync.RWMutex
  226. rand *rand.Rand
  227. selectionMode EndpointSelectionMode
  228. }
  229. func (c *httpClusterClient) getLeaderEndpoint() (string, error) {
  230. mAPI := NewMembersAPI(c)
  231. leader, err := mAPI.Leader(context.Background())
  232. if err != nil {
  233. return "", err
  234. }
  235. return leader.ClientURLs[0], nil // TODO: how to handle multiple client URLs?
  236. }
  237. func (c *httpClusterClient) SetEndpoints(eps []string) error {
  238. if len(eps) == 0 {
  239. return ErrNoEndpoints
  240. }
  241. neps := make([]url.URL, len(eps))
  242. for i, ep := range eps {
  243. u, err := url.Parse(ep)
  244. if err != nil {
  245. return err
  246. }
  247. neps[i] = *u
  248. }
  249. switch c.selectionMode {
  250. case EndpointSelectionRandom:
  251. c.endpoints = shuffleEndpoints(c.rand, neps)
  252. c.pinned = 0
  253. case EndpointSelectionPrioritizeLeader:
  254. c.endpoints = neps
  255. lep, err := c.getLeaderEndpoint()
  256. if err != nil {
  257. return ErrNoLeaderEndpoint
  258. }
  259. for i := range c.endpoints {
  260. if c.endpoints[i].String() == lep {
  261. c.pinned = i
  262. break
  263. }
  264. }
  265. // If endpoints doesn't have the lu, just keep c.pinned = 0.
  266. // Forwarding between follower and leader would be required but it works.
  267. default:
  268. return errors.New(fmt.Sprintf("invalid endpoint selection mode: %d", c.selectionMode))
  269. }
  270. return nil
  271. }
  272. func (c *httpClusterClient) Do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
  273. action := act
  274. c.RLock()
  275. leps := len(c.endpoints)
  276. eps := make([]url.URL, leps)
  277. n := copy(eps, c.endpoints)
  278. pinned := c.pinned
  279. if c.credentials != nil {
  280. action = &authedAction{
  281. act: act,
  282. credentials: *c.credentials,
  283. }
  284. }
  285. c.RUnlock()
  286. if leps == 0 {
  287. return nil, nil, ErrNoEndpoints
  288. }
  289. if leps != n {
  290. return nil, nil, errors.New("unable to pick endpoint: copy failed")
  291. }
  292. var resp *http.Response
  293. var body []byte
  294. var err error
  295. cerr := &ClusterError{}
  296. isOneShot := ctx.Value(&oneShotCtxValue) != nil
  297. for i := pinned; i < leps+pinned; i++ {
  298. k := i % leps
  299. hc := c.clientFactory(eps[k])
  300. resp, body, err = hc.Do(ctx, action)
  301. if err != nil {
  302. cerr.Errors = append(cerr.Errors, err)
  303. if err == ctx.Err() {
  304. return nil, nil, ctx.Err()
  305. }
  306. if err == context.Canceled || err == context.DeadlineExceeded {
  307. return nil, nil, err
  308. }
  309. if isOneShot {
  310. return nil, nil, err
  311. }
  312. continue
  313. }
  314. if resp.StatusCode/100 == 5 {
  315. switch resp.StatusCode {
  316. case http.StatusInternalServerError, http.StatusServiceUnavailable:
  317. // TODO: make sure this is a no leader response
  318. cerr.Errors = append(cerr.Errors, fmt.Errorf("client: etcd member %s has no leader", eps[k].String()))
  319. default:
  320. cerr.Errors = append(cerr.Errors, fmt.Errorf("client: etcd member %s returns server error [%s]", eps[k].String(), http.StatusText(resp.StatusCode)))
  321. }
  322. if isOneShot {
  323. return nil, nil, cerr.Errors[0]
  324. }
  325. continue
  326. }
  327. if k != pinned {
  328. c.Lock()
  329. c.pinned = k
  330. c.Unlock()
  331. }
  332. return resp, body, nil
  333. }
  334. return nil, nil, cerr
  335. }
  336. func (c *httpClusterClient) Endpoints() []string {
  337. c.RLock()
  338. defer c.RUnlock()
  339. eps := make([]string, len(c.endpoints))
  340. for i, ep := range c.endpoints {
  341. eps[i] = ep.String()
  342. }
  343. return eps
  344. }
  345. func (c *httpClusterClient) Sync(ctx context.Context) error {
  346. mAPI := NewMembersAPI(c)
  347. ms, err := mAPI.List(ctx)
  348. if err != nil {
  349. return err
  350. }
  351. c.Lock()
  352. defer c.Unlock()
  353. eps := make([]string, 0)
  354. for _, m := range ms {
  355. eps = append(eps, m.ClientURLs...)
  356. }
  357. sort.Sort(sort.StringSlice(eps))
  358. ceps := make([]string, len(c.endpoints))
  359. for i, cep := range c.endpoints {
  360. ceps[i] = cep.String()
  361. }
  362. sort.Sort(sort.StringSlice(ceps))
  363. // fast path if no change happens
  364. // this helps client to pin the endpoint when no cluster change
  365. if reflect.DeepEqual(eps, ceps) {
  366. return nil
  367. }
  368. return c.SetEndpoints(eps)
  369. }
  370. func (c *httpClusterClient) AutoSync(ctx context.Context, interval time.Duration) error {
  371. ticker := time.NewTicker(interval)
  372. defer ticker.Stop()
  373. for {
  374. err := c.Sync(ctx)
  375. if err != nil {
  376. return err
  377. }
  378. select {
  379. case <-ctx.Done():
  380. return ctx.Err()
  381. case <-ticker.C:
  382. }
  383. }
  384. }
  385. type roundTripResponse struct {
  386. resp *http.Response
  387. err error
  388. }
  389. type simpleHTTPClient struct {
  390. transport CancelableTransport
  391. endpoint url.URL
  392. headerTimeout time.Duration
  393. }
  394. func (c *simpleHTTPClient) Do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
  395. req := act.HTTPRequest(c.endpoint)
  396. if err := printcURL(req); err != nil {
  397. return nil, nil, err
  398. }
  399. isWait := false
  400. if req != nil && req.URL != nil {
  401. ws := req.URL.Query().Get("wait")
  402. if len(ws) != 0 {
  403. var err error
  404. isWait, err = strconv.ParseBool(ws)
  405. if err != nil {
  406. return nil, nil, fmt.Errorf("wrong wait value %s (%v for %+v)", ws, err, req)
  407. }
  408. }
  409. }
  410. var hctx context.Context
  411. var hcancel context.CancelFunc
  412. if !isWait && c.headerTimeout > 0 {
  413. hctx, hcancel = context.WithTimeout(ctx, c.headerTimeout)
  414. } else {
  415. hctx, hcancel = context.WithCancel(ctx)
  416. }
  417. defer hcancel()
  418. reqcancel := requestCanceler(c.transport, req)
  419. rtchan := make(chan roundTripResponse, 1)
  420. go func() {
  421. resp, err := c.transport.RoundTrip(req)
  422. rtchan <- roundTripResponse{resp: resp, err: err}
  423. close(rtchan)
  424. }()
  425. var resp *http.Response
  426. var err error
  427. select {
  428. case rtresp := <-rtchan:
  429. resp, err = rtresp.resp, rtresp.err
  430. case <-hctx.Done():
  431. // cancel and wait for request to actually exit before continuing
  432. reqcancel()
  433. rtresp := <-rtchan
  434. resp = rtresp.resp
  435. switch {
  436. case ctx.Err() != nil:
  437. err = ctx.Err()
  438. case hctx.Err() != nil:
  439. err = fmt.Errorf("client: endpoint %s exceeded header timeout", c.endpoint.String())
  440. default:
  441. panic("failed to get error from context")
  442. }
  443. }
  444. // always check for resp nil-ness to deal with possible
  445. // race conditions between channels above
  446. defer func() {
  447. if resp != nil {
  448. resp.Body.Close()
  449. }
  450. }()
  451. if err != nil {
  452. return nil, nil, err
  453. }
  454. var body []byte
  455. done := make(chan struct{})
  456. go func() {
  457. body, err = ioutil.ReadAll(resp.Body)
  458. done <- struct{}{}
  459. }()
  460. select {
  461. case <-ctx.Done():
  462. resp.Body.Close()
  463. <-done
  464. return nil, nil, ctx.Err()
  465. case <-done:
  466. }
  467. return resp, body, err
  468. }
  469. type authedAction struct {
  470. act httpAction
  471. credentials credentials
  472. }
  473. func (a *authedAction) HTTPRequest(url url.URL) *http.Request {
  474. r := a.act.HTTPRequest(url)
  475. r.SetBasicAuth(a.credentials.username, a.credentials.password)
  476. return r
  477. }
  478. type redirectFollowingHTTPClient struct {
  479. client httpClient
  480. checkRedirect CheckRedirectFunc
  481. }
  482. func (r *redirectFollowingHTTPClient) Do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
  483. next := act
  484. for i := 0; i < 100; i++ {
  485. if i > 0 {
  486. if err := r.checkRedirect(i); err != nil {
  487. return nil, nil, err
  488. }
  489. }
  490. resp, body, err := r.client.Do(ctx, next)
  491. if err != nil {
  492. return nil, nil, err
  493. }
  494. if resp.StatusCode/100 == 3 {
  495. hdr := resp.Header.Get("Location")
  496. if hdr == "" {
  497. return nil, nil, fmt.Errorf("Location header not set")
  498. }
  499. loc, err := url.Parse(hdr)
  500. if err != nil {
  501. return nil, nil, fmt.Errorf("Location header not valid URL: %s", hdr)
  502. }
  503. next = &redirectedHTTPAction{
  504. action: act,
  505. location: *loc,
  506. }
  507. continue
  508. }
  509. return resp, body, nil
  510. }
  511. return nil, nil, errTooManyRedirectChecks
  512. }
  513. type redirectedHTTPAction struct {
  514. action httpAction
  515. location url.URL
  516. }
  517. func (r *redirectedHTTPAction) HTTPRequest(ep url.URL) *http.Request {
  518. orig := r.action.HTTPRequest(ep)
  519. orig.URL = &r.location
  520. return orig
  521. }
  522. func shuffleEndpoints(r *rand.Rand, eps []url.URL) []url.URL {
  523. p := r.Perm(len(eps))
  524. neps := make([]url.URL, len(eps))
  525. for i, k := range p {
  526. neps[i] = eps[k]
  527. }
  528. return neps
  529. }