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.

79 lines
1.7 KiB

6 years ago
6 years ago
  1. package operation
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  6. )
  7. type VolumeAssignRequest struct {
  8. Count uint64
  9. Replication string
  10. Collection string
  11. Ttl string
  12. DataCenter string
  13. Rack string
  14. DataNode string
  15. }
  16. type AssignResult struct {
  17. Fid string `json:"fid,omitempty"`
  18. Url string `json:"url,omitempty"`
  19. PublicUrl string `json:"publicUrl,omitempty"`
  20. Count uint64 `json:"count,omitempty"`
  21. Error string `json:"error,omitempty"`
  22. }
  23. func Assign(server string, primaryRequest *VolumeAssignRequest, alternativeRequests ...*VolumeAssignRequest) (*AssignResult, error) {
  24. var requests []*VolumeAssignRequest
  25. requests = append(requests, primaryRequest)
  26. requests = append(requests, alternativeRequests...)
  27. var lastError error
  28. ret := &AssignResult{}
  29. for i, request := range requests {
  30. if request == nil {
  31. continue
  32. }
  33. lastError = withMasterServerClient(server, func(masterClient master_pb.SeaweedClient) error {
  34. req := &master_pb.AssignRequest{
  35. Count: primaryRequest.Count,
  36. Replication: primaryRequest.Replication,
  37. Collection: primaryRequest.Collection,
  38. Ttl: primaryRequest.Ttl,
  39. DataCenter: primaryRequest.DataCenter,
  40. Rack: primaryRequest.Rack,
  41. DataNode: primaryRequest.DataNode,
  42. }
  43. resp, grpcErr := masterClient.Assign(context.Background(), req)
  44. if grpcErr != nil {
  45. return grpcErr
  46. }
  47. ret.Count = resp.Count
  48. ret.Fid = resp.Fid
  49. ret.Url = resp.Url
  50. ret.PublicUrl = resp.PublicUrl
  51. ret.Error = resp.Error
  52. return nil
  53. })
  54. if lastError != nil {
  55. continue
  56. }
  57. if ret.Count <= 0 {
  58. lastError = fmt.Errorf("assign failure %d: %v", i+1, ret.Error)
  59. continue
  60. }
  61. }
  62. return ret, lastError
  63. }