store.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 membership
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "path"
  19. "github.com/coreos/etcd/pkg/types"
  20. "github.com/coreos/etcd/storage/backend"
  21. "github.com/coreos/etcd/store"
  22. )
  23. const (
  24. attributesSuffix = "attributes"
  25. raftAttributesSuffix = "raftAttributes"
  26. // the prefix for stroing membership related information in store provided by store pkg.
  27. storePrefix = "/0"
  28. )
  29. var (
  30. membersBucketName = []byte("members")
  31. membersRemovedBuckedName = []byte("members_removed")
  32. StoreMembersPrefix = path.Join(storePrefix, "members")
  33. storeRemovedMembersPrefix = path.Join(storePrefix, "removed_members")
  34. )
  35. func mustSaveMemberToBackend(be backend.Backend, m *Member) {
  36. mkey := backendMemberKey(m.ID)
  37. mvalue, err := json.Marshal(m)
  38. if err != nil {
  39. plog.Panicf("marshal raftAttributes should never fail: %v", err)
  40. }
  41. tx := be.BatchTx()
  42. tx.Lock()
  43. tx.UnsafePut(membersBucketName, mkey, mvalue)
  44. tx.Unlock()
  45. }
  46. func mustDeleteMemberFromBackend(be backend.Backend, id types.ID) {
  47. mkey := backendMemberKey(id)
  48. tx := be.BatchTx()
  49. tx.Lock()
  50. tx.UnsafeDelete(membersBucketName, mkey)
  51. tx.UnsafePut(membersRemovedBuckedName, mkey, []byte("removed"))
  52. tx.Unlock()
  53. }
  54. func mustSaveMemberToStore(s store.Store, m *Member) {
  55. b, err := json.Marshal(m.RaftAttributes)
  56. if err != nil {
  57. plog.Panicf("marshal raftAttributes should never fail: %v", err)
  58. }
  59. p := path.Join(MemberStoreKey(m.ID), raftAttributesSuffix)
  60. if _, err := s.Create(p, false, string(b), false, store.TTLOptionSet{ExpireTime: store.Permanent}); err != nil {
  61. plog.Panicf("create raftAttributes should never fail: %v", err)
  62. }
  63. }
  64. func mustDeleteMemberFromStore(s store.Store, id types.ID) {
  65. if _, err := s.Delete(MemberStoreKey(id), true, true); err != nil {
  66. plog.Panicf("delete member should never fail: %v", err)
  67. }
  68. if _, err := s.Create(RemovedMemberStoreKey(id), false, "", false, store.TTLOptionSet{ExpireTime: store.Permanent}); err != nil {
  69. plog.Panicf("create removedMember should never fail: %v", err)
  70. }
  71. }
  72. func mustUpdateMemberInStore(s store.Store, m *Member) {
  73. b, err := json.Marshal(m.RaftAttributes)
  74. if err != nil {
  75. plog.Panicf("marshal raftAttributes should never fail: %v", err)
  76. }
  77. p := path.Join(MemberStoreKey(m.ID), raftAttributesSuffix)
  78. if _, err := s.Update(p, string(b), store.TTLOptionSet{ExpireTime: store.Permanent}); err != nil {
  79. plog.Panicf("update raftAttributes should never fail: %v", err)
  80. }
  81. }
  82. func mustUpdateMemberAttrInStore(s store.Store, m *Member) {
  83. b, err := json.Marshal(m.Attributes)
  84. if err != nil {
  85. plog.Panicf("marshal raftAttributes should never fail: %v", err)
  86. }
  87. p := path.Join(MemberStoreKey(m.ID), attributesSuffix)
  88. if _, err := s.Set(p, false, string(b), store.TTLOptionSet{ExpireTime: store.Permanent}); err != nil {
  89. plog.Panicf("update raftAttributes should never fail: %v", err)
  90. }
  91. }
  92. // nodeToMember builds member from a key value node.
  93. // the child nodes of the given node MUST be sorted by key.
  94. func nodeToMember(n *store.NodeExtern) (*Member, error) {
  95. m := &Member{ID: MustParseMemberIDFromKey(n.Key)}
  96. attrs := make(map[string][]byte)
  97. raftAttrKey := path.Join(n.Key, raftAttributesSuffix)
  98. attrKey := path.Join(n.Key, attributesSuffix)
  99. for _, nn := range n.Nodes {
  100. if nn.Key != raftAttrKey && nn.Key != attrKey {
  101. return nil, fmt.Errorf("unknown key %q", nn.Key)
  102. }
  103. attrs[nn.Key] = []byte(*nn.Value)
  104. }
  105. if data := attrs[raftAttrKey]; data != nil {
  106. if err := json.Unmarshal(data, &m.RaftAttributes); err != nil {
  107. return nil, fmt.Errorf("unmarshal raftAttributes error: %v", err)
  108. }
  109. } else {
  110. return nil, fmt.Errorf("raftAttributes key doesn't exist")
  111. }
  112. if data := attrs[attrKey]; data != nil {
  113. if err := json.Unmarshal(data, &m.Attributes); err != nil {
  114. return m, fmt.Errorf("unmarshal attributes error: %v", err)
  115. }
  116. }
  117. return m, nil
  118. }
  119. func backendMemberKey(id types.ID) []byte {
  120. return []byte(id.String())
  121. }
  122. func mustCreateBackendMemberBucket(be backend.Backend) {
  123. tx := be.BatchTx()
  124. tx.Lock()
  125. defer tx.Unlock()
  126. tx.UnsafeCreateBucket(membersBucketName)
  127. tx.UnsafeCreateBucket(membersRemovedBuckedName)
  128. }
  129. func MemberStoreKey(id types.ID) string {
  130. return path.Join(StoreMembersPrefix, id.String())
  131. }
  132. func MemberAttributesStorePath(id types.ID) string {
  133. return path.Join(MemberStoreKey(id), attributesSuffix)
  134. }
  135. func MustParseMemberIDFromKey(key string) types.ID {
  136. id, err := types.IDFromString(path.Base(key))
  137. if err != nil {
  138. plog.Panicf("unexpected parse member id error: %v", err)
  139. }
  140. return id
  141. }
  142. func RemovedMemberStoreKey(id types.ID) string {
  143. return path.Join(storeRemovedMembersPrefix, id.String())
  144. }