You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
6.5 KiB

  1. package topology
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "sync"
  6. "github.com/chrislusf/weed-fs/go/glog"
  7. "github.com/chrislusf/weed-fs/go/storage"
  8. )
  9. /*
  10. This package is created to resolve these replica placement issues:
  11. 1. growth factor for each replica level, e.g., add 10 volumes for 1 copy, 20 volumes for 2 copies, 30 volumes for 3 copies
  12. 2. in time of tight storage, how to reduce replica level
  13. 3. optimizing for hot data on faster disk, cold data on cheaper storage,
  14. 4. volume allocation for each bucket
  15. */
  16. type VolumeGrowOption struct {
  17. Collection string
  18. ReplicaPlacement *storage.ReplicaPlacement
  19. Ttl *storage.TTL
  20. DataCenter string
  21. Rack string
  22. DataNode string
  23. }
  24. type VolumeGrowth struct {
  25. accessLock sync.Mutex
  26. }
  27. func NewDefaultVolumeGrowth() *VolumeGrowth {
  28. return &VolumeGrowth{}
  29. }
  30. // one replication type may need rp.GetCopyCount() actual volumes
  31. // given copyCount, how many logical volumes to create
  32. func (vg *VolumeGrowth) findVolumeCount(copyCount int) (count int) {
  33. switch copyCount {
  34. case 1:
  35. count = 7
  36. case 2:
  37. count = 6
  38. case 3:
  39. count = 3
  40. default:
  41. count = 1
  42. }
  43. return
  44. }
  45. func (vg *VolumeGrowth) AutomaticGrowByType(option *VolumeGrowOption, topo *Topology) (count int, err error) {
  46. count, err = vg.GrowByCountAndType(vg.findVolumeCount(option.ReplicaPlacement.GetCopyCount()), option, topo)
  47. if count > 0 && count%option.ReplicaPlacement.GetCopyCount() == 0 {
  48. return count, nil
  49. }
  50. return count, err
  51. }
  52. func (vg *VolumeGrowth) GrowByCountAndType(targetCount int, option *VolumeGrowOption, topo *Topology) (counter int, err error) {
  53. vg.accessLock.Lock()
  54. defer vg.accessLock.Unlock()
  55. for i := 0; i < targetCount; i++ {
  56. if c, e := vg.findAndGrow(topo, option); e == nil {
  57. counter += c
  58. } else {
  59. return counter, e
  60. }
  61. }
  62. return
  63. }
  64. func (vg *VolumeGrowth) findAndGrow(topo *Topology, option *VolumeGrowOption) (int, error) {
  65. servers, e := vg.findEmptySlotsForOneVolume(topo, option)
  66. if e != nil {
  67. return 0, e
  68. }
  69. vid := topo.NextVolumeId()
  70. err := vg.grow(topo, vid, option, servers...)
  71. return len(servers), err
  72. }
  73. // 1. find the main data node
  74. // 1.1 collect all data nodes that have 1 slots
  75. // 2.2 collect all racks that have rp.SameRackCount+1
  76. // 2.2 collect all data centers that have DiffRackCount+rp.SameRackCount+1
  77. // 2. find rest data nodes
  78. func (vg *VolumeGrowth) findEmptySlotsForOneVolume(topo *Topology, option *VolumeGrowOption) (servers []*DataNode, err error) {
  79. //find main datacenter and other data centers
  80. rp := option.ReplicaPlacement
  81. mainDataCenter, otherDataCenters, dc_err := topo.RandomlyPickNodes(rp.DiffDataCenterCount+1, func(node Node) error {
  82. if option.DataCenter != "" && node.IsDataCenter() && node.Id() != NodeId(option.DataCenter) {
  83. return fmt.Errorf("Not matching preferred data center:%s", option.DataCenter)
  84. }
  85. if len(node.Children()) < rp.DiffRackCount+1 {
  86. return fmt.Errorf("Only has %d racks, not enough for %d.", len(node.Children()), rp.DiffRackCount+1)
  87. }
  88. if node.FreeSpace() < rp.DiffRackCount+rp.SameRackCount+1 {
  89. return fmt.Errorf("Free:%d < Expected:%d", node.FreeSpace(), rp.DiffRackCount+rp.SameRackCount+1)
  90. }
  91. possibleRacksCount := 0
  92. for _, rack := range node.Children() {
  93. possibleDataNodesCount := 0
  94. for _, n := range rack.Children() {
  95. if n.FreeSpace() >= 1 {
  96. possibleDataNodesCount++
  97. }
  98. }
  99. if possibleDataNodesCount >= rp.SameRackCount+1 {
  100. possibleRacksCount++
  101. }
  102. }
  103. if possibleRacksCount < rp.DiffRackCount+1 {
  104. return fmt.Errorf("Only has %d racks with more than %d free data nodes, not enough for %d.", possibleRacksCount, rp.SameRackCount+1, rp.DiffRackCount+1)
  105. }
  106. return nil
  107. })
  108. if dc_err != nil {
  109. return nil, dc_err
  110. }
  111. //find main rack and other racks
  112. mainRack, otherRacks, rack_err := mainDataCenter.(*DataCenter).RandomlyPickNodes(rp.DiffRackCount+1, func(node Node) error {
  113. if option.Rack != "" && node.IsRack() && node.Id() != NodeId(option.Rack) {
  114. return fmt.Errorf("Not matching preferred rack:%s", option.Rack)
  115. }
  116. if node.FreeSpace() < rp.SameRackCount+1 {
  117. return fmt.Errorf("Free:%d < Expected:%d", node.FreeSpace(), rp.SameRackCount+1)
  118. }
  119. if len(node.Children()) < rp.SameRackCount+1 {
  120. // a bit faster way to test free racks
  121. return fmt.Errorf("Only has %d data nodes, not enough for %d.", len(node.Children()), rp.SameRackCount+1)
  122. }
  123. possibleDataNodesCount := 0
  124. for _, n := range node.Children() {
  125. if n.FreeSpace() >= 1 {
  126. possibleDataNodesCount++
  127. }
  128. }
  129. if possibleDataNodesCount < rp.SameRackCount+1 {
  130. return fmt.Errorf("Only has %d data nodes with a slot, not enough for %d.", possibleDataNodesCount, rp.SameRackCount+1)
  131. }
  132. return nil
  133. })
  134. if rack_err != nil {
  135. return nil, rack_err
  136. }
  137. //find main rack and other racks
  138. mainServer, otherServers, server_err := mainRack.(*Rack).RandomlyPickNodes(rp.SameRackCount+1, func(node Node) error {
  139. if option.DataNode != "" && node.IsDataNode() && node.Id() != NodeId(option.DataNode) {
  140. return fmt.Errorf("Not matching preferred data node:%s", option.DataNode)
  141. }
  142. if node.FreeSpace() < 1 {
  143. return fmt.Errorf("Free:%d < Expected:%d", node.FreeSpace(), 1)
  144. }
  145. return nil
  146. })
  147. if server_err != nil {
  148. return nil, server_err
  149. }
  150. servers = append(servers, mainServer.(*DataNode))
  151. for _, server := range otherServers {
  152. servers = append(servers, server.(*DataNode))
  153. }
  154. for _, rack := range otherRacks {
  155. r := rand.Intn(rack.FreeSpace())
  156. if server, e := rack.ReserveOneVolume(r); e == nil {
  157. servers = append(servers, server)
  158. } else {
  159. return servers, e
  160. }
  161. }
  162. for _, datacenter := range otherDataCenters {
  163. r := rand.Intn(datacenter.FreeSpace())
  164. if server, e := datacenter.ReserveOneVolume(r); e == nil {
  165. servers = append(servers, server)
  166. } else {
  167. return servers, e
  168. }
  169. }
  170. return
  171. }
  172. func (vg *VolumeGrowth) grow(topo *Topology, vid storage.VolumeId, option *VolumeGrowOption, servers ...*DataNode) error {
  173. for _, server := range servers {
  174. if err := AllocateVolume(server, vid, option); err == nil {
  175. vi := storage.VolumeInfo{
  176. Id: vid,
  177. Size: 0,
  178. Collection: option.Collection,
  179. ReplicaPlacement: option.ReplicaPlacement,
  180. Ttl: option.Ttl,
  181. Version: storage.CurrentVersion,
  182. }
  183. server.AddOrUpdateVolume(vi)
  184. topo.RegisterVolumeLayout(vi, server)
  185. glog.V(0).Infoln("Created Volume", vid, "on", server)
  186. } else {
  187. glog.V(0).Infoln("Failed to assign", vid, "to", servers, "error", err)
  188. return fmt.Errorf("Failed to assign %s: %s", vid.String(), err.Error())
  189. }
  190. }
  191. return nil
  192. }