From fbc0510a4eac2bc3e7dd0ab8cec9cf88db7bb0db Mon Sep 17 00:00:00 2001 From: Gyuho Lee Date: Sun, 22 Jul 2018 13:12:37 -0700 Subject: [PATCH] clientv3: fix keepalive send interval when response queue is full client should update next keepalive send time even when lease keepalive response queue becomes full. Otherwise, client sends keepalive request every 500ms regardless of TTL when the send is only expected to happen with the interval of TTL / 3 at minimum. Signed-off-by: Gyuho Lee --- clientv3/integration/lease_test.go | 43 ++++++++++++++++++++++++++++++ clientv3/lease.go | 12 ++++++--- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/clientv3/integration/lease_test.go b/clientv3/integration/lease_test.go index ee7611b1e..703ef80c0 100644 --- a/clientv3/integration/lease_test.go +++ b/clientv3/integration/lease_test.go @@ -310,6 +310,49 @@ func TestLeaseGrantErrConnClosed(t *testing.T) { } } +// TestLeaseKeepAliveFullResponseQueue ensures when response +// queue is full thus dropping keepalive response sends, +// keepalive request is sent with the same rate of TTL / 3. +func TestLeaseKeepAliveFullResponseQueue(t *testing.T) { + defer testutil.AfterTest(t) + + clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1}) + defer clus.Terminate(t) + + lapi := clus.Client(0) + + // expect lease keepalive every 10-second + lresp, err := lapi.Grant(context.Background(), 30) + if err != nil { + t.Fatalf("failed to create lease %v", err) + } + id := lresp.ID + + old := clientv3.LeaseResponseChSize + defer func() { + clientv3.LeaseResponseChSize = old + }() + clientv3.LeaseResponseChSize = 0 + + // never fetch from response queue, and let it become full + _, err = lapi.KeepAlive(context.Background(), id) + if err != nil { + t.Fatalf("failed to keepalive lease %v", err) + } + + // TTL should not be refreshed after 3 seconds + // expect keepalive to be triggered after TTL/3 + time.Sleep(3 * time.Second) + + tr, terr := lapi.TimeToLive(context.Background(), id) + if terr != nil { + t.Fatalf("failed to get lease information %v", terr) + } + if tr.TTL >= 29 { + t.Errorf("unexpected kept-alive lease TTL %d", tr.TTL) + } +} + func TestLeaseGrantNewAfterClose(t *testing.T) { defer testutil.AfterTest(t) diff --git a/clientv3/lease.go b/clientv3/lease.go index 4097b3afa..3729cf37b 100644 --- a/clientv3/lease.go +++ b/clientv3/lease.go @@ -77,8 +77,6 @@ const ( // defaultTTL is the assumed lease TTL used for the first keepalive // deadline before the actual TTL is known to the client. defaultTTL = 5 * time.Second - // a small buffer to store unsent lease responses. - leaseResponseChSize = 16 // NoLease is a lease ID for the absence of a lease. NoLease LeaseID = 0 @@ -86,6 +84,11 @@ const ( retryConnWait = 500 * time.Millisecond ) +// LeaseResponseChSize is the size of buffer to store unsent lease responses. +// WARNING: DO NOT UPDATE. +// Only for testing purposes. +var LeaseResponseChSize = 16 + // ErrKeepAliveHalted is returned if client keep alive loop halts with an unexpected error. // // This usually means that automatic lease renewal via KeepAlive is broken, but KeepAliveOnce will still work as expected. @@ -258,7 +261,7 @@ func (l *lessor) Leases(ctx context.Context) (*LeaseLeasesResponse, error) { } func (l *lessor) KeepAlive(ctx context.Context, id LeaseID) (<-chan *LeaseKeepAliveResponse, error) { - ch := make(chan *LeaseKeepAliveResponse, leaseResponseChSize) + ch := make(chan *LeaseKeepAliveResponse, LeaseResponseChSize) l.mu.Lock() // ensure that recvKeepAliveLoop is still running @@ -514,9 +517,10 @@ func (l *lessor) recvKeepAlive(resp *pb.LeaseKeepAliveResponse) { for _, ch := range ka.chs { select { case ch <- karesp: - ka.nextKeepAlive = nextKeepAlive default: } + // still advance in order to rate-limit keep-alive sends + ka.nextKeepAlive = nextKeepAlive } }