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 d2d8122
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
20 changes: 17 additions & 3 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)
// TxnBegin: see comments on mvcc.KV
TxnBegin() int64
// TxnEnd: see comments on mvcc.KV
TxnEnd(txnID int64) error
// TxnDeleteRange: 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 @@ -219,8 +224,17 @@ func (le *lessor) Revoke(id LeaseID) error {
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)
}
}

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 d2d8122

Please sign in to comment.