2012-09-20 09:47:32 +00:00
|
|
|
package operation
|
|
|
|
|
|
|
|
import (
|
2013-01-17 08:56:56 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2015-01-08 07:54:50 +00:00
|
|
|
"fmt"
|
2014-03-30 18:28:04 +00:00
|
|
|
"math/rand"
|
2013-01-17 08:56:56 +00:00
|
|
|
"net/url"
|
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
|
|
|
|
|
|
|
"github.com/chrislusf/weed-fs/go/util"
|
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 {
|
2014-04-14 06:56:15 +00:00
|
|
|
VolumeId string `json:"volumeId,omitempty"`
|
|
|
|
Locations []Location `json:"locations,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 {
|
|
|
|
return fmt.Sprintf("VolumeId:%s, Locations:%v, Error:%s", lr.VolumeId, lr.Locations, lr.Error)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
func Lookup(server string, vid string) (ret *LookupResult, err error) {
|
|
|
|
locations, cache_err := vc.Get(vid)
|
|
|
|
if cache_err != nil {
|
2014-06-02 16:22:23 +00:00
|
|
|
if ret, err = do_lookup(server, vid); err == nil {
|
2015-01-12 07:01:31 +00:00
|
|
|
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(server string, vid string) (*LookupResult, error) {
|
2013-01-17 08:56:56 +00:00
|
|
|
values := make(url.Values)
|
2014-02-15 01:10:49 +00:00
|
|
|
values.Add("volumeId", vid)
|
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
|
2012-09-20 09:47:32 +00:00
|
|
|
}
|
2013-11-19 07:03:59 +00:00
|
|
|
|
|
|
|
func LookupFileId(server string, fileId string) (fullUrl string, err error) {
|
2014-03-30 18:28:04 +00:00
|
|
|
parts := strings.Split(fileId, ",")
|
|
|
|
if len(parts) != 2 {
|
2014-02-15 01:10:49 +00:00
|
|
|
return "", errors.New("Invalid fileId " + fileId)
|
2013-11-19 07:03:59 +00:00
|
|
|
}
|
2014-03-30 18:28:04 +00:00
|
|
|
lookup, lookupError := Lookup(server, 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")
|
|
|
|
}
|
2014-03-30 18:28:04 +00:00
|
|
|
return "http://" + lookup.Locations[rand.Intn(len(lookup.Locations))].PublicUrl + "/" + fileId, nil
|
2013-11-19 07:03:59 +00:00
|
|
|
}
|
2014-04-14 06:56:15 +00:00
|
|
|
|
2015-01-12 07:01:31 +00:00
|
|
|
// LookupVolumeIds find volume locations by cache and actual lookup
|
2014-04-15 16:09:40 +00:00
|
|
|
func LookupVolumeIds(server string, vids []string) (map[string]LookupResult, error) {
|
2015-01-12 07:01:31 +00:00
|
|
|
ret := make(map[string]LookupResult)
|
|
|
|
var unknown_vids []string
|
|
|
|
|
|
|
|
//check vid cache first
|
2014-04-14 06:56:15 +00:00
|
|
|
for _, vid := range vids {
|
2015-01-12 07:01:31 +00:00
|
|
|
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
|
|
|
|
values := make(url.Values)
|
|
|
|
for _, vid := range unknown_vids {
|
2014-04-14 06:56:15 +00:00
|
|
|
values.Add("volumeId", vid)
|
|
|
|
}
|
|
|
|
jsonBlob, err := util.Post("http://"+server+"/vol/lookup", values)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(jsonBlob, &ret)
|
|
|
|
if err != nil {
|
2014-04-15 16:09:40 +00:00
|
|
|
return nil, errors.New(err.Error() + " " + string(jsonBlob))
|
2014-04-14 06:56:15 +00:00
|
|
|
}
|
2015-01-12 07:01:31 +00:00
|
|
|
|
|
|
|
//set newly checked vids to cache
|
|
|
|
for _, vid := range unknown_vids {
|
|
|
|
locations := ret[vid].Locations
|
|
|
|
vc.Set(vid, locations, 10*time.Minute)
|
|
|
|
}
|
|
|
|
|
2014-04-14 06:56:15 +00:00
|
|
|
return ret, nil
|
|
|
|
}
|