lessor.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2015 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 lease
  15. import (
  16. "fmt"
  17. "sync"
  18. "time"
  19. "github.com/coreos/etcd/pkg/idutil"
  20. )
  21. var (
  22. minLeaseTerm = 5 * time.Second
  23. )
  24. // DeleteableRange defines an interface with DeleteRange method.
  25. // We define this interface only for lessor to limit the number
  26. // of methods of storage.KV to what lessor actually needs.
  27. //
  28. // Having a minimum interface makes testing easy.
  29. type DeleteableRange interface {
  30. DeleteRange(key, end []byte) (int64, int64)
  31. }
  32. // a lessor is the owner of leases. It can grant, revoke,
  33. // renew and modify leases for lessee.
  34. // TODO: persist lease on to stable backend for failure recovery.
  35. // TODO: use clockwork for testability.
  36. type lessor struct {
  37. mu sync.Mutex
  38. // TODO: probably this should be a heap with a secondary
  39. // id index.
  40. // Now it is O(N) to loop over the leases to find expired ones.
  41. // We want to make Grant, Revoke, and FindExpired all O(logN) and
  42. // Renew O(1).
  43. // FindExpired and Renew should be the most frequent operations.
  44. leaseMap map[uint64]*lease
  45. // A DeleteableRange the lessor operates on.
  46. // When a lease expires, the lessor will delete the
  47. // leased range (or key) from the DeleteableRange.
  48. dr DeleteableRange
  49. idgen *idutil.Generator
  50. }
  51. func NewLessor(lessorID uint8, dr DeleteableRange) *lessor {
  52. return &lessor{
  53. leaseMap: make(map[uint64]*lease),
  54. dr: dr,
  55. idgen: idutil.NewGenerator(lessorID, time.Now()),
  56. }
  57. }
  58. // Grant grants a lease that expires at least at the given expiry
  59. // time.
  60. // TODO: when leassor is under highload, it should give out lease
  61. // with longer term to reduce renew load.
  62. func (le *lessor) Grant(expiry time.Time) *lease {
  63. expiry = minExpiry(time.Now(), expiry)
  64. id := le.idgen.Next()
  65. le.mu.Lock()
  66. defer le.mu.Unlock()
  67. l := &lease{id: id, expiry: expiry, itemSet: make(map[leaseItem]struct{})}
  68. if _, ok := le.leaseMap[id]; ok {
  69. panic("lease: unexpected duplicate ID!")
  70. }
  71. le.leaseMap[id] = l
  72. return l
  73. }
  74. // Revoke revokes a lease with given ID. The item attached to the
  75. // given lease will be removed. If the ID does not exist, an error
  76. // will be returned.
  77. func (le *lessor) Revoke(id uint64) error {
  78. le.mu.Lock()
  79. defer le.mu.Unlock()
  80. l := le.leaseMap[id]
  81. if l == nil {
  82. return fmt.Errorf("lease: cannot find lease %x", id)
  83. }
  84. delete(le.leaseMap, l.id)
  85. for item := range l.itemSet {
  86. le.dr.DeleteRange([]byte(item.key), []byte(item.endRange))
  87. }
  88. return nil
  89. }
  90. // Renew renews an existing lease with at least the given expiry.
  91. // If the given lease does not exist or has expired, an error will
  92. // be returned.
  93. func (le *lessor) Renew(id uint64, expiry time.Time) error {
  94. le.mu.Lock()
  95. defer le.mu.Unlock()
  96. l := le.leaseMap[id]
  97. if l == nil {
  98. return fmt.Errorf("lease: cannot find lease %x", id)
  99. }
  100. l.expiry = minExpiry(time.Now(), expiry)
  101. return nil
  102. }
  103. // Attach attaches items to the lease with given ID. When the lease
  104. // expires, the attached items will be automatically removed.
  105. // If the given lease does not exist, an error will be returned.
  106. func (le *lessor) Attach(id uint64, items []leaseItem) error {
  107. le.mu.Lock()
  108. defer le.mu.Unlock()
  109. l := le.leaseMap[id]
  110. if l == nil {
  111. return fmt.Errorf("lease: cannot find lease %x", id)
  112. }
  113. for _, it := range items {
  114. l.itemSet[it] = struct{}{}
  115. }
  116. return nil
  117. }
  118. // findExpiredLeases loops all the leases in the leaseMap and returns the expired
  119. // leases that needed to be revoked.
  120. func (le *lessor) findExpiredLeases() []*lease {
  121. le.mu.Lock()
  122. defer le.mu.Unlock()
  123. leases := make([]*lease, 0, 16)
  124. now := time.Now()
  125. for _, l := range le.leaseMap {
  126. if l.expiry.Sub(now) <= 0 {
  127. leases = append(leases, l)
  128. }
  129. }
  130. return leases
  131. }
  132. // get gets the lease with given id.
  133. // get is a helper fucntion for testing, at least for now.
  134. func (le *lessor) get(id uint64) *lease {
  135. le.mu.Lock()
  136. defer le.mu.Unlock()
  137. return le.leaseMap[id]
  138. }
  139. type lease struct {
  140. id uint64
  141. itemSet map[leaseItem]struct{}
  142. // expiry time in unixnano
  143. expiry time.Time
  144. }
  145. type leaseItem struct {
  146. key string
  147. endRange string
  148. }
  149. // minExpiry returns a minimal expiry. A minimal expiry is the larger on
  150. // between now + minLeaseTerm and the given expectedExpiry.
  151. func minExpiry(now time.Time, expectedExpiry time.Time) time.Time {
  152. minExpiry := time.Now().Add(minLeaseTerm)
  153. if expectedExpiry.Sub(minExpiry) < 0 {
  154. expectedExpiry = minExpiry
  155. }
  156. return expectedExpiry
  157. }