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"
|
2020-02-25 06:28:45 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-11-16 00:58:48 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2020-02-25 06:28:45 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2018-11-20 19:35:45 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
2019-02-15 08:09:19 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/security"
|
|
|
|
"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 {
|
2019-11-10 12:11:03 +00:00
|
|
|
Count uint64
|
|
|
|
Replication string
|
|
|
|
Collection string
|
|
|
|
Ttl string
|
|
|
|
DataCenter string
|
|
|
|
Rack string
|
|
|
|
DataNode string
|
|
|
|
WritableVolumeCount uint32
|
2016-06-26 02:50:18 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 06:26:51 +00:00
|
|
|
type AssignResult struct {
|
2019-02-15 08:09:19 +00:00
|
|
|
Fid string `json:"fid,omitempty"`
|
|
|
|
Url string `json:"url,omitempty"`
|
|
|
|
PublicUrl string `json:"publicUrl,omitempty"`
|
|
|
|
Count uint64 `json:"count,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
Auth security.EncodedJwt `json:"auth,omitempty"`
|
2013-08-14 06:26:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 20:11:52 +00:00
|
|
|
func Assign(server string, grpcDialOption grpc.DialOption, 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
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
lastError = WithMasterServerClient(server, grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
|
2019-01-10 08:43:44 +00:00
|
|
|
|
2018-11-20 19:35:45 +00:00
|
|
|
req := &master_pb.AssignRequest{
|
2019-11-12 06:21:43 +00:00
|
|
|
Count: primaryRequest.Count,
|
|
|
|
Replication: primaryRequest.Replication,
|
|
|
|
Collection: primaryRequest.Collection,
|
|
|
|
Ttl: primaryRequest.Ttl,
|
|
|
|
DataCenter: primaryRequest.DataCenter,
|
|
|
|
Rack: primaryRequest.Rack,
|
|
|
|
DataNode: primaryRequest.DataNode,
|
2019-11-10 12:11:03 +00:00
|
|
|
WritableVolumeCount: primaryRequest.WritableVolumeCount,
|
2018-11-20 19:35:45 +00:00
|
|
|
}
|
2019-02-20 09:01:01 +00:00
|
|
|
resp, grpcErr := masterClient.Assign(context.Background(), 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
|
2019-02-15 08:09:19 +00:00
|
|
|
ret.Auth = security.EncodedJwt(resp.Auth)
|
2018-11-20 19:35:45 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2019-02-15 08:09:19 +00:00
|
|
|
|
|
|
|
func LookupJwt(master string, fileId string) security.EncodedJwt {
|
|
|
|
|
|
|
|
tokenStr := ""
|
|
|
|
|
2020-02-10 21:43:53 +00:00
|
|
|
if h, e := util.Head(fmt.Sprintf("http://%s/dir/lookup?fileId=%s", master, fileId)); e == nil {
|
|
|
|
bearer := h.Get("Authorization")
|
|
|
|
if len(bearer) > 7 && strings.ToUpper(bearer[0:6]) == "BEARER" {
|
|
|
|
tokenStr = bearer[7:]
|
2019-02-15 08:09:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return security.EncodedJwt(tokenStr)
|
|
|
|
}
|
2020-11-16 00:58:48 +00:00
|
|
|
|
|
|
|
type StorageOption struct {
|
|
|
|
Replication string
|
|
|
|
Collection string
|
|
|
|
DataCenter string
|
|
|
|
Rack string
|
|
|
|
TtlSeconds int32
|
|
|
|
Fsync bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (so *StorageOption) TtlString() string {
|
|
|
|
return needle.SecondsToTTL(so.TtlSeconds)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (so *StorageOption) ToAssignRequests(count int) (ar *VolumeAssignRequest, altRequest *VolumeAssignRequest) {
|
|
|
|
ar = &VolumeAssignRequest{
|
|
|
|
Count: uint64(count),
|
|
|
|
Replication: so.Replication,
|
|
|
|
Collection: so.Collection,
|
|
|
|
Ttl: so.TtlString(),
|
|
|
|
DataCenter: so.DataCenter,
|
|
|
|
Rack: so.Rack,
|
|
|
|
}
|
|
|
|
if so.DataCenter != "" || so.Rack != "" {
|
|
|
|
altRequest = &VolumeAssignRequest{
|
|
|
|
Count: uint64(count),
|
|
|
|
Replication: so.Replication,
|
|
|
|
Collection: so.Collection,
|
|
|
|
Ttl: so.TtlString(),
|
|
|
|
DataCenter: "",
|
|
|
|
Rack: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|