rack.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package topology
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  4. "github.com/chrislusf/seaweedfs/weed/storage/types"
  5. "strconv"
  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, 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(ip + ":" + strconv.Itoa(port))
  38. dn.Ip = ip
  39. dn.Port = port
  40. dn.PublicUrl = publicUrl
  41. dn.LastSeen = time.Now().Unix()
  42. r.LinkChildNode(dn)
  43. for diskType, maxVolumeCount := range maxVolumeCounts {
  44. disk := NewDisk(diskType)
  45. disk.diskUsages.getOrCreateDisk(types.ToDiskType(diskType)).maxVolumeCount = int64(maxVolumeCount)
  46. dn.LinkChildNode(disk)
  47. }
  48. return dn
  49. }
  50. func (r *Rack) ToMap() interface{} {
  51. m := make(map[string]interface{})
  52. m["Id"] = r.Id()
  53. var dns []interface{}
  54. for _, c := range r.Children() {
  55. dn := c.(*DataNode)
  56. dns = append(dns, dn.ToMap())
  57. }
  58. m["DataNodes"] = dns
  59. return m
  60. }
  61. func (r *Rack) ToRackInfo() *master_pb.RackInfo {
  62. m := &master_pb.RackInfo{
  63. Id: string(r.Id()),
  64. DiskInfos: r.diskUsages.ToDiskInfo(),
  65. }
  66. for _, c := range r.Children() {
  67. dn := c.(*DataNode)
  68. m.DataNodeInfos = append(m.DataNodeInfos, dn.ToDataNodeInfo())
  69. }
  70. return m
  71. }