util_test.go 4.5 KB

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