stm.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2016 CoreOS, Inc.
  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 recipe
  15. import (
  16. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  17. v3 "github.com/coreos/etcd/clientv3"
  18. )
  19. // STM implements software transactional memory over etcd
  20. type STM struct {
  21. client *v3.Client
  22. kv v3.KV
  23. // rset holds the read key's value and revision of read
  24. rset map[string]*RemoteKV
  25. // wset holds the write key and its value
  26. wset map[string]string
  27. // aborted is whether user aborted the txn
  28. aborted bool
  29. apply func(*STM) error
  30. }
  31. // NewSTM creates new transaction loop for a given apply function.
  32. func NewSTM(client *v3.Client, apply func(*STM) error) <-chan error {
  33. s := &STM{client: client, kv: v3.NewKV(client), apply: apply}
  34. errc := make(chan error, 1)
  35. go func() {
  36. var err error
  37. for {
  38. s.clear()
  39. if err = apply(s); err != nil || s.aborted {
  40. break
  41. }
  42. if ok, cerr := s.commit(); ok || cerr != nil {
  43. err = cerr
  44. break
  45. }
  46. }
  47. errc <- err
  48. }()
  49. return errc
  50. }
  51. // Abort abandons the apply loop, letting the transaction close without a commit.
  52. func (s *STM) Abort() { s.aborted = true }
  53. // Get returns the value for a given key, inserting the key into the txn's rset.
  54. func (s *STM) Get(key string) (string, error) {
  55. if wv, ok := s.wset[key]; ok {
  56. return wv, nil
  57. }
  58. if rk, ok := s.rset[key]; ok {
  59. return rk.Value(), nil
  60. }
  61. rk, err := GetRemoteKV(s.kv, key)
  62. if err != nil {
  63. return "", err
  64. }
  65. // TODO: setup watchers to abort txn early
  66. s.rset[key] = rk
  67. return rk.Value(), nil
  68. }
  69. // Put adds a value for a key to the write set.
  70. func (s *STM) Put(key string, val string) { s.wset[key] = val }
  71. // commit attempts to apply the txn's changes to the server.
  72. func (s *STM) commit() (ok bool, rr error) {
  73. // read set must not change
  74. cmps := make([]v3.Cmp, 0, len(s.rset))
  75. for k, rk := range s.rset {
  76. // use < to support updating keys that don't exist yet
  77. cmp := v3.Compare(v3.ModifiedRevision(k), "<", rk.Revision()+1)
  78. cmps = append(cmps, cmp)
  79. }
  80. // apply all writes
  81. puts := make([]v3.Op, 0, len(s.wset))
  82. for k, v := range s.wset {
  83. puts = append(puts, v3.OpPut(k, v))
  84. }
  85. txnresp, err := s.kv.Txn(context.TODO()).If(cmps...).Then(puts...).Commit()
  86. return txnresp.Succeeded, err
  87. }
  88. func (s *STM) clear() {
  89. s.rset = make(map[string]*RemoteKV)
  90. s.wset = make(map[string]string)
  91. }