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.

370 lines
12 KiB

5 years ago
6 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "math"
  8. "os"
  9. "path"
  10. "path/filepath"
  11. "strings"
  12. "github.com/chrislusf/seaweedfs/weed/glog"
  13. "github.com/chrislusf/seaweedfs/weed/operation"
  14. "github.com/chrislusf/seaweedfs/weed/pb"
  15. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  16. "github.com/chrislusf/seaweedfs/weed/storage"
  17. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  18. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  19. "github.com/chrislusf/seaweedfs/weed/storage/types"
  20. "github.com/chrislusf/seaweedfs/weed/util"
  21. )
  22. /*
  23. Steps to apply erasure coding to .dat .idx files
  24. 0. ensure the volume is readonly
  25. 1. client call VolumeEcShardsGenerate to generate the .ecx and .ec00 ~ .ec13 files
  26. 2. client ask master for possible servers to hold the ec files, at least 4 servers
  27. 3. client call VolumeEcShardsCopy on above target servers to copy ec files from the source server
  28. 4. target servers report the new ec files to the master
  29. 5. master stores vid -> [14]*DataNode
  30. 6. client checks master. If all 14 slices are ready, delete the original .idx, .idx files
  31. */
  32. // VolumeEcShardsGenerate generates the .ecx and .ec00 ~ .ec13 files
  33. func (vs *VolumeServer) VolumeEcShardsGenerate(ctx context.Context, req *volume_server_pb.VolumeEcShardsGenerateRequest) (*volume_server_pb.VolumeEcShardsGenerateResponse, error) {
  34. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  35. if v == nil {
  36. return nil, fmt.Errorf("volume %d not found", req.VolumeId)
  37. }
  38. baseFileName := v.FileName()
  39. if v.Collection != req.Collection {
  40. return nil, fmt.Errorf("existing collection:%v unexpected input: %v", v.Collection, req.Collection)
  41. }
  42. // write .ecx file
  43. if err := erasure_coding.WriteSortedFileFromIdx(baseFileName, ".ecx"); err != nil {
  44. return nil, fmt.Errorf("WriteSortedFileFromIdx %s: %v", baseFileName, err)
  45. }
  46. // write .ec00 ~ .ec13 files
  47. if err := erasure_coding.WriteEcFiles(baseFileName); err != nil {
  48. return nil, fmt.Errorf("WriteEcFiles %s: %v", baseFileName, err)
  49. }
  50. // write .vif files
  51. if err := pb.SaveVolumeInfo(baseFileName+".vif", &volume_server_pb.VolumeInfo{Version: uint32(v.Version())}); err != nil {
  52. return nil, fmt.Errorf("WriteEcFiles %s: %v", baseFileName, err)
  53. }
  54. return &volume_server_pb.VolumeEcShardsGenerateResponse{}, nil
  55. }
  56. // VolumeEcShardsRebuild generates the any of the missing .ec00 ~ .ec13 files
  57. func (vs *VolumeServer) VolumeEcShardsRebuild(ctx context.Context, req *volume_server_pb.VolumeEcShardsRebuildRequest) (*volume_server_pb.VolumeEcShardsRebuildResponse, error) {
  58. baseFileName := erasure_coding.EcShardBaseFileName(req.Collection, int(req.VolumeId))
  59. var rebuiltShardIds []uint32
  60. for _, location := range vs.store.Locations {
  61. if util.FileExists(path.Join(location.Directory, baseFileName+".ecx")) {
  62. // write .ec00 ~ .ec13 files
  63. baseFileName = path.Join(location.Directory, baseFileName)
  64. if generatedShardIds, err := erasure_coding.RebuildEcFiles(baseFileName); err != nil {
  65. return nil, fmt.Errorf("RebuildEcFiles %s: %v", baseFileName, err)
  66. } else {
  67. rebuiltShardIds = generatedShardIds
  68. }
  69. if err := erasure_coding.RebuildEcxFile(baseFileName); err != nil {
  70. return nil, fmt.Errorf("RebuildEcxFile %s: %v", baseFileName, err)
  71. }
  72. break
  73. }
  74. }
  75. return &volume_server_pb.VolumeEcShardsRebuildResponse{
  76. RebuiltShardIds: rebuiltShardIds,
  77. }, nil
  78. }
  79. // VolumeEcShardsCopy copy the .ecx and some ec data slices
  80. func (vs *VolumeServer) VolumeEcShardsCopy(ctx context.Context, req *volume_server_pb.VolumeEcShardsCopyRequest) (*volume_server_pb.VolumeEcShardsCopyResponse, error) {
  81. location := vs.store.FindFreeLocation()
  82. if location == nil {
  83. return nil, fmt.Errorf("no space left")
  84. }
  85. baseFileName := storage.VolumeFileName(location.Directory, req.Collection, int(req.VolumeId))
  86. err := operation.WithVolumeServerClient(req.SourceDataNode, vs.grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {
  87. // copy ec data slices
  88. for _, shardId := range req.ShardIds {
  89. if err := vs.doCopyFile(ctx, client, true, req.Collection, req.VolumeId, math.MaxUint32, math.MaxInt64, baseFileName, erasure_coding.ToExt(int(shardId)), false, false); err != nil {
  90. return err
  91. }
  92. }
  93. if req.CopyEcxFile {
  94. // copy ecx file
  95. if err := vs.doCopyFile(ctx, client, true, req.Collection, req.VolumeId, math.MaxUint32, math.MaxInt64, baseFileName, ".ecx", false, false); err != nil {
  96. return err
  97. }
  98. return nil
  99. }
  100. if req.CopyEcjFile {
  101. // copy ecj file
  102. if err := vs.doCopyFile(ctx, client, true, req.Collection, req.VolumeId, math.MaxUint32, math.MaxInt64, baseFileName, ".ecj", true, true); err != nil {
  103. return err
  104. }
  105. }
  106. if req.CopyVifFile {
  107. // copy vif file
  108. if err := vs.doCopyFile(ctx, client, true, req.Collection, req.VolumeId, math.MaxUint32, math.MaxInt64, baseFileName, ".vif", false, true); err != nil {
  109. return err
  110. }
  111. }
  112. return nil
  113. })
  114. if err != nil {
  115. return nil, fmt.Errorf("VolumeEcShardsCopy volume %d: %v", req.VolumeId, err)
  116. }
  117. return &volume_server_pb.VolumeEcShardsCopyResponse{}, nil
  118. }
  119. // VolumeEcShardsDelete local delete the .ecx and some ec data slices if not needed
  120. // the shard should not be mounted before calling this.
  121. func (vs *VolumeServer) VolumeEcShardsDelete(ctx context.Context, req *volume_server_pb.VolumeEcShardsDeleteRequest) (*volume_server_pb.VolumeEcShardsDeleteResponse, error) {
  122. baseFilename := erasure_coding.EcShardBaseFileName(req.Collection, int(req.VolumeId))
  123. glog.V(0).Infof("ec volume %d shard delete %v", req.VolumeId, req.ShardIds)
  124. found := false
  125. for _, location := range vs.store.Locations {
  126. if util.FileExists(path.Join(location.Directory, baseFilename+".ecx")) {
  127. found = true
  128. baseFilename = path.Join(location.Directory, baseFilename)
  129. for _, shardId := range req.ShardIds {
  130. os.Remove(baseFilename + erasure_coding.ToExt(int(shardId)))
  131. }
  132. break
  133. }
  134. }
  135. if !found {
  136. return nil, nil
  137. }
  138. // check whether to delete the .ecx and .ecj file also
  139. hasEcxFile := false
  140. existingShardCount := 0
  141. bName := filepath.Base(baseFilename)
  142. for _, location := range vs.store.Locations {
  143. fileInfos, err := ioutil.ReadDir(location.Directory)
  144. if err != nil {
  145. continue
  146. }
  147. for _, fileInfo := range fileInfos {
  148. if fileInfo.Name() == bName+".ecx" || fileInfo.Name() == bName+".ecj" {
  149. hasEcxFile = true
  150. continue
  151. }
  152. if strings.HasPrefix(fileInfo.Name(), bName+".ec") {
  153. existingShardCount++
  154. }
  155. }
  156. }
  157. if hasEcxFile && existingShardCount == 0 {
  158. if err := os.Remove(baseFilename + ".ecx"); err != nil {
  159. return nil, err
  160. }
  161. if err := os.Remove(baseFilename + ".ecj"); err != nil {
  162. return nil, err
  163. }
  164. }
  165. return &volume_server_pb.VolumeEcShardsDeleteResponse{}, nil
  166. }
  167. func (vs *VolumeServer) VolumeEcShardsMount(ctx context.Context, req *volume_server_pb.VolumeEcShardsMountRequest) (*volume_server_pb.VolumeEcShardsMountResponse, error) {
  168. for _, shardId := range req.ShardIds {
  169. err := vs.store.MountEcShards(req.Collection, needle.VolumeId(req.VolumeId), erasure_coding.ShardId(shardId))
  170. if err != nil {
  171. glog.Errorf("ec shard mount %v: %v", req, err)
  172. } else {
  173. glog.V(2).Infof("ec shard mount %v", req)
  174. }
  175. if err != nil {
  176. return nil, fmt.Errorf("mount %d.%d: %v", req.VolumeId, shardId, err)
  177. }
  178. }
  179. return &volume_server_pb.VolumeEcShardsMountResponse{}, nil
  180. }
  181. func (vs *VolumeServer) VolumeEcShardsUnmount(ctx context.Context, req *volume_server_pb.VolumeEcShardsUnmountRequest) (*volume_server_pb.VolumeEcShardsUnmountResponse, error) {
  182. for _, shardId := range req.ShardIds {
  183. err := vs.store.UnmountEcShards(needle.VolumeId(req.VolumeId), erasure_coding.ShardId(shardId))
  184. if err != nil {
  185. glog.Errorf("ec shard unmount %v: %v", req, err)
  186. } else {
  187. glog.V(2).Infof("ec shard unmount %v", req)
  188. }
  189. if err != nil {
  190. return nil, fmt.Errorf("unmount %d.%d: %v", req.VolumeId, shardId, err)
  191. }
  192. }
  193. return &volume_server_pb.VolumeEcShardsUnmountResponse{}, nil
  194. }
  195. func (vs *VolumeServer) VolumeEcShardRead(req *volume_server_pb.VolumeEcShardReadRequest, stream volume_server_pb.VolumeServer_VolumeEcShardReadServer) error {
  196. ecVolume, found := vs.store.FindEcVolume(needle.VolumeId(req.VolumeId))
  197. if !found {
  198. return fmt.Errorf("VolumeEcShardRead not found ec volume id %d", req.VolumeId)
  199. }
  200. ecShard, found := ecVolume.FindEcVolumeShard(erasure_coding.ShardId(req.ShardId))
  201. if !found {
  202. return fmt.Errorf("not found ec shard %d.%d", req.VolumeId, req.ShardId)
  203. }
  204. if req.FileKey != 0 {
  205. _, size, _ := ecVolume.FindNeedleFromEcx(types.Uint64ToNeedleId(req.FileKey))
  206. if size == types.TombstoneFileSize {
  207. return stream.Send(&volume_server_pb.VolumeEcShardReadResponse{
  208. IsDeleted: true,
  209. })
  210. }
  211. }
  212. bufSize := req.Size
  213. if bufSize > BufferSizeLimit {
  214. bufSize = BufferSizeLimit
  215. }
  216. buffer := make([]byte, bufSize)
  217. startOffset, bytesToRead := req.Offset, req.Size
  218. for bytesToRead > 0 {
  219. // min of bytesToRead and bufSize
  220. bufferSize := bufSize
  221. if bufferSize > bytesToRead {
  222. bufferSize = bytesToRead
  223. }
  224. bytesread, err := ecShard.ReadAt(buffer[0:bufferSize], startOffset)
  225. // println("read", ecShard.FileName(), "startOffset", startOffset, bytesread, "bytes, with target", bufferSize)
  226. if bytesread > 0 {
  227. if int64(bytesread) > bytesToRead {
  228. bytesread = int(bytesToRead)
  229. }
  230. err = stream.Send(&volume_server_pb.VolumeEcShardReadResponse{
  231. Data: buffer[:bytesread],
  232. })
  233. if err != nil {
  234. // println("sending", bytesread, "bytes err", err.Error())
  235. return err
  236. }
  237. startOffset += int64(bytesread)
  238. bytesToRead -= int64(bytesread)
  239. }
  240. if err != nil {
  241. if err != io.EOF {
  242. return err
  243. }
  244. return nil
  245. }
  246. }
  247. return nil
  248. }
  249. func (vs *VolumeServer) VolumeEcBlobDelete(ctx context.Context, req *volume_server_pb.VolumeEcBlobDeleteRequest) (*volume_server_pb.VolumeEcBlobDeleteResponse, error) {
  250. resp := &volume_server_pb.VolumeEcBlobDeleteResponse{}
  251. for _, location := range vs.store.Locations {
  252. if localEcVolume, found := location.FindEcVolume(needle.VolumeId(req.VolumeId)); found {
  253. _, size, _, err := localEcVolume.LocateEcShardNeedle(types.NeedleId(req.FileKey), needle.Version(req.Version))
  254. if err != nil {
  255. return nil, fmt.Errorf("locate in local ec volume: %v", err)
  256. }
  257. if size == types.TombstoneFileSize {
  258. return resp, nil
  259. }
  260. err = localEcVolume.DeleteNeedleFromEcx(types.NeedleId(req.FileKey))
  261. if err != nil {
  262. return nil, err
  263. }
  264. break
  265. }
  266. }
  267. return resp, nil
  268. }
  269. // VolumeEcShardsToVolume generates the .idx, .dat files from .ecx, .ecj and .ec01 ~ .ec14 files
  270. func (vs *VolumeServer) VolumeEcShardsToVolume(ctx context.Context, req *volume_server_pb.VolumeEcShardsToVolumeRequest) (*volume_server_pb.VolumeEcShardsToVolumeResponse, error) {
  271. v, found := vs.store.FindEcVolume(needle.VolumeId(req.VolumeId))
  272. if !found {
  273. return nil, fmt.Errorf("ec volume %d not found", req.VolumeId)
  274. }
  275. baseFileName := v.FileName()
  276. if v.Collection != req.Collection {
  277. return nil, fmt.Errorf("existing collection:%v unexpected input: %v", v.Collection, req.Collection)
  278. }
  279. // calculate .dat file size
  280. datFileSize, err := erasure_coding.FindDatFileSize(baseFileName)
  281. if err != nil {
  282. return nil, fmt.Errorf("FindDatFileSize %s: %v", baseFileName, err)
  283. }
  284. // write .dat file from .ec00 ~ .ec09 files
  285. if err := erasure_coding.WriteDatFile(baseFileName, datFileSize); err != nil {
  286. return nil, fmt.Errorf("WriteEcFiles %s: %v", baseFileName, err)
  287. }
  288. // write .idx file from .ecx and .ecj files
  289. if err := erasure_coding.WriteIdxFileFromEcIndex(baseFileName); err != nil {
  290. return nil, fmt.Errorf("WriteIdxFileFromEcIndex %s: %v", baseFileName, err)
  291. }
  292. return &volume_server_pb.VolumeEcShardsToVolumeResponse{}, nil
  293. }