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.

216 lines
7.0 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, raftErr := topo.NextVolumeId()
  75. if raftErr != nil {
  76. return 0, raftErr
  77. }
  78. err := vg.grow(grpcDialOption, topo, vid, option, servers...)
  79. return len(servers), err
  80. }
  81. // 1. find the main data node
  82. // 1.1 collect all data nodes that have 1 slots
  83. // 2.2 collect all racks that have rp.SameRackCount+1
  84. // 2.2 collect all data centers that have DiffRackCount+rp.SameRackCount+1
  85. // 2. find rest data nodes
  86. func (vg *VolumeGrowth) findEmptySlotsForOneVolume(topo *Topology, option *VolumeGrowOption) (servers []*DataNode, err error) {
  87. //find main datacenter and other data centers
  88. rp := option.ReplicaPlacement
  89. mainDataCenter, otherDataCenters, dc_err := topo.RandomlyPickNodes(rp.DiffDataCenterCount+1, func(node Node) error {
  90. if option.DataCenter != "" && node.IsDataCenter() && node.Id() != NodeId(option.DataCenter) {
  91. return fmt.Errorf("Not matching preferred data center:%s", option.DataCenter)
  92. }
  93. if len(node.Children()) < rp.DiffRackCount+1 {
  94. return fmt.Errorf("Only has %d racks, not enough for %d.", len(node.Children()), rp.DiffRackCount+1)
  95. }
  96. if node.FreeSpace() < rp.DiffRackCount+rp.SameRackCount+1 {
  97. return fmt.Errorf("Free:%d < Expected:%d", node.FreeSpace(), rp.DiffRackCount+rp.SameRackCount+1)
  98. }
  99. possibleRacksCount := 0
  100. for _, rack := range node.Children() {
  101. possibleDataNodesCount := 0
  102. for _, n := range rack.Children() {
  103. if n.FreeSpace() >= 1 {
  104. possibleDataNodesCount++
  105. }
  106. }
  107. if possibleDataNodesCount >= rp.SameRackCount+1 {
  108. possibleRacksCount++
  109. }
  110. }
  111. if possibleRacksCount < rp.DiffRackCount+1 {
  112. 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)
  113. }
  114. return nil
  115. })
  116. if dc_err != nil {
  117. return nil, dc_err
  118. }
  119. //find main rack and other racks
  120. mainRack, otherRacks, rackErr := mainDataCenter.(*DataCenter).RandomlyPickNodes(rp.DiffRackCount+1, func(node Node) error {
  121. if option.Rack != "" && node.IsRack() && node.Id() != NodeId(option.Rack) {
  122. return fmt.Errorf("Not matching preferred rack:%s", option.Rack)
  123. }
  124. if node.FreeSpace() < rp.SameRackCount+1 {
  125. return fmt.Errorf("Free:%d < Expected:%d", node.FreeSpace(), rp.SameRackCount+1)
  126. }
  127. if len(node.Children()) < rp.SameRackCount+1 {
  128. // a bit faster way to test free racks
  129. return fmt.Errorf("Only has %d data nodes, not enough for %d.", len(node.Children()), rp.SameRackCount+1)
  130. }
  131. possibleDataNodesCount := 0
  132. for _, n := range node.Children() {
  133. if n.FreeSpace() >= 1 {
  134. possibleDataNodesCount++
  135. }
  136. }
  137. if possibleDataNodesCount < rp.SameRackCount+1 {
  138. return fmt.Errorf("Only has %d data nodes with a slot, not enough for %d.", possibleDataNodesCount, rp.SameRackCount+1)
  139. }
  140. return nil
  141. })
  142. if rackErr != nil {
  143. return nil, rackErr
  144. }
  145. //find main rack and other racks
  146. mainServer, otherServers, serverErr := mainRack.(*Rack).RandomlyPickNodes(rp.SameRackCount+1, func(node Node) error {
  147. if option.DataNode != "" && node.IsDataNode() && node.Id() != NodeId(option.DataNode) {
  148. return fmt.Errorf("Not matching preferred data node:%s", option.DataNode)
  149. }
  150. if node.FreeSpace() < 1 {
  151. return fmt.Errorf("Free:%d < Expected:%d", node.FreeSpace(), 1)
  152. }
  153. return nil
  154. })
  155. if serverErr != nil {
  156. return nil, serverErr
  157. }
  158. servers = append(servers, mainServer.(*DataNode))
  159. for _, server := range otherServers {
  160. servers = append(servers, server.(*DataNode))
  161. }
  162. for _, rack := range otherRacks {
  163. r := rand.Intn(rack.FreeSpace())
  164. if server, e := rack.ReserveOneVolume(r); e == nil {
  165. servers = append(servers, server)
  166. } else {
  167. return servers, e
  168. }
  169. }
  170. for _, datacenter := range otherDataCenters {
  171. r := rand.Intn(datacenter.FreeSpace())
  172. if server, e := datacenter.ReserveOneVolume(r); e == nil {
  173. servers = append(servers, server)
  174. } else {
  175. return servers, e
  176. }
  177. }
  178. return
  179. }
  180. func (vg *VolumeGrowth) grow(grpcDialOption grpc.DialOption, topo *Topology, vid storage.VolumeId, option *VolumeGrowOption, servers ...*DataNode) error {
  181. for _, server := range servers {
  182. if err := AllocateVolume(server, grpcDialOption, vid, option); err == nil {
  183. vi := storage.VolumeInfo{
  184. Id: vid,
  185. Size: 0,
  186. Collection: option.Collection,
  187. ReplicaPlacement: option.ReplicaPlacement,
  188. Ttl: option.Ttl,
  189. Version: storage.CurrentVersion,
  190. }
  191. server.AddOrUpdateVolume(vi)
  192. topo.RegisterVolumeLayout(vi, server)
  193. glog.V(0).Infoln("Created Volume", vid, "on", server.NodeImpl.String())
  194. } else {
  195. glog.V(0).Infoln("Failed to assign volume", vid, "to", servers, "error", err)
  196. return fmt.Errorf("Failed to assign %d: %v", vid, err)
  197. }
  198. }
  199. return nil
  200. }