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.

110 lines
3.7 KiB

  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "math"
  6. "os"
  7. "github.com/chrislusf/seaweedfs/weed/operation"
  8. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  9. "github.com/chrislusf/seaweedfs/weed/storage"
  10. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. )
  13. /*
  14. Steps to apply erasure coding to .dat .idx files
  15. 0. ensure the volume is readonly
  16. 1. client call VolumeEcShardsGenerate to generate the .ecx and .ec01~.ec14 files
  17. 2. client ask master for possible servers to hold the ec files, at least 4 servers
  18. 3. client call VolumeEcShardsCopy on above target servers to copy ec files from the source server
  19. 4. target servers report the new ec files to the master
  20. 5. master stores vid -> [14]*DataNode
  21. 6. client checks master. If all 14 slices are ready, delete the original .idx, .idx files
  22. */
  23. // VolumeEcShardsGenerate generates the .ecx and .ec01 ~ .ec14 files
  24. func (vs *VolumeServer) VolumeEcShardsGenerate(ctx context.Context, req *volume_server_pb.VolumeEcShardsGenerateRequest) (*volume_server_pb.VolumeEcShardsGenerateResponse, error) {
  25. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  26. if v == nil {
  27. return nil, fmt.Errorf("volume %d not found", req.VolumeId)
  28. }
  29. baseFileName := v.FileName()
  30. if v.Collection != req.Collection {
  31. return nil, fmt.Errorf("existing collection:%v unexpected input: %v", v.Collection, req.Collection)
  32. }
  33. // write .ecx file
  34. if err := erasure_coding.WriteSortedEcxFile(baseFileName); err != nil {
  35. return nil, fmt.Errorf("WriteSortedEcxFile %s: %v", baseFileName, err)
  36. }
  37. // write .ec01 ~ .ec14 files
  38. if err := erasure_coding.WriteEcFiles(baseFileName); err != nil {
  39. return nil, fmt.Errorf("WriteEcFiles %s: %v", baseFileName, err)
  40. }
  41. return &volume_server_pb.VolumeEcShardsGenerateResponse{}, nil
  42. }
  43. // VolumeEcShardsCopy copy the .ecx and some ec data slices
  44. func (vs *VolumeServer) VolumeEcShardsCopy(ctx context.Context, req *volume_server_pb.VolumeEcShardsCopyRequest) (*volume_server_pb.VolumeEcShardsCopyResponse, error) {
  45. location := vs.store.FindFreeLocation()
  46. if location == nil {
  47. return nil, fmt.Errorf("no space left")
  48. }
  49. baseFileName := storage.VolumeFileName(req.Collection, location.Directory, int(req.VolumeId))
  50. err := operation.WithVolumeServerClient(req.SourceDataNode, vs.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  51. // copy ecx file
  52. if err := vs.doCopyFile(ctx, client, req.VolumeId, math.MaxUint32, math.MaxInt64, baseFileName, ".ecx"); err != nil {
  53. return err
  54. }
  55. // copy ec data slices
  56. for _, ecIndex := range req.EcIndexes {
  57. if err := vs.doCopyFile(ctx, client, req.VolumeId, math.MaxUint32, math.MaxInt64, baseFileName, erasure_coding.ToExt(int(ecIndex))); err != nil {
  58. return err
  59. }
  60. }
  61. return nil
  62. })
  63. if err != nil {
  64. return nil, fmt.Errorf("VolumeEcShardsCopy volume %d: %v", req.VolumeId, err)
  65. }
  66. return &volume_server_pb.VolumeEcShardsCopyResponse{}, nil
  67. }
  68. // VolumeEcShardsDelete local delete the .ecx and some ec data slices if not needed, assuming current server has the source volume
  69. func (vs *VolumeServer) VolumeEcShardsDelete(ctx context.Context, req *volume_server_pb.VolumeEcShardsDeleteRequest) (*volume_server_pb.VolumeEcShardsDeleteResponse, error) {
  70. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  71. if v == nil {
  72. return nil, fmt.Errorf("volume %d not found", req.VolumeId)
  73. }
  74. baseFileName := v.FileName()
  75. for _, shardIndex := range req.EcIndexes {
  76. if err := os.Remove(baseFileName + erasure_coding.ToExt(int(shardIndex))); err != nil {
  77. return nil, err
  78. }
  79. }
  80. if req.ShouldDeleteEcx {
  81. if err := os.Remove(baseFileName + ".ecx"); err != nil {
  82. return nil, err
  83. }
  84. }
  85. return &volume_server_pb.VolumeEcShardsDeleteResponse{}, nil
  86. }