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.

78 lines
2.7 KiB

4 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  7. "github.com/chrislusf/seaweedfs/weed/remote_storage"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. "github.com/chrislusf/seaweedfs/weed/storage/types"
  10. )
  11. func (vs *VolumeServer) ReadNeedleBlob(ctx context.Context, req *volume_server_pb.ReadNeedleBlobRequest) (resp *volume_server_pb.ReadNeedleBlobResponse, err error) {
  12. resp = &volume_server_pb.ReadNeedleBlobResponse{}
  13. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  14. if v == nil {
  15. return nil, fmt.Errorf("not found volume id %d", req.VolumeId)
  16. }
  17. resp.NeedleBlob, err = v.ReadNeedleBlob(req.Offset, types.Size(req.Size))
  18. if err != nil {
  19. return nil, fmt.Errorf("read needle blob offset %d size %d: %v", req.Offset, req.Size, err)
  20. }
  21. return resp, nil
  22. }
  23. func (vs *VolumeServer) WriteNeedleBlob(ctx context.Context, req *volume_server_pb.WriteNeedleBlobRequest) (resp *volume_server_pb.WriteNeedleBlobResponse, err error) {
  24. resp = &volume_server_pb.WriteNeedleBlobResponse{}
  25. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  26. if v == nil {
  27. return nil, fmt.Errorf("not found volume id %d", req.VolumeId)
  28. }
  29. if err = v.WriteNeedleBlob(types.NeedleId(req.NeedleId), req.NeedleBlob, types.Size(req.Size)); err != nil {
  30. return nil, fmt.Errorf("write blob needle %d size %d: %v", req.NeedleId, req.Size, err)
  31. }
  32. return resp, nil
  33. }
  34. func (vs *VolumeServer) FetchAndWriteNeedle(ctx context.Context, req *volume_server_pb.FetchAndWriteNeedleRequest) (resp *volume_server_pb.FetchAndWriteNeedleResponse, err error) {
  35. resp = &volume_server_pb.FetchAndWriteNeedleResponse{}
  36. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  37. if v == nil {
  38. return nil, fmt.Errorf("not found volume id %d", req.VolumeId)
  39. }
  40. remoteConf := &filer_pb.RemoteConf{
  41. Type: req.RemoteType,
  42. Name: req.RemoteName,
  43. S3AccessKey: req.S3AccessKey,
  44. S3SecretKey: req.S3SecretKey,
  45. S3Region: req.S3Region,
  46. S3Endpoint: req.S3Endpoint,
  47. }
  48. client, getClientErr := remote_storage.GetRemoteStorage(remoteConf)
  49. if getClientErr != nil {
  50. return nil, fmt.Errorf("get remote client: %v", getClientErr)
  51. }
  52. remoteStorageLocation := &filer_pb.RemoteStorageLocation{
  53. Name: req.RemoteName,
  54. Bucket: req.RemoteBucket,
  55. Path: req.RemoteKey,
  56. }
  57. data, ReadRemoteErr := client.ReadFile(remoteStorageLocation, req.Offset, req.Size)
  58. if ReadRemoteErr != nil {
  59. return nil, fmt.Errorf("read from remote %+v: %v", remoteStorageLocation, ReadRemoteErr)
  60. }
  61. if err = v.WriteNeedleBlob(types.NeedleId(req.NeedleId), data, types.Size(req.Size)); err != nil {
  62. return nil, fmt.Errorf("write blob needle %d size %d: %v", req.NeedleId, req.Size, err)
  63. }
  64. return resp, nil
  65. }