client.go 14 KB

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