lease.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. "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. // defaultTTL is the assumed lease TTL used for the first keepalive
  42. // deadline before the actual TTL is known to the client.
  43. defaultTTL = 5 * time.Second
  44. // a small buffer to store unsent lease responses.
  45. leaseResponseChSize = 16
  46. // NoLease is a lease ID for the absence of a lease.
  47. NoLease LeaseID = 0
  48. )
  49. type Lease interface {
  50. // Grant creates a new lease.
  51. Grant(ctx context.Context, ttl int64) (*LeaseGrantResponse, error)
  52. // Revoke revokes the given lease.
  53. Revoke(ctx context.Context, id LeaseID) (*LeaseRevokeResponse, error)
  54. // KeepAlive keeps the given lease alive forever.
  55. KeepAlive(ctx context.Context, id LeaseID) (<-chan *LeaseKeepAliveResponse, error)
  56. // KeepAliveOnce renews the lease once. In most of the cases, Keepalive
  57. // should be used instead of KeepAliveOnce.
  58. KeepAliveOnce(ctx context.Context, id LeaseID) (*LeaseKeepAliveResponse, error)
  59. // Close releases all resources Lease keeps for efficient communication
  60. // with the etcd server.
  61. Close() error
  62. }
  63. type lessor struct {
  64. mu sync.Mutex // guards all fields
  65. // donec is closed when recvKeepAliveLoop stops
  66. donec chan struct{}
  67. remote pb.LeaseClient
  68. stream pb.Lease_LeaseKeepAliveClient
  69. streamCancel context.CancelFunc
  70. stopCtx context.Context
  71. stopCancel context.CancelFunc
  72. keepAlives map[LeaseID]*keepAlive
  73. // firstKeepAliveTimeout is the timeout for the first keepalive request
  74. // before the actual TTL is known to the lease client
  75. firstKeepAliveTimeout time.Duration
  76. }
  77. // keepAlive multiplexes a keepalive for a lease over multiple channels
  78. type keepAlive struct {
  79. chs []chan<- *LeaseKeepAliveResponse
  80. ctxs []context.Context
  81. // deadline is the time the keep alive channels close if no response
  82. deadline time.Time
  83. // nextKeepAlive is when to send the next keep alive message
  84. nextKeepAlive time.Time
  85. // donec is closed on lease revoke, expiration, or cancel.
  86. donec chan struct{}
  87. }
  88. func NewLease(c *Client) Lease {
  89. l := &lessor{
  90. donec: make(chan struct{}),
  91. keepAlives: make(map[LeaseID]*keepAlive),
  92. remote: RetryLeaseClient(c),
  93. firstKeepAliveTimeout: c.cfg.DialTimeout + time.Second,
  94. }
  95. if l.firstKeepAliveTimeout == time.Second {
  96. l.firstKeepAliveTimeout = defaultTTL
  97. }
  98. l.stopCtx, l.stopCancel = context.WithCancel(context.Background())
  99. go l.recvKeepAliveLoop()
  100. go l.deadlineLoop()
  101. return l
  102. }
  103. func (l *lessor) Grant(ctx context.Context, ttl int64) (*LeaseGrantResponse, error) {
  104. cctx, cancel := context.WithCancel(ctx)
  105. done := cancelWhenStop(cancel, l.stopCtx.Done())
  106. defer close(done)
  107. for {
  108. r := &pb.LeaseGrantRequest{TTL: ttl}
  109. resp, err := l.remote.LeaseGrant(cctx, r)
  110. if err == nil {
  111. gresp := &LeaseGrantResponse{
  112. ResponseHeader: resp.GetHeader(),
  113. ID: LeaseID(resp.ID),
  114. TTL: resp.TTL,
  115. Error: resp.Error,
  116. }
  117. return gresp, nil
  118. }
  119. if isHaltErr(cctx, err) {
  120. return nil, toErr(ctx, err)
  121. }
  122. }
  123. }
  124. func (l *lessor) Revoke(ctx context.Context, id LeaseID) (*LeaseRevokeResponse, error) {
  125. cctx, cancel := context.WithCancel(ctx)
  126. done := cancelWhenStop(cancel, l.stopCtx.Done())
  127. defer close(done)
  128. for {
  129. r := &pb.LeaseRevokeRequest{ID: int64(id)}
  130. resp, err := l.remote.LeaseRevoke(cctx, r)
  131. if err == nil {
  132. return (*LeaseRevokeResponse)(resp), nil
  133. }
  134. if isHaltErr(ctx, err) {
  135. return nil, toErr(ctx, err)
  136. }
  137. }
  138. }
  139. func (l *lessor) KeepAlive(ctx context.Context, id LeaseID) (<-chan *LeaseKeepAliveResponse, error) {
  140. ch := make(chan *LeaseKeepAliveResponse, leaseResponseChSize)
  141. l.mu.Lock()
  142. ka, ok := l.keepAlives[id]
  143. if !ok {
  144. // create fresh keep alive
  145. ka = &keepAlive{
  146. chs: []chan<- *LeaseKeepAliveResponse{ch},
  147. ctxs: []context.Context{ctx},
  148. deadline: time.Now().Add(l.firstKeepAliveTimeout),
  149. nextKeepAlive: time.Now(),
  150. donec: make(chan struct{}),
  151. }
  152. l.keepAlives[id] = ka
  153. } else {
  154. // add channel and context to existing keep alive
  155. ka.ctxs = append(ka.ctxs, ctx)
  156. ka.chs = append(ka.chs, ch)
  157. }
  158. l.mu.Unlock()
  159. go l.keepAliveCtxCloser(id, ctx, ka.donec)
  160. return ch, nil
  161. }
  162. func (l *lessor) KeepAliveOnce(ctx context.Context, id LeaseID) (*LeaseKeepAliveResponse, error) {
  163. cctx, cancel := context.WithCancel(ctx)
  164. done := cancelWhenStop(cancel, l.stopCtx.Done())
  165. defer close(done)
  166. for {
  167. resp, err := l.keepAliveOnce(cctx, id)
  168. if err == nil {
  169. if resp.TTL == 0 {
  170. err = rpctypes.ErrLeaseNotFound
  171. }
  172. return resp, err
  173. }
  174. if isHaltErr(ctx, err) {
  175. return nil, toErr(ctx, err)
  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.remote.LeaseKeepAlive(cctx, grpc.FailFast(false))
  216. if err != nil {
  217. return nil, toErr(ctx, err)
  218. }
  219. err = stream.Send(&pb.LeaseKeepAliveRequest{ID: int64(id)})
  220. if err != nil {
  221. return nil, toErr(ctx, err)
  222. }
  223. resp, rerr := stream.Recv()
  224. if rerr != nil {
  225. return nil, toErr(ctx, 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.mu.Lock()
  237. close(l.donec)
  238. for _, ka := range l.keepAlives {
  239. ka.Close()
  240. }
  241. l.keepAlives = make(map[LeaseID]*keepAlive)
  242. l.mu.Unlock()
  243. }()
  244. stream, serr := l.resetRecv()
  245. for serr == nil {
  246. resp, err := stream.Recv()
  247. if err != nil {
  248. if isHaltErr(l.stopCtx, err) {
  249. return
  250. }
  251. stream, serr = l.resetRecv()
  252. continue
  253. }
  254. l.recvKeepAlive(resp)
  255. }
  256. }
  257. // resetRecv opens a new lease stream and starts sending LeaseKeepAliveRequests
  258. func (l *lessor) resetRecv() (pb.Lease_LeaseKeepAliveClient, error) {
  259. sctx, cancel := context.WithCancel(l.stopCtx)
  260. stream, err := l.remote.LeaseKeepAlive(sctx, grpc.FailFast(false))
  261. if err = toErr(sctx, err); err != nil {
  262. cancel()
  263. return nil, err
  264. }
  265. l.mu.Lock()
  266. defer l.mu.Unlock()
  267. if l.stream != nil && l.streamCancel != nil {
  268. l.stream.CloseSend()
  269. l.streamCancel()
  270. }
  271. l.streamCancel = cancel
  272. l.stream = stream
  273. go l.sendKeepAliveLoop(stream)
  274. return stream, nil
  275. }
  276. // recvKeepAlive updates a lease based on its LeaseKeepAliveResponse
  277. func (l *lessor) recvKeepAlive(resp *pb.LeaseKeepAliveResponse) {
  278. karesp := &LeaseKeepAliveResponse{
  279. ResponseHeader: resp.GetHeader(),
  280. ID: LeaseID(resp.ID),
  281. TTL: resp.TTL,
  282. }
  283. l.mu.Lock()
  284. defer l.mu.Unlock()
  285. ka, ok := l.keepAlives[karesp.ID]
  286. if !ok {
  287. return
  288. }
  289. if karesp.TTL <= 0 {
  290. // lease expired; close all keep alive channels
  291. delete(l.keepAlives, karesp.ID)
  292. ka.Close()
  293. return
  294. }
  295. // send update to all channels
  296. nextKeepAlive := time.Now().Add(1 + time.Duration(karesp.TTL/3)*time.Second)
  297. ka.deadline = time.Now().Add(time.Duration(karesp.TTL) * time.Second)
  298. for _, ch := range ka.chs {
  299. select {
  300. case ch <- karesp:
  301. ka.nextKeepAlive = nextKeepAlive
  302. default:
  303. }
  304. }
  305. }
  306. // deadlineLoop reaps any keep alive channels that have not received a response
  307. // within the lease TTL
  308. func (l *lessor) deadlineLoop() {
  309. for {
  310. select {
  311. case <-time.After(time.Second):
  312. case <-l.donec:
  313. return
  314. }
  315. now := time.Now()
  316. l.mu.Lock()
  317. for id, ka := range l.keepAlives {
  318. if ka.deadline.Before(now) {
  319. // waited too long for response; lease may be expired
  320. ka.Close()
  321. delete(l.keepAlives, id)
  322. }
  323. }
  324. l.mu.Unlock()
  325. }
  326. }
  327. // sendKeepAliveLoop sends LeaseKeepAliveRequests for the lifetime of a lease stream
  328. func (l *lessor) sendKeepAliveLoop(stream pb.Lease_LeaseKeepAliveClient) {
  329. for {
  330. select {
  331. case <-time.After(500 * time.Millisecond):
  332. case <-stream.Context().Done():
  333. return
  334. case <-l.donec:
  335. return
  336. case <-l.stopCtx.Done():
  337. return
  338. }
  339. tosend := make([]LeaseID, 0)
  340. now := time.Now()
  341. l.mu.Lock()
  342. for id, ka := range l.keepAlives {
  343. if ka.nextKeepAlive.Before(now) {
  344. tosend = append(tosend, id)
  345. }
  346. }
  347. l.mu.Unlock()
  348. for _, id := range tosend {
  349. r := &pb.LeaseKeepAliveRequest{ID: int64(id)}
  350. if err := stream.Send(r); err != nil {
  351. // TODO do something with this error?
  352. return
  353. }
  354. }
  355. }
  356. }
  357. func (ka *keepAlive) Close() {
  358. close(ka.donec)
  359. for _, ch := range ka.chs {
  360. close(ch)
  361. }
  362. }
  363. // cancelWhenStop calls cancel when the given stopc fires. It returns a done chan. done
  364. // should be closed when the work is finished. When done fires, cancelWhenStop will release
  365. // its internal resource.
  366. func cancelWhenStop(cancel context.CancelFunc, stopc <-chan struct{}) chan<- struct{} {
  367. done := make(chan struct{}, 1)
  368. go func() {
  369. select {
  370. case <-stopc:
  371. case <-done:
  372. }
  373. cancel()
  374. }()
  375. return done
  376. }