123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022 |
- // Copyright 2017 The etcd Authors
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package integration
- import (
- "context"
- "fmt"
- "math/rand"
- "reflect"
- "sync"
- "testing"
- "time"
- "github.com/coreos/etcd/clientv3"
- "github.com/coreos/etcd/clientv3/concurrency"
- "github.com/coreos/etcd/clientv3/leasing"
- "github.com/coreos/etcd/integration"
- "github.com/coreos/etcd/pkg/testutil"
- )
- func TestLeasingPutGet(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
- defer clus.Terminate(t)
- lKV1, closeLKV1, err := leasing.NewKV(clus.Client(0), "foo/")
- testutil.AssertNil(t, err)
- defer closeLKV1()
- lKV2, closeLKV2, err := leasing.NewKV(clus.Client(1), "foo/")
- testutil.AssertNil(t, err)
- defer closeLKV2()
- lKV3, closeLKV3, err := leasing.NewKV(clus.Client(2), "foo/")
- testutil.AssertNil(t, err)
- defer closeLKV3()
- resp, err := lKV1.Get(context.TODO(), "abc")
- if err != nil {
- t.Fatal(err)
- }
- if len(resp.Kvs) != 0 {
- t.Errorf("expected nil, got %q", resp.Kvs[0].Key)
- }
- if _, err = lKV1.Put(context.TODO(), "abc", "def"); err != nil {
- t.Fatal(err)
- }
- if resp, err = lKV2.Get(context.TODO(), "abc"); err != nil {
- t.Fatal(err)
- }
- if string(resp.Kvs[0].Key) != "abc" {
- t.Errorf("expected key=%q, got key=%q", "abc", resp.Kvs[0].Key)
- }
- if string(resp.Kvs[0].Value) != "def" {
- t.Errorf("expected value=%q, got value=%q", "bar", resp.Kvs[0].Value)
- }
- if _, err = lKV3.Get(context.TODO(), "abc"); err != nil {
- t.Fatal(err)
- }
- if _, err = lKV2.Put(context.TODO(), "abc", "ghi"); err != nil {
- t.Fatal(err)
- }
- if resp, err = lKV3.Get(context.TODO(), "abc"); err != nil {
- t.Fatal(err)
- }
- if string(resp.Kvs[0].Key) != "abc" {
- t.Errorf("expected key=%q, got key=%q", "abc", resp.Kvs[0].Key)
- }
- if string(resp.Kvs[0].Value) != "ghi" {
- t.Errorf("expected value=%q, got value=%q", "bar", resp.Kvs[0].Value)
- }
- }
- // TestLeasingInterval checks the leasing KV fetches key intervals.
- func TestLeasingInterval(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- keys := []string{"abc/a", "abc/b", "abc/a/a"}
- for _, k := range keys {
- if _, err = clus.Client(0).Put(context.TODO(), k, "v"); err != nil {
- t.Fatal(err)
- }
- }
- resp, err := lkv.Get(context.TODO(), "abc/", clientv3.WithPrefix())
- if err != nil {
- t.Fatal(err)
- }
- if len(resp.Kvs) != 3 {
- t.Fatalf("expected keys %+v, got response keys %+v", keys, resp.Kvs)
- }
- // load into cache
- if resp, err = lkv.Get(context.TODO(), "abc/a"); err != nil {
- t.Fatal(err)
- }
- // get when prefix is also a cached key
- if resp, err = lkv.Get(context.TODO(), "abc/a", clientv3.WithPrefix()); err != nil {
- t.Fatal(err)
- }
- if len(resp.Kvs) != 2 {
- t.Fatalf("expected keys %+v, got response keys %+v", keys, resp.Kvs)
- }
- }
- // TestLeasingPutInvalidateNew checks the leasing KV updates its cache on a Put to a new key.
- func TestLeasingPutInvalidateNew(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = lkv.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- if _, err = lkv.Put(context.TODO(), "k", "v"); err != nil {
- t.Fatal(err)
- }
- lkvResp, err := lkv.Get(context.TODO(), "k")
- if err != nil {
- t.Fatal(err)
- }
- cResp, cerr := clus.Client(0).Get(context.TODO(), "k")
- if cerr != nil {
- t.Fatal(cerr)
- }
- if !reflect.DeepEqual(lkvResp, cResp) {
- t.Fatalf(`expected %+v, got response %+v`, cResp, lkvResp)
- }
- }
- // TestLeasingPutInvalidateExisting checks the leasing KV updates its cache on a Put to an existing key.
- func TestLeasingPutInvalidateExisting(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- if _, err := clus.Client(0).Put(context.TODO(), "k", "abc"); err != nil {
- t.Fatal(err)
- }
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = lkv.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- if _, err = lkv.Put(context.TODO(), "k", "v"); err != nil {
- t.Fatal(err)
- }
- lkvResp, err := lkv.Get(context.TODO(), "k")
- if err != nil {
- t.Fatal(err)
- }
- cResp, cerr := clus.Client(0).Get(context.TODO(), "k")
- if cerr != nil {
- t.Fatal(cerr)
- }
- if !reflect.DeepEqual(lkvResp, cResp) {
- t.Fatalf(`expected %+v, got response %+v`, cResp, lkvResp)
- }
- }
- // TestLeasingGetNoLeaseTTL checks a key with a TTL is not leased.
- func TestLeasingGetNoLeaseTTL(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- lresp, err := clus.Client(0).Grant(context.TODO(), 60)
- testutil.AssertNil(t, err)
- _, err = clus.Client(0).Put(context.TODO(), "k", "v", clientv3.WithLease(lresp.ID))
- testutil.AssertNil(t, err)
- gresp, err := lkv.Get(context.TODO(), "k")
- testutil.AssertNil(t, err)
- testutil.AssertEqual(t, len(gresp.Kvs), 1)
- clus.Members[0].Stop(t)
- ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
- _, err = lkv.Get(ctx, "k")
- cancel()
- testutil.AssertEqual(t, err, ctx.Err())
- }
- // TestLeasingGetSerializable checks the leasing KV can make serialized requests
- // when the etcd cluster is partitioned.
- func TestLeasingGetSerializable(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = clus.Client(0).Put(context.TODO(), "cached", "abc"); err != nil {
- t.Fatal(err)
- }
- if _, err = lkv.Get(context.TODO(), "cached"); err != nil {
- t.Fatal(err)
- }
- clus.Members[1].Stop(t)
- // don't necessarily try to acquire leasing key ownership for new key
- resp, err := lkv.Get(context.TODO(), "uncached", clientv3.WithSerializable())
- if err != nil {
- t.Fatal(err)
- }
- if len(resp.Kvs) != 0 {
- t.Fatalf(`expected no keys, got response %+v`, resp)
- }
- clus.Members[0].Stop(t)
- // leasing key ownership should have "cached" locally served
- cachedResp, err := lkv.Get(context.TODO(), "cached", clientv3.WithSerializable())
- if err != nil {
- t.Fatal(err)
- }
- if len(cachedResp.Kvs) != 1 || string(cachedResp.Kvs[0].Value) != "abc" {
- t.Fatalf(`expected "cached"->"abc", got response %+v`, cachedResp)
- }
- }
- // TestLeasingPrevKey checks the cache respects WithPrevKV on puts.
- func TestLeasingPrevKey(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 2})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = clus.Client(0).Put(context.TODO(), "k", "abc"); err != nil {
- t.Fatal(err)
- }
- // acquire leasing key
- if _, err = lkv.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- resp, err := lkv.Put(context.TODO(), "k", "def", clientv3.WithPrevKV())
- if err != nil {
- t.Fatal(err)
- }
- if resp.PrevKv == nil || string(resp.PrevKv.Value) != "abc" {
- t.Fatalf(`expected PrevKV.Value="abc", got response %+v`, resp)
- }
- }
- // TestLeasingRevGet checks the cache respects Get by Revision.
- func TestLeasingRevGet(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- putResp, err := clus.Client(0).Put(context.TODO(), "k", "abc")
- if err != nil {
- t.Fatal(err)
- }
- if _, err = clus.Client(0).Put(context.TODO(), "k", "def"); err != nil {
- t.Fatal(err)
- }
- // check historic revision
- getResp, gerr := lkv.Get(context.TODO(), "k", clientv3.WithRev(putResp.Header.Revision))
- if gerr != nil {
- t.Fatal(gerr)
- }
- if len(getResp.Kvs) != 1 || string(getResp.Kvs[0].Value) != "abc" {
- t.Fatalf(`expeted "k"->"abc" at rev=%d, got response %+v`, putResp.Header.Revision, getResp)
- }
- // check current revision
- getResp, gerr = lkv.Get(context.TODO(), "k")
- if gerr != nil {
- t.Fatal(gerr)
- }
- if len(getResp.Kvs) != 1 || string(getResp.Kvs[0].Value) != "def" {
- t.Fatalf(`expeted "k"->"abc" at rev=%d, got response %+v`, putResp.Header.Revision, getResp)
- }
- }
- // TestLeasingGetWithOpts checks options that can be served through the cache do not depend on the server.
- func TestLeasingGetWithOpts(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = clus.Client(0).Put(context.TODO(), "k", "abc"); err != nil {
- t.Fatal(err)
- }
- // in cache
- if _, err = lkv.Get(context.TODO(), "k", clientv3.WithKeysOnly()); err != nil {
- t.Fatal(err)
- }
- clus.Members[0].Stop(t)
- opts := []clientv3.OpOption{
- clientv3.WithKeysOnly(),
- clientv3.WithLimit(1),
- clientv3.WithMinCreateRev(1),
- clientv3.WithMinModRev(1),
- clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend),
- clientv3.WithSerializable(),
- }
- for _, opt := range opts {
- if _, err := lkv.Get(context.TODO(), "k", opt); err != nil {
- t.Fatal(err)
- }
- }
- getOpts := []clientv3.OpOption{}
- for i := 0; i < len(opts); i++ {
- getOpts = append(getOpts, opts[rand.Intn(len(opts))])
- }
- getOpts = getOpts[:rand.Intn(len(opts))]
- if _, err := lkv.Get(context.TODO(), "k", getOpts...); err != nil {
- t.Fatal(err)
- }
- }
- // TestLeasingConcurrentPut ensures that a get after concurrent puts returns
- // the recently put data.
- func TestLeasingConcurrentPut(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- // force key into leasing key cache
- if _, err = lkv.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- // concurrently put through leasing client
- numPuts := 16
- putc := make(chan *clientv3.PutResponse, numPuts)
- for i := 0; i < numPuts; i++ {
- go func() {
- resp, perr := lkv.Put(context.TODO(), "k", "abc")
- if perr != nil {
- t.Fatal(perr)
- }
- putc <- resp
- }()
- }
- // record maximum revision from puts
- maxRev := int64(0)
- for i := 0; i < numPuts; i++ {
- if resp := <-putc; resp.Header.Revision > maxRev {
- maxRev = resp.Header.Revision
- }
- }
- // confirm Get gives most recently put revisions
- getResp, gerr := lkv.Get(context.TODO(), "k")
- if gerr != nil {
- t.Fatal(err)
- }
- if mr := getResp.Kvs[0].ModRevision; mr != maxRev {
- t.Errorf("expected ModRevision %d, got %d", maxRev, mr)
- }
- if ver := getResp.Kvs[0].Version; ver != int64(numPuts) {
- t.Errorf("expected Version %d, got %d", numPuts, ver)
- }
- }
- func TestLeasingDisconnectedGet(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = clus.Client(0).Put(context.TODO(), "cached", "abc"); err != nil {
- t.Fatal(err)
- }
- // get key so it's cached
- if _, err = lkv.Get(context.TODO(), "cached"); err != nil {
- t.Fatal(err)
- }
- clus.Members[0].Stop(t)
- // leasing key ownership should have "cached" locally served
- cachedResp, err := lkv.Get(context.TODO(), "cached")
- if err != nil {
- t.Fatal(err)
- }
- if len(cachedResp.Kvs) != 1 || string(cachedResp.Kvs[0].Value) != "abc" {
- t.Fatalf(`expected "cached"->"abc", got response %+v`, cachedResp)
- }
- }
- func TestLeasingDeleteOwner(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = clus.Client(0).Put(context.TODO(), "k", "abc"); err != nil {
- t.Fatal(err)
- }
- // get+own / delete / get
- if _, err = lkv.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- if _, err = lkv.Delete(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- resp, err := lkv.Get(context.TODO(), "k")
- if err != nil {
- t.Fatal(err)
- }
- if len(resp.Kvs) != 0 {
- t.Fatalf(`expected "k" to be deleted, got response %+v`, resp)
- }
- // try to double delete
- if _, err = lkv.Delete(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- }
- func TestLeasingDeleteNonOwner(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv1, closeLKV1, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV1()
- lkv2, closeLKV2, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV2()
- if _, err = clus.Client(0).Put(context.TODO(), "k", "abc"); err != nil {
- t.Fatal(err)
- }
- // acquire ownership
- if _, err = lkv1.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- // delete via non-owner
- if _, err = lkv2.Delete(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- // key should be removed from lkv1
- resp, err := lkv1.Get(context.TODO(), "k")
- if err != nil {
- t.Fatal(err)
- }
- if len(resp.Kvs) != 0 {
- t.Fatalf(`expected "k" to be deleted, got response %+v`, resp)
- }
- }
- func TestLeasingOverwriteResponse(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = clus.Client(0).Put(context.TODO(), "k", "abc"); err != nil {
- t.Fatal(err)
- }
- resp, err := lkv.Get(context.TODO(), "k")
- if err != nil {
- t.Fatal(err)
- }
- resp.Kvs[0].Key[0] = 'z'
- resp.Kvs[0].Value[0] = 'z'
- resp, err = lkv.Get(context.TODO(), "k")
- if err != nil {
- t.Fatal(err)
- }
- if string(resp.Kvs[0].Key) != "k" {
- t.Errorf(`expected key "k", got %q`, string(resp.Kvs[0].Key))
- }
- if string(resp.Kvs[0].Value) != "abc" {
- t.Errorf(`expected value "abc", got %q`, string(resp.Kvs[0].Value))
- }
- }
- func TestLeasingOwnerPutResponse(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = clus.Client(0).Put(context.TODO(), "k", "abc"); err != nil {
- t.Fatal(err)
- }
- gresp, gerr := lkv.Get(context.TODO(), "k")
- if gerr != nil {
- t.Fatal(gerr)
- }
- presp, err := lkv.Put(context.TODO(), "k", "def")
- if err != nil {
- t.Fatal(err)
- }
- if presp == nil {
- t.Fatal("expected put response, got nil")
- }
- clus.Members[0].Stop(t)
- gresp, gerr = lkv.Get(context.TODO(), "k")
- if gerr != nil {
- t.Fatal(gerr)
- }
- if gresp.Kvs[0].ModRevision != presp.Header.Revision {
- t.Errorf("expected mod revision %d, got %d", presp.Header.Revision, gresp.Kvs[0].ModRevision)
- }
- if gresp.Kvs[0].Version != 2 {
- t.Errorf("expected version 2, got version %d", gresp.Kvs[0].Version)
- }
- }
- func TestLeasingTxnOwnerGetRange(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- keyCount := rand.Intn(10) + 1
- for i := 0; i < keyCount; i++ {
- k := fmt.Sprintf("k-%d", i)
- if _, err := clus.Client(0).Put(context.TODO(), k, k+k); err != nil {
- t.Fatal(err)
- }
- }
- if _, err := lkv.Get(context.TODO(), "k-"); err != nil {
- t.Fatal(err)
- }
- tresp, terr := lkv.Txn(context.TODO()).Then(clientv3.OpGet("k-", clientv3.WithPrefix())).Commit()
- if terr != nil {
- t.Fatal(terr)
- }
- if resp := tresp.Responses[0].GetResponseRange(); len(resp.Kvs) != keyCount {
- t.Fatalf("expected %d keys, got response %+v", keyCount, resp.Kvs)
- }
- }
- func TestLeasingTxnOwnerGet(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- keyCount := rand.Intn(10) + 1
- var ops []clientv3.Op
- presps := make([]*clientv3.PutResponse, keyCount)
- for i := range presps {
- k := fmt.Sprintf("k-%d", i)
- presp, err := clus.Client(0).Put(context.TODO(), k, k+k)
- if err != nil {
- t.Fatal(err)
- }
- presps[i] = presp
- if _, err = lkv.Get(context.TODO(), k); err != nil {
- t.Fatal(err)
- }
- ops = append(ops, clientv3.OpGet(k))
- }
- ops = ops[:rand.Intn(len(ops))]
- // served through cache
- clus.Members[0].Stop(t)
- var thenOps, elseOps []clientv3.Op
- cmps, useThen := randCmps("k-", presps)
- if useThen {
- thenOps = ops
- elseOps = []clientv3.Op{clientv3.OpPut("k", "1")}
- } else {
- thenOps = []clientv3.Op{clientv3.OpPut("k", "1")}
- elseOps = ops
- }
- tresp, terr := lkv.Txn(context.TODO()).
- If(cmps...).
- Then(thenOps...).
- Else(elseOps...).Commit()
- if terr != nil {
- t.Fatal(terr)
- }
- if tresp.Succeeded != useThen {
- t.Fatalf("expected succeeded=%v, got tresp=%+v", useThen, tresp)
- }
- if len(tresp.Responses) != len(ops) {
- t.Fatalf("expected %d responses, got %d", len(ops), len(tresp.Responses))
- }
- wrev := presps[len(presps)-1].Header.Revision
- if tresp.Header.Revision < wrev {
- t.Fatalf("expected header revision >= %d, got %d", wrev, tresp.Header.Revision)
- }
- for i := range ops {
- k := fmt.Sprintf("k-%d", i)
- rr := tresp.Responses[i].GetResponseRange()
- if rr == nil {
- t.Errorf("expected get response, got %+v", tresp.Responses[i])
- }
- if string(rr.Kvs[0].Key) != k || string(rr.Kvs[0].Value) != k+k {
- t.Errorf(`expected key for %q, got %+v`, k, rr.Kvs)
- }
- }
- }
- func TestLeasingTxnOwnerDeleteRange(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- keyCount := rand.Intn(10) + 1
- for i := 0; i < keyCount; i++ {
- k := fmt.Sprintf("k-%d", i)
- if _, perr := clus.Client(0).Put(context.TODO(), k, k+k); perr != nil {
- t.Fatal(perr)
- }
- }
- // cache in lkv
- resp, err := lkv.Get(context.TODO(), "k-", clientv3.WithPrefix())
- if err != nil {
- t.Fatal(err)
- }
- if len(resp.Kvs) != keyCount {
- t.Fatalf("expected %d keys, got %d", keyCount, len(resp.Kvs))
- }
- if _, terr := lkv.Txn(context.TODO()).Then(clientv3.OpDelete("k-", clientv3.WithPrefix())).Commit(); terr != nil {
- t.Fatal(terr)
- }
- resp, err = lkv.Get(context.TODO(), "k-", clientv3.WithPrefix())
- if err != nil {
- t.Fatal(err)
- }
- if len(resp.Kvs) != 0 {
- t.Fatalf("expected no keys, got %d", len(resp.Kvs))
- }
- }
- func TestLeasingTxnOwnerDelete(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = clus.Client(0).Put(context.TODO(), "k", "abc"); err != nil {
- t.Fatal(err)
- }
- // cache in lkv
- if _, gerr := lkv.Get(context.TODO(), "k"); gerr != nil {
- t.Fatal(gerr)
- }
- if _, terr := lkv.Txn(context.TODO()).Then(clientv3.OpDelete("k")).Commit(); terr != nil {
- t.Fatal(terr)
- }
- resp, err := lkv.Get(context.TODO(), "k")
- if err != nil {
- t.Fatal(err)
- }
- if len(resp.Kvs) != 0 {
- t.Fatalf("expected no keys, got %d", len(resp.Kvs))
- }
- }
- func TestLeasingTxnOwnerIf(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = clus.Client(0).Put(context.TODO(), "k", "abc"); err != nil {
- t.Fatal(err)
- }
- if _, err = lkv.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- // served through cache
- clus.Members[0].Stop(t)
- tests := []struct {
- cmps []clientv3.Cmp
- wSucceeded bool
- wResponses int
- }{
- // success
- {
- cmps: []clientv3.Cmp{clientv3.Compare(clientv3.Value("k"), "=", "abc")},
- wSucceeded: true,
- wResponses: 1,
- },
- {
- cmps: []clientv3.Cmp{clientv3.Compare(clientv3.CreateRevision("k"), "=", 2)},
- wSucceeded: true,
- wResponses: 1,
- },
- {
- cmps: []clientv3.Cmp{clientv3.Compare(clientv3.ModRevision("k"), "=", 2)},
- wSucceeded: true,
- wResponses: 1,
- },
- {
- cmps: []clientv3.Cmp{clientv3.Compare(clientv3.Version("k"), "=", 1)},
- wSucceeded: true,
- wResponses: 1,
- },
- // failure
- {
- cmps: []clientv3.Cmp{clientv3.Compare(clientv3.Value("k"), ">", "abc")},
- },
- {
- cmps: []clientv3.Cmp{clientv3.Compare(clientv3.CreateRevision("k"), ">", 2)},
- },
- {
- cmps: []clientv3.Cmp{clientv3.Compare(clientv3.ModRevision("k"), "=", 2)},
- wSucceeded: true,
- wResponses: 1,
- },
- {
- cmps: []clientv3.Cmp{clientv3.Compare(clientv3.Version("k"), ">", 1)},
- },
- {
- cmps: []clientv3.Cmp{clientv3.Compare(clientv3.Value("k"), "<", "abc")},
- },
- {
- cmps: []clientv3.Cmp{clientv3.Compare(clientv3.CreateRevision("k"), "<", 2)},
- },
- {
- cmps: []clientv3.Cmp{clientv3.Compare(clientv3.ModRevision("k"), "<", 2)},
- },
- {
- cmps: []clientv3.Cmp{clientv3.Compare(clientv3.Version("k"), "<", 1)},
- },
- {
- cmps: []clientv3.Cmp{
- clientv3.Compare(clientv3.Version("k"), "=", 1),
- clientv3.Compare(clientv3.Version("k"), "<", 1),
- },
- },
- }
- for i, tt := range tests {
- tresp, terr := lkv.Txn(context.TODO()).If(tt.cmps...).Then(clientv3.OpGet("k")).Commit()
- if terr != nil {
- t.Fatal(terr)
- }
- if tresp.Succeeded != tt.wSucceeded {
- t.Errorf("#%d: expected succeeded %v, got %v", i, tt.wSucceeded, tresp.Succeeded)
- }
- if len(tresp.Responses) != tt.wResponses {
- t.Errorf("#%d: expected %d responses, got %d", i, tt.wResponses, len(tresp.Responses))
- }
- }
- }
- func TestLeasingTxnCancel(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
- defer clus.Terminate(t)
- lkv1, closeLKV1, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV1()
- lkv2, closeLKV2, err := leasing.NewKV(clus.Client(1), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV2()
- // acquire lease but disconnect so no revoke in time
- if _, err = lkv1.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- clus.Members[0].Stop(t)
- // wait for leader election, if any
- if _, err = clus.Client(1).Get(context.TODO(), "abc"); err != nil {
- t.Fatal(err)
- }
- ctx, cancel := context.WithCancel(context.TODO())
- go func() {
- time.Sleep(100 * time.Millisecond)
- cancel()
- }()
- if _, err := lkv2.Txn(ctx).Then(clientv3.OpPut("k", "v")).Commit(); err != context.Canceled {
- t.Fatalf("expected %v, got %v", context.Canceled, err)
- }
- }
- func TestLeasingTxnNonOwnerPut(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- lkv2, closeLKV2, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV2()
- if _, err = clus.Client(0).Put(context.TODO(), "k", "abc"); err != nil {
- t.Fatal(err)
- }
- if _, err = clus.Client(0).Put(context.TODO(), "k2", "123"); err != nil {
- t.Fatal(err)
- }
- // cache in lkv
- if _, err = lkv.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- if _, err = lkv.Get(context.TODO(), "k2"); err != nil {
- t.Fatal(err)
- }
- // invalidate via lkv2 txn
- opArray := make([]clientv3.Op, 0)
- opArray = append(opArray, clientv3.OpPut("k2", "456"))
- tresp, terr := lkv2.Txn(context.TODO()).Then(
- clientv3.OpTxn(nil, opArray, nil),
- clientv3.OpPut("k", "def"),
- clientv3.OpPut("k3", "999"), // + a key not in any cache
- ).Commit()
- if terr != nil {
- t.Fatal(terr)
- }
- if !tresp.Succeeded || len(tresp.Responses) != 3 {
- t.Fatalf("expected txn success, got %+v", tresp)
- }
- // check cache was invalidated
- gresp, gerr := lkv.Get(context.TODO(), "k")
- if gerr != nil {
- t.Fatal(err)
- }
- if len(gresp.Kvs) != 1 || string(gresp.Kvs[0].Value) != "def" {
- t.Errorf(`expected value "def", got %+v`, gresp)
- }
- gresp, gerr = lkv.Get(context.TODO(), "k2")
- if gerr != nil {
- t.Fatal(gerr)
- }
- if len(gresp.Kvs) != 1 || string(gresp.Kvs[0].Value) != "456" {
- t.Errorf(`expected value "def", got %+v`, gresp)
- }
- // check puts were applied and are all in the same revision
- w := clus.Client(0).Watch(
- clus.Client(0).Ctx(),
- "k",
- clientv3.WithRev(tresp.Header.Revision),
- clientv3.WithPrefix())
- wresp := <-w
- c := 0
- evs := []clientv3.Event{}
- for _, ev := range wresp.Events {
- evs = append(evs, *ev)
- if ev.Kv.ModRevision == tresp.Header.Revision {
- c++
- }
- }
- if c != 3 {
- t.Fatalf("expected 3 put events, got %+v", evs)
- }
- }
- // TestLeasingTxnRandIfThenOrElse randomly leases keys two separate clients, then
- // issues a random If/{Then,Else} transaction on those keys to one client.
- func TestLeasingTxnRandIfThenOrElse(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv1, closeLKV1, err1 := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err1)
- defer closeLKV1()
- lkv2, closeLKV2, err2 := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err2)
- defer closeLKV2()
- keyCount := 16
- dat := make([]*clientv3.PutResponse, keyCount)
- for i := 0; i < keyCount; i++ {
- k, v := fmt.Sprintf("k-%d", i), fmt.Sprintf("%d", i)
- dat[i], err1 = clus.Client(0).Put(context.TODO(), k, v)
- if err1 != nil {
- t.Fatal(err1)
- }
- }
- // nondeterministically populate leasing caches
- var wg sync.WaitGroup
- getc := make(chan struct{}, keyCount)
- getRandom := func(kv clientv3.KV) {
- defer wg.Done()
- for i := 0; i < keyCount/2; i++ {
- k := fmt.Sprintf("k-%d", rand.Intn(keyCount))
- if _, err := kv.Get(context.TODO(), k); err != nil {
- t.Fatal(err)
- }
- getc <- struct{}{}
- }
- }
- wg.Add(2)
- defer wg.Wait()
- go getRandom(lkv1)
- go getRandom(lkv2)
- // random list of comparisons, all true
- cmps, useThen := randCmps("k-", dat)
- // random list of puts/gets; unique keys
- ops := []clientv3.Op{}
- usedIdx := make(map[int]struct{})
- for i := 0; i < keyCount; i++ {
- idx := rand.Intn(keyCount)
- if _, ok := usedIdx[idx]; ok {
- continue
- }
- usedIdx[idx] = struct{}{}
- k := fmt.Sprintf("k-%d", idx)
- switch rand.Intn(2) {
- case 0:
- ops = append(ops, clientv3.OpGet(k))
- case 1:
- ops = append(ops, clientv3.OpPut(k, "a"))
- // TODO: add delete
- }
- }
- // random lengths
- ops = ops[:rand.Intn(len(ops))]
- // wait for some gets to populate the leasing caches before committing
- for i := 0; i < keyCount/2; i++ {
- <-getc
- }
- // randomly choose between then and else blocks
- var thenOps, elseOps []clientv3.Op
- if useThen {
- thenOps = ops
- } else {
- // force failure
- elseOps = ops
- }
- tresp, terr := lkv1.Txn(context.TODO()).If(cmps...).Then(thenOps...).Else(elseOps...).Commit()
- if terr != nil {
- t.Fatal(terr)
- }
- // cmps always succeed
- if tresp.Succeeded != useThen {
- t.Fatalf("expected succeeded=%v, got tresp=%+v", useThen, tresp)
- }
- // get should match what was put
- checkPuts := func(s string, kv clientv3.KV) {
- for _, op := range ops {
- if !op.IsPut() {
- continue
- }
- resp, rerr := kv.Get(context.TODO(), string(op.KeyBytes()))
- if rerr != nil {
- t.Fatal(rerr)
- }
- if len(resp.Kvs) != 1 || string(resp.Kvs[0].Value) != "a" {
- t.Fatalf(`%s: expected value="a", got %+v`, s, resp.Kvs)
- }
- }
- }
- checkPuts("client(0)", clus.Client(0))
- checkPuts("lkv1", lkv1)
- checkPuts("lkv2", lkv2)
- }
- func TestLeasingOwnerPutError(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = lkv.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- clus.Members[0].Stop(t)
- ctx, cancel := context.WithTimeout(context.TODO(), 100*time.Millisecond)
- defer cancel()
- if resp, err := lkv.Put(ctx, "k", "v"); err == nil {
- t.Fatalf("expected error, got response %+v", resp)
- }
- }
- func TestLeasingOwnerDeleteError(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = lkv.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- clus.Members[0].Stop(t)
- ctx, cancel := context.WithTimeout(context.TODO(), 100*time.Millisecond)
- defer cancel()
- if resp, err := lkv.Delete(ctx, "k"); err == nil {
- t.Fatalf("expected error, got response %+v", resp)
- }
- }
- func TestLeasingNonOwnerPutError(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- clus.Members[0].Stop(t)
- ctx, cancel := context.WithTimeout(context.TODO(), 100*time.Millisecond)
- defer cancel()
- if resp, err := lkv.Put(ctx, "k", "v"); err == nil {
- t.Fatalf("expected error, got response %+v", resp)
- }
- }
- func TestLeasingOwnerDeletePrefix(t *testing.T) {
- testLeasingOwnerDelete(t, clientv3.OpDelete("key/", clientv3.WithPrefix()))
- }
- func TestLeasingOwnerDeleteFrom(t *testing.T) {
- testLeasingOwnerDelete(t, clientv3.OpDelete("kd", clientv3.WithFromKey()))
- }
- func testLeasingOwnerDelete(t *testing.T, del clientv3.Op) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "0/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- for i := 0; i < 8; i++ {
- if _, err = clus.Client(0).Put(context.TODO(), fmt.Sprintf("key/%d", i), "123"); err != nil {
- t.Fatal(err)
- }
- }
- if _, err = lkv.Get(context.TODO(), "key/1"); err != nil {
- t.Fatal(err)
- }
- opResp, delErr := lkv.Do(context.TODO(), del)
- if delErr != nil {
- t.Fatal(delErr)
- }
- delResp := opResp.Del()
- // confirm keys are invalidated from cache and deleted on etcd
- for i := 0; i < 8; i++ {
- resp, err := lkv.Get(context.TODO(), fmt.Sprintf("key/%d", i))
- if err != nil {
- t.Fatal(err)
- }
- if len(resp.Kvs) != 0 {
- t.Fatalf("expected no keys on key/%d, got %+v", i, resp)
- }
- }
- // confirm keys were deleted atomically
- w := clus.Client(0).Watch(
- clus.Client(0).Ctx(),
- "key/",
- clientv3.WithRev(delResp.Header.Revision),
- clientv3.WithPrefix())
- if wresp := <-w; len(wresp.Events) != 8 {
- t.Fatalf("expected %d delete events,got %d", 8, len(wresp.Events))
- }
- }
- func TestLeasingDeleteRangeBounds(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- delkv, closeDelKV, err := leasing.NewKV(clus.Client(0), "0/")
- testutil.AssertNil(t, err)
- defer closeDelKV()
- getkv, closeGetKv, err := leasing.NewKV(clus.Client(0), "0/")
- testutil.AssertNil(t, err)
- defer closeGetKv()
- for _, k := range []string{"j", "m"} {
- if _, err = clus.Client(0).Put(context.TODO(), k, "123"); err != nil {
- t.Fatal(err)
- }
- if _, err = getkv.Get(context.TODO(), k); err != nil {
- t.Fatal(err)
- }
- }
- if _, err = delkv.Delete(context.TODO(), "k", clientv3.WithPrefix()); err != nil {
- t.Fatal(err)
- }
- // leases still on server?
- for _, k := range []string{"j", "m"} {
- resp, geterr := clus.Client(0).Get(context.TODO(), "0/"+k, clientv3.WithPrefix())
- if geterr != nil {
- t.Fatal(geterr)
- }
- if len(resp.Kvs) != 1 {
- t.Fatalf("expected leasing key, got %+v", resp)
- }
- }
- // j and m should still have leases registered since not under k*
- clus.Members[0].Stop(t)
- if _, err = getkv.Get(context.TODO(), "j"); err != nil {
- t.Fatal(err)
- }
- if _, err = getkv.Get(context.TODO(), "m"); err != nil {
- t.Fatal(err)
- }
- }
- func TestLeasingDeleteRangeContendTxn(t *testing.T) {
- then := []clientv3.Op{clientv3.OpDelete("key/", clientv3.WithPrefix())}
- testLeasingDeleteRangeContend(t, clientv3.OpTxn(nil, then, nil))
- }
- func TestLeaseDeleteRangeContendDel(t *testing.T) {
- op := clientv3.OpDelete("key/", clientv3.WithPrefix())
- testLeasingDeleteRangeContend(t, op)
- }
- func testLeasingDeleteRangeContend(t *testing.T, op clientv3.Op) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- delkv, closeDelKV, err := leasing.NewKV(clus.Client(0), "0/")
- testutil.AssertNil(t, err)
- defer closeDelKV()
- putkv, closePutKV, err := leasing.NewKV(clus.Client(0), "0/")
- testutil.AssertNil(t, err)
- defer closePutKV()
- for i := 0; i < 8; i++ {
- key := fmt.Sprintf("key/%d", i)
- if _, err = clus.Client(0).Put(context.TODO(), key, "123"); err != nil {
- t.Fatal(err)
- }
- if _, err = putkv.Get(context.TODO(), key); err != nil {
- t.Fatal(err)
- }
- }
- ctx, cancel := context.WithCancel(context.TODO())
- donec := make(chan struct{})
- go func() {
- defer close(donec)
- for i := 0; ctx.Err() == nil; i++ {
- key := fmt.Sprintf("key/%d", i%8)
- putkv.Put(ctx, key, "123")
- putkv.Get(ctx, key)
- }
- }()
- _, delErr := delkv.Do(context.TODO(), op)
- cancel()
- <-donec
- if delErr != nil {
- t.Fatal(delErr)
- }
- // confirm keys on non-deleter match etcd
- for i := 0; i < 8; i++ {
- key := fmt.Sprintf("key/%d", i)
- resp, err := putkv.Get(context.TODO(), key)
- if err != nil {
- t.Fatal(err)
- }
- servResp, err := clus.Client(0).Get(context.TODO(), key)
- if err != nil {
- t.Fatal(err)
- }
- if !reflect.DeepEqual(resp.Kvs, servResp.Kvs) {
- t.Errorf("#%d: expected %+v, got %+v", i, servResp.Kvs, resp.Kvs)
- }
- }
- }
- func TestLeasingPutGetDeleteConcurrent(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkvs := make([]clientv3.KV, 16)
- for i := range lkvs {
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "pfx/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- lkvs[i] = lkv
- }
- getdel := func(kv clientv3.KV) {
- if _, err := kv.Put(context.TODO(), "k", "abc"); err != nil {
- t.Fatal(err)
- }
- time.Sleep(time.Millisecond)
- if _, err := kv.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- if _, err := kv.Delete(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- time.Sleep(2 * time.Millisecond)
- }
- var wg sync.WaitGroup
- wg.Add(16)
- for i := 0; i < 16; i++ {
- go func() {
- defer wg.Done()
- for _, kv := range lkvs {
- getdel(kv)
- }
- }()
- }
- wg.Wait()
- resp, err := lkvs[0].Get(context.TODO(), "k")
- if err != nil {
- t.Fatal(err)
- }
- if len(resp.Kvs) > 0 {
- t.Fatalf("expected no kvs, got %+v", resp.Kvs)
- }
- resp, err = clus.Client(0).Get(context.TODO(), "k")
- if err != nil {
- t.Fatal(err)
- }
- if len(resp.Kvs) > 0 {
- t.Fatalf("expected no kvs, got %+v", resp.Kvs)
- }
- }
- // TestLeasingReconnectOwnerRevoke checks that revocation works if
- // disconnected when trying to submit revoke txn.
- func TestLeasingReconnectOwnerRevoke(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
- defer clus.Terminate(t)
- lkv1, closeLKV1, err1 := leasing.NewKV(clus.Client(0), "foo/")
- testutil.AssertNil(t, err1)
- defer closeLKV1()
- lkv2, closeLKV2, err2 := leasing.NewKV(clus.Client(1), "foo/")
- testutil.AssertNil(t, err2)
- defer closeLKV2()
- if _, err := lkv1.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- // force leader away from member 0
- clus.Members[0].Stop(t)
- clus.WaitLeader(t)
- clus.Members[0].Restart(t)
- cctx, cancel := context.WithCancel(context.TODO())
- sdonec, pdonec := make(chan struct{}), make(chan struct{})
- // make lkv1 connection choppy so Txn fails
- go func() {
- defer close(sdonec)
- for i := 0; i < 10 && cctx.Err() == nil; i++ {
- clus.Members[0].Stop(t)
- time.Sleep(10 * time.Millisecond)
- clus.Members[0].Restart(t)
- }
- }()
- go func() {
- defer close(pdonec)
- if _, err := lkv2.Put(cctx, "k", "v"); err != nil {
- t.Log(err)
- }
- resp, err := lkv1.Get(cctx, "k")
- if err != nil {
- t.Fatal(err)
- }
- if string(resp.Kvs[0].Value) != "v" {
- t.Fatalf(`expected "v" value, got %+v`, resp)
- }
- }()
- select {
- case <-pdonec:
- cancel()
- <-sdonec
- case <-time.After(10 * time.Second):
- cancel()
- <-sdonec
- <-pdonec
- t.Fatal("took to long to revoke and put")
- }
- }
- // TestLeasingReconnectOwnerRevokeCompact checks that revocation works if
- // disconnected and the watch is compacted.
- func TestLeasingReconnectOwnerRevokeCompact(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
- defer clus.Terminate(t)
- lkv1, closeLKV1, err1 := leasing.NewKV(clus.Client(0), "foo/")
- testutil.AssertNil(t, err1)
- defer closeLKV1()
- lkv2, closeLKV2, err2 := leasing.NewKV(clus.Client(1), "foo/")
- testutil.AssertNil(t, err2)
- defer closeLKV2()
- if _, err := lkv1.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- clus.Members[0].Stop(t)
- clus.WaitLeader(t)
- // put some more revisions for compaction
- presp, err := clus.Client(1).Put(context.TODO(), "a", "123")
- if err != nil {
- t.Fatal(err)
- }
- presp, err = clus.Client(1).Put(context.TODO(), "a", "123")
- if err != nil {
- t.Fatal(err)
- }
- // compact while lkv1 is disconnected
- rev := presp.Header.Revision
- if _, err = clus.Client(1).Compact(context.TODO(), rev); err != nil {
- t.Fatal(err)
- }
- clus.Members[0].Restart(t)
- cctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
- defer cancel()
- if _, err = lkv2.Put(cctx, "k", "v"); err != nil {
- t.Fatal(err)
- }
- resp, err := lkv1.Get(cctx, "k")
- if err != nil {
- t.Fatal(err)
- }
- if string(resp.Kvs[0].Value) != "v" {
- t.Fatalf(`expected "v" value, got %+v`, resp)
- }
- }
- // TestLeasingReconnectOwnerConsistency checks a write error on an owner will
- // not cause inconsistency between the server and the client.
- func TestLeasingReconnectOwnerConsistency(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "foo/")
- defer closeLKV()
- testutil.AssertNil(t, err)
- if _, err = lkv.Put(context.TODO(), "k", "x"); err != nil {
- t.Fatal(err)
- }
- if _, err = lkv.Put(context.TODO(), "kk", "y"); err != nil {
- t.Fatal(err)
- }
- if _, err = lkv.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- for i := 0; i < 10; i++ {
- v := fmt.Sprintf("%d", i)
- donec := make(chan struct{})
- clus.Members[0].DropConnections()
- go func() {
- defer close(donec)
- for i := 0; i < 20; i++ {
- clus.Members[0].DropConnections()
- time.Sleep(time.Millisecond)
- }
- }()
- switch rand.Intn(7) {
- case 0:
- _, err = lkv.Put(context.TODO(), "k", v)
- case 1:
- _, err = lkv.Delete(context.TODO(), "k")
- case 2:
- txn := lkv.Txn(context.TODO()).Then(
- clientv3.OpGet("k"),
- clientv3.OpDelete("k"),
- )
- _, err = txn.Commit()
- case 3:
- txn := lkv.Txn(context.TODO()).Then(
- clientv3.OpGet("k"),
- clientv3.OpPut("k", v),
- )
- _, err = txn.Commit()
- case 4:
- _, err = lkv.Do(context.TODO(), clientv3.OpPut("k", v))
- case 5:
- _, err = lkv.Do(context.TODO(), clientv3.OpDelete("k"))
- case 6:
- _, err = lkv.Delete(context.TODO(), "k", clientv3.WithPrefix())
- }
- <-donec
- if err != nil {
- // TODO wrap input client to generate errors
- break
- }
- }
- lresp, lerr := lkv.Get(context.TODO(), "k")
- if lerr != nil {
- t.Fatal(lerr)
- }
- cresp, cerr := clus.Client(0).Get(context.TODO(), "k")
- if cerr != nil {
- t.Fatal(cerr)
- }
- if !reflect.DeepEqual(lresp.Kvs, cresp.Kvs) {
- t.Fatalf("expected %+v, got %+v", cresp, lresp)
- }
- }
- func TestLeasingTxnAtomicCache(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "foo/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- puts, gets := make([]clientv3.Op, 16), make([]clientv3.Op, 16)
- for i := range puts {
- k := fmt.Sprintf("k-%d", i)
- puts[i], gets[i] = clientv3.OpPut(k, k), clientv3.OpGet(k)
- }
- if _, err = clus.Client(0).Txn(context.TODO()).Then(puts...).Commit(); err != nil {
- t.Fatal(err)
- }
- for i := range gets {
- if _, err = lkv.Do(context.TODO(), gets[i]); err != nil {
- t.Fatal(err)
- }
- }
- numPutters, numGetters := 16, 16
- var wgPutters, wgGetters sync.WaitGroup
- wgPutters.Add(numPutters)
- wgGetters.Add(numGetters)
- f := func() {
- defer wgPutters.Done()
- for i := 0; i < 10; i++ {
- if _, txnerr := lkv.Txn(context.TODO()).Then(puts...).Commit(); err != nil {
- t.Fatal(txnerr)
- }
- }
- }
- donec := make(chan struct{}, numPutters)
- g := func() {
- defer wgGetters.Done()
- for {
- select {
- case <-donec:
- return
- default:
- }
- tresp, err := lkv.Txn(context.TODO()).Then(gets...).Commit()
- if err != nil {
- t.Fatal(err)
- }
- revs := make([]int64, len(gets))
- for i, resp := range tresp.Responses {
- rr := resp.GetResponseRange()
- revs[i] = rr.Kvs[0].ModRevision
- }
- for i := 1; i < len(revs); i++ {
- if revs[i] != revs[i-1] {
- t.Fatalf("expected matching revisions, got %+v", revs)
- }
- }
- }
- }
- for i := 0; i < numGetters; i++ {
- go g()
- }
- for i := 0; i < numPutters; i++ {
- go f()
- }
- wgPutters.Wait()
- close(donec)
- wgGetters.Wait()
- }
- // TestLeasingReconnectTxn checks that Txn is resilient to disconnects.
- func TestLeasingReconnectTxn(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "foo/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = lkv.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- donec := make(chan struct{})
- go func() {
- defer close(donec)
- clus.Members[0].DropConnections()
- for i := 0; i < 10; i++ {
- clus.Members[0].DropConnections()
- time.Sleep(time.Millisecond)
- }
- }()
- _, lerr := lkv.Txn(context.TODO()).
- If(clientv3.Compare(clientv3.Version("k"), "=", 0)).
- Then(clientv3.OpGet("k")).
- Commit()
- <-donec
- if lerr != nil {
- t.Fatal(lerr)
- }
- }
- // TestLeasingReconnectNonOwnerGet checks a get error on an owner will
- // not cause inconsistency between the server and the client.
- func TestLeasingReconnectNonOwnerGet(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "foo/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- // populate a few keys so some leasing gets have keys
- for i := 0; i < 4; i++ {
- k := fmt.Sprintf("k-%d", i*2)
- if _, err = lkv.Put(context.TODO(), k, k[2:]); err != nil {
- t.Fatal(err)
- }
- }
- n := 0
- for i := 0; i < 10; i++ {
- donec := make(chan struct{})
- clus.Members[0].DropConnections()
- go func() {
- defer close(donec)
- for j := 0; j < 10; j++ {
- clus.Members[0].DropConnections()
- time.Sleep(time.Millisecond)
- }
- }()
- _, err = lkv.Get(context.TODO(), fmt.Sprintf("k-%d", i))
- <-donec
- n++
- if err != nil {
- break
- }
- }
- for i := 0; i < n; i++ {
- k := fmt.Sprintf("k-%d", i)
- lresp, lerr := lkv.Get(context.TODO(), k)
- if lerr != nil {
- t.Fatal(lerr)
- }
- cresp, cerr := clus.Client(0).Get(context.TODO(), k)
- if cerr != nil {
- t.Fatal(cerr)
- }
- if !reflect.DeepEqual(lresp.Kvs, cresp.Kvs) {
- t.Fatalf("expected %+v, got %+v", cresp, lresp)
- }
- }
- }
- func TestLeasingTxnRangeCmp(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "foo/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = clus.Client(0).Put(context.TODO(), "k", "a"); err != nil {
- t.Fatal(err)
- }
- // k2 version = 2
- if _, err = clus.Client(0).Put(context.TODO(), "k2", "a"); err != nil {
- t.Fatal(err)
- }
- if _, err = clus.Client(0).Put(context.TODO(), "k2", "a"); err != nil {
- t.Fatal(err)
- }
- // cache k
- if _, err = lkv.Get(context.TODO(), "k"); err != nil {
- t.Fatal(err)
- }
- cmp := clientv3.Compare(clientv3.Version("k").WithPrefix(), "=", 1)
- tresp, terr := lkv.Txn(context.TODO()).If(cmp).Commit()
- if terr != nil {
- t.Fatal(err)
- }
- if tresp.Succeeded {
- t.Fatalf("expected Succeeded=false, got %+v", tresp)
- }
- }
- func TestLeasingDo(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "foo/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- ops := []clientv3.Op{
- clientv3.OpTxn(nil, nil, nil),
- clientv3.OpGet("a"),
- clientv3.OpPut("a/abc", "v"),
- clientv3.OpDelete("a", clientv3.WithPrefix()),
- clientv3.OpTxn(nil, nil, nil),
- }
- for i, op := range ops {
- resp, resperr := lkv.Do(context.TODO(), op)
- if resperr != nil {
- t.Errorf("#%d: failed (%v)", i, resperr)
- }
- switch {
- case op.IsGet() && resp.Get() == nil:
- t.Errorf("#%d: get but nil get response", i)
- case op.IsPut() && resp.Put() == nil:
- t.Errorf("#%d: put op but nil get response", i)
- case op.IsDelete() && resp.Del() == nil:
- t.Errorf("#%d: delete op but nil delete response", i)
- case op.IsTxn() && resp.Txn() == nil:
- t.Errorf("#%d: txn op but nil txn response", i)
- }
- }
- gresp, err := clus.Client(0).Get(context.TODO(), "a", clientv3.WithPrefix())
- if err != nil {
- t.Fatal(err)
- }
- if len(gresp.Kvs) != 0 {
- t.Fatalf("expected no keys, got %+v", gresp.Kvs)
- }
- }
- func TestLeasingTxnOwnerPutBranch(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "foo/")
- testutil.AssertNil(t, err)
- defer closeLKV()
- n := 0
- treeOp := makePutTreeOp("tree", &n, 4)
- for i := 0; i < n; i++ {
- k := fmt.Sprintf("tree/%d", i)
- if _, err = clus.Client(0).Put(context.TODO(), k, "a"); err != nil {
- t.Fatal(err)
- }
- if _, err = lkv.Get(context.TODO(), k); err != nil {
- t.Fatal(err)
- }
- }
- if _, err = lkv.Do(context.TODO(), treeOp); err != nil {
- t.Fatal(err)
- }
- // lkv shouldn't need to call out to server for updated leased keys
- clus.Members[0].Stop(t)
- for i := 0; i < n; i++ {
- k := fmt.Sprintf("tree/%d", i)
- lkvResp, err := lkv.Get(context.TODO(), k)
- if err != nil {
- t.Fatal(err)
- }
- clusResp, err := clus.Client(1).Get(context.TODO(), k)
- if err != nil {
- t.Fatal(err)
- }
- if !reflect.DeepEqual(clusResp.Kvs, lkvResp.Kvs) {
- t.Fatalf("expected %+v, got %+v", clusResp.Kvs, lkvResp.Kvs)
- }
- }
- }
- func makePutTreeOp(pfx string, v *int, depth int) clientv3.Op {
- key := fmt.Sprintf("%s/%d", pfx, *v)
- *v = *v + 1
- if depth == 0 {
- return clientv3.OpPut(key, "leaf")
- }
- t, e := makePutTreeOp(pfx, v, depth-1), makePutTreeOp(pfx, v, depth-1)
- tPut, ePut := clientv3.OpPut(key, "then"), clientv3.OpPut(key, "else")
- cmps := make([]clientv3.Cmp, 1)
- if rand.Intn(2) == 0 {
- // follow then path
- cmps[0] = clientv3.Compare(clientv3.Version("nokey"), "=", 0)
- } else {
- // follow else path
- cmps[0] = clientv3.Compare(clientv3.Version("nokey"), ">", 0)
- }
- return clientv3.OpTxn(cmps, []clientv3.Op{t, tPut}, []clientv3.Op{e, ePut})
- }
- func randCmps(pfx string, dat []*clientv3.PutResponse) (cmps []clientv3.Cmp, then bool) {
- for i := 0; i < len(dat); i++ {
- idx := rand.Intn(len(dat))
- k := fmt.Sprintf("%s%d", pfx, idx)
- rev := dat[idx].Header.Revision
- var cmp clientv3.Cmp
- switch rand.Intn(4) {
- case 0:
- cmp = clientv3.Compare(clientv3.CreateRevision(k), ">", rev-1)
- case 1:
- cmp = clientv3.Compare(clientv3.Version(k), "=", 1)
- case 2:
- cmp = clientv3.Compare(clientv3.CreateRevision(k), "=", rev)
- case 3:
- cmp = clientv3.Compare(clientv3.CreateRevision(k), "!=", rev+1)
- }
- cmps = append(cmps, cmp)
- }
- cmps = cmps[:rand.Intn(len(dat))]
- if rand.Intn(2) == 0 {
- return cmps, true
- }
- i := rand.Intn(len(dat))
- cmps = append(cmps, clientv3.Compare(clientv3.Version(fmt.Sprintf("k-%d", i)), "=", 0))
- return cmps, false
- }
- func TestLeasingSessionExpire(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
- defer clus.Terminate(t)
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "foo/", concurrency.WithTTL(1))
- testutil.AssertNil(t, err)
- defer closeLKV()
- lkv2, closeLKV2, err := leasing.NewKV(clus.Client(0), "foo/")
- testutil.AssertNil(t, err)
- defer closeLKV2()
- // acquire lease on abc
- if _, err = lkv.Get(context.TODO(), "abc"); err != nil {
- t.Fatal(err)
- }
- // down endpoint lkv uses for keepalives
- clus.Members[0].Stop(t)
- if err = waitForLeasingExpire(clus.Client(1), "foo/abc"); err != nil {
- t.Fatal(err)
- }
- waitForExpireAck(t, lkv)
- clus.Members[0].Restart(t)
- if _, err = lkv2.Put(context.TODO(), "abc", "def"); err != nil {
- t.Fatal(err)
- }
- resp, err := lkv.Get(context.TODO(), "abc")
- if err != nil {
- t.Fatal(err)
- }
- if v := string(resp.Kvs[0].Value); v != "def" {
- t.Fatalf("expected %q, got %q", "v", v)
- }
- }
- func TestLeasingSessionExpireCancel(t *testing.T) {
- defer testutil.AfterTest(t)
- clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
- defer clus.Terminate(t)
- tests := []func(context.Context, clientv3.KV) error{
- func(ctx context.Context, kv clientv3.KV) error {
- _, err := kv.Get(ctx, "abc")
- return err
- },
- func(ctx context.Context, kv clientv3.KV) error {
- _, err := kv.Delete(ctx, "abc")
- return err
- },
- func(ctx context.Context, kv clientv3.KV) error {
- _, err := kv.Put(ctx, "abc", "v")
- return err
- },
- func(ctx context.Context, kv clientv3.KV) error {
- _, err := kv.Txn(ctx).Then(clientv3.OpGet("abc")).Commit()
- return err
- },
- func(ctx context.Context, kv clientv3.KV) error {
- _, err := kv.Do(ctx, clientv3.OpPut("abc", "v"))
- return err
- },
- func(ctx context.Context, kv clientv3.KV) error {
- _, err := kv.Do(ctx, clientv3.OpDelete("abc"))
- return err
- },
- func(ctx context.Context, kv clientv3.KV) error {
- _, err := kv.Do(ctx, clientv3.OpGet("abc"))
- return err
- },
- func(ctx context.Context, kv clientv3.KV) error {
- op := clientv3.OpTxn(nil, []clientv3.Op{clientv3.OpGet("abc")}, nil)
- _, err := kv.Do(ctx, op)
- return err
- },
- }
- for i := range tests {
- lkv, closeLKV, err := leasing.NewKV(clus.Client(0), "foo/", concurrency.WithTTL(1))
- testutil.AssertNil(t, err)
- defer closeLKV()
- if _, err = lkv.Get(context.TODO(), "abc"); err != nil {
- t.Fatal(err)
- }
- // down endpoint lkv uses for keepalives
- clus.Members[0].Stop(t)
- if err := waitForLeasingExpire(clus.Client(1), "foo/abc"); err != nil {
- t.Fatal(err)
- }
- waitForExpireAck(t, lkv)
- ctx, cancel := context.WithCancel(context.TODO())
- errc := make(chan error, 1)
- go func() { errc <- tests[i](ctx, lkv) }()
- // some delay to get past for ctx.Err() != nil {} loops
- time.Sleep(100 * time.Millisecond)
- cancel()
- select {
- case err := <-errc:
- if err != ctx.Err() {
- t.Errorf("#%d: expected %v, got %v", i, ctx.Err(), err)
- }
- case <-time.After(5 * time.Second):
- t.Errorf("#%d: timed out waiting for cancel", i)
- }
- clus.Members[0].Restart(t)
- }
- }
- func waitForLeasingExpire(kv clientv3.KV, lkey string) error {
- for {
- time.Sleep(1 * time.Second)
- resp, err := kv.Get(context.TODO(), lkey, clientv3.WithPrefix())
- if err != nil {
- return err
- }
- if len(resp.Kvs) == 0 {
- // server expired the leasing key
- return nil
- }
- }
- }
- func waitForExpireAck(t *testing.T, kv clientv3.KV) {
- // wait for leasing client to acknowledge lost lease
- for i := 0; i < 10; i++ {
- ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
- _, err := kv.Get(ctx, "abc")
- cancel()
- if err == ctx.Err() {
- return
- }
- time.Sleep(time.Second)
- }
- t.Fatalf("waited too long to acknlowedge lease expiration")
- }
|