lease_test.go 21 KB

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