volume_growth.go 8.3 KB

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