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.

198 lines
6.4 KiB

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