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.

80 lines
2.0 KiB

  1. package operation
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. type VolumeAssignRequest struct {
  11. Count uint64
  12. Replication string
  13. Collection string
  14. Ttl string
  15. DataCenter string
  16. Rack string
  17. DataNode string
  18. }
  19. type AssignResult struct {
  20. Fid string `json:"fid,omitempty"`
  21. Url string `json:"url,omitempty"`
  22. PublicUrl string `json:"publicUrl,omitempty"`
  23. Count uint64 `json:"count,omitempty"`
  24. Error string `json:"error,omitempty"`
  25. }
  26. func Assign(server string, primaryRequest *VolumeAssignRequest, alternativeRequests ...*VolumeAssignRequest) (*AssignResult, error) {
  27. var requests []*VolumeAssignRequest
  28. requests = append(requests, primaryRequest)
  29. requests = append(requests, alternativeRequests...)
  30. var lastError error
  31. for i, request := range requests {
  32. if request == nil {
  33. continue
  34. }
  35. values := make(url.Values)
  36. values.Add("count", strconv.FormatUint(request.Count, 10))
  37. if request.Replication != "" {
  38. values.Add("replication", request.Replication)
  39. }
  40. if request.Collection != "" {
  41. values.Add("collection", request.Collection)
  42. }
  43. if request.Ttl != "" {
  44. values.Add("ttl", request.Ttl)
  45. }
  46. if request.DataCenter != "" {
  47. values.Add("dataCenter", request.DataCenter)
  48. }
  49. if request.Rack != "" {
  50. values.Add("rack", request.Rack)
  51. }
  52. if request.DataNode != "" {
  53. values.Add("dataNode", request.DataNode)
  54. }
  55. postUrl := fmt.Sprintf("http://%s/dir/assign", server)
  56. jsonBlob, err := util.Post(postUrl, values)
  57. glog.V(2).Infof("assign %d result from %s %+v : %s", i, postUrl, values, string(jsonBlob))
  58. if err != nil {
  59. return nil, err
  60. }
  61. var ret AssignResult
  62. err = json.Unmarshal(jsonBlob, &ret)
  63. if err != nil {
  64. return nil, fmt.Errorf("/dir/assign result JSON unmarshal error:%v, json:%s", err, string(jsonBlob))
  65. }
  66. if ret.Count <= 0 {
  67. lastError = fmt.Errorf("assign failure %d: %v", i+1, ret.Error)
  68. continue
  69. }
  70. return &ret, nil
  71. }
  72. return nil, lastError
  73. }