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.

142 lines
3.7 KiB

6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package operation
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  7. "google.golang.org/grpc"
  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. WritableVolumeCount uint32
  21. }
  22. type AssignResult struct {
  23. Fid string `json:"fid,omitempty"`
  24. Url string `json:"url,omitempty"`
  25. PublicUrl string `json:"publicUrl,omitempty"`
  26. Count uint64 `json:"count,omitempty"`
  27. Error string `json:"error,omitempty"`
  28. Auth security.EncodedJwt `json:"auth,omitempty"`
  29. }
  30. func Assign(server string, grpcDialOption grpc.DialOption, primaryRequest *VolumeAssignRequest, alternativeRequests ...*VolumeAssignRequest) (*AssignResult, error) {
  31. var requests []*VolumeAssignRequest
  32. requests = append(requests, primaryRequest)
  33. requests = append(requests, alternativeRequests...)
  34. var lastError error
  35. ret := &AssignResult{}
  36. for i, request := range requests {
  37. if request == nil {
  38. continue
  39. }
  40. lastError = WithMasterServerClient(server, grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  41. req := &master_pb.AssignRequest{
  42. Count: request.Count,
  43. Replication: request.Replication,
  44. Collection: request.Collection,
  45. Ttl: request.Ttl,
  46. DataCenter: request.DataCenter,
  47. Rack: request.Rack,
  48. DataNode: request.DataNode,
  49. WritableVolumeCount: request.WritableVolumeCount,
  50. }
  51. resp, grpcErr := masterClient.Assign(context.Background(), 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. }
  83. type StorageOption struct {
  84. Replication string
  85. Collection string
  86. DataCenter string
  87. Rack string
  88. TtlSeconds int32
  89. Fsync bool
  90. VolumeGrowthCount uint32
  91. }
  92. func (so *StorageOption) TtlString() string {
  93. return needle.SecondsToTTL(so.TtlSeconds)
  94. }
  95. func (so *StorageOption) ToAssignRequests(count int) (ar *VolumeAssignRequest, altRequest *VolumeAssignRequest) {
  96. ar = &VolumeAssignRequest{
  97. Count: uint64(count),
  98. Replication: so.Replication,
  99. Collection: so.Collection,
  100. Ttl: so.TtlString(),
  101. DataCenter: so.DataCenter,
  102. Rack: so.Rack,
  103. WritableVolumeCount: so.VolumeGrowthCount,
  104. }
  105. if so.DataCenter != "" || so.Rack != "" {
  106. altRequest = &VolumeAssignRequest{
  107. Count: uint64(count),
  108. Replication: so.Replication,
  109. Collection: so.Collection,
  110. Ttl: so.TtlString(),
  111. DataCenter: "",
  112. Rack: "",
  113. WritableVolumeCount: so.VolumeGrowthCount,
  114. }
  115. }
  116. return
  117. }