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.

232 lines
7.9 KiB

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