lessor.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. // Copyright 2015 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 lease
  15. import (
  16. "container/heap"
  17. "encoding/binary"
  18. "errors"
  19. "math"
  20. "sort"
  21. "sync"
  22. "time"
  23. "github.com/coreos/etcd/lease/leasepb"
  24. "github.com/coreos/etcd/mvcc/backend"
  25. )
  26. // NoLease is a special LeaseID representing the absence of a lease.
  27. const NoLease = LeaseID(0)
  28. // MaxLeaseTTL is the maximum lease TTL value
  29. const MaxLeaseTTL = 9000000000
  30. var (
  31. forever = time.Time{}
  32. leaseBucketName = []byte("lease")
  33. // maximum number of leases to revoke per second; configurable for tests
  34. leaseRevokeRate = 1000
  35. ErrNotPrimary = errors.New("not a primary lessor")
  36. ErrLeaseNotFound = errors.New("lease not found")
  37. ErrLeaseExists = errors.New("lease already exists")
  38. ErrLeaseTTLTooLarge = errors.New("too large lease TTL")
  39. )
  40. // TxnDelete is a TxnWrite that only permits deletes. Defined here
  41. // to avoid circular dependency with mvcc.
  42. type TxnDelete interface {
  43. DeleteRange(key, end []byte) (n, rev int64)
  44. End()
  45. }
  46. // RangeDeleter is a TxnDelete constructor.
  47. type RangeDeleter func() TxnDelete
  48. type LeaseID int64
  49. // Lessor owns leases. It can grant, revoke, renew and modify leases for lessee.
  50. type Lessor interface {
  51. // SetRangeDeleter lets the lessor create TxnDeletes to the store.
  52. // Lessor deletes the items in the revoked or expired lease by creating
  53. // new TxnDeletes.
  54. SetRangeDeleter(rd RangeDeleter)
  55. // Grant grants a lease that expires at least after TTL seconds.
  56. Grant(id LeaseID, ttl int64) (*Lease, error)
  57. // Revoke revokes a lease with given ID. The item attached to the
  58. // given lease will be removed. If the ID does not exist, an error
  59. // will be returned.
  60. Revoke(id LeaseID) error
  61. // Attach attaches given leaseItem to the lease with given LeaseID.
  62. // If the lease does not exist, an error will be returned.
  63. Attach(id LeaseID, items []LeaseItem) error
  64. // GetLease returns LeaseID for given item.
  65. // If no lease found, NoLease value will be returned.
  66. GetLease(item LeaseItem) LeaseID
  67. // Detach detaches given leaseItem from the lease with given LeaseID.
  68. // If the lease does not exist, an error will be returned.
  69. Detach(id LeaseID, items []LeaseItem) error
  70. // Promote promotes the lessor to be the primary lessor. Primary lessor manages
  71. // the expiration and renew of leases.
  72. // Newly promoted lessor renew the TTL of all lease to extend + previous TTL.
  73. Promote(extend time.Duration)
  74. // Demote demotes the lessor from being the primary lessor.
  75. Demote()
  76. // Renew renews a lease with given ID. It returns the renewed TTL. If the ID does not exist,
  77. // an error will be returned.
  78. Renew(id LeaseID) (int64, error)
  79. // Lookup gives the lease at a given lease id, if any
  80. Lookup(id LeaseID) *Lease
  81. // Leases lists all leases.
  82. Leases() []*Lease
  83. // ExpiredLeasesC returns a chan that is used to receive expired leases.
  84. ExpiredLeasesC() <-chan []*Lease
  85. // Recover recovers the lessor state from the given backend and RangeDeleter.
  86. Recover(b backend.Backend, rd RangeDeleter)
  87. // Stop stops the lessor for managing leases. The behavior of calling Stop multiple
  88. // times is undefined.
  89. Stop()
  90. }
  91. // lessor implements Lessor interface.
  92. // TODO: use clockwork for testability.
  93. type lessor struct {
  94. mu sync.RWMutex
  95. // demotec is set when the lessor is the primary.
  96. // demotec will be closed if the lessor is demoted.
  97. demotec chan struct{}
  98. leaseMap map[LeaseID]*Lease
  99. leaseHeap LeaseQueue
  100. itemMap map[LeaseItem]LeaseID
  101. // When a lease expires, the lessor will delete the
  102. // leased range (or key) by the RangeDeleter.
  103. rd RangeDeleter
  104. // backend to persist leases. We only persist lease ID and expiry for now.
  105. // The leased items can be recovered by iterating all the keys in kv.
  106. b backend.Backend
  107. // minLeaseTTL is the minimum lease TTL that can be granted for a lease. Any
  108. // requests for shorter TTLs are extended to the minimum TTL.
  109. minLeaseTTL int64
  110. expiredC chan []*Lease
  111. // stopC is a channel whose closure indicates that the lessor should be stopped.
  112. stopC chan struct{}
  113. // doneC is a channel whose closure indicates that the lessor is stopped.
  114. doneC chan struct{}
  115. }
  116. func NewLessor(b backend.Backend, minLeaseTTL int64) Lessor {
  117. return newLessor(b, minLeaseTTL)
  118. }
  119. func newLessor(b backend.Backend, minLeaseTTL int64) *lessor {
  120. l := &lessor{
  121. leaseMap: make(map[LeaseID]*Lease),
  122. itemMap: make(map[LeaseItem]LeaseID),
  123. leaseHeap: make(LeaseQueue, 0),
  124. b: b,
  125. minLeaseTTL: minLeaseTTL,
  126. // expiredC is a small buffered chan to avoid unnecessary blocking.
  127. expiredC: make(chan []*Lease, 16),
  128. stopC: make(chan struct{}),
  129. doneC: make(chan struct{}),
  130. }
  131. l.initAndRecover()
  132. go l.runLoop()
  133. return l
  134. }
  135. // isPrimary indicates if this lessor is the primary lessor. The primary
  136. // lessor manages lease expiration and renew.
  137. //
  138. // in etcd, raft leader is the primary. Thus there might be two primary
  139. // leaders at the same time (raft allows concurrent leader but with different term)
  140. // for at most a leader election timeout.
  141. // The old primary leader cannot affect the correctness since its proposal has a
  142. // smaller term and will not be committed.
  143. //
  144. // TODO: raft follower do not forward lease management proposals. There might be a
  145. // very small window (within second normally which depends on go scheduling) that
  146. // a raft follow is the primary between the raft leader demotion and lessor demotion.
  147. // Usually this should not be a problem. Lease should not be that sensitive to timing.
  148. func (le *lessor) isPrimary() bool {
  149. return le.demotec != nil
  150. }
  151. func (le *lessor) SetRangeDeleter(rd RangeDeleter) {
  152. le.mu.Lock()
  153. defer le.mu.Unlock()
  154. le.rd = rd
  155. }
  156. func (le *lessor) Grant(id LeaseID, ttl int64) (*Lease, error) {
  157. if id == NoLease {
  158. return nil, ErrLeaseNotFound
  159. }
  160. if ttl > MaxLeaseTTL {
  161. return nil, ErrLeaseTTLTooLarge
  162. }
  163. // TODO: when lessor is under high load, it should give out lease
  164. // with longer TTL to reduce renew load.
  165. l := &Lease{
  166. ID: id,
  167. ttl: ttl,
  168. itemSet: make(map[LeaseItem]struct{}),
  169. revokec: make(chan struct{}),
  170. }
  171. le.mu.Lock()
  172. defer le.mu.Unlock()
  173. if _, ok := le.leaseMap[id]; ok {
  174. return nil, ErrLeaseExists
  175. }
  176. if l.ttl < le.minLeaseTTL {
  177. l.ttl = le.minLeaseTTL
  178. }
  179. if le.isPrimary() {
  180. l.refresh(0)
  181. } else {
  182. l.forever()
  183. }
  184. le.leaseMap[id] = l
  185. item := &LeaseWithTime{id: l.ID, expiration: l.expiry.UnixNano()}
  186. heap.Push(&le.leaseHeap, item)
  187. l.persistTo(le.b)
  188. leaseTotalTTLs.Observe(float64(l.ttl))
  189. leaseGranted.Inc()
  190. return l, nil
  191. }
  192. func (le *lessor) Revoke(id LeaseID) error {
  193. le.mu.Lock()
  194. l := le.leaseMap[id]
  195. if l == nil {
  196. le.mu.Unlock()
  197. return ErrLeaseNotFound
  198. }
  199. defer close(l.revokec)
  200. // unlock before doing external work
  201. le.mu.Unlock()
  202. if le.rd == nil {
  203. return nil
  204. }
  205. txn := le.rd()
  206. // sort keys so deletes are in same order among all members,
  207. // otherwise the backened hashes will be different
  208. keys := l.Keys()
  209. sort.StringSlice(keys).Sort()
  210. for _, key := range keys {
  211. txn.DeleteRange([]byte(key), nil)
  212. }
  213. le.mu.Lock()
  214. defer le.mu.Unlock()
  215. delete(le.leaseMap, l.ID)
  216. // lease deletion needs to be in the same backend transaction with the
  217. // kv deletion. Or we might end up with not executing the revoke or not
  218. // deleting the keys if etcdserver fails in between.
  219. le.b.BatchTx().UnsafeDelete(leaseBucketName, int64ToBytes(int64(l.ID)))
  220. txn.End()
  221. leaseRevoked.Inc()
  222. return nil
  223. }
  224. // Renew renews an existing lease. If the given lease does not exist or
  225. // has expired, an error will be returned.
  226. func (le *lessor) Renew(id LeaseID) (int64, error) {
  227. le.mu.Lock()
  228. unlock := func() { le.mu.Unlock() }
  229. defer func() { unlock() }()
  230. if !le.isPrimary() {
  231. // forward renew request to primary instead of returning error.
  232. return -1, ErrNotPrimary
  233. }
  234. demotec := le.demotec
  235. l := le.leaseMap[id]
  236. if l == nil {
  237. return -1, ErrLeaseNotFound
  238. }
  239. if l.expired() {
  240. le.mu.Unlock()
  241. unlock = func() {}
  242. select {
  243. // A expired lease might be pending for revoking or going through
  244. // quorum to be revoked. To be accurate, renew request must wait for the
  245. // deletion to complete.
  246. case <-l.revokec:
  247. return -1, ErrLeaseNotFound
  248. // The expired lease might fail to be revoked if the primary changes.
  249. // The caller will retry on ErrNotPrimary.
  250. case <-demotec:
  251. return -1, ErrNotPrimary
  252. case <-le.stopC:
  253. return -1, ErrNotPrimary
  254. }
  255. }
  256. l.refresh(0)
  257. item := &LeaseWithTime{id: l.ID, expiration: l.expiry.UnixNano()}
  258. heap.Push(&le.leaseHeap, item)
  259. leaseRenewed.Inc()
  260. return l.ttl, nil
  261. }
  262. func (le *lessor) Lookup(id LeaseID) *Lease {
  263. le.mu.RLock()
  264. defer le.mu.RUnlock()
  265. return le.leaseMap[id]
  266. }
  267. func (le *lessor) unsafeLeases() []*Lease {
  268. leases := make([]*Lease, 0, len(le.leaseMap))
  269. for _, l := range le.leaseMap {
  270. leases = append(leases, l)
  271. }
  272. return leases
  273. }
  274. func (le *lessor) Leases() []*Lease {
  275. le.mu.RLock()
  276. ls := le.unsafeLeases()
  277. le.mu.RUnlock()
  278. sort.Sort(leasesByExpiry(ls))
  279. return ls
  280. }
  281. func (le *lessor) Promote(extend time.Duration) {
  282. le.mu.Lock()
  283. defer le.mu.Unlock()
  284. le.demotec = make(chan struct{})
  285. // refresh the expiries of all leases.
  286. for _, l := range le.leaseMap {
  287. l.refresh(extend)
  288. item := &LeaseWithTime{id: l.ID, expiration: l.expiry.UnixNano()}
  289. heap.Push(&le.leaseHeap, item)
  290. }
  291. if len(le.leaseMap) < leaseRevokeRate {
  292. // no possibility of lease pile-up
  293. return
  294. }
  295. // adjust expiries in case of overlap
  296. leases := le.unsafeLeases()
  297. sort.Sort(leasesByExpiry(leases))
  298. baseWindow := leases[0].Remaining()
  299. nextWindow := baseWindow + time.Second
  300. expires := 0
  301. // have fewer expires than the total revoke rate so piled up leases
  302. // don't consume the entire revoke limit
  303. targetExpiresPerSecond := (3 * leaseRevokeRate) / 4
  304. for _, l := range leases {
  305. remaining := l.Remaining()
  306. if remaining > nextWindow {
  307. baseWindow = remaining
  308. nextWindow = baseWindow + time.Second
  309. expires = 1
  310. continue
  311. }
  312. expires++
  313. if expires <= targetExpiresPerSecond {
  314. continue
  315. }
  316. rateDelay := float64(time.Second) * (float64(expires) / float64(targetExpiresPerSecond))
  317. // If leases are extended by n seconds, leases n seconds ahead of the
  318. // base window should be extended by only one second.
  319. rateDelay -= float64(remaining - baseWindow)
  320. delay := time.Duration(rateDelay)
  321. nextWindow = baseWindow + delay
  322. l.refresh(delay + extend)
  323. item := &LeaseWithTime{id: l.ID, expiration: l.expiry.UnixNano()}
  324. heap.Push(&le.leaseHeap, item)
  325. }
  326. }
  327. type leasesByExpiry []*Lease
  328. func (le leasesByExpiry) Len() int { return len(le) }
  329. func (le leasesByExpiry) Less(i, j int) bool { return le[i].Remaining() < le[j].Remaining() }
  330. func (le leasesByExpiry) Swap(i, j int) { le[i], le[j] = le[j], le[i] }
  331. func (le *lessor) Demote() {
  332. le.mu.Lock()
  333. defer le.mu.Unlock()
  334. // set the expiries of all leases to forever
  335. for _, l := range le.leaseMap {
  336. l.forever()
  337. }
  338. if le.demotec != nil {
  339. close(le.demotec)
  340. le.demotec = nil
  341. }
  342. }
  343. // Attach attaches items to the lease with given ID. When the lease
  344. // expires, the attached items will be automatically removed.
  345. // If the given lease does not exist, an error will be returned.
  346. func (le *lessor) Attach(id LeaseID, items []LeaseItem) error {
  347. le.mu.Lock()
  348. defer le.mu.Unlock()
  349. l := le.leaseMap[id]
  350. if l == nil {
  351. return ErrLeaseNotFound
  352. }
  353. l.mu.Lock()
  354. for _, it := range items {
  355. l.itemSet[it] = struct{}{}
  356. le.itemMap[it] = id
  357. }
  358. l.mu.Unlock()
  359. return nil
  360. }
  361. func (le *lessor) GetLease(item LeaseItem) LeaseID {
  362. le.mu.RLock()
  363. id := le.itemMap[item]
  364. le.mu.RUnlock()
  365. return id
  366. }
  367. // Detach detaches items from the lease with given ID.
  368. // If the given lease does not exist, an error will be returned.
  369. func (le *lessor) Detach(id LeaseID, items []LeaseItem) error {
  370. le.mu.Lock()
  371. defer le.mu.Unlock()
  372. l := le.leaseMap[id]
  373. if l == nil {
  374. return ErrLeaseNotFound
  375. }
  376. l.mu.Lock()
  377. for _, it := range items {
  378. delete(l.itemSet, it)
  379. delete(le.itemMap, it)
  380. }
  381. l.mu.Unlock()
  382. return nil
  383. }
  384. func (le *lessor) Recover(b backend.Backend, rd RangeDeleter) {
  385. le.mu.Lock()
  386. defer le.mu.Unlock()
  387. le.b = b
  388. le.rd = rd
  389. le.leaseMap = make(map[LeaseID]*Lease)
  390. le.itemMap = make(map[LeaseItem]LeaseID)
  391. le.initAndRecover()
  392. }
  393. func (le *lessor) ExpiredLeasesC() <-chan []*Lease {
  394. return le.expiredC
  395. }
  396. func (le *lessor) Stop() {
  397. close(le.stopC)
  398. <-le.doneC
  399. }
  400. func (le *lessor) runLoop() {
  401. defer close(le.doneC)
  402. for {
  403. var ls []*Lease
  404. // rate limit
  405. revokeLimit := leaseRevokeRate / 2
  406. le.mu.RLock()
  407. if le.isPrimary() {
  408. ls = le.findExpiredLeases(revokeLimit)
  409. }
  410. le.mu.RUnlock()
  411. if len(ls) != 0 {
  412. select {
  413. case <-le.stopC:
  414. return
  415. case le.expiredC <- ls:
  416. default:
  417. // the receiver of expiredC is probably busy handling
  418. // other stuff
  419. // let's try this next time after 500ms
  420. }
  421. }
  422. select {
  423. case <-time.After(500 * time.Millisecond):
  424. case <-le.stopC:
  425. return
  426. }
  427. }
  428. }
  429. // expireExists returns true if expiry items exist.
  430. // It pops only when expiry item exists.
  431. // "next" is true, to indicate that it may exist in next attempt.
  432. func (le *lessor) expireExists() (l *Lease, ok bool, next bool) {
  433. if le.leaseHeap.Len() == 0 {
  434. return nil, false, false
  435. }
  436. item := le.leaseHeap[0]
  437. l = le.leaseMap[item.id]
  438. if l == nil {
  439. // lease has expired or been revoked
  440. // no need to revoke (nothing is expiry)
  441. heap.Pop(&le.leaseHeap) // O(log N)
  442. return nil, false, true
  443. }
  444. if time.Now().UnixNano() < item.expiration {
  445. // Candidate expirations are caught up, reinsert this item
  446. // and no need to revoke (nothing is expiry)
  447. return l, false, false
  448. }
  449. // if the lease is actually expired, add to the removal list. If it is not expired, we can ignore it because another entry will have been inserted into the heap
  450. heap.Pop(&le.leaseHeap) // O(log N)
  451. return l, true, false
  452. }
  453. // findExpiredLeases loops leases in the leaseMap until reaching expired limit
  454. // and returns the expired leases that needed to be revoked.
  455. func (le *lessor) findExpiredLeases(limit int) []*Lease {
  456. leases := make([]*Lease, 0, 16)
  457. for {
  458. l, ok, next := le.expireExists()
  459. if !ok && !next {
  460. break
  461. }
  462. if !ok {
  463. continue
  464. }
  465. if next {
  466. continue
  467. }
  468. if l.expired() {
  469. leases = append(leases, l)
  470. // reach expired limit
  471. if len(leases) == limit {
  472. break
  473. }
  474. }
  475. }
  476. return leases
  477. }
  478. func (le *lessor) initAndRecover() {
  479. tx := le.b.BatchTx()
  480. tx.Lock()
  481. tx.UnsafeCreateBucket(leaseBucketName)
  482. _, vs := tx.UnsafeRange(leaseBucketName, int64ToBytes(0), int64ToBytes(math.MaxInt64), 0)
  483. // TODO: copy vs and do decoding outside tx lock if lock contention becomes an issue.
  484. for i := range vs {
  485. var lpb leasepb.Lease
  486. err := lpb.Unmarshal(vs[i])
  487. if err != nil {
  488. tx.Unlock()
  489. panic("failed to unmarshal lease proto item")
  490. }
  491. ID := LeaseID(lpb.ID)
  492. if lpb.TTL < le.minLeaseTTL {
  493. lpb.TTL = le.minLeaseTTL
  494. }
  495. le.leaseMap[ID] = &Lease{
  496. ID: ID,
  497. ttl: lpb.TTL,
  498. // itemSet will be filled in when recover key-value pairs
  499. // set expiry to forever, refresh when promoted
  500. itemSet: make(map[LeaseItem]struct{}),
  501. expiry: forever,
  502. revokec: make(chan struct{}),
  503. }
  504. }
  505. heap.Init(&le.leaseHeap)
  506. tx.Unlock()
  507. le.b.ForceCommit()
  508. }
  509. type Lease struct {
  510. ID LeaseID
  511. ttl int64 // time to live in seconds
  512. // expiryMu protects concurrent accesses to expiry
  513. expiryMu sync.RWMutex
  514. // expiry is time when lease should expire. no expiration when expiry.IsZero() is true
  515. expiry time.Time
  516. // mu protects concurrent accesses to itemSet
  517. mu sync.RWMutex
  518. itemSet map[LeaseItem]struct{}
  519. revokec chan struct{}
  520. }
  521. func (l *Lease) expired() bool {
  522. return l.Remaining() <= 0
  523. }
  524. func (l *Lease) persistTo(b backend.Backend) {
  525. key := int64ToBytes(int64(l.ID))
  526. lpb := leasepb.Lease{ID: int64(l.ID), TTL: l.ttl}
  527. val, err := lpb.Marshal()
  528. if err != nil {
  529. panic("failed to marshal lease proto item")
  530. }
  531. b.BatchTx().Lock()
  532. b.BatchTx().UnsafePut(leaseBucketName, key, val)
  533. b.BatchTx().Unlock()
  534. }
  535. // TTL returns the TTL of the Lease.
  536. func (l *Lease) TTL() int64 {
  537. return l.ttl
  538. }
  539. // refresh refreshes the expiry of the lease.
  540. func (l *Lease) refresh(extend time.Duration) {
  541. newExpiry := time.Now().Add(extend + time.Duration(l.ttl)*time.Second)
  542. l.expiryMu.Lock()
  543. defer l.expiryMu.Unlock()
  544. l.expiry = newExpiry
  545. }
  546. // forever sets the expiry of lease to be forever.
  547. func (l *Lease) forever() {
  548. l.expiryMu.Lock()
  549. defer l.expiryMu.Unlock()
  550. l.expiry = forever
  551. }
  552. // Keys returns all the keys attached to the lease.
  553. func (l *Lease) Keys() []string {
  554. l.mu.RLock()
  555. keys := make([]string, 0, len(l.itemSet))
  556. for k := range l.itemSet {
  557. keys = append(keys, k.Key)
  558. }
  559. l.mu.RUnlock()
  560. return keys
  561. }
  562. // Remaining returns the remaining time of the lease.
  563. func (l *Lease) Remaining() time.Duration {
  564. l.expiryMu.RLock()
  565. defer l.expiryMu.RUnlock()
  566. if l.expiry.IsZero() {
  567. return time.Duration(math.MaxInt64)
  568. }
  569. return time.Until(l.expiry)
  570. }
  571. type LeaseItem struct {
  572. Key string
  573. }
  574. func int64ToBytes(n int64) []byte {
  575. bytes := make([]byte, 8)
  576. binary.BigEndian.PutUint64(bytes, uint64(n))
  577. return bytes
  578. }
  579. // FakeLessor is a fake implementation of Lessor interface.
  580. // Used for testing only.
  581. type FakeLessor struct{}
  582. func (fl *FakeLessor) SetRangeDeleter(dr RangeDeleter) {}
  583. func (fl *FakeLessor) Grant(id LeaseID, ttl int64) (*Lease, error) { return nil, nil }
  584. func (fl *FakeLessor) Revoke(id LeaseID) error { return nil }
  585. func (fl *FakeLessor) Attach(id LeaseID, items []LeaseItem) error { return nil }
  586. func (fl *FakeLessor) GetLease(item LeaseItem) LeaseID { return 0 }
  587. func (fl *FakeLessor) Detach(id LeaseID, items []LeaseItem) error { return nil }
  588. func (fl *FakeLessor) Promote(extend time.Duration) {}
  589. func (fl *FakeLessor) Demote() {}
  590. func (fl *FakeLessor) Renew(id LeaseID) (int64, error) { return 10, nil }
  591. func (fl *FakeLessor) Lookup(id LeaseID) *Lease { return nil }
  592. func (fl *FakeLessor) Leases() []*Lease { return nil }
  593. func (fl *FakeLessor) ExpiredLeasesC() <-chan []*Lease { return nil }
  594. func (fl *FakeLessor) Recover(b backend.Backend, rd RangeDeleter) {}
  595. func (fl *FakeLessor) Stop() {}