stm.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "github.com/coreos/etcd/clientv3"
  18. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  19. )
  20. // STM implements software transactional memory over etcd
  21. type STM struct {
  22. client *clientv3.Client
  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 *clientv3.Client, apply func(*STM) error) <-chan error {
  33. s := &STM{client: 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, err := s.commit(); ok || err != nil {
  43. break
  44. }
  45. }
  46. errc <- err
  47. }()
  48. return errc
  49. }
  50. // Abort abandons the apply loop, letting the transaction close without a commit.
  51. func (s *STM) Abort() { s.aborted = true }
  52. // Get returns the value for a given key, inserting the key into the txn's rset.
  53. func (s *STM) Get(key string) (string, error) {
  54. if wv, ok := s.wset[key]; ok {
  55. return wv, nil
  56. }
  57. if rk, ok := s.rset[key]; ok {
  58. return rk.Value(), nil
  59. }
  60. rk, err := GetRemoteKV(s.client, key)
  61. if err != nil {
  62. return "", err
  63. }
  64. // TODO: setup watchers to abort txn early
  65. s.rset[key] = rk
  66. return rk.Value(), nil
  67. }
  68. // Put adds a value for a key to the write set.
  69. func (s *STM) Put(key string, val string) { s.wset[key] = val }
  70. // commit attempts to apply the txn's changes to the server.
  71. func (s *STM) commit() (ok bool, err error) {
  72. // read set must not change
  73. cmps := []*pb.Compare{}
  74. for k, rk := range s.rset {
  75. // use < to support updating keys that don't exist yet
  76. cmp := &pb.Compare{
  77. Result: pb.Compare_LESS,
  78. Target: pb.Compare_MOD,
  79. Key: []byte(k),
  80. TargetUnion: &pb.Compare_ModRevision{ModRevision: rk.Revision() + 1},
  81. }
  82. cmps = append(cmps, cmp)
  83. }
  84. // apply all writes
  85. puts := []*pb.RequestUnion{}
  86. for k, v := range s.wset {
  87. puts = append(puts, &pb.RequestUnion{
  88. Request: &pb.RequestUnion_RequestPut{
  89. RequestPut: &pb.PutRequest{
  90. Key: []byte(k),
  91. Value: []byte(v),
  92. }}})
  93. }
  94. txnresp, err := s.client.KV.Txn(context.TODO(), &pb.TxnRequest{cmps, puts, nil})
  95. return txnresp.Succeeded, err
  96. }
  97. func (s *STM) clear() {
  98. s.rset = make(map[string]*RemoteKV)
  99. s.wset = make(map[string]string)
  100. }