seaweedfs/weed/operation/assign_file_id.go

173 lines
4.6 KiB
Go
Raw Normal View History

package operation
import (
2018-11-23 16:26:15 +08:00
"context"
"fmt"
"github.com/chrislusf/seaweedfs/weed/pb"
2020-11-16 08:58:48 +08:00
"github.com/chrislusf/seaweedfs/weed/storage/needle"
"google.golang.org/grpc"
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
2019-02-15 16:09:19 +08:00
"github.com/chrislusf/seaweedfs/weed/security"
)
2016-06-26 10:50:18 +08:00
type VolumeAssignRequest struct {
Count uint64
Replication string
Collection string
Ttl string
2020-12-17 01:14:05 +08:00
DiskType string
DataCenter string
Rack string
DataNode string
WritableVolumeCount uint32
2016-06-26 10:50:18 +08:00
}
type AssignResult struct {
Fid string `json:"fid,omitempty"`
Url string `json:"url,omitempty"`
PublicUrl string `json:"publicUrl,omitempty"`
GrpcPort int `json:"grpcPort,omitempty"`
Count uint64 `json:"count,omitempty"`
Error string `json:"error,omitempty"`
Auth security.EncodedJwt `json:"auth,omitempty"`
Replicas []Location `json:"replicas,omitempty"`
}
func Assign(masterFn GetMasterFn, grpcDialOption grpc.DialOption, primaryRequest *VolumeAssignRequest, alternativeRequests ...*VolumeAssignRequest) (*AssignResult, error) {
var requests []*VolumeAssignRequest
requests = append(requests, primaryRequest)
requests = append(requests, alternativeRequests...)
2016-06-23 11:19:09 +08:00
var lastError error
ret := &AssignResult{}
for i, request := range requests {
if request == nil {
continue
}
lastError = WithMasterServerClient(masterFn(), grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
req := &master_pb.AssignRequest{
2020-11-17 16:36:21 +08:00
Count: request.Count,
Replication: request.Replication,
Collection: request.Collection,
Ttl: request.Ttl,
2020-12-17 01:14:05 +08:00
DiskType: request.DiskType,
2020-11-17 16:36:21 +08:00
DataCenter: request.DataCenter,
Rack: request.Rack,
DataNode: request.DataNode,
WritableVolumeCount: request.WritableVolumeCount,
}
resp, grpcErr := masterClient.Assign(context.Background(), req)
if grpcErr != nil {
return grpcErr
}
if resp.Error != "" {
return fmt.Errorf("assignRequest: %v", resp.Error)
}
ret.Count = resp.Count
ret.Fid = resp.Fid
ret.Url = resp.Location.Url
ret.PublicUrl = resp.Location.PublicUrl
ret.GrpcPort = int(resp.Location.GrpcPort)
ret.Error = resp.Error
2019-02-15 16:09:19 +08:00
ret.Auth = security.EncodedJwt(resp.Auth)
2021-09-09 06:54:55 +08:00
for _, r := range resp.Replicas {
ret.Replicas = append(ret.Replicas, Location{
Url: r.Url,
PublicUrl: r.PublicUrl,
})
}
return nil
})
if lastError != nil {
continue
}
if ret.Count <= 0 {
lastError = fmt.Errorf("assign failure %d: %v", i+1, ret.Error)
continue
}
2021-04-14 18:29:28 +08:00
break
}
return ret, lastError
}
2019-02-15 16:09:19 +08:00
func LookupJwt(master pb.ServerAddress, grpcDialOption grpc.DialOption, fileId string) (token security.EncodedJwt) {
2019-02-15 16:09:19 +08:00
2021-08-13 12:40:33 +08:00
WithMasterServerClient(master, grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
2019-02-15 16:09:19 +08:00
2021-08-13 12:40:33 +08:00
resp, grpcErr := masterClient.LookupVolume(context.Background(), &master_pb.LookupVolumeRequest{
VolumeOrFileIds: []string{fileId},
})
if grpcErr != nil {
return grpcErr
2019-02-15 16:09:19 +08:00
}
2021-08-13 12:40:33 +08:00
if len(resp.VolumeIdLocations) == 0 {
return nil
}
token = security.EncodedJwt(resp.VolumeIdLocations[0].Auth)
return nil
})
return
2019-02-15 16:09:19 +08:00
}
2020-11-16 08:58:48 +08:00
type StorageOption struct {
Replication string
2020-12-17 01:14:05 +08:00
DiskType string
Collection string
DataCenter string
Rack string
DataNode string
TtlSeconds int32
Fsync bool
VolumeGrowthCount uint32
2020-11-16 08:58:48 +08:00
}
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(),
2020-12-17 01:14:05 +08:00
DiskType: so.DiskType,
DataCenter: so.DataCenter,
Rack: so.Rack,
DataNode: so.DataNode,
WritableVolumeCount: so.VolumeGrowthCount,
2020-11-16 08:58:48 +08:00
}
if so.DataCenter != "" || so.Rack != "" || so.DataNode != "" {
2020-11-16 08:58:48 +08:00
altRequest = &VolumeAssignRequest{
Count: uint64(count),
Replication: so.Replication,
Collection: so.Collection,
Ttl: so.TtlString(),
2020-12-17 01:14:05 +08:00
DiskType: so.DiskType,
DataCenter: "",
Rack: "",
DataNode: "",
WritableVolumeCount: so.VolumeGrowthCount,
2020-11-16 08:58:48 +08:00
}
}
return
}