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.

213 lines
6.9 KiB

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