store.go 5.8 KB

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