client.go 16 KB

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