2012-09-20 09:47:32 +00:00
|
|
|
package operation
|
|
|
|
|
|
|
|
import (
|
2018-10-14 07:30:20 +00:00
|
|
|
"context"
|
2013-01-17 08:56:56 +00:00
|
|
|
"errors"
|
2015-01-08 07:54:50 +00:00
|
|
|
"fmt"
|
2019-02-18 20:11:52 +00:00
|
|
|
"google.golang.org/grpc"
|
2014-03-30 18:28:04 +00:00
|
|
|
"math/rand"
|
2014-02-15 01:10:49 +00:00
|
|
|
"strings"
|
2014-05-25 21:01:54 +00:00
|
|
|
"time"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2018-10-14 07:12:28 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
2012-09-20 09:47:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Location struct {
|
2014-04-14 06:56:15 +00:00
|
|
|
Url string `json:"url,omitempty"`
|
|
|
|
PublicUrl string `json:"publicUrl,omitempty"`
|
2012-09-20 09:47:32 +00:00
|
|
|
}
|
|
|
|
type LookupResult struct {
|
2021-08-13 04:40:33 +00:00
|
|
|
VolumeOrFileId string `json:"volumeOrFileId,omitempty"`
|
|
|
|
Locations []Location `json:"locations,omitempty"`
|
|
|
|
Jwt string `json:"jwt,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
2012-09-20 09:47:32 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 07:54:50 +00:00
|
|
|
func (lr *LookupResult) String() string {
|
2021-08-13 04:40:33 +00:00
|
|
|
return fmt.Sprintf("VolumeOrFileId:%s, Locations:%v, Error:%s", lr.VolumeOrFileId, lr.Locations, lr.Error)
|
2015-01-08 07:54:50 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 21:01:54 +00:00
|
|
|
var (
|
2015-01-12 07:01:31 +00:00
|
|
|
vc VidCache // caching of volume locations, re-check if after 10 minutes
|
2014-05-25 21:01:54 +00:00
|
|
|
)
|
|
|
|
|
2021-08-13 04:40:33 +00:00
|
|
|
func LookupFileId(masterFn GetMasterFn, grpcDialOption grpc.DialOption, fileId string) (fullUrl string, jwt string, err error) {
|
2014-03-30 18:28:04 +00:00
|
|
|
parts := strings.Split(fileId, ",")
|
|
|
|
if len(parts) != 2 {
|
2021-08-13 04:40:33 +00:00
|
|
|
return "", jwt, errors.New("Invalid fileId " + fileId)
|
2013-11-19 07:03:59 +00:00
|
|
|
}
|
2021-08-13 04:40:33 +00:00
|
|
|
lookup, lookupError := LookupVolumeId(masterFn, grpcDialOption, parts[0])
|
2013-11-19 07:03:59 +00:00
|
|
|
if lookupError != nil {
|
2021-08-13 04:40:33 +00:00
|
|
|
return "", jwt, lookupError
|
2013-11-19 07:03:59 +00:00
|
|
|
}
|
|
|
|
if len(lookup.Locations) == 0 {
|
2021-08-13 04:40:33 +00:00
|
|
|
return "", jwt, errors.New("File Not Found")
|
2013-11-19 07:03:59 +00:00
|
|
|
}
|
2021-08-13 04:40:33 +00:00
|
|
|
return "http://" + lookup.Locations[rand.Intn(len(lookup.Locations))].Url + "/" + fileId, lookup.Jwt, nil
|
2013-11-19 07:03:59 +00:00
|
|
|
}
|
2014-04-14 06:56:15 +00:00
|
|
|
|
2021-08-13 03:33:00 +00:00
|
|
|
func LookupVolumeId(masterFn GetMasterFn, grpcDialOption grpc.DialOption, vid string) (*LookupResult, error) {
|
|
|
|
results, err := LookupVolumeIds(masterFn, grpcDialOption, []string{vid})
|
|
|
|
return results[vid], err
|
|
|
|
}
|
|
|
|
|
2015-01-12 07:01:31 +00:00
|
|
|
// LookupVolumeIds find volume locations by cache and actual lookup
|
2021-08-13 03:33:00 +00:00
|
|
|
func LookupVolumeIds(masterFn GetMasterFn, grpcDialOption grpc.DialOption, vids []string) (map[string]*LookupResult, error) {
|
|
|
|
ret := make(map[string]*LookupResult)
|
2015-01-12 07:01:31 +00:00
|
|
|
var unknown_vids []string
|
|
|
|
|
|
|
|
//check vid cache first
|
2014-04-14 06:56:15 +00:00
|
|
|
for _, vid := range vids {
|
2021-08-13 03:33:00 +00:00
|
|
|
locations, cacheErr := vc.Get(vid)
|
|
|
|
if cacheErr == nil {
|
2021-08-13 04:40:33 +00:00
|
|
|
ret[vid] = &LookupResult{VolumeOrFileId: vid, Locations: locations}
|
2015-01-12 07:01:31 +00:00
|
|
|
} 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
|
2018-10-14 07:12:28 +00:00
|
|
|
|
2021-02-18 04:55:55 +00:00
|
|
|
err := WithMasterServerClient(masterFn(), grpcDialOption, func(masterClient master_pb.SeaweedClient) error {
|
2019-01-10 08:43:44 +00:00
|
|
|
|
2018-10-14 07:12:28 +00:00
|
|
|
req := &master_pb.LookupVolumeRequest{
|
2021-08-13 04:40:33 +00:00
|
|
|
VolumeOrFileIds: unknown_vids,
|
2018-10-14 07:12:28 +00:00
|
|
|
}
|
2020-02-26 05:50:12 +00:00
|
|
|
resp, grpcErr := masterClient.LookupVolume(context.Background(), req)
|
2018-10-14 07:12:28 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if vidLocations.Error != "" {
|
2021-08-13 04:40:33 +00:00
|
|
|
vc.Set(vidLocations.VolumeOrFileId, locations, 10*time.Minute)
|
2018-10-14 07:12:28 +00:00
|
|
|
}
|
2021-08-13 04:40:33 +00:00
|
|
|
ret[vidLocations.VolumeOrFileId] = &LookupResult{
|
|
|
|
VolumeOrFileId: vidLocations.VolumeOrFileId,
|
|
|
|
Locations: locations,
|
|
|
|
Jwt: vidLocations.Auth,
|
|
|
|
Error: vidLocations.Error,
|
2018-10-14 07:12:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2014-04-14 06:56:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-12 07:01:31 +00:00
|
|
|
|
2014-04-14 06:56:15 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|