apply_v2.go 4.4 KB

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