member.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcdserver
  14. import (
  15. "crypto/sha1"
  16. "encoding/binary"
  17. "fmt"
  18. "log"
  19. "path"
  20. "strconv"
  21. "time"
  22. "github.com/coreos/etcd/pkg/types"
  23. )
  24. // RaftAttributes represents the raft related attributes of an etcd member.
  25. type RaftAttributes struct {
  26. // TODO(philips): ensure these are URLs
  27. PeerURLs []string
  28. }
  29. // Attributes represents all the non-raft related attributes of an etcd member.
  30. type Attributes struct {
  31. Name string
  32. ClientURLs []string
  33. }
  34. type Member struct {
  35. ID uint64
  36. RaftAttributes
  37. Attributes
  38. }
  39. // newMember creates a Member without an ID and generates one based on the
  40. // name, peer URLs. This is used for bootstrapping.
  41. func newMember(name string, peerURLs types.URLs, now *time.Time) *Member {
  42. m := &Member{
  43. RaftAttributes: RaftAttributes{PeerURLs: peerURLs.StringSlice()},
  44. Attributes: Attributes{Name: name},
  45. }
  46. b := []byte(m.Name)
  47. for _, p := range m.PeerURLs {
  48. b = append(b, []byte(p)...)
  49. }
  50. if now != nil {
  51. b = append(b, []byte(fmt.Sprintf("%d", now.Unix()))...)
  52. }
  53. hash := sha1.Sum(b)
  54. m.ID = binary.BigEndian.Uint64(hash[:8])
  55. return m
  56. }
  57. func memberStoreKey(id uint64) string {
  58. return path.Join(storeMembersPrefix, idAsHex(id))
  59. }
  60. func parseMemberID(key string) uint64 {
  61. id, err := strconv.ParseUint(path.Base(key), 16, 64)
  62. if err != nil {
  63. log.Panicf("unexpected parse member id error: %v", err)
  64. }
  65. return id
  66. }
  67. func removedMemberStoreKey(id uint64) string {
  68. return path.Join(storeRemovedMembersPrefix, idAsHex(id))
  69. }