Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lease expiration check #19092

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions client/v3/concurrency/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,9 @@ func WithContext(ctx context.Context) SessionOption {
so.ctx = ctx
}
}

// Unexpired returns true iff the session was unexpired at some point during the
// Unexpired() call.
func (s *Session) Unexpired() bool {
return s.client.Unexpired(s.id)
}
14 changes: 14 additions & 0 deletions client/v3/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ type Lease interface {
// (see https://github.com/etcd-io/etcd/pull/7866)
KeepAlive(ctx context.Context, id LeaseID) (<-chan *LeaseKeepAliveResponse, error)

// Unexpired returns true iff the lease is unexpired (more precisely: iff the
// lease was unexpired during the execution of the Unexpired() call).
Unexpired(id LeaseID) bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid negative logic in the interface. !Expired(id) should be simpler.


// KeepAliveOnce renews the lease once. The response corresponds to the
// first message from calling KeepAlive. If the response has a recoverable
// error, KeepAliveOnce will retry the RPC with a new keep alive message.
Expand Down Expand Up @@ -624,3 +628,13 @@ func (ka *keepAlive) close() {
close(ch)
}
}

func (l *lessor) Unexpired(id LeaseID) bool {
l.mu.Lock()
defer l.mu.Unlock()
ka, ok := l.keepAlives[id]
if !ok {
return false
}
return ka.deadline.After(time.Now())
}
7 changes: 1 addition & 6 deletions client/v3/leasing/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,7 @@ func (lkv *leasingKV) readySession() bool {
if lkv.session == nil {
return false
}
select {
case <-lkv.session.Done():
default:
return true
}
return false
return lkv.session.Unexpired()
}

func (lkv *leasingKV) leaseID() v3.LeaseID {
Expand Down
74 changes: 74 additions & 0 deletions tests/integration/clientv3/lease/leasing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,80 @@ func TestLeasingConcurrentPut(t *testing.T) {
}
}

func TestLeasingGetChecksForExpiration(t *testing.T) {
integration2.BeforeTest(t)

ttl := 6

// There's no way to partition a client from the server, so use multiple
// servers and shut down a single server to partition the client from the
// system.
clus := integration2.NewCluster(t, &integration2.ClusterConfig{Size: 3, UseBridge: true})
defer clus.Terminate(t)

// Try to make sure server 0 is not the leader
if clus.Members[0].Server.Leader() == clus.Members[0].Server.MemberID() {
err := clus.Members[0].Server.MoveLeader(context.TODO(),
clus.Members[0].Server.Lead(),
uint64(clus.Members[1].Server.MemberID()))
if err != nil {
panic(err)
}
}
time.Sleep(2 * time.Second)

leaderID := clus.Members[0].Server.Leader()
if leaderID == clus.Members[0].Server.MemberID() {
panic("test wants 0 to not be leader")
}

// This client will get partitioned away from the system (by killing the
// node it's connected to).
lkv0, closeLKV0, err := leasing.NewKV(clus.Client(0), "pfx/", concurrency.WithTTL(ttl))
require.NoError(t, err)
defer closeLKV0()

lkv1, closeLKV1, err := leasing.NewKV(clus.Client(1), "pfx/")
require.NoError(t, err)
defer closeLKV1()

if _, err = lkv0.Put(context.TODO(), "k", "abc"); err != nil {
t.Fatal(err)
}
if _, err = lkv0.Get(context.TODO(), "k"); err != nil {
t.Fatal(err)
}

time.Sleep(6500 * time.Millisecond)
clus.Members[0].Stop(t)

if _, err = lkv1.Put(context.TODO(), "k", "def"); err != nil {
t.Fatal(err)
}

resp, err := lkv1.Get(context.TODO(), "k")
if err != nil {
t.Fatal(err)
}
if len(resp.Kvs) != 1 || string(resp.Kvs[0].Value) != "def" {
t.Fatalf(`expected "k"->"def" from lkv1, got response %+v`, resp)
}

go func() {
// Eventually bring back the server so the disconnected client can
// finish its last `Get()`.
time.Sleep(1 * time.Second)
clus.Members[0].Restart(t)
}()
cachedResp, err := lkv0.Get(context.TODO(), "k")
if err != nil {
t.Fatal(err)
}
if len(cachedResp.Kvs) != 1 || string(cachedResp.Kvs[0].Value) != "def" {
t.Fatalf(`expected "k"->"def", got response %+v`, cachedResp)
}
}

func TestLeasingDisconnectedGet(t *testing.T) {
integration2.BeforeTest(t)
clus := integration2.NewCluster(t, &integration2.ClusterConfig{Size: 1, UseBridge: true})
Expand Down