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.

172 lines
4.6 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
4 years ago
  1. package operation
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  7. "google.golang.org/grpc"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/security"
  10. )
  11. type VolumeAssignRequest struct {
  12. Count uint64
  13. Replication string
  14. Collection string
  15. Ttl string
  16. DiskType 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. GrpcPort int `json:"grpcPort,omitempty"`
  27. Count uint64 `json:"count,omitempty"`
  28. Error string `json:"error,omitempty"`
  29. Auth security.EncodedJwt `json:"auth,omitempty"`
  30. Replicas []Location `json:"replicas,omitempty"`
  31. }
  32. func Assign(masterFn GetMasterFn, grpcDialOption grpc.DialOption, primaryRequest *VolumeAssignRequest, alternativeRequests ...*VolumeAssignRequest) (*AssignResult, error) {
  33. var requests []*VolumeAssignRequest
  34. requests = append(requests, primaryRequest)
  35. requests = append(requests, alternativeRequests...)
  36. var lastError error
  37. ret := &AssignResult{}
  38. for i, request := range requests {
  39. if request == nil {
  40. continue
  41. }
  42. lastError = WithMasterServerClient(false, masterFn(), grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  43. req := &master_pb.AssignRequest{
  44. Count: request.Count,
  45. Replication: request.Replication,
  46. Collection: request.Collection,
  47. Ttl: request.Ttl,
  48. DiskType: request.DiskType,
  49. DataCenter: request.DataCenter,
  50. Rack: request.Rack,
  51. DataNode: request.DataNode,
  52. WritableVolumeCount: request.WritableVolumeCount,
  53. }
  54. resp, grpcErr := masterClient.Assign(context.Background(), req)
  55. if grpcErr != nil {
  56. return grpcErr
  57. }
  58. if resp.Error != "" {
  59. return fmt.Errorf("assignRequest: %v", resp.Error)
  60. }
  61. ret.Count = resp.Count
  62. ret.Fid = resp.Fid
  63. ret.Url = resp.Location.Url
  64. ret.PublicUrl = resp.Location.PublicUrl
  65. ret.GrpcPort = int(resp.Location.GrpcPort)
  66. ret.Error = resp.Error
  67. ret.Auth = security.EncodedJwt(resp.Auth)
  68. for _, r := range resp.Replicas {
  69. ret.Replicas = append(ret.Replicas, Location{
  70. Url: r.Url,
  71. PublicUrl: r.PublicUrl,
  72. })
  73. }
  74. return nil
  75. })
  76. if lastError != nil {
  77. continue
  78. }
  79. if ret.Count <= 0 {
  80. lastError = fmt.Errorf("assign failure %d: %v", i+1, ret.Error)
  81. continue
  82. }
  83. break
  84. }
  85. return ret, lastError
  86. }
  87. func LookupJwt(master pb.ServerAddress, grpcDialOption grpc.DialOption, fileId string) (token security.EncodedJwt) {
  88. WithMasterServerClient(false, master, grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
  89. resp, grpcErr := masterClient.LookupVolume(context.Background(), &master_pb.LookupVolumeRequest{
  90. VolumeOrFileIds: []string{fileId},
  91. })
  92. if grpcErr != nil {
  93. return grpcErr
  94. }
  95. if len(resp.VolumeIdLocations) == 0 {
  96. return nil
  97. }
  98. token = security.EncodedJwt(resp.VolumeIdLocations[0].Auth)
  99. return nil
  100. })
  101. return
  102. }
  103. type StorageOption struct {
  104. Replication string
  105. DiskType string
  106. Collection string
  107. DataCenter string
  108. Rack string
  109. DataNode string
  110. TtlSeconds int32
  111. Fsync bool
  112. VolumeGrowthCount uint32
  113. }
  114. func (so *StorageOption) TtlString() string {
  115. return needle.SecondsToTTL(so.TtlSeconds)
  116. }
  117. func (so *StorageOption) ToAssignRequests(count int) (ar *VolumeAssignRequest, altRequest *VolumeAssignRequest) {
  118. ar = &VolumeAssignRequest{
  119. Count: uint64(count),
  120. Replication: so.Replication,
  121. Collection: so.Collection,
  122. Ttl: so.TtlString(),
  123. DiskType: so.DiskType,
  124. DataCenter: so.DataCenter,
  125. Rack: so.Rack,
  126. DataNode: so.DataNode,
  127. WritableVolumeCount: so.VolumeGrowthCount,
  128. }
  129. if so.DataCenter != "" || so.Rack != "" || so.DataNode != "" {
  130. altRequest = &VolumeAssignRequest{
  131. Count: uint64(count),
  132. Replication: so.Replication,
  133. Collection: so.Collection,
  134. Ttl: so.TtlString(),
  135. DiskType: so.DiskType,
  136. DataCenter: "",
  137. Rack: "",
  138. DataNode: "",
  139. WritableVolumeCount: so.VolumeGrowthCount,
  140. }
  141. }
  142. return
  143. }