client.go 17 KB

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