Skip to content

Commit

Permalink
lease: delete kvs in a txn
Browse files Browse the repository at this point in the history
  • Loading branch information
xiang90 committed Aug 4, 2016
1 parent 29a077b commit 8e0520a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
26 changes: 18 additions & 8 deletions lease/lessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ var (

type LeaseID int64

// RangeDeleter defines an interface with DeleteRange method.
// RangeDeleter defines an interface with Txn and DeleteRange method.
// We define this interface only for lessor to limit the number
// of methods of mvcc.KV to what lessor actually needs.
//
// Having a minimum interface makes testing easy.
type RangeDeleter interface {
DeleteRange(key, end []byte) (int64, int64)
// See comments on mvcc.KV
TxnBegin() int64
// See comments on mvcc.KV
TxnEnd(txnID int64) error
// See comments on mvcc.KV
TxnDeleteRange(txnID int64, key, end []byte) (n, rev int64, err error)
}

// Lessor owns leases. It can grant, revoke, renew and modify leases for lessee.
Expand Down Expand Up @@ -215,20 +220,25 @@ func (le *lessor) Revoke(id LeaseID) error {
le.mu.Unlock()
return ErrLeaseNotFound
}
delete(le.leaseMap, l.ID)
l.removeFrom(le.b)
// unlock before doing external work
le.mu.Unlock()

if le.rd != nil {
tid := le.rd.TxnBegin()
for item := range l.itemSet {
le.rd.DeleteRange([]byte(item.Key), nil)
_, _, err := le.rd.TxnDeleteRange(tid, []byte(item.Key), nil)
if err != nil {
panic(err)
}
}
err := le.rd.TxnEnd(tid)
if err != nil {
panic(err)
}
}

le.mu.Lock()
defer le.mu.Unlock()
delete(le.leaseMap, l.ID)
l.removeFrom(le.b)

return nil
}

Expand Down
12 changes: 10 additions & 2 deletions lease/lessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,17 @@ type fakeDeleter struct {
deleted []string
}

func (fd *fakeDeleter) DeleteRange(key, end []byte) (int64, int64) {
func (fd *fakeDeleter) TxnBegin() int64 {
return 0
}

func (fd *fakeDeleter) TxnEnd(txnID int64) error {
return nil
}

func (fd *fakeDeleter) TxnDeleteRange(tid int64, key, end []byte) (int64, int64, error) {
fd.deleted = append(fd.deleted, string(key)+"_"+string(end))
return 0, 0
return 0, 0, nil
}

func NewTestBackend(t *testing.T) (string, backend.Backend) {
Expand Down

0 comments on commit 8e0520a

Please sign in to comment.