kv_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. gContext "context"
  18. "errors"
  19. "sync"
  20. "testing"
  21. "time"
  22. "github.com/coreos/etcd/clientv3"
  23. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  24. "github.com/coreos/etcd/integration"
  25. "github.com/coreos/etcd/pkg/testutil"
  26. )
  27. func TestDetectKvOrderViolation(t *testing.T) {
  28. var errOrderViolation = errors.New("Detected Order Violation")
  29. defer testutil.AfterTest(t)
  30. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  31. defer clus.Terminate(t)
  32. cfg := clientv3.Config{
  33. Endpoints: []string{
  34. clus.Members[0].GRPCAddr(),
  35. clus.Members[1].GRPCAddr(),
  36. clus.Members[2].GRPCAddr(),
  37. },
  38. }
  39. cli, err := clientv3.New(cfg)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. ctx := context.TODO()
  44. if _, err = clus.Client(0).Put(ctx, "foo", "bar"); err != nil {
  45. t.Fatal(err)
  46. }
  47. // ensure that the second member has the current revision for the key foo
  48. if _, err = clus.Client(1).Get(ctx, "foo"); err != nil {
  49. t.Fatal(err)
  50. }
  51. // stop third member in order to force the member to have an outdated revision
  52. clus.Members[2].Stop(t)
  53. time.Sleep(1 * time.Second) // give enough time for operation
  54. _, err = cli.Put(ctx, "foo", "buzz")
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. // perform get request against the first member, in order to
  59. // set up kvOrdering to expect "foo" revisions greater than that of
  60. // the third member.
  61. orderingKv := NewKV(cli.KV,
  62. func(op clientv3.Op, resp clientv3.OpResponse, prevRev int64) error {
  63. return errOrderViolation
  64. })
  65. _, err = orderingKv.Get(ctx, "foo")
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. // ensure that only the third member is queried during requests
  70. clus.Members[0].Stop(t)
  71. clus.Members[1].Stop(t)
  72. clus.Members[2].Restart(t)
  73. // force OrderingKv to query the third member
  74. cli.SetEndpoints(clus.Members[2].GRPCAddr())
  75. _, err = orderingKv.Get(ctx, "foo", clientv3.WithSerializable())
  76. if err != errOrderViolation {
  77. t.Fatalf("expected %v, got %v", errOrderViolation, err)
  78. }
  79. }
  80. func TestDetectTxnOrderViolation(t *testing.T) {
  81. var errOrderViolation = errors.New("Detected Order Violation")
  82. defer testutil.AfterTest(t)
  83. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  84. defer clus.Terminate(t)
  85. cfg := clientv3.Config{
  86. Endpoints: []string{
  87. clus.Members[0].GRPCAddr(),
  88. clus.Members[1].GRPCAddr(),
  89. clus.Members[2].GRPCAddr(),
  90. },
  91. }
  92. cli, err := clientv3.New(cfg)
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. ctx := context.TODO()
  97. if _, err = clus.Client(0).Put(ctx, "foo", "bar"); err != nil {
  98. t.Fatal(err)
  99. }
  100. // ensure that the second member has the current revision for the key foo
  101. if _, err = clus.Client(1).Get(ctx, "foo"); err != nil {
  102. t.Fatal(err)
  103. }
  104. // stop third member in order to force the member to have an outdated revision
  105. clus.Members[2].Stop(t)
  106. time.Sleep(1 * time.Second) // give enough time for operation
  107. if _, err = clus.Client(1).Put(ctx, "foo", "buzz"); err != nil {
  108. t.Fatal(err)
  109. }
  110. // perform get request against the first member, in order to
  111. // set up kvOrdering to expect "foo" revisions greater than that of
  112. // the third member.
  113. orderingKv := NewKV(cli.KV,
  114. func(op clientv3.Op, resp clientv3.OpResponse, prevRev int64) error {
  115. return errOrderViolation
  116. })
  117. orderingTxn := orderingKv.Txn(ctx)
  118. _, err = orderingTxn.If(
  119. clientv3.Compare(clientv3.Value("b"), ">", "a"),
  120. ).Then(
  121. clientv3.OpGet("foo"),
  122. ).Commit()
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. // ensure that only the third member is queried during requests
  127. clus.Members[0].Stop(t)
  128. clus.Members[1].Stop(t)
  129. clus.Members[2].Restart(t)
  130. // force OrderingKv to query the third member
  131. cli.SetEndpoints(clus.Members[2].GRPCAddr())
  132. _, err = orderingKv.Get(ctx, "foo", clientv3.WithSerializable())
  133. if err != errOrderViolation {
  134. t.Fatalf("expected %v, got %v", errOrderViolation, err)
  135. }
  136. orderingTxn = orderingKv.Txn(ctx)
  137. _, err = orderingTxn.If(
  138. clientv3.Compare(clientv3.Value("b"), ">", "a"),
  139. ).Then(
  140. clientv3.OpGet("foo", clientv3.WithSerializable()),
  141. ).Commit()
  142. if err != errOrderViolation {
  143. t.Fatalf("expected %v, got %v", errOrderViolation, err)
  144. }
  145. }
  146. type mockKV struct {
  147. clientv3.KV
  148. response clientv3.OpResponse
  149. }
  150. func (kv *mockKV) Do(ctx gContext.Context, op clientv3.Op) (clientv3.OpResponse, error) {
  151. return kv.response, nil
  152. }
  153. var rangeTests = []struct {
  154. prevRev int64
  155. response *clientv3.GetResponse
  156. }{
  157. {
  158. 5,
  159. &clientv3.GetResponse{
  160. Header: &pb.ResponseHeader{
  161. Revision: 5,
  162. },
  163. },
  164. },
  165. {
  166. 5,
  167. &clientv3.GetResponse{
  168. Header: &pb.ResponseHeader{
  169. Revision: 4,
  170. },
  171. },
  172. },
  173. {
  174. 5,
  175. &clientv3.GetResponse{
  176. Header: &pb.ResponseHeader{
  177. Revision: 6,
  178. },
  179. },
  180. },
  181. }
  182. func TestKvOrdering(t *testing.T) {
  183. for i, tt := range rangeTests {
  184. mKV := &mockKV{clientv3.NewKVFromKVClient(nil, nil), tt.response.OpResponse()}
  185. kv := &kvOrdering{
  186. mKV,
  187. func(r *clientv3.GetResponse) OrderViolationFunc {
  188. return func(op clientv3.Op, resp clientv3.OpResponse, prevRev int64) error {
  189. r.Header.Revision++
  190. return nil
  191. }
  192. }(tt.response),
  193. tt.prevRev,
  194. sync.RWMutex{},
  195. }
  196. res, err := kv.Get(nil, "mockKey")
  197. if err != nil {
  198. t.Errorf("#%d: expected response %+v, got error %+v", i, tt.response, err)
  199. }
  200. if rev := res.Header.Revision; rev < tt.prevRev {
  201. t.Errorf("#%d: expected revision %d, got %d", i, tt.prevRev, rev)
  202. }
  203. }
  204. }
  205. var txnTests = []struct {
  206. prevRev int64
  207. response *clientv3.TxnResponse
  208. }{
  209. {
  210. 5,
  211. &clientv3.TxnResponse{
  212. Header: &pb.ResponseHeader{
  213. Revision: 5,
  214. },
  215. },
  216. },
  217. {
  218. 5,
  219. &clientv3.TxnResponse{
  220. Header: &pb.ResponseHeader{
  221. Revision: 8,
  222. },
  223. },
  224. },
  225. {
  226. 5,
  227. &clientv3.TxnResponse{
  228. Header: &pb.ResponseHeader{
  229. Revision: 4,
  230. },
  231. },
  232. },
  233. }
  234. func TestTxnOrdering(t *testing.T) {
  235. for i, tt := range txnTests {
  236. mKV := &mockKV{clientv3.NewKVFromKVClient(nil, nil), tt.response.OpResponse()}
  237. kv := &kvOrdering{
  238. mKV,
  239. func(r *clientv3.TxnResponse) OrderViolationFunc {
  240. return func(op clientv3.Op, resp clientv3.OpResponse, prevRev int64) error {
  241. r.Header.Revision++
  242. return nil
  243. }
  244. }(tt.response),
  245. tt.prevRev,
  246. sync.RWMutex{},
  247. }
  248. txn := &txnOrdering{
  249. kv.Txn(context.Background()),
  250. kv,
  251. context.Background(),
  252. sync.Mutex{},
  253. []clientv3.Cmp{},
  254. []clientv3.Op{},
  255. []clientv3.Op{},
  256. }
  257. res, err := txn.Commit()
  258. if err != nil {
  259. t.Errorf("#%d: expected response %+v, got error %+v", i, tt.response, err)
  260. }
  261. if rev := res.Header.Revision; rev < tt.prevRev {
  262. t.Errorf("#%d: expected revision %d, got %d", i, tt.prevRev, rev)
  263. }
  264. }
  265. }