client.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // Copyright 2016 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 clientv3
  15. import (
  16. "crypto/tls"
  17. "errors"
  18. "fmt"
  19. "net"
  20. "net/url"
  21. "strconv"
  22. "strings"
  23. "sync"
  24. "time"
  25. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  26. "golang.org/x/net/context"
  27. "google.golang.org/grpc"
  28. "google.golang.org/grpc/codes"
  29. "google.golang.org/grpc/credentials"
  30. "google.golang.org/grpc/keepalive"
  31. "google.golang.org/grpc/metadata"
  32. )
  33. var (
  34. ErrNoAvailableEndpoints = errors.New("etcdclient: no available endpoints")
  35. ErrOldCluster = errors.New("etcdclient: old cluster version")
  36. )
  37. // Client provides and manages an etcd v3 client session.
  38. type Client struct {
  39. Cluster
  40. KV
  41. Lease
  42. Watcher
  43. Auth
  44. Maintenance
  45. conn *grpc.ClientConn
  46. dialerrc chan error
  47. cfg Config
  48. creds *credentials.TransportCredentials
  49. balancer *simpleBalancer
  50. retryWrapper retryRpcFunc
  51. retryAuthWrapper retryRpcFunc
  52. ctx context.Context
  53. cancel context.CancelFunc
  54. // Username is a username for authentication
  55. Username string
  56. // Password is a password for authentication
  57. Password string
  58. // tokenCred is an instance of WithPerRPCCredentials()'s argument
  59. tokenCred *authTokenCredential
  60. }
  61. // New creates a new etcdv3 client from a given configuration.
  62. func New(cfg Config) (*Client, error) {
  63. if len(cfg.Endpoints) == 0 {
  64. return nil, ErrNoAvailableEndpoints
  65. }
  66. return newClient(&cfg)
  67. }
  68. // NewCtxClient creates a client with a context but no underlying grpc
  69. // connection. This is useful for embedded cases that override the
  70. // service interface implementations and do not need connection management.
  71. func NewCtxClient(ctx context.Context) *Client {
  72. cctx, cancel := context.WithCancel(ctx)
  73. return &Client{ctx: cctx, cancel: cancel}
  74. }
  75. // NewFromURL creates a new etcdv3 client from a URL.
  76. func NewFromURL(url string) (*Client, error) {
  77. return New(Config{Endpoints: []string{url}})
  78. }
  79. // Close shuts down the client's etcd connections.
  80. func (c *Client) Close() error {
  81. c.cancel()
  82. c.Watcher.Close()
  83. c.Lease.Close()
  84. if c.conn != nil {
  85. return toErr(c.ctx, c.conn.Close())
  86. }
  87. return c.ctx.Err()
  88. }
  89. // Ctx is a context for "out of band" messages (e.g., for sending
  90. // "clean up" message when another context is canceled). It is
  91. // canceled on client Close().
  92. func (c *Client) Ctx() context.Context { return c.ctx }
  93. // Endpoints lists the registered endpoints for the client.
  94. func (c *Client) Endpoints() (eps []string) {
  95. // copy the slice; protect original endpoints from being changed
  96. eps = make([]string, len(c.cfg.Endpoints))
  97. copy(eps, c.cfg.Endpoints)
  98. return
  99. }
  100. // SetEndpoints updates client's endpoints.
  101. func (c *Client) SetEndpoints(eps ...string) {
  102. c.cfg.Endpoints = eps
  103. c.balancer.updateAddrs(eps)
  104. }
  105. // Sync synchronizes client's endpoints with the known endpoints from the etcd membership.
  106. func (c *Client) Sync(ctx context.Context) error {
  107. mresp, err := c.MemberList(ctx)
  108. if err != nil {
  109. return err
  110. }
  111. var eps []string
  112. for _, m := range mresp.Members {
  113. eps = append(eps, m.ClientURLs...)
  114. }
  115. c.SetEndpoints(eps...)
  116. return nil
  117. }
  118. func (c *Client) autoSync() {
  119. if c.cfg.AutoSyncInterval == time.Duration(0) {
  120. return
  121. }
  122. for {
  123. select {
  124. case <-c.ctx.Done():
  125. return
  126. case <-time.After(c.cfg.AutoSyncInterval):
  127. ctx, _ := context.WithTimeout(c.ctx, 5*time.Second)
  128. if err := c.Sync(ctx); err != nil && err != c.ctx.Err() {
  129. logger.Println("Auto sync endpoints failed:", err)
  130. }
  131. }
  132. }
  133. }
  134. type authTokenCredential struct {
  135. token string
  136. tokenMu *sync.RWMutex
  137. }
  138. func (cred authTokenCredential) RequireTransportSecurity() bool {
  139. return false
  140. }
  141. func (cred authTokenCredential) GetRequestMetadata(ctx context.Context, s ...string) (map[string]string, error) {
  142. cred.tokenMu.RLock()
  143. defer cred.tokenMu.RUnlock()
  144. return map[string]string{
  145. "token": cred.token,
  146. }, nil
  147. }
  148. func parseEndpoint(endpoint string) (proto string, host string, scheme string) {
  149. proto = "tcp"
  150. host = endpoint
  151. url, uerr := url.Parse(endpoint)
  152. if uerr != nil || !strings.Contains(endpoint, "://") {
  153. return
  154. }
  155. scheme = url.Scheme
  156. // strip scheme:// prefix since grpc dials by host
  157. host = url.Host
  158. switch url.Scheme {
  159. case "http", "https":
  160. case "unix", "unixs":
  161. proto = "unix"
  162. host = url.Host + url.Path
  163. default:
  164. proto, host = "", ""
  165. }
  166. return
  167. }
  168. func (c *Client) processCreds(scheme string) (creds *credentials.TransportCredentials) {
  169. creds = c.creds
  170. switch scheme {
  171. case "unix":
  172. case "http":
  173. creds = nil
  174. case "https", "unixs":
  175. if creds != nil {
  176. break
  177. }
  178. tlsconfig := &tls.Config{}
  179. emptyCreds := credentials.NewTLS(tlsconfig)
  180. creds = &emptyCreds
  181. default:
  182. creds = nil
  183. }
  184. return
  185. }
  186. // dialSetupOpts gives the dial opts prior to any authentication
  187. func (c *Client) dialSetupOpts(endpoint string, dopts ...grpc.DialOption) (opts []grpc.DialOption) {
  188. if c.cfg.DialTimeout > 0 {
  189. opts = []grpc.DialOption{grpc.WithTimeout(c.cfg.DialTimeout)}
  190. }
  191. if c.cfg.DialKeepAliveTime > 0 {
  192. params := keepalive.ClientParameters{
  193. Time: c.cfg.DialKeepAliveTime,
  194. }
  195. // Only relevant when KeepAliveTime is non-zero
  196. if c.cfg.DialKeepAliveTimeout > 0 {
  197. params.Timeout = c.cfg.DialKeepAliveTimeout
  198. }
  199. opts = append(opts, grpc.WithKeepaliveParams(params))
  200. }
  201. opts = append(opts, dopts...)
  202. f := func(host string, t time.Duration) (net.Conn, error) {
  203. proto, host, _ := parseEndpoint(c.balancer.getEndpoint(host))
  204. if host == "" && endpoint != "" {
  205. // dialing an endpoint not in the balancer; use
  206. // endpoint passed into dial
  207. proto, host, _ = parseEndpoint(endpoint)
  208. }
  209. if proto == "" {
  210. return nil, fmt.Errorf("unknown scheme for %q", host)
  211. }
  212. select {
  213. case <-c.ctx.Done():
  214. return nil, c.ctx.Err()
  215. default:
  216. }
  217. dialer := &net.Dialer{Timeout: t}
  218. conn, err := dialer.DialContext(c.ctx, proto, host)
  219. if err != nil {
  220. select {
  221. case c.dialerrc <- err:
  222. default:
  223. }
  224. }
  225. return conn, err
  226. }
  227. opts = append(opts, grpc.WithDialer(f))
  228. creds := c.creds
  229. if _, _, scheme := parseEndpoint(endpoint); len(scheme) != 0 {
  230. creds = c.processCreds(scheme)
  231. }
  232. if creds != nil {
  233. opts = append(opts, grpc.WithTransportCredentials(*creds))
  234. } else {
  235. opts = append(opts, grpc.WithInsecure())
  236. }
  237. return opts
  238. }
  239. // Dial connects to a single endpoint using the client's config.
  240. func (c *Client) Dial(endpoint string) (*grpc.ClientConn, error) {
  241. return c.dial(endpoint)
  242. }
  243. func (c *Client) getToken(ctx context.Context) error {
  244. var err error // return last error in a case of fail
  245. var auth *authenticator
  246. for i := 0; i < len(c.cfg.Endpoints); i++ {
  247. endpoint := c.cfg.Endpoints[i]
  248. host := getHost(endpoint)
  249. // use dial options without dopts to avoid reusing the client balancer
  250. auth, err = newAuthenticator(host, c.dialSetupOpts(endpoint))
  251. if err != nil {
  252. continue
  253. }
  254. defer auth.close()
  255. var resp *AuthenticateResponse
  256. resp, err = auth.authenticate(ctx, c.Username, c.Password)
  257. if err != nil {
  258. continue
  259. }
  260. c.tokenCred.tokenMu.Lock()
  261. c.tokenCred.token = resp.Token
  262. c.tokenCred.tokenMu.Unlock()
  263. return nil
  264. }
  265. return err
  266. }
  267. func (c *Client) dial(endpoint string, dopts ...grpc.DialOption) (*grpc.ClientConn, error) {
  268. opts := c.dialSetupOpts(endpoint, dopts...)
  269. host := getHost(endpoint)
  270. if c.Username != "" && c.Password != "" {
  271. c.tokenCred = &authTokenCredential{
  272. tokenMu: &sync.RWMutex{},
  273. }
  274. ctx := c.ctx
  275. if c.cfg.DialTimeout > 0 {
  276. cctx, cancel := context.WithTimeout(ctx, c.cfg.DialTimeout)
  277. defer cancel()
  278. ctx = cctx
  279. }
  280. err := c.getToken(ctx)
  281. if err != nil {
  282. if toErr(ctx, err) != rpctypes.ErrAuthNotEnabled {
  283. if err == ctx.Err() && ctx.Err() != c.ctx.Err() {
  284. err = grpc.ErrClientConnTimeout
  285. }
  286. return nil, err
  287. }
  288. } else {
  289. opts = append(opts, grpc.WithPerRPCCredentials(c.tokenCred))
  290. }
  291. }
  292. opts = append(opts, c.cfg.DialOptions...)
  293. conn, err := grpc.DialContext(c.ctx, host, opts...)
  294. if err != nil {
  295. return nil, err
  296. }
  297. return conn, nil
  298. }
  299. // WithRequireLeader requires client requests to only succeed
  300. // when the cluster has a leader.
  301. func WithRequireLeader(ctx context.Context) context.Context {
  302. md := metadata.Pairs(rpctypes.MetadataRequireLeaderKey, rpctypes.MetadataHasLeader)
  303. return metadata.NewOutgoingContext(ctx, md)
  304. }
  305. func newClient(cfg *Config) (*Client, error) {
  306. if cfg == nil {
  307. cfg = &Config{}
  308. }
  309. var creds *credentials.TransportCredentials
  310. if cfg.TLS != nil {
  311. c := credentials.NewTLS(cfg.TLS)
  312. creds = &c
  313. }
  314. // use a temporary skeleton client to bootstrap first connection
  315. baseCtx := context.TODO()
  316. if cfg.Context != nil {
  317. baseCtx = cfg.Context
  318. }
  319. ctx, cancel := context.WithCancel(baseCtx)
  320. client := &Client{
  321. conn: nil,
  322. dialerrc: make(chan error, 1),
  323. cfg: *cfg,
  324. creds: creds,
  325. ctx: ctx,
  326. cancel: cancel,
  327. }
  328. if cfg.Username != "" && cfg.Password != "" {
  329. client.Username = cfg.Username
  330. client.Password = cfg.Password
  331. }
  332. client.balancer = newSimpleBalancer(cfg.Endpoints)
  333. // use Endpoints[0] so that for https:// without any tls config given, then
  334. // grpc will assume the ServerName is in the endpoint.
  335. conn, err := client.dial(cfg.Endpoints[0], grpc.WithBalancer(client.balancer))
  336. if err != nil {
  337. client.cancel()
  338. client.balancer.Close()
  339. return nil, err
  340. }
  341. client.conn = conn
  342. client.retryWrapper = client.newRetryWrapper()
  343. client.retryAuthWrapper = client.newAuthRetryWrapper()
  344. // wait for a connection
  345. if cfg.DialTimeout > 0 {
  346. hasConn := false
  347. waitc := time.After(cfg.DialTimeout)
  348. select {
  349. case <-client.balancer.readyc:
  350. hasConn = true
  351. case <-ctx.Done():
  352. case <-waitc:
  353. }
  354. if !hasConn {
  355. err := grpc.ErrClientConnTimeout
  356. select {
  357. case err = <-client.dialerrc:
  358. default:
  359. }
  360. client.cancel()
  361. client.balancer.Close()
  362. conn.Close()
  363. return nil, err
  364. }
  365. }
  366. client.Cluster = NewCluster(client)
  367. client.KV = NewKV(client)
  368. client.Lease = NewLease(client)
  369. client.Watcher = NewWatcher(client)
  370. client.Auth = NewAuth(client)
  371. client.Maintenance = NewMaintenance(client)
  372. if cfg.RejectOldCluster {
  373. if err := client.checkVersion(); err != nil {
  374. client.Close()
  375. return nil, err
  376. }
  377. }
  378. go client.autoSync()
  379. return client, nil
  380. }
  381. func (c *Client) checkVersion() (err error) {
  382. var wg sync.WaitGroup
  383. errc := make(chan error, len(c.cfg.Endpoints))
  384. ctx, cancel := context.WithCancel(c.ctx)
  385. if c.cfg.DialTimeout > 0 {
  386. ctx, _ = context.WithTimeout(ctx, c.cfg.DialTimeout)
  387. }
  388. wg.Add(len(c.cfg.Endpoints))
  389. for _, ep := range c.cfg.Endpoints {
  390. // if cluster is current, any endpoint gives a recent version
  391. go func(e string) {
  392. defer wg.Done()
  393. resp, rerr := c.Status(ctx, e)
  394. if rerr != nil {
  395. errc <- rerr
  396. return
  397. }
  398. vs := strings.Split(resp.Version, ".")
  399. maj, min := 0, 0
  400. if len(vs) >= 2 {
  401. maj, rerr = strconv.Atoi(vs[0])
  402. min, rerr = strconv.Atoi(vs[1])
  403. }
  404. if maj < 3 || (maj == 3 && min < 2) {
  405. rerr = ErrOldCluster
  406. }
  407. errc <- rerr
  408. }(ep)
  409. }
  410. // wait for success
  411. for i := 0; i < len(c.cfg.Endpoints); i++ {
  412. if err = <-errc; err == nil {
  413. break
  414. }
  415. }
  416. cancel()
  417. wg.Wait()
  418. return err
  419. }
  420. // ActiveConnection returns the current in-use connection
  421. func (c *Client) ActiveConnection() *grpc.ClientConn { return c.conn }
  422. // isHaltErr returns true if the given error and context indicate no forward
  423. // progress can be made, even after reconnecting.
  424. func isHaltErr(ctx context.Context, err error) bool {
  425. if ctx != nil && ctx.Err() != nil {
  426. return true
  427. }
  428. if err == nil {
  429. return false
  430. }
  431. code := grpc.Code(err)
  432. // Unavailable codes mean the system will be right back.
  433. // (e.g., can't connect, lost leader)
  434. // Treat Internal codes as if something failed, leaving the
  435. // system in an inconsistent state, but retrying could make progress.
  436. // (e.g., failed in middle of send, corrupted frame)
  437. // TODO: are permanent Internal errors possible from grpc?
  438. return code != codes.Unavailable && code != codes.Internal
  439. }
  440. func toErr(ctx context.Context, err error) error {
  441. if err == nil {
  442. return nil
  443. }
  444. err = rpctypes.Error(err)
  445. if _, ok := err.(rpctypes.EtcdError); ok {
  446. return err
  447. }
  448. code := grpc.Code(err)
  449. switch code {
  450. case codes.DeadlineExceeded:
  451. fallthrough
  452. case codes.Canceled:
  453. if ctx.Err() != nil {
  454. err = ctx.Err()
  455. }
  456. case codes.Unavailable:
  457. err = ErrNoAvailableEndpoints
  458. case codes.FailedPrecondition:
  459. err = grpc.ErrClientConnClosing
  460. }
  461. return err
  462. }
  463. func canceledByCaller(stopCtx context.Context, err error) bool {
  464. if stopCtx.Err() == nil || err == nil {
  465. return false
  466. }
  467. return err == context.Canceled || err == context.DeadlineExceeded
  468. }