group_members.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package cluster
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb"
  4. "time"
  5. )
  6. type GroupMembers struct {
  7. members map[pb.ServerAddress]*ClusterNode
  8. }
  9. func newGroupMembers() *GroupMembers {
  10. return &GroupMembers{
  11. members: make(map[pb.ServerAddress]*ClusterNode),
  12. }
  13. }
  14. func (m *GroupMembers) addMember(dataCenter DataCenter, rack Rack, address pb.ServerAddress, version string) *ClusterNode {
  15. if existingNode, found := m.members[address]; found {
  16. existingNode.counter++
  17. return nil
  18. }
  19. t := &ClusterNode{
  20. Address: address,
  21. Version: version,
  22. counter: 1,
  23. CreatedTs: time.Now(),
  24. DataCenter: dataCenter,
  25. Rack: rack,
  26. }
  27. m.members[address] = t
  28. return t
  29. }
  30. func (m *GroupMembers) removeMember(address pb.ServerAddress) bool {
  31. if existingNode, found := m.members[address]; !found {
  32. return false
  33. } else {
  34. existingNode.counter--
  35. if existingNode.counter <= 0 {
  36. delete(m.members, address)
  37. return true
  38. }
  39. }
  40. return false
  41. }
  42. func (m *GroupMembers) GetMembers() (addresses []pb.ServerAddress) {
  43. for k := range m.members {
  44. addresses = append(addresses, k)
  45. }
  46. return
  47. }