volume_growth.go 7.7 KB

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