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.

169 lines
4.4 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/chrislusf/seaweedfs/weed/pb"
  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. )
  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(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. ret.Count = resp.Count
  59. ret.Fid = resp.Fid
  60. ret.Url = resp.Location.Url
  61. ret.PublicUrl = resp.Location.PublicUrl
  62. ret.GrpcPort = int(resp.Location.GrpcPort)
  63. ret.Error = resp.Error
  64. ret.Auth = security.EncodedJwt(resp.Auth)
  65. for _, r := range resp.Replicas {
  66. ret.Replicas = append(ret.Replicas, Location{
  67. Url: r.Url,
  68. PublicUrl: r.PublicUrl,
  69. })
  70. }
  71. if resp.Error != "" {
  72. return fmt.Errorf("assignRequest: %v", resp.Error)
  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(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. TtlSeconds int32
  110. Fsync bool
  111. VolumeGrowthCount uint32
  112. }
  113. func (so *StorageOption) TtlString() string {
  114. return needle.SecondsToTTL(so.TtlSeconds)
  115. }
  116. func (so *StorageOption) ToAssignRequests(count int) (ar *VolumeAssignRequest, altRequest *VolumeAssignRequest) {
  117. ar = &VolumeAssignRequest{
  118. Count: uint64(count),
  119. Replication: so.Replication,
  120. Collection: so.Collection,
  121. Ttl: so.TtlString(),
  122. DiskType: so.DiskType,
  123. DataCenter: so.DataCenter,
  124. Rack: so.Rack,
  125. WritableVolumeCount: so.VolumeGrowthCount,
  126. }
  127. if so.DataCenter != "" || so.Rack != "" {
  128. altRequest = &VolumeAssignRequest{
  129. Count: uint64(count),
  130. Replication: so.Replication,
  131. Collection: so.Collection,
  132. Ttl: so.TtlString(),
  133. DiskType: so.DiskType,
  134. DataCenter: "",
  135. Rack: "",
  136. WritableVolumeCount: so.VolumeGrowthCount,
  137. }
  138. }
  139. return
  140. }