seaweedfs/weed/operation/lookup.go

141 lines
3.5 KiB
Go
Raw Permalink Normal View History

package operation
import (
2018-10-14 07:30:20 +00:00
"context"
2013-01-17 08:56:56 +00:00
"encoding/json"
"errors"
"fmt"
2019-02-18 20:11:52 +00:00
"google.golang.org/grpc"
2014-03-30 18:28:04 +00:00
"math/rand"
2013-01-17 08:56:56 +00:00
"net/url"
"strings"
2014-05-25 21:01:54 +00:00
"time"
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
2018-10-14 07:30:20 +00:00
"github.com/chrislusf/seaweedfs/weed/util"
)
type Location struct {
Url string `json:"url,omitempty"`
PublicUrl string `json:"publicUrl,omitempty"`
}
type LookupResult struct {
VolumeId string `json:"volumeId,omitempty"`
Locations []Location `json:"locations,omitempty"`
Error string `json:"error,omitempty"`
}
func (lr *LookupResult) String() string {
return fmt.Sprintf("VolumeId:%s, Locations:%v, Error:%s", lr.VolumeId, lr.Locations, lr.Error)
}
2014-05-25 21:01:54 +00:00
var (
vc VidCache // caching of volume locations, re-check if after 10 minutes
2014-05-25 21:01:54 +00:00
)
func Lookup(masterFn GetMasterFn, vid string) (ret *LookupResult, err error) {
2014-05-25 21:01:54 +00:00
locations, cache_err := vc.Get(vid)
if cache_err != nil {
if ret, err = do_lookup(masterFn, vid); err == nil {
vc.Set(vid, ret.Locations, 10*time.Minute)
2014-06-02 16:22:23 +00:00
}
2014-05-25 21:01:54 +00:00
} else {
ret = &LookupResult{VolumeId: vid, Locations: locations}
}
return
}
func do_lookup(masterFn GetMasterFn, vid string) (*LookupResult, error) {
2013-01-17 08:56:56 +00:00
values := make(url.Values)
values.Add("volumeId", vid)
server := masterFn()
2013-01-17 08:56:56 +00:00
jsonBlob, err := util.Post("http://"+server+"/dir/lookup", values)
if err != nil {
return nil, err
}
var ret LookupResult
err = json.Unmarshal(jsonBlob, &ret)
if err != nil {
return nil, err
}
if ret.Error != "" {
return nil, errors.New(ret.Error)
}
return &ret, nil
}
2013-11-19 07:03:59 +00:00
func LookupFileId(masterFn GetMasterFn, fileId string) (fullUrl string, err error) {
2014-03-30 18:28:04 +00:00
parts := strings.Split(fileId, ",")
if len(parts) != 2 {
return "", errors.New("Invalid fileId " + fileId)
2013-11-19 07:03:59 +00:00
}
lookup, lookupError := Lookup(masterFn, parts[0])
2013-11-19 07:03:59 +00:00
if lookupError != nil {
return "", lookupError
}
if len(lookup.Locations) == 0 {
return "", errors.New("File Not Found")
}
return "http://" + lookup.Locations[rand.Intn(len(lookup.Locations))].Url + "/" + fileId, nil
2013-11-19 07:03:59 +00:00
}
// 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, cache_err := vc.Get(vid)
if cache_err == nil {
ret[vid] = LookupResult{VolumeId: 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{
VolumeIds: 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,
})
}
if vidLocations.Error != "" {
vc.Set(vidLocations.VolumeId, locations, 10*time.Minute)
}
ret[vidLocations.VolumeId] = LookupResult{
VolumeId: vidLocations.VolumeId,
Locations: locations,
Error: vidLocations.Error,
}
}
return nil
})
if err != nil {
return nil, err
}
return ret, nil
}