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
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. jsonBlob, err := util.Post("http://"+server+"/dir/assign", values)
  56. glog.V(2).Infof("assign result from %s : %s", server, string(jsonBlob))
  57. if err != nil {
  58. return nil, err
  59. }
  60. var ret AssignResult
  61. err = json.Unmarshal(jsonBlob, &ret)
  62. if err != nil {
  63. return nil, fmt.Errorf("/dir/assign result JSON unmarshal error:%v, json:%s", err, string(jsonBlob))
  64. }
  65. if ret.Count <= 0 {
  66. lastError = fmt.Errorf("assign failure %d: %v", i+1, ret.Error)
  67. continue
  68. }
  69. return &ret, nil
  70. }
  71. return nil, lastError
  72. }