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