lease_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. // Copyright 2016 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 integration
  15. import (
  16. "context"
  17. "reflect"
  18. "sort"
  19. "sync"
  20. "testing"
  21. "time"
  22. "github.com/coreos/etcd/clientv3"
  23. "github.com/coreos/etcd/clientv3/concurrency"
  24. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  25. "github.com/coreos/etcd/integration"
  26. "github.com/coreos/etcd/pkg/testutil"
  27. "google.golang.org/grpc"
  28. )
  29. func TestLeaseNotFoundError(t *testing.T) {
  30. defer testutil.AfterTest(t)
  31. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  32. defer clus.Terminate(t)
  33. kv := clus.RandClient()
  34. _, err := kv.Put(context.TODO(), "foo", "bar", clientv3.WithLease(clientv3.LeaseID(500)))
  35. if err != rpctypes.ErrLeaseNotFound {
  36. t.Fatalf("expected %v, got %v", rpctypes.ErrLeaseNotFound, err)
  37. }
  38. }
  39. func TestLeaseGrant(t *testing.T) {
  40. defer testutil.AfterTest(t)
  41. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  42. defer clus.Terminate(t)
  43. lapi := clus.RandClient()
  44. kv := clus.RandClient()
  45. _, merr := lapi.Grant(context.Background(), clientv3.MaxLeaseTTL+1)
  46. if merr != rpctypes.ErrLeaseTTLTooLarge {
  47. t.Fatalf("err = %v, want %v", merr, rpctypes.ErrLeaseTTLTooLarge)
  48. }
  49. resp, err := lapi.Grant(context.Background(), 10)
  50. if err != nil {
  51. t.Errorf("failed to create lease %v", err)
  52. }
  53. _, err = kv.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID))
  54. if err != nil {
  55. t.Fatalf("failed to create key with lease %v", err)
  56. }
  57. }
  58. func TestLeaseRevoke(t *testing.T) {
  59. defer testutil.AfterTest(t)
  60. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  61. defer clus.Terminate(t)
  62. lapi := clus.RandClient()
  63. kv := clus.RandClient()
  64. resp, err := lapi.Grant(context.Background(), 10)
  65. if err != nil {
  66. t.Errorf("failed to create lease %v", err)
  67. }
  68. _, err = lapi.Revoke(context.Background(), clientv3.LeaseID(resp.ID))
  69. if err != nil {
  70. t.Errorf("failed to revoke lease %v", err)
  71. }
  72. _, err = kv.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID))
  73. if err != rpctypes.ErrLeaseNotFound {
  74. t.Fatalf("err = %v, want %v", err, rpctypes.ErrLeaseNotFound)
  75. }
  76. }
  77. func TestLeaseKeepAliveOnce(t *testing.T) {
  78. defer testutil.AfterTest(t)
  79. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  80. defer clus.Terminate(t)
  81. lapi := clus.RandClient()
  82. resp, err := lapi.Grant(context.Background(), 10)
  83. if err != nil {
  84. t.Errorf("failed to create lease %v", err)
  85. }
  86. _, err = lapi.KeepAliveOnce(context.Background(), resp.ID)
  87. if err != nil {
  88. t.Errorf("failed to keepalive lease %v", err)
  89. }
  90. _, err = lapi.KeepAliveOnce(context.Background(), clientv3.LeaseID(0))
  91. if err != rpctypes.ErrLeaseNotFound {
  92. t.Errorf("expected %v, got %v", rpctypes.ErrLeaseNotFound, err)
  93. }
  94. }
  95. func TestLeaseKeepAlive(t *testing.T) {
  96. defer testutil.AfterTest(t)
  97. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  98. defer clus.Terminate(t)
  99. lapi := clus.Client(0)
  100. clus.TakeClient(0)
  101. resp, err := lapi.Grant(context.Background(), 10)
  102. if err != nil {
  103. t.Errorf("failed to create lease %v", err)
  104. }
  105. rc, kerr := lapi.KeepAlive(context.Background(), resp.ID)
  106. if kerr != nil {
  107. t.Errorf("failed to keepalive lease %v", kerr)
  108. }
  109. kresp, ok := <-rc
  110. if !ok {
  111. t.Errorf("chan is closed, want not closed")
  112. }
  113. if kresp.ID != resp.ID {
  114. t.Errorf("ID = %x, want %x", kresp.ID, resp.ID)
  115. }
  116. lapi.Close()
  117. _, ok = <-rc
  118. if ok {
  119. t.Errorf("chan is not closed, want lease Close() closes chan")
  120. }
  121. }
  122. func TestLeaseKeepAliveOneSecond(t *testing.T) {
  123. defer testutil.AfterTest(t)
  124. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  125. defer clus.Terminate(t)
  126. cli := clus.Client(0)
  127. resp, err := cli.Grant(context.Background(), 1)
  128. if err != nil {
  129. t.Errorf("failed to create lease %v", err)
  130. }
  131. rc, kerr := cli.KeepAlive(context.Background(), resp.ID)
  132. if kerr != nil {
  133. t.Errorf("failed to keepalive lease %v", kerr)
  134. }
  135. for i := 0; i < 3; i++ {
  136. if _, ok := <-rc; !ok {
  137. t.Errorf("chan is closed, want not closed")
  138. }
  139. }
  140. }
  141. // TODO: add a client that can connect to all the members of cluster via unix sock.
  142. // TODO: test handle more complicated failures.
  143. func TestLeaseKeepAliveHandleFailure(t *testing.T) {
  144. t.Skip("test it when we have a cluster client")
  145. defer testutil.AfterTest(t)
  146. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  147. defer clus.Terminate(t)
  148. // TODO: change this line to get a cluster client
  149. lapi := clus.RandClient()
  150. resp, err := lapi.Grant(context.Background(), 10)
  151. if err != nil {
  152. t.Errorf("failed to create lease %v", err)
  153. }
  154. rc, kerr := lapi.KeepAlive(context.Background(), resp.ID)
  155. if kerr != nil {
  156. t.Errorf("failed to keepalive lease %v", kerr)
  157. }
  158. kresp := <-rc
  159. if kresp.ID != resp.ID {
  160. t.Errorf("ID = %x, want %x", kresp.ID, resp.ID)
  161. }
  162. // restart the connected member.
  163. clus.Members[0].Stop(t)
  164. select {
  165. case <-rc:
  166. t.Fatalf("unexpected keepalive")
  167. case <-time.After(10*time.Second/3 + 1):
  168. }
  169. // recover the member.
  170. clus.Members[0].Restart(t)
  171. kresp = <-rc
  172. if kresp.ID != resp.ID {
  173. t.Errorf("ID = %x, want %x", kresp.ID, resp.ID)
  174. }
  175. lapi.Close()
  176. _, ok := <-rc
  177. if ok {
  178. t.Errorf("chan is not closed, want lease Close() closes chan")
  179. }
  180. }
  181. type leaseCh struct {
  182. lid clientv3.LeaseID
  183. ch <-chan *clientv3.LeaseKeepAliveResponse
  184. }
  185. // TestLeaseKeepAliveNotFound ensures a revoked lease won't halt other leases.
  186. func TestLeaseKeepAliveNotFound(t *testing.T) {
  187. defer testutil.AfterTest(t)
  188. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  189. defer clus.Terminate(t)
  190. cli := clus.RandClient()
  191. lchs := []leaseCh{}
  192. for i := 0; i < 3; i++ {
  193. resp, rerr := cli.Grant(context.TODO(), 5)
  194. if rerr != nil {
  195. t.Fatal(rerr)
  196. }
  197. kach, kaerr := cli.KeepAlive(context.Background(), resp.ID)
  198. if kaerr != nil {
  199. t.Fatal(kaerr)
  200. }
  201. lchs = append(lchs, leaseCh{resp.ID, kach})
  202. }
  203. if _, err := cli.Revoke(context.TODO(), lchs[1].lid); err != nil {
  204. t.Fatal(err)
  205. }
  206. <-lchs[0].ch
  207. if _, ok := <-lchs[0].ch; !ok {
  208. t.Fatalf("closed keepalive on wrong lease")
  209. }
  210. timec := time.After(5 * time.Second)
  211. for range lchs[1].ch {
  212. select {
  213. case <-timec:
  214. t.Fatalf("revoke did not close keep alive")
  215. default:
  216. }
  217. }
  218. }
  219. func TestLeaseGrantErrConnClosed(t *testing.T) {
  220. defer testutil.AfterTest(t)
  221. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  222. defer clus.Terminate(t)
  223. cli := clus.Client(0)
  224. clus.TakeClient(0)
  225. donec := make(chan struct{})
  226. go func() {
  227. defer close(donec)
  228. _, err := cli.Grant(context.TODO(), 5)
  229. if err != nil && err != grpc.ErrClientConnClosing && err != context.Canceled {
  230. // grpc.ErrClientConnClosing if grpc-go balancer calls 'Get' after client.Close.
  231. // context.Canceled if grpc-go balancer calls 'Get' with an inflight client.Close.
  232. t.Fatalf("expected %v or %v, got %v", grpc.ErrClientConnClosing, context.Canceled, err)
  233. }
  234. }()
  235. if err := cli.Close(); err != nil {
  236. t.Fatal(err)
  237. }
  238. select {
  239. case <-time.After(integration.RequestWaitTimeout):
  240. t.Fatal("le.Grant took too long")
  241. case <-donec:
  242. }
  243. }
  244. // TestLeaseKeepAliveFullResponseQueue ensures when response
  245. // queue is full thus dropping keepalive response sends,
  246. // keepalive request is sent with the same rate of TTL / 3.
  247. func TestLeaseKeepAliveFullResponseQueue(t *testing.T) {
  248. defer testutil.AfterTest(t)
  249. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  250. defer clus.Terminate(t)
  251. lapi := clus.Client(0)
  252. // expect lease keepalive every 10-second
  253. lresp, err := lapi.Grant(context.Background(), 30)
  254. if err != nil {
  255. t.Fatalf("failed to create lease %v", err)
  256. }
  257. id := lresp.ID
  258. old := clientv3.LeaseResponseChSize
  259. defer func() {
  260. clientv3.LeaseResponseChSize = old
  261. }()
  262. clientv3.LeaseResponseChSize = 0
  263. // never fetch from response queue, and let it become full
  264. _, err = lapi.KeepAlive(context.Background(), id)
  265. if err != nil {
  266. t.Fatalf("failed to keepalive lease %v", err)
  267. }
  268. // TTL should not be refreshed after 3 seconds
  269. // expect keepalive to be triggered after TTL/3
  270. time.Sleep(3 * time.Second)
  271. tr, terr := lapi.TimeToLive(context.Background(), id)
  272. if terr != nil {
  273. t.Fatalf("failed to get lease information %v", terr)
  274. }
  275. if tr.TTL >= 29 {
  276. t.Errorf("unexpected kept-alive lease TTL %d", tr.TTL)
  277. }
  278. }
  279. func TestLeaseGrantNewAfterClose(t *testing.T) {
  280. defer testutil.AfterTest(t)
  281. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  282. defer clus.Terminate(t)
  283. cli := clus.Client(0)
  284. clus.TakeClient(0)
  285. if err := cli.Close(); err != nil {
  286. t.Fatal(err)
  287. }
  288. donec := make(chan struct{})
  289. go func() {
  290. if _, err := cli.Grant(context.TODO(), 5); err != context.Canceled && err != grpc.ErrClientConnClosing {
  291. t.Fatalf("expected %v or %v, got %v", err != context.Canceled, grpc.ErrClientConnClosing, err)
  292. }
  293. close(donec)
  294. }()
  295. select {
  296. case <-time.After(integration.RequestWaitTimeout):
  297. t.Fatal("le.Grant took too long")
  298. case <-donec:
  299. }
  300. }
  301. func TestLeaseRevokeNewAfterClose(t *testing.T) {
  302. defer testutil.AfterTest(t)
  303. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  304. defer clus.Terminate(t)
  305. cli := clus.Client(0)
  306. resp, err := cli.Grant(context.TODO(), 5)
  307. if err != nil {
  308. t.Fatal(err)
  309. }
  310. leaseID := resp.ID
  311. clus.TakeClient(0)
  312. if err := cli.Close(); err != nil {
  313. t.Fatal(err)
  314. }
  315. donec := make(chan struct{})
  316. go func() {
  317. if _, err := cli.Revoke(context.TODO(), leaseID); err != context.Canceled && err != grpc.ErrClientConnClosing {
  318. t.Fatalf("expected %v or %v, got %v", err != context.Canceled, grpc.ErrClientConnClosing, err)
  319. }
  320. close(donec)
  321. }()
  322. select {
  323. case <-time.After(integration.RequestWaitTimeout):
  324. t.Fatal("le.Revoke took too long")
  325. case <-donec:
  326. }
  327. }
  328. // TestLeaseKeepAliveCloseAfterDisconnectRevoke ensures the keep alive channel is closed
  329. // following a disconnection, lease revoke, then reconnect.
  330. func TestLeaseKeepAliveCloseAfterDisconnectRevoke(t *testing.T) {
  331. defer testutil.AfterTest(t)
  332. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  333. defer clus.Terminate(t)
  334. cli := clus.Client(0)
  335. // setup lease and do a keepalive
  336. resp, err := cli.Grant(context.Background(), 10)
  337. if err != nil {
  338. t.Fatal(err)
  339. }
  340. rc, kerr := cli.KeepAlive(context.Background(), resp.ID)
  341. if kerr != nil {
  342. t.Fatal(kerr)
  343. }
  344. kresp := <-rc
  345. if kresp.ID != resp.ID {
  346. t.Fatalf("ID = %x, want %x", kresp.ID, resp.ID)
  347. }
  348. // keep client disconnected
  349. clus.Members[0].Stop(t)
  350. time.Sleep(time.Second)
  351. clus.WaitLeader(t)
  352. if _, err := clus.Client(1).Revoke(context.TODO(), resp.ID); err != nil {
  353. t.Fatal(err)
  354. }
  355. clus.Members[0].Restart(t)
  356. // some responses may still be buffered; drain until close
  357. timer := time.After(time.Duration(kresp.TTL) * time.Second)
  358. for kresp != nil {
  359. select {
  360. case kresp = <-rc:
  361. case <-timer:
  362. t.Fatalf("keepalive channel did not close")
  363. }
  364. }
  365. }
  366. // TestLeaseKeepAliveInitTimeout ensures the keep alive channel closes if
  367. // the initial keep alive request never gets a response.
  368. func TestLeaseKeepAliveInitTimeout(t *testing.T) {
  369. defer testutil.AfterTest(t)
  370. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  371. defer clus.Terminate(t)
  372. cli := clus.Client(0)
  373. // setup lease and do a keepalive
  374. resp, err := cli.Grant(context.Background(), 5)
  375. if err != nil {
  376. t.Fatal(err)
  377. }
  378. // keep client disconnected
  379. clus.Members[0].Stop(t)
  380. rc, kerr := cli.KeepAlive(context.Background(), resp.ID)
  381. if kerr != nil {
  382. t.Fatal(kerr)
  383. }
  384. select {
  385. case ka, ok := <-rc:
  386. if ok {
  387. t.Fatalf("unexpected keepalive %v, expected closed channel", ka)
  388. }
  389. case <-time.After(10 * time.Second):
  390. t.Fatalf("keepalive channel did not close")
  391. }
  392. clus.Members[0].Restart(t)
  393. }
  394. // TestLeaseKeepAliveInitTimeout ensures the keep alive channel closes if
  395. // a keep alive request after the first never gets a response.
  396. func TestLeaseKeepAliveTTLTimeout(t *testing.T) {
  397. defer testutil.AfterTest(t)
  398. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  399. defer clus.Terminate(t)
  400. cli := clus.Client(0)
  401. // setup lease and do a keepalive
  402. resp, err := cli.Grant(context.Background(), 5)
  403. if err != nil {
  404. t.Fatal(err)
  405. }
  406. rc, kerr := cli.KeepAlive(context.Background(), resp.ID)
  407. if kerr != nil {
  408. t.Fatal(kerr)
  409. }
  410. if kresp := <-rc; kresp.ID != resp.ID {
  411. t.Fatalf("ID = %x, want %x", kresp.ID, resp.ID)
  412. }
  413. // keep client disconnected
  414. clus.Members[0].Stop(t)
  415. select {
  416. case ka, ok := <-rc:
  417. if ok {
  418. t.Fatalf("unexpected keepalive %v, expected closed channel", ka)
  419. }
  420. case <-time.After(10 * time.Second):
  421. t.Fatalf("keepalive channel did not close")
  422. }
  423. clus.Members[0].Restart(t)
  424. }
  425. func TestLeaseTimeToLive(t *testing.T) {
  426. defer testutil.AfterTest(t)
  427. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  428. defer clus.Terminate(t)
  429. c := clus.RandClient()
  430. lapi := c
  431. resp, err := lapi.Grant(context.Background(), 10)
  432. if err != nil {
  433. t.Errorf("failed to create lease %v", err)
  434. }
  435. kv := clus.RandClient()
  436. keys := []string{"foo1", "foo2"}
  437. for i := range keys {
  438. if _, err = kv.Put(context.TODO(), keys[i], "bar", clientv3.WithLease(resp.ID)); err != nil {
  439. t.Fatal(err)
  440. }
  441. }
  442. // linearized read to ensure Puts propagated to server backing lapi
  443. if _, err := c.Get(context.TODO(), "abc"); err != nil {
  444. t.Fatal(err)
  445. }
  446. lresp, lerr := lapi.TimeToLive(context.Background(), resp.ID, clientv3.WithAttachedKeys())
  447. if lerr != nil {
  448. t.Fatal(lerr)
  449. }
  450. if lresp.ID != resp.ID {
  451. t.Fatalf("leaseID expected %d, got %d", resp.ID, lresp.ID)
  452. }
  453. if lresp.GrantedTTL != int64(10) {
  454. t.Fatalf("GrantedTTL expected %d, got %d", 10, lresp.GrantedTTL)
  455. }
  456. if lresp.TTL == 0 || lresp.TTL > lresp.GrantedTTL {
  457. t.Fatalf("unexpected TTL %d (granted %d)", lresp.TTL, lresp.GrantedTTL)
  458. }
  459. ks := make([]string, len(lresp.Keys))
  460. for i := range lresp.Keys {
  461. ks[i] = string(lresp.Keys[i])
  462. }
  463. sort.Strings(ks)
  464. if !reflect.DeepEqual(ks, keys) {
  465. t.Fatalf("keys expected %v, got %v", keys, ks)
  466. }
  467. lresp, lerr = lapi.TimeToLive(context.Background(), resp.ID)
  468. if lerr != nil {
  469. t.Fatal(lerr)
  470. }
  471. if len(lresp.Keys) != 0 {
  472. t.Fatalf("unexpected keys %+v", lresp.Keys)
  473. }
  474. }
  475. func TestLeaseTimeToLiveLeaseNotFound(t *testing.T) {
  476. defer testutil.AfterTest(t)
  477. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  478. defer clus.Terminate(t)
  479. cli := clus.RandClient()
  480. resp, err := cli.Grant(context.Background(), 10)
  481. if err != nil {
  482. t.Errorf("failed to create lease %v", err)
  483. }
  484. _, err = cli.Revoke(context.Background(), resp.ID)
  485. if err != nil {
  486. t.Errorf("failed to Revoke lease %v", err)
  487. }
  488. lresp, err := cli.TimeToLive(context.Background(), resp.ID)
  489. // TimeToLive() should return a response with TTL=-1.
  490. if err != nil {
  491. t.Fatalf("expected err to be nil")
  492. }
  493. if lresp == nil {
  494. t.Fatalf("expected lresp not to be nil")
  495. }
  496. if lresp.ResponseHeader == nil {
  497. t.Fatalf("expected ResponseHeader not to be nil")
  498. }
  499. if lresp.ID != resp.ID {
  500. t.Fatalf("expected Lease ID %v, but got %v", resp.ID, lresp.ID)
  501. }
  502. if lresp.TTL != -1 {
  503. t.Fatalf("expected TTL %v, but got %v", lresp.TTL, lresp.TTL)
  504. }
  505. }
  506. func TestLeaseLeases(t *testing.T) {
  507. defer testutil.AfterTest(t)
  508. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  509. defer clus.Terminate(t)
  510. cli := clus.RandClient()
  511. ids := []clientv3.LeaseID{}
  512. for i := 0; i < 5; i++ {
  513. resp, err := cli.Grant(context.Background(), 10)
  514. if err != nil {
  515. t.Errorf("failed to create lease %v", err)
  516. }
  517. ids = append(ids, resp.ID)
  518. }
  519. resp, err := cli.Leases(context.Background())
  520. if err != nil {
  521. t.Fatal(err)
  522. }
  523. if len(resp.Leases) != 5 {
  524. t.Fatalf("len(resp.Leases) expected 5, got %d", len(resp.Leases))
  525. }
  526. for i := range resp.Leases {
  527. if ids[i] != resp.Leases[i].ID {
  528. t.Fatalf("#%d: lease ID expected %d, got %d", i, ids[i], resp.Leases[i].ID)
  529. }
  530. }
  531. }
  532. // TestLeaseRenewLostQuorum ensures keepalives work after losing quorum
  533. // for a while.
  534. func TestLeaseRenewLostQuorum(t *testing.T) {
  535. defer testutil.AfterTest(t)
  536. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  537. defer clus.Terminate(t)
  538. cli := clus.Client(0)
  539. r, err := cli.Grant(context.TODO(), 4)
  540. if err != nil {
  541. t.Fatal(err)
  542. }
  543. kctx, kcancel := context.WithCancel(context.Background())
  544. defer kcancel()
  545. ka, err := cli.KeepAlive(kctx, r.ID)
  546. if err != nil {
  547. t.Fatal(err)
  548. }
  549. // consume first keepalive so next message sends when cluster is down
  550. <-ka
  551. lastKa := time.Now()
  552. // force keepalive stream message to timeout
  553. clus.Members[1].Stop(t)
  554. clus.Members[2].Stop(t)
  555. // Use TTL-2 since the client closes the keepalive channel if no
  556. // keepalive arrives before the lease deadline; the client will
  557. // try to resend a keepalive after TTL/3 seconds, so for a TTL of 4,
  558. // sleeping for 2s should be sufficient time for issuing a retry.
  559. // The cluster has two seconds to recover and reply to the keepalive.
  560. time.Sleep(time.Duration(r.TTL-2) * time.Second)
  561. clus.Members[1].Restart(t)
  562. clus.Members[2].Restart(t)
  563. if time.Since(lastKa) > time.Duration(r.TTL)*time.Second {
  564. t.Skip("waited too long for server stop and restart")
  565. }
  566. select {
  567. case _, ok := <-ka:
  568. if !ok {
  569. t.Fatalf("keepalive closed")
  570. }
  571. case <-time.After(time.Duration(r.TTL) * time.Second):
  572. t.Fatalf("timed out waiting for keepalive")
  573. }
  574. }
  575. func TestLeaseKeepAliveLoopExit(t *testing.T) {
  576. defer testutil.AfterTest(t)
  577. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  578. defer clus.Terminate(t)
  579. ctx := context.Background()
  580. cli := clus.Client(0)
  581. clus.TakeClient(0)
  582. resp, err := cli.Grant(ctx, 5)
  583. if err != nil {
  584. t.Fatal(err)
  585. }
  586. cli.Close()
  587. _, err = cli.KeepAlive(ctx, resp.ID)
  588. if _, ok := err.(clientv3.ErrKeepAliveHalted); !ok {
  589. t.Fatalf("expected %T, got %v(%T)", clientv3.ErrKeepAliveHalted{}, err, err)
  590. }
  591. }
  592. // TestV3LeaseFailureOverlap issues Grant and KeepAlive requests to a cluster
  593. // before, during, and after quorum loss to confirm Grant/KeepAlive tolerates
  594. // transient cluster failure.
  595. func TestV3LeaseFailureOverlap(t *testing.T) {
  596. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
  597. defer clus.Terminate(t)
  598. numReqs := 5
  599. cli := clus.Client(0)
  600. // bring up a session, tear it down
  601. updown := func(i int) error {
  602. sess, err := concurrency.NewSession(cli)
  603. if err != nil {
  604. return err
  605. }
  606. ch := make(chan struct{})
  607. go func() {
  608. defer close(ch)
  609. sess.Close()
  610. }()
  611. select {
  612. case <-ch:
  613. case <-time.After(time.Minute / 4):
  614. t.Fatalf("timeout %d", i)
  615. }
  616. return nil
  617. }
  618. var wg sync.WaitGroup
  619. mkReqs := func(n int) {
  620. wg.Add(numReqs)
  621. for i := 0; i < numReqs; i++ {
  622. go func() {
  623. defer wg.Done()
  624. err := updown(n)
  625. if err == nil || err == rpctypes.ErrTimeoutDueToConnectionLost {
  626. return
  627. }
  628. t.Fatal(err)
  629. }()
  630. }
  631. }
  632. mkReqs(1)
  633. clus.Members[1].Stop(t)
  634. mkReqs(2)
  635. time.Sleep(time.Second)
  636. mkReqs(3)
  637. clus.Members[1].Restart(t)
  638. mkReqs(4)
  639. wg.Wait()
  640. }
  641. // TestLeaseWithRequireLeader checks keep-alive channel close when no leader.
  642. func TestLeaseWithRequireLeader(t *testing.T) {
  643. defer testutil.AfterTest(t)
  644. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
  645. defer clus.Terminate(t)
  646. c := clus.Client(0)
  647. lid1, err1 := c.Grant(context.TODO(), 60)
  648. if err1 != nil {
  649. t.Fatal(err1)
  650. }
  651. lid2, err2 := c.Grant(context.TODO(), 60)
  652. if err2 != nil {
  653. t.Fatal(err2)
  654. }
  655. // kaReqLeader close if the leader is lost
  656. kaReqLeader, kerr1 := c.KeepAlive(clientv3.WithRequireLeader(context.TODO()), lid1.ID)
  657. if kerr1 != nil {
  658. t.Fatal(kerr1)
  659. }
  660. // kaWait will wait even if the leader is lost
  661. kaWait, kerr2 := c.KeepAlive(context.TODO(), lid2.ID)
  662. if kerr2 != nil {
  663. t.Fatal(kerr2)
  664. }
  665. select {
  666. case <-kaReqLeader:
  667. case <-time.After(5 * time.Second):
  668. t.Fatalf("require leader first keep-alive timed out")
  669. }
  670. select {
  671. case <-kaWait:
  672. case <-time.After(5 * time.Second):
  673. t.Fatalf("leader not required first keep-alive timed out")
  674. }
  675. clus.Members[1].Stop(t)
  676. // kaReqLeader may issue multiple requests while waiting for the first
  677. // response from proxy server; drain any stray keepalive responses
  678. time.Sleep(100 * time.Millisecond)
  679. for len(kaReqLeader) > 0 {
  680. <-kaReqLeader
  681. }
  682. select {
  683. case resp, ok := <-kaReqLeader:
  684. if ok {
  685. t.Fatalf("expected closed require leader, got response %+v", resp)
  686. }
  687. case <-time.After(5 * time.Second):
  688. t.Fatal("keepalive with require leader took too long to close")
  689. }
  690. select {
  691. case _, ok := <-kaWait:
  692. if !ok {
  693. t.Fatalf("got closed channel with no require leader, expected non-closed")
  694. }
  695. case <-time.After(10 * time.Millisecond):
  696. // wait some to detect any closes happening soon after kaReqLeader closing
  697. }
  698. }