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.

99 lines
2.4 KiB

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