assign_file_id.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package operation
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  6. "github.com/chrislusf/seaweedfs/weed/security"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. "google.golang.org/grpc"
  9. "strings"
  10. )
  11. type VolumeAssignRequest struct {
  12. Count uint64
  13. Replication string
  14. Collection string
  15. Ttl string
  16. DataCenter string
  17. Rack string
  18. DataNode string
  19. WritableVolumeCount uint32
  20. }
  21. type AssignResult struct {
  22. Fid string `json:"fid,omitempty"`
  23. Url string `json:"url,omitempty"`
  24. PublicUrl string `json:"publicUrl,omitempty"`
  25. Count uint64 `json:"count,omitempty"`
  26. Error string `json:"error,omitempty"`
  27. Auth security.EncodedJwt `json:"auth,omitempty"`
  28. }
  29. func Assign(server string, grpcDialOption grpc.DialOption, primaryRequest *VolumeAssignRequest, alternativeRequests ...*VolumeAssignRequest) (*AssignResult, error) {
  30. var requests []*VolumeAssignRequest
  31. requests = append(requests, primaryRequest)
  32. requests = append(requests, alternativeRequests...)
  33. var lastError error
  34. ret := &AssignResult{}
  35. for i, request := range requests {
  36. if request == nil {
  37. continue
  38. }
  39. lastError = WithMasterServerClient(server, grpcDialOption, func(ctx context.Context, masterClient master_pb.SeaweedClient) error {
  40. req := &master_pb.AssignRequest{
  41. Count: primaryRequest.Count,
  42. Replication: primaryRequest.Replication,
  43. Collection: primaryRequest.Collection,
  44. Ttl: primaryRequest.Ttl,
  45. DataCenter: primaryRequest.DataCenter,
  46. Rack: primaryRequest.Rack,
  47. DataNode: primaryRequest.DataNode,
  48. WritableVolumeCount: primaryRequest.WritableVolumeCount,
  49. }
  50. resp, grpcErr := masterClient.Assign(context.Background(), req)
  51. if grpcErr != nil {
  52. return grpcErr
  53. }
  54. ret.Count = resp.Count
  55. ret.Fid = resp.Fid
  56. ret.Url = resp.Url
  57. ret.PublicUrl = resp.PublicUrl
  58. ret.Error = resp.Error
  59. ret.Auth = security.EncodedJwt(resp.Auth)
  60. return nil
  61. })
  62. if lastError != nil {
  63. continue
  64. }
  65. if ret.Count <= 0 {
  66. lastError = fmt.Errorf("assign failure %d: %v", i+1, ret.Error)
  67. continue
  68. }
  69. }
  70. return ret, lastError
  71. }
  72. func LookupJwt(master string, fileId string) security.EncodedJwt {
  73. tokenStr := ""
  74. if h, e := util.Head(fmt.Sprintf("http://%s/dir/lookup?fileId=%s", master, fileId)); e == nil {
  75. bearer := h.Get("Authorization")
  76. if len(bearer) > 7 && strings.ToUpper(bearer[0:6]) == "BEARER" {
  77. tokenStr = bearer[7:]
  78. }
  79. }
  80. return security.EncodedJwt(tokenStr)
  81. }