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.

45 lines
1.5 KiB

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/volume_server_pb"
  6. "github.com/chrislusf/seaweedfs/weed/remote_storage"
  7. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  8. "github.com/chrislusf/seaweedfs/weed/storage/types"
  9. )
  10. func (vs *VolumeServer) FetchAndWriteNeedle(ctx context.Context, req *volume_server_pb.FetchAndWriteNeedleRequest) (resp *volume_server_pb.FetchAndWriteNeedleResponse, err error) {
  11. resp = &volume_server_pb.FetchAndWriteNeedleResponse{}
  12. v := vs.store.GetVolume(needle.VolumeId(req.VolumeId))
  13. if v == nil {
  14. return nil, fmt.Errorf("not found volume id %d", req.VolumeId)
  15. }
  16. remoteConf := req.RemoteConf
  17. client, getClientErr := remote_storage.GetRemoteStorage(remoteConf)
  18. if getClientErr != nil {
  19. return nil, fmt.Errorf("get remote client: %v", getClientErr)
  20. }
  21. remoteStorageLocation := req.RemoteLocation
  22. data, ReadRemoteErr := client.ReadFile(remoteStorageLocation, req.Offset, req.Size)
  23. if ReadRemoteErr != nil {
  24. return nil, fmt.Errorf("read from remote %+v: %v", remoteStorageLocation, ReadRemoteErr)
  25. }
  26. n := new(needle.Needle)
  27. n.Id = types.NeedleId(req.NeedleId)
  28. n.Cookie = types.Cookie(req.Cookie)
  29. n.Data, n.DataSize = data, uint32(len(data))
  30. // copied from *Needle.prepareWriteBuffer()
  31. n.Size = 4 + types.Size(n.DataSize) + 1
  32. n.Checksum = needle.NewCRC(n.Data)
  33. if _, err = vs.store.WriteVolumeNeedle(v.Id, n, true, false); err != nil {
  34. return nil, fmt.Errorf("write needle %d size %d: %v", req.NeedleId, req.Size, err)
  35. }
  36. return resp, nil
  37. }