seaweedfs/weed/operation/lookup.go

122 lines
3.3 KiB
Go
Raw Normal View History

package operation
import (
2018-10-14 15:30:20 +08:00
"context"
2013-01-17 16:56:56 +08:00
"errors"
"fmt"
"github.com/chrislusf/seaweedfs/weed/pb"
2019-02-19 04:11:52 +08:00
"google.golang.org/grpc"
2014-03-31 02:28:04 +08:00
"math/rand"
"strings"
2014-05-26 05:01:54 +08:00
"time"
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
)
type Location struct {
Url string `json:"url,omitempty"`
PublicUrl string `json:"publicUrl,omitempty"`
GrpcPort int `json:"grpcPort,omitempty"`
}
func (l *Location) ServerAddress() pb.ServerAddress {
return pb.NewServerAddressWithGrpcPort(l.Url, l.GrpcPort)
}
type LookupResult struct {
2021-08-13 12:40:33 +08:00
VolumeOrFileId string `json:"volumeOrFileId,omitempty"`
Locations []Location `json:"locations,omitempty"`
Jwt string `json:"jwt,omitempty"`
Error string `json:"error,omitempty"`
}
func (lr *LookupResult) String() string {
2021-08-13 12:40:33 +08:00
return fmt.Sprintf("VolumeOrFileId:%s, Locations:%v, Error:%s", lr.VolumeOrFileId, lr.Locations, lr.Error)
}
2014-05-26 05:01:54 +08:00
var (
vc VidCache // caching of volume locations, re-check if after 10 minutes
2014-05-26 05:01:54 +08:00
)
2021-08-13 12:40:33 +08:00
func LookupFileId(masterFn GetMasterFn, grpcDialOption grpc.DialOption, fileId string) (fullUrl string, jwt string, err error) {
2014-03-31 02:28:04 +08:00
parts := strings.Split(fileId, ",")
if len(parts) != 2 {
2021-08-13 12:40:33 +08:00
return "", jwt, errors.New("Invalid fileId " + fileId)
2013-11-19 15:03:59 +08:00
}
2021-08-13 12:40:33 +08:00
lookup, lookupError := LookupVolumeId(masterFn, grpcDialOption, parts[0])
2013-11-19 15:03:59 +08:00
if lookupError != nil {
2021-08-13 12:40:33 +08:00
return "", jwt, lookupError
2013-11-19 15:03:59 +08:00
}
if len(lookup.Locations) == 0 {
2021-08-13 12:40:33 +08:00
return "", jwt, errors.New("File Not Found")
2013-11-19 15:03:59 +08:00
}
2021-08-13 12:40:33 +08:00
return "http://" + lookup.Locations[rand.Intn(len(lookup.Locations))].Url + "/" + fileId, lookup.Jwt, nil
2013-11-19 15:03:59 +08:00
}
func LookupVolumeId(masterFn GetMasterFn, grpcDialOption grpc.DialOption, vid string) (*LookupResult, error) {
results, err := LookupVolumeIds(masterFn, grpcDialOption, []string{vid})
return results[vid], err
}
// LookupVolumeIds find volume locations by cache and actual lookup
func LookupVolumeIds(masterFn GetMasterFn, grpcDialOption grpc.DialOption, vids []string) (map[string]*LookupResult, error) {
ret := make(map[string]*LookupResult)
var unknown_vids []string
//check vid cache first
for _, vid := range vids {
locations, cacheErr := vc.Get(vid)
if cacheErr == nil {
2021-08-13 12:40:33 +08:00
ret[vid] = &LookupResult{VolumeOrFileId: vid, Locations: locations}
} else {
unknown_vids = append(unknown_vids, vid)
}
}
//return success if all volume ids are known
if len(unknown_vids) == 0 {
return ret, nil
}
//only query unknown_vids
err := WithMasterServerClient(masterFn(), grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
req := &master_pb.LookupVolumeRequest{
2021-08-13 12:40:33 +08:00
VolumeOrFileIds: unknown_vids,
}
resp, grpcErr := masterClient.LookupVolume(context.Background(), req)
if grpcErr != nil {
return grpcErr
}
//set newly checked vids to cache
for _, vidLocations := range resp.VolumeIdLocations {
var locations []Location
for _, loc := range vidLocations.Locations {
locations = append(locations, Location{
Url: loc.Url,
PublicUrl: loc.PublicUrl,
2021-09-15 01:37:06 +08:00
GrpcPort: int(loc.GrpcPort),
})
}
if vidLocations.Error != "" {
2021-08-13 12:40:33 +08:00
vc.Set(vidLocations.VolumeOrFileId, locations, 10*time.Minute)
}
2021-08-13 12:40:33 +08:00
ret[vidLocations.VolumeOrFileId] = &LookupResult{
VolumeOrFileId: vidLocations.VolumeOrFileId,
Locations: locations,
Jwt: vidLocations.Auth,
Error: vidLocations.Error,
}
}
return nil
})
if err != nil {
return nil, err
}
return ret, nil
}