lease.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // Copyright 2016 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 clientv3
  15. import (
  16. "sync"
  17. "time"
  18. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "golang.org/x/net/context"
  21. "google.golang.org/grpc"
  22. )
  23. type (
  24. LeaseRevokeResponse pb.LeaseRevokeResponse
  25. LeaseID int64
  26. )
  27. // LeaseGrantResponse is used to convert the protobuf grant response.
  28. type LeaseGrantResponse struct {
  29. *pb.ResponseHeader
  30. ID LeaseID
  31. TTL int64
  32. Error string
  33. }
  34. // LeaseKeepAliveResponse is used to convert the protobuf keepalive response.
  35. type LeaseKeepAliveResponse struct {
  36. *pb.ResponseHeader
  37. ID LeaseID
  38. TTL int64
  39. }
  40. const (
  41. // a small buffer to store unsent lease responses.
  42. leaseResponseChSize = 16
  43. // NoLease is a lease ID for the absence of a lease.
  44. NoLease LeaseID = 0
  45. )
  46. type Lease interface {
  47. // Grant creates a new lease.
  48. Grant(ctx context.Context, ttl int64) (*LeaseGrantResponse, error)
  49. // Revoke revokes the given lease.
  50. Revoke(ctx context.Context, id LeaseID) (*LeaseRevokeResponse, error)
  51. // KeepAlive keeps the given lease alive forever.
  52. KeepAlive(ctx context.Context, id LeaseID) (<-chan *LeaseKeepAliveResponse, error)
  53. // KeepAliveOnce renews the lease once. In most of the cases, Keepalive
  54. // should be used instead of KeepAliveOnce.
  55. KeepAliveOnce(ctx context.Context, id LeaseID) (*LeaseKeepAliveResponse, error)
  56. // Close releases all resources Lease keeps for efficient communication
  57. // with the etcd server.
  58. Close() error
  59. }
  60. type lessor struct {
  61. c *Client
  62. mu sync.Mutex // guards all fields
  63. conn *grpc.ClientConn // conn in-use
  64. // donec is closed when recvKeepAliveLoop stops
  65. donec chan struct{}
  66. remote pb.LeaseClient
  67. stream pb.Lease_LeaseKeepAliveClient
  68. streamCancel context.CancelFunc
  69. stopCtx context.Context
  70. stopCancel context.CancelFunc
  71. keepAlives map[LeaseID]*keepAlive
  72. }
  73. // keepAlive multiplexes a keepalive for a lease over multiple channels
  74. type keepAlive struct {
  75. chs []chan<- *LeaseKeepAliveResponse
  76. ctxs []context.Context
  77. // deadline is the next time to send a keep alive message
  78. deadline time.Time
  79. // donec is closed on lease revoke, expiration, or cancel.
  80. donec chan struct{}
  81. }
  82. func NewLease(c *Client) Lease {
  83. l := &lessor{
  84. c: c,
  85. conn: c.ActiveConnection(),
  86. donec: make(chan struct{}),
  87. keepAlives: make(map[LeaseID]*keepAlive),
  88. }
  89. l.remote = pb.NewLeaseClient(l.conn)
  90. l.stopCtx, l.stopCancel = context.WithCancel(context.Background())
  91. go l.recvKeepAliveLoop()
  92. return l
  93. }
  94. func (l *lessor) Grant(ctx context.Context, ttl int64) (*LeaseGrantResponse, error) {
  95. cctx, cancel := context.WithCancel(ctx)
  96. done := cancelWhenStop(cancel, l.stopCtx.Done())
  97. defer close(done)
  98. for {
  99. r := &pb.LeaseGrantRequest{TTL: ttl}
  100. resp, err := l.getRemote().LeaseGrant(cctx, r)
  101. if err == nil {
  102. gresp := &LeaseGrantResponse{
  103. ResponseHeader: resp.GetHeader(),
  104. ID: LeaseID(resp.ID),
  105. TTL: resp.TTL,
  106. Error: resp.Error,
  107. }
  108. return gresp, nil
  109. }
  110. if isHaltErr(cctx, err) {
  111. return nil, rpctypes.Error(err)
  112. }
  113. if nerr := l.switchRemoteAndStream(err); nerr != nil {
  114. return nil, nerr
  115. }
  116. }
  117. }
  118. func (l *lessor) Revoke(ctx context.Context, id LeaseID) (*LeaseRevokeResponse, error) {
  119. cctx, cancel := context.WithCancel(ctx)
  120. done := cancelWhenStop(cancel, l.stopCtx.Done())
  121. defer close(done)
  122. for {
  123. r := &pb.LeaseRevokeRequest{ID: int64(id)}
  124. resp, err := l.getRemote().LeaseRevoke(cctx, r)
  125. if err == nil {
  126. return (*LeaseRevokeResponse)(resp), nil
  127. }
  128. if isHaltErr(ctx, err) {
  129. return nil, rpctypes.Error(err)
  130. }
  131. if nerr := l.switchRemoteAndStream(err); nerr != nil {
  132. return nil, nerr
  133. }
  134. }
  135. }
  136. func (l *lessor) KeepAlive(ctx context.Context, id LeaseID) (<-chan *LeaseKeepAliveResponse, error) {
  137. ch := make(chan *LeaseKeepAliveResponse, leaseResponseChSize)
  138. l.mu.Lock()
  139. ka, ok := l.keepAlives[id]
  140. if !ok {
  141. // create fresh keep alive
  142. ka = &keepAlive{
  143. chs: []chan<- *LeaseKeepAliveResponse{ch},
  144. ctxs: []context.Context{ctx},
  145. deadline: time.Now(),
  146. donec: make(chan struct{}),
  147. }
  148. l.keepAlives[id] = ka
  149. } else {
  150. // add channel and context to existing keep alive
  151. ka.ctxs = append(ka.ctxs, ctx)
  152. ka.chs = append(ka.chs, ch)
  153. }
  154. l.mu.Unlock()
  155. go l.keepAliveCtxCloser(id, ctx, ka.donec)
  156. return ch, nil
  157. }
  158. func (l *lessor) KeepAliveOnce(ctx context.Context, id LeaseID) (*LeaseKeepAliveResponse, error) {
  159. cctx, cancel := context.WithCancel(ctx)
  160. done := cancelWhenStop(cancel, l.stopCtx.Done())
  161. defer close(done)
  162. for {
  163. resp, err := l.keepAliveOnce(cctx, id)
  164. if err == nil {
  165. if resp.TTL == 0 {
  166. err = rpctypes.ErrLeaseNotFound
  167. }
  168. return resp, err
  169. }
  170. if isHaltErr(ctx, err) {
  171. return nil, rpctypes.Error(err)
  172. }
  173. nerr := l.switchRemoteAndStream(err)
  174. if nerr != nil {
  175. return nil, nerr
  176. }
  177. }
  178. }
  179. func (l *lessor) Close() error {
  180. l.stopCancel()
  181. <-l.donec
  182. return nil
  183. }
  184. func (l *lessor) keepAliveCtxCloser(id LeaseID, ctx context.Context, donec <-chan struct{}) {
  185. select {
  186. case <-donec:
  187. return
  188. case <-l.donec:
  189. return
  190. case <-ctx.Done():
  191. }
  192. l.mu.Lock()
  193. defer l.mu.Unlock()
  194. ka, ok := l.keepAlives[id]
  195. if !ok {
  196. return
  197. }
  198. // close channel and remove context if still associated with keep alive
  199. for i, c := range ka.ctxs {
  200. if c == ctx {
  201. close(ka.chs[i])
  202. ka.ctxs = append(ka.ctxs[:i], ka.ctxs[i+1:]...)
  203. ka.chs = append(ka.chs[:i], ka.chs[i+1:]...)
  204. break
  205. }
  206. }
  207. // remove if no one more listeners
  208. if len(ka.chs) == 0 {
  209. delete(l.keepAlives, id)
  210. }
  211. }
  212. func (l *lessor) keepAliveOnce(ctx context.Context, id LeaseID) (*LeaseKeepAliveResponse, error) {
  213. cctx, cancel := context.WithCancel(ctx)
  214. defer cancel()
  215. stream, err := l.getRemote().LeaseKeepAlive(cctx)
  216. if err != nil {
  217. return nil, rpctypes.Error(err)
  218. }
  219. err = stream.Send(&pb.LeaseKeepAliveRequest{ID: int64(id)})
  220. if err != nil {
  221. return nil, rpctypes.Error(err)
  222. }
  223. resp, rerr := stream.Recv()
  224. if rerr != nil {
  225. return nil, rpctypes.Error(rerr)
  226. }
  227. karesp := &LeaseKeepAliveResponse{
  228. ResponseHeader: resp.GetHeader(),
  229. ID: LeaseID(resp.ID),
  230. TTL: resp.TTL,
  231. }
  232. return karesp, nil
  233. }
  234. func (l *lessor) recvKeepAliveLoop() {
  235. defer func() {
  236. l.stopCancel()
  237. l.mu.Lock()
  238. close(l.donec)
  239. for _, ka := range l.keepAlives {
  240. ka.Close()
  241. }
  242. l.keepAlives = make(map[LeaseID]*keepAlive)
  243. l.mu.Unlock()
  244. }()
  245. stream, serr := l.resetRecv()
  246. for serr == nil {
  247. resp, err := stream.Recv()
  248. if err != nil {
  249. if isHaltErr(l.stopCtx, err) {
  250. return
  251. }
  252. stream, serr = l.resetRecv()
  253. continue
  254. }
  255. l.recvKeepAlive(resp)
  256. }
  257. }
  258. // resetRecv opens a new lease stream and starts sending LeaseKeepAliveRequests
  259. func (l *lessor) resetRecv() (pb.Lease_LeaseKeepAliveClient, error) {
  260. if err := l.switchRemoteAndStream(nil); err != nil {
  261. return nil, err
  262. }
  263. stream := l.getKeepAliveStream()
  264. go l.sendKeepAliveLoop(stream)
  265. return stream, nil
  266. }
  267. // recvKeepAlive updates a lease based on its LeaseKeepAliveResponse
  268. func (l *lessor) recvKeepAlive(resp *pb.LeaseKeepAliveResponse) {
  269. karesp := &LeaseKeepAliveResponse{
  270. ResponseHeader: resp.GetHeader(),
  271. ID: LeaseID(resp.ID),
  272. TTL: resp.TTL,
  273. }
  274. l.mu.Lock()
  275. defer l.mu.Unlock()
  276. ka, ok := l.keepAlives[karesp.ID]
  277. if !ok {
  278. return
  279. }
  280. if karesp.TTL <= 0 {
  281. // lease expired; close all keep alive channels
  282. delete(l.keepAlives, karesp.ID)
  283. ka.Close()
  284. return
  285. }
  286. // send update to all channels
  287. nextDeadline := time.Now().Add(1 + time.Duration(karesp.TTL/3)*time.Second)
  288. for _, ch := range ka.chs {
  289. select {
  290. case ch <- karesp:
  291. ka.deadline = nextDeadline
  292. default:
  293. }
  294. }
  295. }
  296. // sendKeepAliveLoop sends LeaseKeepAliveRequests for the lifetime of a lease stream
  297. func (l *lessor) sendKeepAliveLoop(stream pb.Lease_LeaseKeepAliveClient) {
  298. for {
  299. select {
  300. case <-time.After(500 * time.Millisecond):
  301. case <-stream.Context().Done():
  302. return
  303. case <-l.donec:
  304. return
  305. case <-l.stopCtx.Done():
  306. return
  307. }
  308. tosend := make([]LeaseID, 0)
  309. now := time.Now()
  310. l.mu.Lock()
  311. for id, ka := range l.keepAlives {
  312. if ka.deadline.Before(now) {
  313. tosend = append(tosend, id)
  314. }
  315. }
  316. l.mu.Unlock()
  317. for _, id := range tosend {
  318. r := &pb.LeaseKeepAliveRequest{ID: int64(id)}
  319. if err := stream.Send(r); err != nil {
  320. // TODO do something with this error?
  321. return
  322. }
  323. }
  324. }
  325. }
  326. func (l *lessor) getRemote() pb.LeaseClient {
  327. l.mu.Lock()
  328. defer l.mu.Unlock()
  329. return l.remote
  330. }
  331. func (l *lessor) getKeepAliveStream() pb.Lease_LeaseKeepAliveClient {
  332. l.mu.Lock()
  333. defer l.mu.Unlock()
  334. return l.stream
  335. }
  336. func (l *lessor) switchRemoteAndStream(prevErr error) error {
  337. for {
  338. l.mu.Lock()
  339. conn := l.conn
  340. l.mu.Unlock()
  341. var (
  342. err error
  343. newConn *grpc.ClientConn
  344. )
  345. if prevErr != nil {
  346. conn.Close()
  347. newConn, err = l.c.retryConnection(conn, prevErr)
  348. if err != nil {
  349. return rpctypes.Error(err)
  350. }
  351. }
  352. l.mu.Lock()
  353. if newConn != nil {
  354. l.conn = newConn
  355. }
  356. l.remote = pb.NewLeaseClient(l.conn)
  357. l.mu.Unlock()
  358. prevErr = l.newStream()
  359. if prevErr != nil {
  360. continue
  361. }
  362. return nil
  363. }
  364. }
  365. func (l *lessor) newStream() error {
  366. sctx, cancel := context.WithCancel(l.stopCtx)
  367. stream, err := l.getRemote().LeaseKeepAlive(sctx)
  368. if err != nil {
  369. cancel()
  370. return rpctypes.Error(err)
  371. }
  372. l.mu.Lock()
  373. defer l.mu.Unlock()
  374. if l.stream != nil && l.streamCancel != nil {
  375. l.stream.CloseSend()
  376. l.streamCancel()
  377. }
  378. l.streamCancel = cancel
  379. l.stream = stream
  380. return nil
  381. }
  382. func (ka *keepAlive) Close() {
  383. close(ka.donec)
  384. for _, ch := range ka.chs {
  385. close(ch)
  386. }
  387. }
  388. // cancelWhenStop calls cancel when the given stopc fires. It returns a done chan. done
  389. // should be closed when the work is finished. When done fires, cancelWhenStop will release
  390. // its internal resource.
  391. func cancelWhenStop(cancel context.CancelFunc, stopc <-chan struct{}) chan<- struct{} {
  392. done := make(chan struct{}, 1)
  393. go func() {
  394. select {
  395. case <-stopc:
  396. case <-done:
  397. }
  398. cancel()
  399. }()
  400. return done
  401. }