2013-08-14 06:26:51 +00:00
|
|
|
package operation
|
|
|
|
|
|
|
|
import (
|
2018-11-23 08:26:15 +00:00
|
|
|
"context"
|
2015-03-09 08:10:01 +00:00
|
|
|
"fmt"
|
2019-01-10 08:43:44 +00:00
|
|
|
"time"
|
2018-11-23 08:26:15 +00:00
|
|
|
|
2018-11-20 19:35:45 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
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) {
|
2018-11-20 19:35:45 +00:00
|
|
|
|
2018-07-10 06:18:20 +00:00
|
|
|
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
|
2018-11-20 19:35:45 +00:00
|
|
|
ret := &AssignResult{}
|
|
|
|
|
2018-07-10 06:18:20 +00:00
|
|
|
for i, request := range requests {
|
|
|
|
if request == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-20 19:35:45 +00:00
|
|
|
lastError = withMasterServerClient(server, func(masterClient master_pb.SeaweedClient) error {
|
2019-01-10 08:43:44 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
|
|
|
|
defer cancel()
|
|
|
|
|
2018-11-20 19:35:45 +00:00
|
|
|
req := &master_pb.AssignRequest{
|
|
|
|
Count: primaryRequest.Count,
|
|
|
|
Replication: primaryRequest.Replication,
|
|
|
|
Collection: primaryRequest.Collection,
|
|
|
|
Ttl: primaryRequest.Ttl,
|
|
|
|
DataCenter: primaryRequest.DataCenter,
|
|
|
|
Rack: primaryRequest.Rack,
|
|
|
|
DataNode: primaryRequest.DataNode,
|
|
|
|
}
|
2019-01-10 08:43:44 +00:00
|
|
|
resp, grpcErr := masterClient.Assign(ctx, req)
|
2018-11-20 19:35:45 +00:00
|
|
|
if grpcErr != nil {
|
|
|
|
return grpcErr
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.Count = resp.Count
|
|
|
|
ret.Fid = resp.Fid
|
|
|
|
ret.Url = resp.Url
|
|
|
|
ret.PublicUrl = resp.PublicUrl
|
|
|
|
ret.Error = resp.Error
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
if lastError != nil {
|
|
|
|
continue
|
2018-07-10 06:18:20 +00:00
|
|
|
}
|
2018-11-20 19:35:45 +00:00
|
|
|
|
2018-07-10 06:18:20 +00:00
|
|
|
if ret.Count <= 0 {
|
|
|
|
lastError = fmt.Errorf("assign failure %d: %v", i+1, ret.Error)
|
|
|
|
continue
|
|
|
|
}
|
2018-11-20 19:35:45 +00:00
|
|
|
|
2013-08-14 06:26:51 +00:00
|
|
|
}
|
2018-11-20 19:35:45 +00:00
|
|
|
|
|
|
|
return ret, lastError
|
2013-08-14 06:26:51 +00:00
|
|
|
}
|