rack.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package topology
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  4. "github.com/chrislusf/seaweedfs/weed/storage/types"
  5. "github.com/chrislusf/seaweedfs/weed/util"
  6. "time"
  7. )
  8. type Rack struct {
  9. NodeImpl
  10. }
  11. func NewRack(id string) *Rack {
  12. r := &Rack{}
  13. r.id = NodeId(id)
  14. r.nodeType = "Rack"
  15. r.diskUsages = newDiskUsages()
  16. r.children = make(map[NodeId]Node)
  17. r.NodeImpl.value = r
  18. return r
  19. }
  20. func (r *Rack) FindDataNode(ip string, port int) *DataNode {
  21. for _, c := range r.Children() {
  22. dn := c.(*DataNode)
  23. if dn.MatchLocation(ip, port) {
  24. return dn
  25. }
  26. }
  27. return nil
  28. }
  29. func (r *Rack) GetOrCreateDataNode(ip string, port int, grpcPort int, publicUrl string, maxVolumeCounts map[string]uint32) *DataNode {
  30. for _, c := range r.Children() {
  31. dn := c.(*DataNode)
  32. if dn.MatchLocation(ip, port) {
  33. dn.LastSeen = time.Now().Unix()
  34. return dn
  35. }
  36. }
  37. dn := NewDataNode(util.JoinHostPort(ip, port))
  38. dn.Ip = ip
  39. dn.Port = port
  40. dn.GrpcPort = grpcPort
  41. dn.PublicUrl = publicUrl
  42. dn.LastSeen = time.Now().Unix()
  43. r.LinkChildNode(dn)
  44. for diskType, maxVolumeCount := range maxVolumeCounts {
  45. disk := NewDisk(diskType)
  46. disk.diskUsages.getOrCreateDisk(types.ToDiskType(diskType)).maxVolumeCount = int64(maxVolumeCount)
  47. dn.LinkChildNode(disk)
  48. }
  49. return dn
  50. }
  51. func (r *Rack) ToMap() interface{} {
  52. m := make(map[string]interface{})
  53. m["Id"] = r.Id()
  54. var dns []interface{}
  55. for _, c := range r.Children() {
  56. dn := c.(*DataNode)
  57. dns = append(dns, dn.ToMap())
  58. }
  59. m["DataNodes"] = dns
  60. return m
  61. }
  62. func (r *Rack) ToRackInfo() *master_pb.RackInfo {
  63. m := &master_pb.RackInfo{
  64. Id: string(r.Id()),
  65. DiskInfos: r.diskUsages.ToDiskInfo(),
  66. }
  67. for _, c := range r.Children() {
  68. dn := c.(*DataNode)
  69. m.DataNodeInfos = append(m.DataNodeInfos, dn.ToDataNodeInfo())
  70. }
  71. return m
  72. }