aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2015-01-11 23:01:31 -0800
committerChris Lu <chris.lu@gmail.com>2015-01-11 23:01:31 -0800
commit352ef2830c32116ede27dbd0eccd753ced217f48 (patch)
tree37f19b392595bebea22a73a593f23cb4a3232ad9
parent2495ce6707befb8720d6bada365be48b473b5e04 (diff)
downloadseaweedfs-352ef2830c32116ede27dbd0eccd753ced217f48.tar.xz
seaweedfs-352ef2830c32116ede27dbd0eccd753ced217f48.zip
Add caching volume locations to batch volume id lookup.
-rw-r--r--go/operation/lookup.go33
1 files changed, 29 insertions, 4 deletions
diff --git a/go/operation/lookup.go b/go/operation/lookup.go
index 70bc7146e..c05eb1d2b 100644
--- a/go/operation/lookup.go
+++ b/go/operation/lookup.go
@@ -27,14 +27,14 @@ func (lr *LookupResult) String() string {
}
var (
- vc VidCache
+ vc VidCache // caching of volume locations, re-check if after 10 minutes
)
func Lookup(server string, vid string) (ret *LookupResult, err error) {
locations, cache_err := vc.Get(vid)
if cache_err != nil {
if ret, err = do_lookup(server, vid); err == nil {
- vc.Set(vid, ret.Locations, 1*time.Minute)
+ vc.Set(vid, ret.Locations, 10*time.Minute)
}
} else {
ret = &LookupResult{VolumeId: vid, Locations: locations}
@@ -75,19 +75,44 @@ func LookupFileId(server string, fileId string) (fullUrl string, err error) {
return "http://" + lookup.Locations[rand.Intn(len(lookup.Locations))].PublicUrl + "/" + fileId, nil
}
+// LookupVolumeIds find volume locations by cache and actual lookup
func LookupVolumeIds(server string, vids []string) (map[string]LookupResult, error) {
- values := make(url.Values)
+ 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
+ values := make(url.Values)
+ for _, vid := range unknown_vids {
values.Add("volumeId", vid)
}
jsonBlob, err := util.Post("http://"+server+"/vol/lookup", values)
if err != nil {
return nil, err
}
- ret := make(map[string]LookupResult)
err = json.Unmarshal(jsonBlob, &ret)
if err != nil {
return nil, errors.New(err.Error() + " " + string(jsonBlob))
}
+
+ //set newly checked vids to cache
+ for _, vid := range unknown_vids {
+ locations := ret[vid].Locations
+ vc.Set(vid, locations, 10*time.Minute)
+ }
+
return ret, nil
}