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.

83 lines
1.8 KiB

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