kv_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. // Copyright 2016 CoreOS, Inc.
  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. "bytes"
  17. "reflect"
  18. "strings"
  19. "testing"
  20. "time"
  21. "github.com/coreos/etcd/clientv3"
  22. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  23. "github.com/coreos/etcd/integration"
  24. "github.com/coreos/etcd/mvcc/mvccpb"
  25. "github.com/coreos/etcd/pkg/testutil"
  26. "golang.org/x/net/context"
  27. )
  28. func TestKVPutError(t *testing.T) {
  29. defer testutil.AfterTest(t)
  30. var (
  31. maxReqBytes = 1.5 * 1024 * 1024
  32. quota = int64(maxReqBytes * 1.2)
  33. )
  34. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1, QuotaBackendBytes: quota})
  35. defer clus.Terminate(t)
  36. kv := clientv3.NewKV(clus.RandClient())
  37. ctx := context.TODO()
  38. _, err := kv.Put(ctx, "", "bar")
  39. if err != rpctypes.ErrEmptyKey {
  40. t.Fatalf("expected %v, got %v", rpctypes.ErrEmptyKey, err)
  41. }
  42. _, err = kv.Put(ctx, "key", strings.Repeat("a", int(maxReqBytes+100))) // 1.5MB
  43. if err != rpctypes.ErrRequestTooLarge {
  44. t.Fatalf("expected %v, got %v", rpctypes.ErrRequestTooLarge, err)
  45. }
  46. _, err = kv.Put(ctx, "foo1", strings.Repeat("a", int(maxReqBytes-50)))
  47. if err != nil { // below quota
  48. t.Fatal(err)
  49. }
  50. time.Sleep(500 * time.Millisecond) // give enough time for commit
  51. _, err = kv.Put(ctx, "foo2", strings.Repeat("a", int(maxReqBytes-50)))
  52. if err != rpctypes.ErrNoSpace { // over quota
  53. t.Fatalf("expected %v, got %v", rpctypes.ErrNoSpace, err)
  54. }
  55. }
  56. func TestKVPut(t *testing.T) {
  57. defer testutil.AfterTest(t)
  58. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  59. defer clus.Terminate(t)
  60. lapi := clientv3.NewLease(clus.RandClient())
  61. defer lapi.Close()
  62. kv := clientv3.NewKV(clus.RandClient())
  63. ctx := context.TODO()
  64. resp, err := lapi.Grant(context.Background(), 10)
  65. if err != nil {
  66. t.Fatalf("failed to create lease %v", err)
  67. }
  68. tests := []struct {
  69. key, val string
  70. leaseID clientv3.LeaseID
  71. }{
  72. {"foo", "bar", clientv3.NoLease},
  73. {"hello", "world", resp.ID},
  74. }
  75. for i, tt := range tests {
  76. if _, err := kv.Put(ctx, tt.key, tt.val, clientv3.WithLease(tt.leaseID)); err != nil {
  77. t.Fatalf("#%d: couldn't put %q (%v)", i, tt.key, err)
  78. }
  79. resp, err := kv.Get(ctx, tt.key)
  80. if err != nil {
  81. t.Fatalf("#%d: couldn't get key (%v)", i, err)
  82. }
  83. if len(resp.Kvs) != 1 {
  84. t.Fatalf("#%d: expected 1 key, got %d", i, len(resp.Kvs))
  85. }
  86. if !bytes.Equal([]byte(tt.val), resp.Kvs[0].Value) {
  87. t.Errorf("#%d: val = %s, want %s", i, tt.val, resp.Kvs[0].Value)
  88. }
  89. if tt.leaseID != clientv3.LeaseID(resp.Kvs[0].Lease) {
  90. t.Errorf("#%d: val = %d, want %d", i, tt.leaseID, resp.Kvs[0].Lease)
  91. }
  92. }
  93. }
  94. func TestKVRange(t *testing.T) {
  95. defer testutil.AfterTest(t)
  96. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  97. defer clus.Terminate(t)
  98. kv := clientv3.NewKV(clus.RandClient())
  99. ctx := context.TODO()
  100. keySet := []string{"a", "b", "c", "c", "c", "foo", "foo/abc", "fop"}
  101. for i, key := range keySet {
  102. if _, err := kv.Put(ctx, key, ""); err != nil {
  103. t.Fatalf("#%d: couldn't put %q (%v)", i, key, err)
  104. }
  105. }
  106. resp, err := kv.Get(ctx, keySet[0])
  107. if err != nil {
  108. t.Fatalf("couldn't get key (%v)", err)
  109. }
  110. wheader := resp.Header
  111. tests := []struct {
  112. begin, end string
  113. rev int64
  114. opts []clientv3.OpOption
  115. wantSet []*mvccpb.KeyValue
  116. }{
  117. // range first two
  118. {
  119. "a", "c",
  120. 0,
  121. nil,
  122. []*mvccpb.KeyValue{
  123. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  124. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  125. },
  126. },
  127. // range first two with serializable
  128. {
  129. "a", "c",
  130. 0,
  131. []clientv3.OpOption{clientv3.WithSerializable()},
  132. []*mvccpb.KeyValue{
  133. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  134. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  135. },
  136. },
  137. // range all with rev
  138. {
  139. "a", "x",
  140. 2,
  141. nil,
  142. []*mvccpb.KeyValue{
  143. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  144. },
  145. },
  146. // range all with SortByKey, SortAscend
  147. {
  148. "a", "x",
  149. 0,
  150. []clientv3.OpOption{clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend)},
  151. []*mvccpb.KeyValue{
  152. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  153. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  154. {Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
  155. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  156. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  157. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  158. },
  159. },
  160. // range all with SortByCreateRevision, SortDescend
  161. {
  162. "a", "x",
  163. 0,
  164. []clientv3.OpOption{clientv3.WithSort(clientv3.SortByCreateRevision, clientv3.SortDescend)},
  165. []*mvccpb.KeyValue{
  166. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  167. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  168. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  169. {Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
  170. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  171. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  172. },
  173. },
  174. // range all with SortByModRevision, SortDescend
  175. {
  176. "a", "x",
  177. 0,
  178. []clientv3.OpOption{clientv3.WithSort(clientv3.SortByModRevision, clientv3.SortDescend)},
  179. []*mvccpb.KeyValue{
  180. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  181. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  182. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  183. {Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
  184. {Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
  185. {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
  186. },
  187. },
  188. // WithPrefix
  189. {
  190. "foo", "",
  191. 0,
  192. []clientv3.OpOption{clientv3.WithPrefix()},
  193. []*mvccpb.KeyValue{
  194. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  195. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  196. },
  197. },
  198. // WithFromKey
  199. {
  200. "fo", "",
  201. 0,
  202. []clientv3.OpOption{clientv3.WithFromKey()},
  203. []*mvccpb.KeyValue{
  204. {Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
  205. {Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
  206. {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
  207. },
  208. },
  209. }
  210. for i, tt := range tests {
  211. opts := []clientv3.OpOption{clientv3.WithRange(tt.end), clientv3.WithRev(tt.rev)}
  212. opts = append(opts, tt.opts...)
  213. resp, err := kv.Get(ctx, tt.begin, opts...)
  214. if err != nil {
  215. t.Fatalf("#%d: couldn't range (%v)", i, err)
  216. }
  217. if !reflect.DeepEqual(wheader, resp.Header) {
  218. t.Fatalf("#%d: wheader expected %+v, got %+v", i, wheader, resp.Header)
  219. }
  220. if !reflect.DeepEqual(tt.wantSet, resp.Kvs) {
  221. t.Fatalf("#%d: resp.Kvs expected %+v, got %+v", i, tt.wantSet, resp.Kvs)
  222. }
  223. }
  224. }
  225. func TestKVDeleteRange(t *testing.T) {
  226. defer testutil.AfterTest(t)
  227. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  228. defer clus.Terminate(t)
  229. kv := clientv3.NewKV(clus.RandClient())
  230. ctx := context.TODO()
  231. tests := []struct {
  232. key string
  233. opts []clientv3.OpOption
  234. wkeys []string
  235. }{
  236. // [a, c)
  237. {
  238. key: "a",
  239. opts: []clientv3.OpOption{clientv3.WithRange("c")},
  240. wkeys: []string{"c", "c/abc", "d"},
  241. },
  242. // >= c
  243. {
  244. key: "c",
  245. opts: []clientv3.OpOption{clientv3.WithFromKey()},
  246. wkeys: []string{"a", "b"},
  247. },
  248. // c*
  249. {
  250. key: "c",
  251. opts: []clientv3.OpOption{clientv3.WithPrefix()},
  252. wkeys: []string{"a", "b", "d"},
  253. },
  254. // *
  255. {
  256. key: "\x00",
  257. opts: []clientv3.OpOption{clientv3.WithFromKey()},
  258. wkeys: []string{},
  259. },
  260. }
  261. for i, tt := range tests {
  262. keySet := []string{"a", "b", "c", "c/abc", "d"}
  263. for j, key := range keySet {
  264. if _, err := kv.Put(ctx, key, ""); err != nil {
  265. t.Fatalf("#%d: couldn't put %q (%v)", j, key, err)
  266. }
  267. }
  268. _, err := kv.Delete(ctx, tt.key, tt.opts...)
  269. if err != nil {
  270. t.Fatalf("#%d: couldn't delete range (%v)", i, err)
  271. }
  272. resp, err := kv.Get(ctx, "a", clientv3.WithFromKey())
  273. if err != nil {
  274. t.Fatalf("#%d: couldn't get keys (%v)", i, err)
  275. }
  276. keys := []string{}
  277. for _, kv := range resp.Kvs {
  278. keys = append(keys, string(kv.Key))
  279. }
  280. if !reflect.DeepEqual(tt.wkeys, keys) {
  281. t.Errorf("#%d: resp.Kvs got %v, expected %v", i, keys, tt.wkeys)
  282. }
  283. }
  284. }
  285. func TestKVDelete(t *testing.T) {
  286. defer testutil.AfterTest(t)
  287. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  288. defer clus.Terminate(t)
  289. kv := clientv3.NewKV(clus.RandClient())
  290. ctx := context.TODO()
  291. presp, err := kv.Put(ctx, "foo", "")
  292. if err != nil {
  293. t.Fatalf("couldn't put 'foo' (%v)", err)
  294. }
  295. if presp.Header.Revision != 2 {
  296. t.Fatalf("presp.Header.Revision got %d, want %d", presp.Header.Revision, 2)
  297. }
  298. resp, err := kv.Delete(ctx, "foo")
  299. if err != nil {
  300. t.Fatalf("couldn't delete key (%v)", err)
  301. }
  302. if resp.Header.Revision != 3 {
  303. t.Fatalf("resp.Header.Revision got %d, want %d", resp.Header.Revision, 3)
  304. }
  305. gresp, err := kv.Get(ctx, "foo")
  306. if err != nil {
  307. t.Fatalf("couldn't get key (%v)", err)
  308. }
  309. if len(gresp.Kvs) > 0 {
  310. t.Fatalf("gresp.Kvs got %+v, want none", gresp.Kvs)
  311. }
  312. }
  313. func TestKVCompactError(t *testing.T) {
  314. defer testutil.AfterTest(t)
  315. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  316. defer clus.Terminate(t)
  317. kv := clientv3.NewKV(clus.RandClient())
  318. ctx := context.TODO()
  319. for i := 0; i < 5; i++ {
  320. if _, err := kv.Put(ctx, "foo", "bar"); err != nil {
  321. t.Fatalf("couldn't put 'foo' (%v)", err)
  322. }
  323. }
  324. err := kv.Compact(ctx, 6)
  325. if err != nil {
  326. t.Fatalf("couldn't compact 6 (%v)", err)
  327. }
  328. err = kv.Compact(ctx, 6)
  329. if err != rpctypes.ErrCompacted {
  330. t.Fatalf("expected %v, got %v", rpctypes.ErrCompacted, err)
  331. }
  332. err = kv.Compact(ctx, 100)
  333. if err != rpctypes.ErrFutureRev {
  334. t.Fatalf("expected %v, got %v", rpctypes.ErrFutureRev, err)
  335. }
  336. }
  337. func TestKVCompact(t *testing.T) {
  338. defer testutil.AfterTest(t)
  339. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  340. defer clus.Terminate(t)
  341. kv := clientv3.NewKV(clus.RandClient())
  342. ctx := context.TODO()
  343. for i := 0; i < 10; i++ {
  344. if _, err := kv.Put(ctx, "foo", "bar"); err != nil {
  345. t.Fatalf("couldn't put 'foo' (%v)", err)
  346. }
  347. }
  348. err := kv.Compact(ctx, 7)
  349. if err != nil {
  350. t.Fatalf("couldn't compact kv space (%v)", err)
  351. }
  352. err = kv.Compact(ctx, 7)
  353. if err == nil || err != rpctypes.ErrCompacted {
  354. t.Fatalf("error got %v, want %v", err, rpctypes.ErrFutureRev)
  355. }
  356. wcli := clus.RandClient()
  357. // new watcher could precede receiving the compaction without quorum first
  358. wcli.Get(ctx, "quorum-get")
  359. wc := clientv3.NewWatcher(wcli)
  360. defer wc.Close()
  361. wchan := wc.Watch(ctx, "foo", clientv3.WithRev(3))
  362. if wr := <-wchan; wr.CompactRevision != 7 {
  363. t.Fatalf("wchan CompactRevision got %v, want 7", wr.CompactRevision)
  364. }
  365. if wr, ok := <-wchan; ok {
  366. t.Fatalf("wchan got %v, expected closed", wr)
  367. }
  368. err = kv.Compact(ctx, 1000)
  369. if err == nil || err != rpctypes.ErrFutureRev {
  370. t.Fatalf("error got %v, want %v", err, rpctypes.ErrFutureRev)
  371. }
  372. }
  373. // TestKVGetRetry ensures get will retry on disconnect.
  374. func TestKVGetRetry(t *testing.T) {
  375. defer testutil.AfterTest(t)
  376. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  377. defer clus.Terminate(t)
  378. kv := clientv3.NewKV(clus.Client(0))
  379. ctx := context.TODO()
  380. if _, err := kv.Put(ctx, "foo", "bar"); err != nil {
  381. t.Fatal(err)
  382. }
  383. clus.Members[0].Stop(t)
  384. <-clus.Members[0].StopNotify()
  385. donec := make(chan struct{})
  386. go func() {
  387. // Get will fail, but reconnect will trigger
  388. gresp, gerr := kv.Get(ctx, "foo")
  389. if gerr != nil {
  390. t.Fatal(gerr)
  391. }
  392. wkvs := []*mvccpb.KeyValue{
  393. {
  394. Key: []byte("foo"),
  395. Value: []byte("bar"),
  396. CreateRevision: 2,
  397. ModRevision: 2,
  398. Version: 1,
  399. },
  400. }
  401. if !reflect.DeepEqual(gresp.Kvs, wkvs) {
  402. t.Fatalf("bad get: got %v, want %v", gresp.Kvs, wkvs)
  403. }
  404. donec <- struct{}{}
  405. }()
  406. time.Sleep(100 * time.Millisecond)
  407. clus.Members[0].Restart(t)
  408. select {
  409. case <-time.After(5 * time.Second):
  410. t.Fatalf("timed out waiting for get")
  411. case <-donec:
  412. }
  413. }
  414. // TestKVPutFailGetRetry ensures a get will retry following a failed put.
  415. func TestKVPutFailGetRetry(t *testing.T) {
  416. defer testutil.AfterTest(t)
  417. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  418. defer clus.Terminate(t)
  419. kv := clientv3.NewKV(clus.Client(0))
  420. ctx := context.TODO()
  421. clus.Members[0].Stop(t)
  422. <-clus.Members[0].StopNotify()
  423. _, err := kv.Put(ctx, "foo", "bar")
  424. if err == nil {
  425. t.Fatalf("got success on disconnected put, wanted error")
  426. }
  427. donec := make(chan struct{})
  428. go func() {
  429. // Get will fail, but reconnect will trigger
  430. gresp, gerr := kv.Get(ctx, "foo")
  431. if gerr != nil {
  432. t.Fatal(gerr)
  433. }
  434. if len(gresp.Kvs) != 0 {
  435. t.Fatalf("bad get kvs: got %+v, want empty", gresp.Kvs)
  436. }
  437. donec <- struct{}{}
  438. }()
  439. time.Sleep(100 * time.Millisecond)
  440. clus.Members[0].Restart(t)
  441. select {
  442. case <-time.After(5 * time.Second):
  443. t.Fatalf("timed out waiting for get")
  444. case <-donec:
  445. }
  446. }
  447. // TestKVGetCancel tests that a context cancel on a Get terminates as expected.
  448. func TestKVGetCancel(t *testing.T) {
  449. defer testutil.AfterTest(t)
  450. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  451. defer clus.Terminate(t)
  452. oldconn := clus.Client(0).ActiveConnection()
  453. kv := clientv3.NewKV(clus.Client(0))
  454. ctx, cancel := context.WithCancel(context.TODO())
  455. cancel()
  456. resp, err := kv.Get(ctx, "abc")
  457. if err == nil {
  458. t.Fatalf("cancel on get response %v, expected context error", resp)
  459. }
  460. newconn := clus.Client(0).ActiveConnection()
  461. if oldconn != newconn {
  462. t.Fatalf("cancel on get broke client connection")
  463. }
  464. }