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.

103 lines
2.5 KiB

6 years ago
6 years ago
  1. package operation
  2. import (
  3. "context"
  4. "fmt"
  5. "google.golang.org/grpc"
  6. "strings"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  9. "github.com/chrislusf/seaweedfs/weed/security"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. type VolumeAssignRequest struct {
  13. Count uint64
  14. Replication string
  15. Collection string
  16. Ttl string
  17. DataCenter string
  18. Rack string
  19. DataNode string
  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(masterClient master_pb.SeaweedClient) error {
  40. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
  41. defer cancel()
  42. req := &master_pb.AssignRequest{
  43. Count: primaryRequest.Count,
  44. Replication: primaryRequest.Replication,
  45. Collection: primaryRequest.Collection,
  46. Ttl: primaryRequest.Ttl,
  47. DataCenter: primaryRequest.DataCenter,
  48. Rack: primaryRequest.Rack,
  49. DataNode: primaryRequest.DataNode,
  50. }
  51. resp, grpcErr := masterClient.Assign(ctx, req)
  52. if grpcErr != nil {
  53. return grpcErr
  54. }
  55. ret.Count = resp.Count
  56. ret.Fid = resp.Fid
  57. ret.Url = resp.Url
  58. ret.PublicUrl = resp.PublicUrl
  59. ret.Error = resp.Error
  60. ret.Auth = security.EncodedJwt(resp.Auth)
  61. return nil
  62. })
  63. if lastError != nil {
  64. continue
  65. }
  66. if ret.Count <= 0 {
  67. lastError = fmt.Errorf("assign failure %d: %v", i+1, ret.Error)
  68. continue
  69. }
  70. }
  71. return ret, lastError
  72. }
  73. func LookupJwt(master string, fileId string) security.EncodedJwt {
  74. tokenStr := ""
  75. if h, e := util.Head(fmt.Sprintf("http://%s/dir/lookup?fileId=%s", master, fileId)); e == nil {
  76. bearer := h.Get("Authorization")
  77. if len(bearer) > 7 && strings.ToUpper(bearer[0:6]) == "BEARER" {
  78. tokenStr = bearer[7:]
  79. }
  80. }
  81. return security.EncodedJwt(tokenStr)
  82. }