2013-08-14 06:26:51 +00:00
|
|
|
package operation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-03-09 08:10:01 +00:00
|
|
|
"fmt"
|
2013-08-14 06:26:51 +00:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2016-06-03 01:09:14 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2013-08-14 06:26:51 +00:00
|
|
|
)
|
|
|
|
|
2016-06-26 02:50:18 +00:00
|
|
|
type VolumeAssignRequest struct {
|
|
|
|
Count uint64
|
|
|
|
Replication string
|
|
|
|
Collection string
|
|
|
|
Ttl string
|
|
|
|
DataCenter string
|
|
|
|
Rack string
|
|
|
|
DataNode string
|
|
|
|
}
|
|
|
|
|
2013-08-14 06:26:51 +00:00
|
|
|
type AssignResult struct {
|
2014-04-15 16:20:04 +00:00
|
|
|
Fid string `json:"fid,omitempty"`
|
|
|
|
Url string `json:"url,omitempty"`
|
|
|
|
PublicUrl string `json:"publicUrl,omitempty"`
|
2015-04-06 21:17:36 +00:00
|
|
|
Count uint64 `json:"count,omitempty"`
|
2014-04-15 16:20:04 +00:00
|
|
|
Error string `json:"error,omitempty"`
|
2013-08-14 06:26:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 06:18:20 +00:00
|
|
|
func Assign(server string, primaryRequest *VolumeAssignRequest, alternativeRequests ...*VolumeAssignRequest) (*AssignResult, error) {
|
|
|
|
var requests []*VolumeAssignRequest
|
|
|
|
requests = append(requests, primaryRequest)
|
|
|
|
requests = append(requests, alternativeRequests...)
|
2016-06-23 03:19:09 +00:00
|
|
|
|
2018-07-10 06:18:20 +00:00
|
|
|
var lastError error
|
|
|
|
for i, request := range requests {
|
|
|
|
if request == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
values := make(url.Values)
|
|
|
|
values.Add("count", strconv.FormatUint(request.Count, 10))
|
|
|
|
if request.Replication != "" {
|
|
|
|
values.Add("replication", request.Replication)
|
|
|
|
}
|
|
|
|
if request.Collection != "" {
|
|
|
|
values.Add("collection", request.Collection)
|
|
|
|
}
|
|
|
|
if request.Ttl != "" {
|
|
|
|
values.Add("ttl", request.Ttl)
|
|
|
|
}
|
|
|
|
if request.DataCenter != "" {
|
|
|
|
values.Add("dataCenter", request.DataCenter)
|
|
|
|
}
|
|
|
|
if request.Rack != "" {
|
|
|
|
values.Add("rack", request.Rack)
|
|
|
|
}
|
|
|
|
if request.DataNode != "" {
|
|
|
|
values.Add("dataNode", request.DataNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBlob, err := util.Post("http://"+server+"/dir/assign", values)
|
|
|
|
glog.V(2).Infof("assign result from %s : %s", server, string(jsonBlob))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var ret AssignResult
|
|
|
|
err = json.Unmarshal(jsonBlob, &ret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("/dir/assign result JSON unmarshal error:%v, json:%s", err, string(jsonBlob))
|
|
|
|
}
|
|
|
|
if ret.Count <= 0 {
|
|
|
|
lastError = fmt.Errorf("assign failure %d: %v", i+1, ret.Error)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return &ret, nil
|
2013-08-14 06:26:51 +00:00
|
|
|
}
|
2018-07-10 06:18:20 +00:00
|
|
|
return nil, lastError
|
2013-08-14 06:26:51 +00:00
|
|
|
}
|