lease.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 v3rpc
  15. import (
  16. "context"
  17. "io"
  18. "github.com/coreos/etcd/etcdserver"
  19. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/etcd/lease"
  22. )
  23. type LeaseServer struct {
  24. hdr header
  25. le etcdserver.Lessor
  26. }
  27. func NewLeaseServer(s *etcdserver.EtcdServer) pb.LeaseServer {
  28. return &LeaseServer{le: s, hdr: newHeader(s)}
  29. }
  30. func (ls *LeaseServer) LeaseGrant(ctx context.Context, cr *pb.LeaseGrantRequest) (*pb.LeaseGrantResponse, error) {
  31. resp, err := ls.le.LeaseGrant(ctx, cr)
  32. if err != nil {
  33. return nil, togRPCError(err)
  34. }
  35. ls.hdr.fill(resp.Header)
  36. return resp, nil
  37. }
  38. func (ls *LeaseServer) LeaseRevoke(ctx context.Context, rr *pb.LeaseRevokeRequest) (*pb.LeaseRevokeResponse, error) {
  39. resp, err := ls.le.LeaseRevoke(ctx, rr)
  40. if err != nil {
  41. return nil, togRPCError(err)
  42. }
  43. ls.hdr.fill(resp.Header)
  44. return resp, nil
  45. }
  46. func (ls *LeaseServer) LeaseTimeToLive(ctx context.Context, rr *pb.LeaseTimeToLiveRequest) (*pb.LeaseTimeToLiveResponse, error) {
  47. resp, err := ls.le.LeaseTimeToLive(ctx, rr)
  48. if err != nil && err != lease.ErrLeaseNotFound {
  49. return nil, togRPCError(err)
  50. }
  51. if err == lease.ErrLeaseNotFound {
  52. resp = &pb.LeaseTimeToLiveResponse{
  53. Header: &pb.ResponseHeader{},
  54. ID: rr.ID,
  55. TTL: -1,
  56. }
  57. }
  58. ls.hdr.fill(resp.Header)
  59. return resp, nil
  60. }
  61. func (ls *LeaseServer) LeaseLeases(ctx context.Context, rr *pb.LeaseLeasesRequest) (*pb.LeaseLeasesResponse, error) {
  62. resp, err := ls.le.LeaseLeases(ctx, rr)
  63. if err != nil && err != lease.ErrLeaseNotFound {
  64. return nil, togRPCError(err)
  65. }
  66. if err == lease.ErrLeaseNotFound {
  67. resp = &pb.LeaseLeasesResponse{
  68. Header: &pb.ResponseHeader{},
  69. Leases: []*pb.LeaseStatus{},
  70. }
  71. }
  72. ls.hdr.fill(resp.Header)
  73. return resp, nil
  74. }
  75. func (ls *LeaseServer) LeaseKeepAlive(stream pb.Lease_LeaseKeepAliveServer) (err error) {
  76. errc := make(chan error, 1)
  77. go func() {
  78. errc <- ls.leaseKeepAlive(stream)
  79. }()
  80. select {
  81. case err = <-errc:
  82. case <-stream.Context().Done():
  83. // the only server-side cancellation is noleader for now.
  84. err = stream.Context().Err()
  85. if err == context.Canceled {
  86. err = rpctypes.ErrGRPCNoLeader
  87. }
  88. }
  89. return err
  90. }
  91. func (ls *LeaseServer) leaseKeepAlive(stream pb.Lease_LeaseKeepAliveServer) error {
  92. for {
  93. req, err := stream.Recv()
  94. if err == io.EOF {
  95. return nil
  96. }
  97. if err != nil {
  98. if isClientCtxErr(stream.Context().Err(), err) {
  99. plog.Debugf("failed to receive lease keepalive request from gRPC stream (%q)", err.Error())
  100. } else {
  101. plog.Warningf("failed to receive lease keepalive request from gRPC stream (%q)", err.Error())
  102. }
  103. return err
  104. }
  105. // Create header before we sent out the renew request.
  106. // This can make sure that the revision is strictly smaller or equal to
  107. // when the keepalive happened at the local server (when the local server is the leader)
  108. // or remote leader.
  109. // Without this, a lease might be revoked at rev 3 but client can see the keepalive succeeded
  110. // at rev 4.
  111. resp := &pb.LeaseKeepAliveResponse{ID: req.ID, Header: &pb.ResponseHeader{}}
  112. ls.hdr.fill(resp.Header)
  113. ttl, err := ls.le.LeaseRenew(stream.Context(), lease.LeaseID(req.ID))
  114. if err == lease.ErrLeaseNotFound {
  115. err = nil
  116. ttl = 0
  117. }
  118. if err != nil {
  119. return togRPCError(err)
  120. }
  121. resp.TTL = ttl
  122. err = stream.Send(resp)
  123. if err != nil {
  124. if isClientCtxErr(stream.Context().Err(), err) {
  125. plog.Debugf("failed to send lease keepalive response to gRPC stream (%q)", err.Error())
  126. } else {
  127. plog.Warningf("failed to send lease keepalive response to gRPC stream (%q)", err.Error())
  128. }
  129. return err
  130. }
  131. }
  132. }