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.

147 lines
3.8 KiB

6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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. DiskType string
  18. DataCenter string
  19. Rack string
  20. DataNode string
  21. WritableVolumeCount uint32
  22. }
  23. type AssignResult struct {
  24. Fid string `json:"fid,omitempty"`
  25. Url string `json:"url,omitempty"`
  26. PublicUrl string `json:"publicUrl,omitempty"`
  27. Count uint64 `json:"count,omitempty"`
  28. Error string `json:"error,omitempty"`
  29. Auth security.EncodedJwt `json:"auth,omitempty"`
  30. }
  31. func Assign(server string, grpcDialOption grpc.DialOption, primaryRequest *VolumeAssignRequest, alternativeRequests ...*VolumeAssignRequest) (*AssignResult, error) {
  32. var requests []*VolumeAssignRequest
  33. requests = append(requests, primaryRequest)
  34. requests = append(requests, alternativeRequests...)
  35. var lastError error
  36. ret := &AssignResult{}
  37. for i, request := range requests {
  38. if request == nil {
  39. continue
  40. }
  41. lastError = WithMasterServerClient(server, grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  42. req := &master_pb.AssignRequest{
  43. Count: request.Count,
  44. Replication: request.Replication,
  45. Collection: request.Collection,
  46. Ttl: request.Ttl,
  47. DiskType: request.DiskType,
  48. DataCenter: request.DataCenter,
  49. Rack: request.Rack,
  50. DataNode: request.DataNode,
  51. WritableVolumeCount: request.WritableVolumeCount,
  52. }
  53. resp, grpcErr := masterClient.Assign(context.Background(), req)
  54. if grpcErr != nil {
  55. return grpcErr
  56. }
  57. ret.Count = resp.Count
  58. ret.Fid = resp.Fid
  59. ret.Url = resp.Url
  60. ret.PublicUrl = resp.PublicUrl
  61. ret.Error = resp.Error
  62. ret.Auth = security.EncodedJwt(resp.Auth)
  63. return nil
  64. })
  65. if lastError != nil {
  66. continue
  67. }
  68. if ret.Count <= 0 {
  69. lastError = fmt.Errorf("assign failure %d: %v", i+1, ret.Error)
  70. continue
  71. }
  72. }
  73. return ret, lastError
  74. }
  75. func LookupJwt(master string, fileId string) security.EncodedJwt {
  76. tokenStr := ""
  77. if h, e := util.Head(fmt.Sprintf("http://%s/dir/lookup?fileId=%s", master, fileId)); e == nil {
  78. bearer := h.Get("Authorization")
  79. if len(bearer) > 7 && strings.ToUpper(bearer[0:6]) == "BEARER" {
  80. tokenStr = bearer[7:]
  81. }
  82. }
  83. return security.EncodedJwt(tokenStr)
  84. }
  85. type StorageOption struct {
  86. Replication string
  87. DiskType string
  88. Collection string
  89. DataCenter string
  90. Rack string
  91. TtlSeconds int32
  92. Fsync bool
  93. VolumeGrowthCount uint32
  94. }
  95. func (so *StorageOption) TtlString() string {
  96. return needle.SecondsToTTL(so.TtlSeconds)
  97. }
  98. func (so *StorageOption) ToAssignRequests(count int) (ar *VolumeAssignRequest, altRequest *VolumeAssignRequest) {
  99. ar = &VolumeAssignRequest{
  100. Count: uint64(count),
  101. Replication: so.Replication,
  102. Collection: so.Collection,
  103. Ttl: so.TtlString(),
  104. DiskType: so.DiskType,
  105. DataCenter: so.DataCenter,
  106. Rack: so.Rack,
  107. WritableVolumeCount: so.VolumeGrowthCount,
  108. }
  109. if so.DataCenter != "" || so.Rack != "" {
  110. altRequest = &VolumeAssignRequest{
  111. Count: uint64(count),
  112. Replication: so.Replication,
  113. Collection: so.Collection,
  114. Ttl: so.TtlString(),
  115. DiskType: so.DiskType,
  116. DataCenter: "",
  117. Rack: "",
  118. WritableVolumeCount: so.VolumeGrowthCount,
  119. }
  120. }
  121. return
  122. }