assign_file_id.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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(masterFn GetMasterFn, 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(masterFn(), 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. break
  73. }
  74. return ret, lastError
  75. }
  76. func LookupJwt(master string, fileId string) security.EncodedJwt {
  77. tokenStr := ""
  78. if h, e := util.Head(fmt.Sprintf("http://%s/dir/lookup?fileId=%s", master, fileId)); e == nil {
  79. bearer := h.Get("Authorization")
  80. if len(bearer) > 7 && strings.ToUpper(bearer[0:6]) == "BEARER" {
  81. tokenStr = bearer[7:]
  82. }
  83. }
  84. return security.EncodedJwt(tokenStr)
  85. }
  86. type StorageOption struct {
  87. Replication string
  88. DiskType string
  89. Collection string
  90. DataCenter string
  91. Rack string
  92. TtlSeconds int32
  93. Fsync bool
  94. VolumeGrowthCount uint32
  95. }
  96. func (so *StorageOption) TtlString() string {
  97. return needle.SecondsToTTL(so.TtlSeconds)
  98. }
  99. func (so *StorageOption) ToAssignRequests(count int) (ar *VolumeAssignRequest, altRequest *VolumeAssignRequest) {
  100. ar = &VolumeAssignRequest{
  101. Count: uint64(count),
  102. Replication: so.Replication,
  103. Collection: so.Collection,
  104. Ttl: so.TtlString(),
  105. DiskType: so.DiskType,
  106. DataCenter: so.DataCenter,
  107. Rack: so.Rack,
  108. WritableVolumeCount: so.VolumeGrowthCount,
  109. }
  110. if so.DataCenter != "" || so.Rack != "" {
  111. altRequest = &VolumeAssignRequest{
  112. Count: uint64(count),
  113. Replication: so.Replication,
  114. Collection: so.Collection,
  115. Ttl: so.TtlString(),
  116. DiskType: so.DiskType,
  117. DataCenter: "",
  118. Rack: "",
  119. WritableVolumeCount: so.VolumeGrowthCount,
  120. }
  121. }
  122. return
  123. }