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.

297 lines
9.5 KiB

5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
5 years ago
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/pb"
  7. "io"
  8. "sync"
  9. "time"
  10. "google.golang.org/grpc"
  11. "github.com/chrislusf/seaweedfs/weed/operation"
  12. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  13. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  14. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  15. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  16. "github.com/chrislusf/seaweedfs/weed/wdclient"
  17. )
  18. func init() {
  19. Commands = append(Commands, &commandEcEncode{})
  20. }
  21. type commandEcEncode struct {
  22. }
  23. func (c *commandEcEncode) Name() string {
  24. return "ec.encode"
  25. }
  26. func (c *commandEcEncode) Help() string {
  27. return `apply erasure coding to a volume
  28. ec.encode [-collection=""] [-fullPercent=95 -quietFor=1h]
  29. ec.encode [-collection=""] [-volumeId=<volume_id>]
  30. This command will:
  31. 1. freeze one volume
  32. 2. apply erasure coding to the volume
  33. 3. move the encoded shards to multiple volume servers
  34. The erasure coding is 10.4. So ideally you have more than 14 volume servers, and you can afford
  35. to lose 4 volume servers.
  36. If the number of volumes are not high, the worst case is that you only have 4 volume servers,
  37. and the shards are spread as 4,4,3,3, respectively. You can afford to lose one volume server.
  38. If you only have less than 4 volume servers, with erasure coding, at least you can afford to
  39. have 4 corrupted shard files.
  40. `
  41. }
  42. func (c *commandEcEncode) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  43. encodeCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  44. volumeId := encodeCommand.Int("volumeId", 0, "the volume id")
  45. collection := encodeCommand.String("collection", "", "the collection name")
  46. fullPercentage := encodeCommand.Float64("fullPercent", 95, "the volume reaches the percentage of max volume size")
  47. quietPeriod := encodeCommand.Duration("quietFor", time.Hour, "select volumes without no writes for this period")
  48. parallelCopy := encodeCommand.Bool("parallelCopy", true, "copy shards in parallel")
  49. if err = encodeCommand.Parse(args); err != nil {
  50. return nil
  51. }
  52. if err = commandEnv.confirmIsLocked(); err != nil {
  53. return
  54. }
  55. vid := needle.VolumeId(*volumeId)
  56. // volumeId is provided
  57. if vid != 0 {
  58. return doEcEncode(commandEnv, *collection, vid, *parallelCopy)
  59. }
  60. // apply to all volumes in the collection
  61. volumeIds, err := collectVolumeIdsForEcEncode(commandEnv, *collection, *fullPercentage, *quietPeriod)
  62. if err != nil {
  63. return err
  64. }
  65. fmt.Printf("ec encode volumes: %v\n", volumeIds)
  66. for _, vid := range volumeIds {
  67. if err = doEcEncode(commandEnv, *collection, vid, *parallelCopy); err != nil {
  68. return err
  69. }
  70. }
  71. return nil
  72. }
  73. func doEcEncode(commandEnv *CommandEnv, collection string, vid needle.VolumeId, parallelCopy bool) (err error) {
  74. // find volume location
  75. locations, found := commandEnv.MasterClient.GetLocations(uint32(vid))
  76. if !found {
  77. return fmt.Errorf("volume %d not found", vid)
  78. }
  79. // fmt.Printf("found ec %d shards on %v\n", vid, locations)
  80. // mark the volume as readonly
  81. err = markVolumeReplicasWritable(commandEnv.option.GrpcDialOption, vid, locations, false)
  82. if err != nil {
  83. return fmt.Errorf("mark volume %d as readonly on %s: %v", vid, locations[0].Url, err)
  84. }
  85. // generate ec shards
  86. err = generateEcShards(commandEnv.option.GrpcDialOption, vid, collection, locations[0].ServerAddress())
  87. if err != nil {
  88. return fmt.Errorf("generate ec shards for volume %d on %s: %v", vid, locations[0].Url, err)
  89. }
  90. // balance the ec shards to current cluster
  91. err = spreadEcShards(commandEnv, vid, collection, locations, parallelCopy)
  92. if err != nil {
  93. return fmt.Errorf("spread ec shards for volume %d from %s: %v", vid, locations[0].Url, err)
  94. }
  95. return nil
  96. }
  97. func generateEcShards(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, collection string, sourceVolumeServer pb.ServerAddress) error {
  98. fmt.Printf("generateEcShards %s %d on %s ...\n", collection, volumeId, sourceVolumeServer)
  99. err := operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  100. _, genErr := volumeServerClient.VolumeEcShardsGenerate(context.Background(), &volume_server_pb.VolumeEcShardsGenerateRequest{
  101. VolumeId: uint32(volumeId),
  102. Collection: collection,
  103. })
  104. return genErr
  105. })
  106. return err
  107. }
  108. func spreadEcShards(commandEnv *CommandEnv, volumeId needle.VolumeId, collection string, existingLocations []wdclient.Location, parallelCopy bool) (err error) {
  109. allEcNodes, totalFreeEcSlots, err := collectEcNodes(commandEnv, "")
  110. if err != nil {
  111. return err
  112. }
  113. if totalFreeEcSlots < erasure_coding.TotalShardsCount {
  114. return fmt.Errorf("not enough free ec shard slots. only %d left", totalFreeEcSlots)
  115. }
  116. allocatedDataNodes := allEcNodes
  117. if len(allocatedDataNodes) > erasure_coding.TotalShardsCount {
  118. allocatedDataNodes = allocatedDataNodes[:erasure_coding.TotalShardsCount]
  119. }
  120. // calculate how many shards to allocate for these servers
  121. allocatedEcIds := balancedEcDistribution(allocatedDataNodes)
  122. // ask the data nodes to copy from the source volume server
  123. copiedShardIds, err := parallelCopyEcShardsFromSource(commandEnv.option.GrpcDialOption, allocatedDataNodes, allocatedEcIds, volumeId, collection, existingLocations[0], parallelCopy)
  124. if err != nil {
  125. return err
  126. }
  127. // unmount the to be deleted shards
  128. err = unmountEcShards(commandEnv.option.GrpcDialOption, volumeId, existingLocations[0].ServerAddress(), copiedShardIds)
  129. if err != nil {
  130. return err
  131. }
  132. // ask the source volume server to clean up copied ec shards
  133. err = sourceServerDeleteEcShards(commandEnv.option.GrpcDialOption, collection, volumeId, existingLocations[0].ServerAddress(), copiedShardIds)
  134. if err != nil {
  135. return fmt.Errorf("source delete copied ecShards %s %d.%v: %v", existingLocations[0].Url, volumeId, copiedShardIds, err)
  136. }
  137. // ask the source volume server to delete the original volume
  138. for _, location := range existingLocations {
  139. fmt.Printf("delete volume %d from %s\n", volumeId, location.Url)
  140. err = deleteVolume(commandEnv.option.GrpcDialOption, volumeId, location.ServerAddress())
  141. if err != nil {
  142. return fmt.Errorf("deleteVolume %s volume %d: %v", location.Url, volumeId, err)
  143. }
  144. }
  145. return err
  146. }
  147. func parallelCopyEcShardsFromSource(grpcDialOption grpc.DialOption, targetServers []*EcNode, allocatedEcIds [][]uint32, volumeId needle.VolumeId, collection string, existingLocation wdclient.Location, parallelCopy bool) (actuallyCopied []uint32, err error) {
  148. fmt.Printf("parallelCopyEcShardsFromSource %d %s\n", volumeId, existingLocation.Url)
  149. var wg sync.WaitGroup
  150. shardIdChan := make(chan []uint32, len(targetServers))
  151. copyFunc := func(server *EcNode, allocatedEcShardIds []uint32) {
  152. defer wg.Done()
  153. copiedShardIds, copyErr := oneServerCopyAndMountEcShardsFromSource(grpcDialOption, server,
  154. allocatedEcShardIds, volumeId, collection, existingLocation.ServerAddress())
  155. if copyErr != nil {
  156. err = copyErr
  157. } else {
  158. shardIdChan <- copiedShardIds
  159. server.addEcVolumeShards(volumeId, collection, copiedShardIds)
  160. }
  161. }
  162. cleanupFunc := func(server *EcNode, allocatedEcShardIds []uint32) {
  163. if err := unmountEcShards(grpcDialOption, volumeId, pb.NewServerAddressFromDataNode(server.info), allocatedEcShardIds); err != nil {
  164. fmt.Printf("unmount aborted shards %d.%v on %s: %v\n", volumeId, allocatedEcShardIds, server.info.Id, err)
  165. }
  166. if err := sourceServerDeleteEcShards(grpcDialOption, collection, volumeId, pb.NewServerAddressFromDataNode(server.info), allocatedEcShardIds); err != nil {
  167. fmt.Printf("remove aborted shards %d.%v on %s: %v\n", volumeId, allocatedEcShardIds, server.info.Id, err)
  168. }
  169. }
  170. // maybe parallelize
  171. for i, server := range targetServers {
  172. if len(allocatedEcIds[i]) <= 0 {
  173. continue
  174. }
  175. wg.Add(1)
  176. if parallelCopy {
  177. go copyFunc(server, allocatedEcIds[i])
  178. } else {
  179. copyFunc(server, allocatedEcIds[i])
  180. }
  181. }
  182. wg.Wait()
  183. close(shardIdChan)
  184. if err != nil {
  185. for i, server := range targetServers {
  186. if len(allocatedEcIds[i]) <= 0 {
  187. continue
  188. }
  189. cleanupFunc(server, allocatedEcIds[i])
  190. }
  191. return nil, err
  192. }
  193. for shardIds := range shardIdChan {
  194. actuallyCopied = append(actuallyCopied, shardIds...)
  195. }
  196. return
  197. }
  198. func balancedEcDistribution(servers []*EcNode) (allocated [][]uint32) {
  199. allocated = make([][]uint32, len(servers))
  200. allocatedShardIdIndex := uint32(0)
  201. serverIndex := 0
  202. for allocatedShardIdIndex < erasure_coding.TotalShardsCount {
  203. if servers[serverIndex].freeEcSlot > 0 {
  204. allocated[serverIndex] = append(allocated[serverIndex], allocatedShardIdIndex)
  205. allocatedShardIdIndex++
  206. }
  207. serverIndex++
  208. if serverIndex >= len(servers) {
  209. serverIndex = 0
  210. }
  211. }
  212. return allocated
  213. }
  214. func collectVolumeIdsForEcEncode(commandEnv *CommandEnv, selectedCollection string, fullPercentage float64, quietPeriod time.Duration) (vids []needle.VolumeId, err error) {
  215. // collect topology information
  216. topologyInfo, volumeSizeLimitMb, err := collectTopologyInfo(commandEnv)
  217. if err != nil {
  218. return
  219. }
  220. quietSeconds := int64(quietPeriod / time.Second)
  221. nowUnixSeconds := time.Now().Unix()
  222. fmt.Printf("collect volumes quiet for: %d seconds\n", quietSeconds)
  223. vidMap := make(map[uint32]bool)
  224. eachDataNode(topologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  225. for _, diskInfo := range dn.DiskInfos {
  226. for _, v := range diskInfo.VolumeInfos {
  227. if v.Collection == selectedCollection && v.ModifiedAtSecond+quietSeconds < nowUnixSeconds {
  228. if float64(v.Size) > fullPercentage/100*float64(volumeSizeLimitMb)*1024*1024 {
  229. vidMap[v.Id] = true
  230. }
  231. }
  232. }
  233. }
  234. })
  235. for vid := range vidMap {
  236. vids = append(vids, needle.VolumeId(vid))
  237. }
  238. return
  239. }