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.

56 lines
1.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 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) FetchAndWriteNeedle(ctx context.Context, req *volume_server_pb.FetchAndWriteNeedleRequest) (resp *volume_server_pb.FetchAndWriteNeedleResponse, err error) {
  12. resp = &volume_server_pb.FetchAndWriteNeedleResponse{}
  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. remoteConf := &filer_pb.RemoteConf{
  18. Type: req.RemoteType,
  19. Name: req.RemoteName,
  20. S3AccessKey: req.S3AccessKey,
  21. S3SecretKey: req.S3SecretKey,
  22. S3Region: req.S3Region,
  23. S3Endpoint: req.S3Endpoint,
  24. }
  25. client, getClientErr := remote_storage.GetRemoteStorage(remoteConf)
  26. if getClientErr != nil {
  27. return nil, fmt.Errorf("get remote client: %v", getClientErr)
  28. }
  29. remoteStorageLocation := &filer_pb.RemoteStorageLocation{
  30. Name: req.RemoteName,
  31. Bucket: req.RemoteBucket,
  32. Path: req.RemotePath,
  33. }
  34. data, ReadRemoteErr := client.ReadFile(remoteStorageLocation, req.Offset, req.Size)
  35. if ReadRemoteErr != nil {
  36. return nil, fmt.Errorf("read from remote %+v: %v", remoteStorageLocation, ReadRemoteErr)
  37. }
  38. n := new(needle.Needle)
  39. n.Id = types.NeedleId(req.NeedleId)
  40. n.Cookie = types.Cookie(req.Cookie)
  41. n.Data, n.DataSize = data, uint32(len(data))
  42. // copied from *Needle.prepareWriteBuffer()
  43. n.Size = 4 + types.Size(n.DataSize) + 1
  44. n.Checksum = needle.NewCRC(n.Data)
  45. if _, err = vs.store.WriteVolumeNeedle(v.Id, n, true, false); err != nil {
  46. return nil, fmt.Errorf("write needle %d size %d: %v", req.NeedleId, req.Size, err)
  47. }
  48. return resp, nil
  49. }