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.

218 lines
7.0 KiB

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