volume_growth.go 6.7 KB

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