zk.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package dao
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "path"
  6. "go-common/app/admin/main/bfs/model"
  7. "go-common/library/log"
  8. )
  9. // Racks get rack infos.
  10. func (d *Dao) Racks(cluster string) (racks map[string]*model.Rack, err error) {
  11. zkConn, ok := d.zkcs[cluster]
  12. if !ok {
  13. err = fmt.Errorf("not support cluster:%s", cluster)
  14. return
  15. }
  16. root := d.c.Zookeepers[cluster].RackRoot
  17. // get all rocks
  18. rackNames, _, err := zkConn.Children(root)
  19. if err != nil {
  20. log.Error("get from:%s children error(%v)", root, err)
  21. return
  22. }
  23. racks = make(map[string]*model.Rack)
  24. for _, rackName := range rackNames {
  25. // get all the store
  26. rackPath := path.Join(root, rackName)
  27. storeNames, _, err := zkConn.Children(rackPath)
  28. if err != nil {
  29. log.Error("cannot get /rack/%s info error(%v)", rackName, err)
  30. continue
  31. }
  32. log.Info("rack:%s got store:%v", rackName, storeNames)
  33. rack := &model.Rack{Stores: make(map[string]*model.Store)}
  34. racks[rackName] = rack
  35. // get the volume of store
  36. for _, storeName := range storeNames {
  37. storePath := path.Join(rackPath, storeName)
  38. storeMeta, _, err := zkConn.Get(storePath)
  39. if err != nil {
  40. log.Error("cannot get from:%s info error(%v)", storePath, err)
  41. continue
  42. }
  43. var data model.Store
  44. if err = json.Unmarshal(storeMeta, &data); err != nil {
  45. log.Error("cannot Unmarshal %s info get from:%s error(%v)", storeMeta, storePath, err)
  46. continue
  47. }
  48. volumes, _, err := zkConn.Children(storePath)
  49. if err != nil {
  50. log.Error("cannot list %s info error(%v)", storePath, err)
  51. continue
  52. }
  53. data.Volumes = volumes
  54. log.Info("rack:%s store:%s got volume:%v", rackName, storeName, volumes)
  55. rack.Stores[storeName] = &data
  56. }
  57. }
  58. return
  59. }
  60. // Volumes get volume infos.
  61. func (d *Dao) Volumes(cluster string) (volumes map[string]*model.VolumeState, err error) {
  62. zkConn, ok := d.zkcs[cluster]
  63. if !ok {
  64. err = fmt.Errorf("not support cluster:%s", cluster)
  65. return
  66. }
  67. root := d.c.Zookeepers[cluster].VolumeRoot
  68. // get all volumes
  69. volumeIDs, _, err := zkConn.Children(root)
  70. if err != nil {
  71. log.Error("cannot get from:%s info error(%v)", root, err)
  72. return
  73. }
  74. log.Info("got volumes: %s", volumeIDs)
  75. volumes = make(map[string]*model.VolumeState)
  76. for _, id := range volumeIDs {
  77. // get all the volumes
  78. volumePath := path.Join(root, id)
  79. volumeState, _, err := zkConn.Get(volumePath)
  80. if err != nil {
  81. log.Error("cannot get %s info error(%v)", volumePath, err)
  82. continue
  83. }
  84. var data model.VolumeState
  85. if err := json.Unmarshal(volumeState, &data); err != nil {
  86. log.Error("cannot Unmarshal %s info get from:%s error(%v)", volumeState, volumePath, err)
  87. continue
  88. }
  89. volumes[id] = &data
  90. }
  91. return
  92. }
  93. // Groups get group infos.
  94. func (d *Dao) Groups(cluster string) (groups map[string]*model.Group, err error) {
  95. zkConn, ok := d.zkcs[cluster]
  96. if !ok {
  97. err = fmt.Errorf("not support cluster:%s", cluster)
  98. return
  99. }
  100. root := d.c.Zookeepers[cluster].GroupRoot
  101. // get all groups
  102. groupNames, _, err := zkConn.Children(root)
  103. if err != nil {
  104. log.Error("cannot get from:%s info error(%v)", root, err)
  105. return
  106. }
  107. log.Info("got groups: %s", groupNames)
  108. groups = make(map[string]*model.Group)
  109. for _, groupName := range groupNames {
  110. // get all the volumes
  111. groupPath := path.Join(root, groupName)
  112. storeNames, _, err := zkConn.Children(groupPath)
  113. if err != nil {
  114. log.Error("cannot get from:%s info error(%v)", groupPath, err)
  115. continue
  116. }
  117. groups[groupName] = &model.Group{
  118. Stores: storeNames,
  119. }
  120. }
  121. return
  122. }