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.

327 lines
9.8 KiB

  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "sort"
  8. "sync"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  12. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  13. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  14. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  15. "github.com/chrislusf/seaweedfs/weed/wdclient"
  16. "google.golang.org/grpc"
  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. This command will:
  29. 1. freeze one volume
  30. 2. apply erasure coding to the volume
  31. 3. move the encoded shards to multiple volume servers
  32. The erasure coding is 10.4. So ideally you have more than 14 volume servers, and you can afford
  33. to lose 4 volume servers.
  34. If the number of volumes are not high, the worst case is that you only have 4 volume servers,
  35. and the shards are spread as 4,4,3,3, respectively. You can afford to lose one volume server.
  36. If you only have less than 4 volume servers, with erasure coding, at least you can afford to
  37. have 4 corrupted shard files.
  38. `
  39. }
  40. func (c *commandEcEncode) Do(args []string, commandEnv *commandEnv, writer io.Writer) (err error) {
  41. encodeCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  42. volumeId := encodeCommand.Int("volumeId", 0, "the volume id")
  43. collection := encodeCommand.String("collection", "", "the collection name")
  44. if err = encodeCommand.Parse(args); err != nil {
  45. return nil
  46. }
  47. ctx := context.Background()
  48. // find volume location
  49. locations := commandEnv.masterClient.GetLocations(uint32(*volumeId))
  50. if len(locations) == 0 {
  51. return fmt.Errorf("volume %d not found", *volumeId)
  52. }
  53. // generate ec shards
  54. err = generateEcShards(ctx, commandEnv.option.GrpcDialOption, needle.VolumeId(*volumeId), *collection, locations[0].Url)
  55. if err != nil {
  56. return fmt.Errorf("generate ec shards for volume %d on %s: %v", *volumeId, locations[0].Url, err)
  57. }
  58. // balance the ec shards to current cluster
  59. err = balanceEcShards(ctx, commandEnv, needle.VolumeId(*volumeId), *collection, locations)
  60. if err != nil {
  61. return fmt.Errorf("balance ec shards for volume %d on %s: %v", *volumeId, locations[0].Url, err)
  62. }
  63. return err
  64. }
  65. func generateEcShards(ctx context.Context, grpcDialOption grpc.DialOption, volumeId needle.VolumeId, collection string, sourceVolumeServer string) error {
  66. err := operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  67. _, genErr := volumeServerClient.VolumeEcShardsGenerate(ctx, &volume_server_pb.VolumeEcShardsGenerateRequest{
  68. VolumeId: uint32(volumeId),
  69. Collection: collection,
  70. })
  71. return genErr
  72. })
  73. return err
  74. }
  75. func balanceEcShards(ctx context.Context, commandEnv *commandEnv, volumeId needle.VolumeId, collection string, existingLocations []wdclient.Location) (err error) {
  76. allEcNodes, totalFreeEcSlots, err := collectEcNodes(ctx, commandEnv)
  77. if err != nil {
  78. return err
  79. }
  80. if totalFreeEcSlots < erasure_coding.TotalShardsCount {
  81. return fmt.Errorf("not enough free ec shard slots. only %d left", totalFreeEcSlots)
  82. }
  83. allocatedDataNodes := allEcNodes
  84. if len(allocatedDataNodes) > erasure_coding.TotalShardsCount {
  85. allocatedDataNodes = allocatedDataNodes[:erasure_coding.TotalShardsCount]
  86. }
  87. // calculate how many shards to allocate for these servers
  88. allocated := balancedEcDistribution(allocatedDataNodes)
  89. // ask the data nodes to copy from the source volume server
  90. copiedShardIds, err := parallelCopyEcShardsFromSource(ctx, commandEnv.option.GrpcDialOption, allocatedDataNodes, allocated, volumeId, collection, existingLocations[0])
  91. if err != nil {
  92. return nil
  93. }
  94. // ask the source volume server to clean up copied ec shards
  95. err = sourceServerDeleteEcShards(ctx, commandEnv.option.GrpcDialOption, volumeId, existingLocations[0], copiedShardIds)
  96. if err != nil {
  97. return fmt.Errorf("sourceServerDeleteEcShards %s %d.%v: %v", existingLocations[0], volumeId, copiedShardIds, err)
  98. }
  99. // ask the source volume server to delete the original volume
  100. for _, location := range existingLocations {
  101. err = deleteVolume(ctx, commandEnv.option.GrpcDialOption, volumeId, location.Url)
  102. if err != nil {
  103. return fmt.Errorf("deleteVolume %s volume %d: %v", location.Url, volumeId, err)
  104. }
  105. }
  106. return err
  107. }
  108. func parallelCopyEcShardsFromSource(ctx context.Context, grpcDialOption grpc.DialOption,
  109. targetServers []*EcNode, allocated []int,
  110. volumeId needle.VolumeId, collection string, existingLocation wdclient.Location) (actuallyCopied []uint32, err error) {
  111. // parallelize
  112. shardIdChan := make(chan []uint32, len(targetServers))
  113. var wg sync.WaitGroup
  114. startFromShardId := uint32(0)
  115. for i, server := range targetServers {
  116. if allocated[i] <= 0 {
  117. continue
  118. }
  119. wg.Add(1)
  120. go func(server *EcNode, startFromShardId uint32, shardCount int) {
  121. defer wg.Done()
  122. copiedShardIds, copyErr := oneServerCopyEcShardsFromSource(ctx, grpcDialOption, server,
  123. startFromShardId, shardCount, volumeId, collection, existingLocation)
  124. if copyErr != nil {
  125. err = copyErr
  126. } else {
  127. shardIdChan <- copiedShardIds
  128. server.freeEcSlot -= len(copiedShardIds)
  129. }
  130. }(server, startFromShardId, allocated[i])
  131. startFromShardId += uint32(allocated[i])
  132. }
  133. wg.Wait()
  134. close(shardIdChan)
  135. if err != nil {
  136. return nil, err
  137. }
  138. for shardIds := range shardIdChan {
  139. actuallyCopied = append(actuallyCopied, shardIds...)
  140. }
  141. return
  142. }
  143. func oneServerCopyEcShardsFromSource(ctx context.Context, grpcDialOption grpc.DialOption,
  144. targetServer *EcNode, startFromShardId uint32, shardCount int,
  145. volumeId needle.VolumeId, collection string, existingLocation wdclient.Location) (copiedShardIds []uint32, err error) {
  146. var shardIdsToCopy []uint32
  147. for shardId := startFromShardId; shardId < startFromShardId+uint32(shardCount); shardId++ {
  148. fmt.Printf("allocate %d.%d %s => %s\n", volumeId, shardId, existingLocation.Url, targetServer.info.Id)
  149. shardIdsToCopy = append(shardIdsToCopy, shardId)
  150. }
  151. err = operation.WithVolumeServerClient(targetServer.info.Id, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  152. if targetServer.info.Id != existingLocation.Url {
  153. _, copyErr := volumeServerClient.VolumeEcShardsCopy(ctx, &volume_server_pb.VolumeEcShardsCopyRequest{
  154. VolumeId: uint32(volumeId),
  155. Collection: collection,
  156. ShardIds: shardIdsToCopy,
  157. SourceDataNode: existingLocation.Url,
  158. })
  159. if copyErr != nil {
  160. return copyErr
  161. }
  162. }
  163. _, mountErr := volumeServerClient.VolumeEcShardsMount(ctx, &volume_server_pb.VolumeEcShardsMountRequest{
  164. VolumeId: uint32(volumeId),
  165. Collection: collection,
  166. ShardIds: shardIdsToCopy,
  167. })
  168. if mountErr != nil {
  169. return mountErr
  170. }
  171. if targetServer.info.Id != existingLocation.Url {
  172. copiedShardIds = shardIdsToCopy
  173. glog.V(0).Infof("%s ec volume %d deletes shards %+v", existingLocation.Url, volumeId, copiedShardIds)
  174. }
  175. return nil
  176. })
  177. if err != nil {
  178. return
  179. }
  180. return
  181. }
  182. func sourceServerDeleteEcShards(ctx context.Context, grpcDialOption grpc.DialOption,
  183. volumeId needle.VolumeId, sourceLocation wdclient.Location, toBeDeletedShardIds []uint32) error {
  184. shouldDeleteEcx := len(toBeDeletedShardIds) == erasure_coding.TotalShardsCount
  185. return operation.WithVolumeServerClient(sourceLocation.Url, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  186. _, deleteErr := volumeServerClient.VolumeEcShardsDelete(ctx, &volume_server_pb.VolumeEcShardsDeleteRequest{
  187. VolumeId: uint32(volumeId),
  188. ShardIds: toBeDeletedShardIds,
  189. ShouldDeleteEcx: shouldDeleteEcx,
  190. })
  191. return deleteErr
  192. })
  193. }
  194. func balancedEcDistribution(servers []*EcNode) (allocated []int) {
  195. freeSlots := make([]int, len(servers))
  196. allocated = make([]int, len(servers))
  197. for i, server := range servers {
  198. freeSlots[i] = countFreeShardSlots(server.info)
  199. }
  200. allocatedCount := 0
  201. for allocatedCount < erasure_coding.TotalShardsCount {
  202. for i, _ := range servers {
  203. if freeSlots[i]-allocated[i] > 0 {
  204. allocated[i] += 1
  205. allocatedCount += 1
  206. }
  207. if allocatedCount >= erasure_coding.TotalShardsCount {
  208. break
  209. }
  210. }
  211. }
  212. return allocated
  213. }
  214. func eachDataNode(topo *master_pb.TopologyInfo, fn func(*master_pb.DataNodeInfo)) {
  215. for _, dc := range topo.DataCenterInfos {
  216. for _, rack := range dc.RackInfos {
  217. for _, dn := range rack.DataNodeInfos {
  218. fn(dn)
  219. }
  220. }
  221. }
  222. }
  223. func countShards(ecShardInfos []*master_pb.VolumeEcShardInformationMessage) (count int) {
  224. for _, ecShardInfo := range ecShardInfos {
  225. shardBits := erasure_coding.ShardBits(ecShardInfo.EcIndexBits)
  226. count += shardBits.ShardIdCount()
  227. }
  228. return
  229. }
  230. func countFreeShardSlots(dn *master_pb.DataNodeInfo) (count int) {
  231. return int(dn.FreeVolumeCount)*10 - countShards(dn.EcShardInfos)
  232. }
  233. type EcNode struct {
  234. info *master_pb.DataNodeInfo
  235. freeEcSlot int
  236. }
  237. func sortEcNodes(ecNodes []*EcNode) {
  238. sort.Slice(ecNodes, func(i, j int) bool {
  239. return ecNodes[i].freeEcSlot > ecNodes[j].freeEcSlot
  240. })
  241. }
  242. func collectEcNodes(ctx context.Context, commandEnv *commandEnv) (ecNodes []*EcNode, totalFreeEcSlots int, err error) {
  243. // list all possible locations
  244. var resp *master_pb.VolumeListResponse
  245. err = commandEnv.masterClient.WithClient(ctx, func(client master_pb.SeaweedClient) error {
  246. resp, err = client.VolumeList(ctx, &master_pb.VolumeListRequest{})
  247. return err
  248. })
  249. if err != nil {
  250. return nil, 0, err
  251. }
  252. // find out all volume servers with one slot left.
  253. eachDataNode(resp.TopologyInfo, func(dn *master_pb.DataNodeInfo) {
  254. if freeEcSlots := countFreeShardSlots(dn); freeEcSlots > 0 {
  255. ecNodes = append(ecNodes, &EcNode{
  256. info: dn,
  257. freeEcSlot: int(freeEcSlots),
  258. })
  259. totalFreeEcSlots += freeEcSlots
  260. }
  261. })
  262. sortEcNodes(ecNodes)
  263. return
  264. }