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.

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