apply_v2.go 3.9 KB

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