client.go 14 KB

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