volume_growth.go 8.3 KB

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