apply_v2.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 etcdserver
  15. import (
  16. "encoding/json"
  17. "path"
  18. "time"
  19. "github.com/coreos/etcd/etcdserver/api"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/etcd/etcdserver/membership"
  22. "github.com/coreos/etcd/pkg/pbutil"
  23. "github.com/coreos/etcd/store"
  24. "github.com/coreos/go-semver/semver"
  25. )
  26. // ApplierV2 is the interface for processing V2 raft messages
  27. type ApplierV2 interface {
  28. Delete(r *pb.Request) Response
  29. Post(r *pb.Request) Response
  30. Put(r *pb.Request) Response
  31. QGet(r *pb.Request) Response
  32. Sync(r *pb.Request) Response
  33. }
  34. func NewApplierV2(s store.Store, c *membership.RaftCluster) ApplierV2 {
  35. return &applierV2store{store: s, cluster: c}
  36. }
  37. type applierV2store struct {
  38. store store.Store
  39. cluster *membership.RaftCluster
  40. }
  41. func (a *applierV2store) Delete(r *pb.Request) Response {
  42. switch {
  43. case r.PrevIndex > 0 || r.PrevValue != "":
  44. return toResponse(a.store.CompareAndDelete(r.Path, r.PrevValue, r.PrevIndex))
  45. default:
  46. return toResponse(a.store.Delete(r.Path, r.Dir, r.Recursive))
  47. }
  48. }
  49. func (a *applierV2store) Post(r *pb.Request) Response {
  50. return toResponse(a.store.Create(r.Path, r.Dir, r.Val, true, toTTLOptions(r)))
  51. }
  52. func (a *applierV2store) Put(r *pb.Request) Response {
  53. ttlOptions := toTTLOptions(r)
  54. exists, existsSet := pbutil.GetBool(r.PrevExist)
  55. switch {
  56. case existsSet:
  57. if exists {
  58. if r.PrevIndex == 0 && r.PrevValue == "" {
  59. return toResponse(a.store.Update(r.Path, r.Val, ttlOptions))
  60. }
  61. return toResponse(a.store.CompareAndSwap(r.Path, r.PrevValue, r.PrevIndex, r.Val, ttlOptions))
  62. }
  63. return toResponse(a.store.Create(r.Path, r.Dir, r.Val, false, ttlOptions))
  64. case r.PrevIndex > 0 || r.PrevValue != "":
  65. return toResponse(a.store.CompareAndSwap(r.Path, r.PrevValue, r.PrevIndex, r.Val, ttlOptions))
  66. default:
  67. if storeMemberAttributeRegexp.MatchString(r.Path) {
  68. id := membership.MustParseMemberIDFromKey(path.Dir(r.Path))
  69. var attr membership.Attributes
  70. if err := json.Unmarshal([]byte(r.Val), &attr); err != nil {
  71. plog.Panicf("unmarshal %s should never fail: %v", r.Val, err)
  72. }
  73. if a.cluster != nil {
  74. a.cluster.UpdateAttributes(id, attr)
  75. }
  76. // return an empty response since there is no consumer.
  77. return Response{}
  78. }
  79. if r.Path == membership.StoreClusterVersionKey() {
  80. if a.cluster != nil {
  81. a.cluster.SetVersion(semver.Must(semver.NewVersion(r.Val)), api.UpdateCapability)
  82. }
  83. // return an empty response since there is no consumer.
  84. return Response{}
  85. }
  86. return toResponse(a.store.Set(r.Path, r.Dir, r.Val, ttlOptions))
  87. }
  88. }
  89. func (a *applierV2store) QGet(r *pb.Request) Response {
  90. return toResponse(a.store.Get(r.Path, r.Recursive, r.Sorted))
  91. }
  92. func (a *applierV2store) Sync(r *pb.Request) Response {
  93. a.store.DeleteExpiredKeys(time.Unix(0, r.Time))
  94. return Response{}
  95. }
  96. // applyV2Request interprets r as a call to store.X and returns a Response interpreted
  97. // from store.Event
  98. func (s *EtcdServer) applyV2Request(r *pb.Request) Response {
  99. toTTLOptions(r)
  100. switch r.Method {
  101. case "POST":
  102. return s.applyV2.Post(r)
  103. case "PUT":
  104. return s.applyV2.Put(r)
  105. case "DELETE":
  106. return s.applyV2.Delete(r)
  107. case "QGET":
  108. return s.applyV2.QGet(r)
  109. case "SYNC":
  110. return s.applyV2.Sync(r)
  111. default:
  112. // This should never be reached, but just in case:
  113. return Response{err: ErrUnknownMethod}
  114. }
  115. }
  116. func toTTLOptions(r *pb.Request) store.TTLOptionSet {
  117. refresh, _ := pbutil.GetBool(r.Refresh)
  118. ttlOptions := store.TTLOptionSet{Refresh: refresh}
  119. if r.Expiration != 0 {
  120. ttlOptions.ExpireTime = time.Unix(0, r.Expiration)
  121. }
  122. return ttlOptions
  123. }
  124. func toResponse(ev *store.Event, err error) Response {
  125. return Response{Event: ev, err: err}
  126. }