client.go 14 KB

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