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

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