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.

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