lease.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. "io"
  17. "github.com/coreos/etcd/etcdserver"
  18. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "github.com/coreos/etcd/lease"
  21. "golang.org/x/net/context"
  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) LeaseKeepAlive(stream pb.Lease_LeaseKeepAliveServer) (err error) {
  62. errc := make(chan error, 1)
  63. go func() {
  64. errc <- ls.leaseKeepAlive(stream)
  65. }()
  66. select {
  67. case err = <-errc:
  68. case <-stream.Context().Done():
  69. // the only server-side cancellation is noleader for now.
  70. err = stream.Context().Err()
  71. if err == context.Canceled {
  72. err = rpctypes.ErrGRPCNoLeader
  73. }
  74. }
  75. return err
  76. }
  77. func (ls *LeaseServer) leaseKeepAlive(stream pb.Lease_LeaseKeepAliveServer) error {
  78. for {
  79. req, err := stream.Recv()
  80. if err == io.EOF {
  81. return nil
  82. }
  83. if err != nil {
  84. if isClientCtxErr(stream.Context().Err(), err) {
  85. plog.Debugf("failed to receive lease keepalive request from gRPC stream (%q)", err.Error())
  86. } else {
  87. plog.Warningf("failed to receive lease keepalive request from gRPC stream (%q)", err.Error())
  88. }
  89. return err
  90. }
  91. // Create header before we sent out the renew request.
  92. // This can make sure that the revision is strictly smaller or equal to
  93. // when the keepalive happened at the local server (when the local server is the leader)
  94. // or remote leader.
  95. // Without this, a lease might be revoked at rev 3 but client can see the keepalive succeeded
  96. // at rev 4.
  97. resp := &pb.LeaseKeepAliveResponse{ID: req.ID, Header: &pb.ResponseHeader{}}
  98. ls.hdr.fill(resp.Header)
  99. ttl, err := ls.le.LeaseRenew(stream.Context(), lease.LeaseID(req.ID))
  100. if err == lease.ErrLeaseNotFound {
  101. err = nil
  102. ttl = 0
  103. }
  104. if err != nil {
  105. return togRPCError(err)
  106. }
  107. resp.TTL = ttl
  108. err = stream.Send(resp)
  109. if err != nil {
  110. if isClientCtxErr(stream.Context().Err(), err) {
  111. plog.Debugf("failed to send lease keepalive response to gRPC stream (%q)", err.Error())
  112. } else {
  113. plog.Warningf("failed to send lease keepalive response to gRPC stream (%q)", err.Error())
  114. }
  115. return err
  116. }
  117. }
  118. }