v3_stm_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright 2016 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 integration
  15. import (
  16. "fmt"
  17. "math/rand"
  18. "strconv"
  19. "testing"
  20. v3 "github.com/coreos/etcd/clientv3"
  21. "github.com/coreos/etcd/clientv3/concurrency"
  22. "golang.org/x/net/context"
  23. )
  24. // TestSTMConflict tests that conflicts are retried.
  25. func TestSTMConflict(t *testing.T) {
  26. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  27. defer clus.Terminate(t)
  28. etcdc := clus.RandClient()
  29. keys := make([]string, 5)
  30. for i := 0; i < len(keys); i++ {
  31. keys[i] = fmt.Sprintf("foo-%d", i)
  32. if _, err := etcdc.Put(context.TODO(), keys[i], "100"); err != nil {
  33. t.Fatalf("could not make key (%v)", err)
  34. }
  35. }
  36. errc := make(chan error)
  37. for i := range keys {
  38. curEtcdc := clus.RandClient()
  39. srcKey := keys[i]
  40. applyf := func(stm concurrency.STM) error {
  41. src := stm.Get(srcKey)
  42. // must be different key to avoid double-adding
  43. dstKey := srcKey
  44. for dstKey == srcKey {
  45. dstKey = keys[rand.Intn(len(keys))]
  46. }
  47. dst := stm.Get(dstKey)
  48. srcV, _ := strconv.ParseInt(src, 10, 64)
  49. dstV, _ := strconv.ParseInt(dst, 10, 64)
  50. if srcV == 0 {
  51. // can't rand.Intn on 0, so skip this transaction
  52. return nil
  53. }
  54. xfer := int64(rand.Intn(int(srcV)) / 2)
  55. stm.Put(srcKey, fmt.Sprintf("%d", srcV-xfer))
  56. stm.Put(dstKey, fmt.Sprintf("%d", dstV+xfer))
  57. return nil
  58. }
  59. go func() {
  60. _, err := concurrency.NewSTMRepeatable(context.TODO(), curEtcdc, applyf)
  61. errc <- err
  62. }()
  63. }
  64. // wait for txns
  65. for range keys {
  66. if err := <-errc; err != nil {
  67. t.Fatalf("apply failed (%v)", err)
  68. }
  69. }
  70. // ensure sum matches initial sum
  71. sum := 0
  72. for _, oldkey := range keys {
  73. rk, err := etcdc.Get(context.TODO(), oldkey)
  74. if err != nil {
  75. t.Fatalf("couldn't fetch key %s (%v)", oldkey, err)
  76. }
  77. v, _ := strconv.ParseInt(string(rk.Kvs[0].Value), 10, 64)
  78. sum += int(v)
  79. }
  80. if sum != len(keys)*100 {
  81. t.Fatalf("bad sum. got %d, expected %d", sum, len(keys)*100)
  82. }
  83. }
  84. // TestSTMPutNewKey confirms a STM put on a new key is visible after commit.
  85. func TestSTMPutNewKey(t *testing.T) {
  86. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  87. defer clus.Terminate(t)
  88. etcdc := clus.RandClient()
  89. applyf := func(stm concurrency.STM) error {
  90. stm.Put("foo", "bar")
  91. return nil
  92. }
  93. if _, err := concurrency.NewSTMRepeatable(context.TODO(), etcdc, applyf); err != nil {
  94. t.Fatalf("error on stm txn (%v)", err)
  95. }
  96. resp, err := etcdc.Get(context.TODO(), "foo")
  97. if err != nil {
  98. t.Fatalf("error fetching key (%v)", err)
  99. }
  100. if string(resp.Kvs[0].Value) != "bar" {
  101. t.Fatalf("bad value. got %+v, expected 'bar' value", resp)
  102. }
  103. }
  104. // TestSTMAbort tests that an aborted txn does not modify any keys.
  105. func TestSTMAbort(t *testing.T) {
  106. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  107. defer clus.Terminate(t)
  108. etcdc := clus.RandClient()
  109. ctx, cancel := context.WithCancel(context.TODO())
  110. applyf := func(stm concurrency.STM) error {
  111. stm.Put("foo", "baz")
  112. cancel()
  113. stm.Put("foo", "bap")
  114. return nil
  115. }
  116. if _, err := concurrency.NewSTMRepeatable(ctx, etcdc, applyf); err == nil {
  117. t.Fatalf("no error on stm txn")
  118. }
  119. resp, err := etcdc.Get(context.TODO(), "foo")
  120. if err != nil {
  121. t.Fatalf("error fetching key (%v)", err)
  122. }
  123. if len(resp.Kvs) != 0 {
  124. t.Fatalf("bad value. got %+v, expected nothing", resp)
  125. }
  126. }
  127. // TestSTMSerialize tests that serialization is honored when serializable.
  128. func TestSTMSerialize(t *testing.T) {
  129. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  130. defer clus.Terminate(t)
  131. etcdc := clus.RandClient()
  132. // set up initial keys
  133. keys := make([]string, 5)
  134. for i := 0; i < len(keys); i++ {
  135. keys[i] = fmt.Sprintf("foo-%d", i)
  136. }
  137. // update keys in full batches
  138. updatec := make(chan struct{})
  139. go func() {
  140. defer close(updatec)
  141. for i := 0; i < 5; i++ {
  142. s := fmt.Sprintf("%d", i)
  143. ops := []v3.Op{}
  144. for _, k := range keys {
  145. ops = append(ops, v3.OpPut(k, s))
  146. }
  147. if _, err := etcdc.Txn(context.TODO()).Then(ops...).Commit(); err != nil {
  148. t.Fatalf("couldn't put keys (%v)", err)
  149. }
  150. updatec <- struct{}{}
  151. }
  152. }()
  153. // read all keys in txn, make sure all values match
  154. errc := make(chan error)
  155. for range updatec {
  156. curEtcdc := clus.RandClient()
  157. applyf := func(stm concurrency.STM) error {
  158. vs := []string{}
  159. for i := range keys {
  160. vs = append(vs, stm.Get(keys[i]))
  161. }
  162. for i := range vs {
  163. if vs[0] != vs[i] {
  164. return fmt.Errorf("got vs[%d] = %v, want %v", i, vs[i], vs[0])
  165. }
  166. }
  167. return nil
  168. }
  169. go func() {
  170. _, err := concurrency.NewSTMSerializable(context.TODO(), curEtcdc, applyf)
  171. errc <- err
  172. }()
  173. }
  174. for i := 0; i < 5; i++ {
  175. if err := <-errc; err != nil {
  176. t.Error(err)
  177. }
  178. }
  179. }
  180. // TestSTMApplyOnConcurrentDeletion ensures that concurrent key deletion
  181. // fails the first GET revision comparison within STM; trigger retry.
  182. func TestSTMApplyOnConcurrentDeletion(t *testing.T) {
  183. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  184. defer clus.Terminate(t)
  185. etcdc := clus.RandClient()
  186. if _, err := etcdc.Put(context.TODO(), "foo", "bar"); err != nil {
  187. t.Fatal(err)
  188. }
  189. donec, readyc := make(chan struct{}), make(chan struct{})
  190. go func() {
  191. <-readyc
  192. if _, err := etcdc.Delete(context.TODO(), "foo"); err != nil {
  193. t.Fatal(err)
  194. }
  195. close(donec)
  196. }()
  197. try := 0
  198. applyf := func(stm concurrency.STM) error {
  199. try++
  200. stm.Get("foo")
  201. if try == 1 {
  202. // trigger delete to make GET rev comparison outdated
  203. close(readyc)
  204. <-donec
  205. }
  206. stm.Put("foo2", "bar2")
  207. return nil
  208. }
  209. if _, err := concurrency.NewSTMRepeatable(context.TODO(), etcdc, applyf); err != nil {
  210. t.Fatalf("error on stm txn (%v)", err)
  211. }
  212. if try != 2 {
  213. t.Fatalf("STM apply expected to run twice, got %d", try)
  214. }
  215. resp, err := etcdc.Get(context.TODO(), "foo2")
  216. if err != nil {
  217. t.Fatalf("error fetching key (%v)", err)
  218. }
  219. if string(resp.Kvs[0].Value) != "bar2" {
  220. t.Fatalf("bad value. got %+v, expected 'bar2' value", resp)
  221. }
  222. }