lease.go 9.5 KB

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