util_test.go 4.7 KB

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