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.

230 lines
7.6 KiB

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