go.etcd.io/etcd@v3.3.27+incompatible/clientv3/ordering/util_test.go (about) 1 // Copyright 2017 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 15 package ordering 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "github.com/coreos/etcd/clientv3" 23 "github.com/coreos/etcd/integration" 24 "github.com/coreos/etcd/pkg/testutil" 25 ) 26 27 func TestEndpointSwitchResolvesViolation(t *testing.T) { 28 defer testutil.AfterTest(t) 29 clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3}) 30 defer clus.Terminate(t) 31 eps := []string{ 32 clus.Members[0].GRPCAddr(), 33 clus.Members[1].GRPCAddr(), 34 clus.Members[2].GRPCAddr(), 35 } 36 cfg := clientv3.Config{Endpoints: []string{clus.Members[0].GRPCAddr()}} 37 cli, err := clientv3.New(cfg) 38 if err != nil { 39 t.Fatal(err) 40 } 41 42 ctx := context.TODO() 43 44 if _, err = clus.Client(0).Put(ctx, "foo", "bar"); err != nil { 45 t.Fatal(err) 46 } 47 // ensure that the second member has current revision for key "foo" 48 if _, err = clus.Client(1).Get(ctx, "foo"); err != nil { 49 t.Fatal(err) 50 } 51 52 // create partition between third members and the first two members 53 // in order to guarantee that the third member's revision of "foo" 54 // falls behind as updates to "foo" are issued to the first two members. 55 clus.Members[2].InjectPartition(t, clus.Members[:2]...) 56 time.Sleep(1 * time.Second) // give enough time for the operation 57 58 // update to "foo" will not be replicated to the third member due to the partition 59 if _, err = clus.Client(1).Put(ctx, "foo", "buzz"); err != nil { 60 t.Fatal(err) 61 } 62 63 // reset client endpoints to all members such that the copy of cli sent to 64 // NewOrderViolationSwitchEndpointClosure will be able to 65 // access the full list of endpoints. 66 cli.SetEndpoints(eps...) 67 OrderingKv := NewKV(cli.KV, NewOrderViolationSwitchEndpointClosure(*cli)) 68 // set prevRev to the second member's revision of "foo" such that 69 // the revision is higher than the third member's revision of "foo" 70 _, err = OrderingKv.Get(ctx, "foo") 71 if err != nil { 72 t.Fatal(err) 73 } 74 75 cli.SetEndpoints(clus.Members[2].GRPCAddr()) 76 time.Sleep(1 * time.Second) // give enough time for operation 77 _, err = OrderingKv.Get(ctx, "foo", clientv3.WithSerializable()) 78 if err != nil { 79 t.Fatalf("failed to resolve order violation %v", err) 80 } 81 } 82 83 func TestUnresolvableOrderViolation(t *testing.T) { 84 defer testutil.AfterTest(t) 85 clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 5, SkipCreatingClient: true}) 86 defer clus.Terminate(t) 87 cfg := clientv3.Config{ 88 Endpoints: []string{ 89 clus.Members[0].GRPCAddr(), 90 clus.Members[1].GRPCAddr(), 91 clus.Members[2].GRPCAddr(), 92 clus.Members[3].GRPCAddr(), 93 clus.Members[4].GRPCAddr(), 94 }, 95 } 96 cli, err := clientv3.New(cfg) 97 if err != nil { 98 t.Fatal(err) 99 } 100 eps := cli.Endpoints() 101 ctx := context.TODO() 102 103 cli.SetEndpoints(clus.Members[0].GRPCAddr()) 104 time.Sleep(1 * time.Second) 105 _, err = cli.Put(ctx, "foo", "bar") 106 if err != nil { 107 t.Fatal(err) 108 } 109 110 // stop fourth member in order to force the member to have an outdated revision 111 clus.Members[3].Stop(t) 112 time.Sleep(1 * time.Second) // give enough time for operation 113 // stop fifth member in order to force the member to have an outdated revision 114 clus.Members[4].Stop(t) 115 time.Sleep(1 * time.Second) // give enough time for operation 116 _, err = cli.Put(ctx, "foo", "buzz") 117 if err != nil { 118 t.Fatal(err) 119 } 120 121 // reset client endpoints to all members such that the copy of cli sent to 122 // NewOrderViolationSwitchEndpointClosure will be able to 123 // access the full list of endpoints. 124 cli.SetEndpoints(eps...) 125 OrderingKv := NewKV(cli.KV, NewOrderViolationSwitchEndpointClosure(*cli)) 126 // set prevRev to the first member's revision of "foo" such that 127 // the revision is higher than the fourth and fifth members' revision of "foo" 128 _, err = OrderingKv.Get(ctx, "foo") 129 if err != nil { 130 t.Fatal(err) 131 } 132 133 clus.Members[0].Stop(t) 134 clus.Members[1].Stop(t) 135 clus.Members[2].Stop(t) 136 clus.Members[3].Restart(t) 137 clus.Members[4].Restart(t) 138 cli.SetEndpoints(clus.Members[3].GRPCAddr()) 139 time.Sleep(1 * time.Second) // give enough time for operation 140 141 _, err = OrderingKv.Get(ctx, "foo", clientv3.WithSerializable()) 142 if err != ErrNoGreaterRev { 143 t.Fatalf("expected %v, got %v", ErrNoGreaterRev, err) 144 } 145 }