From 32ba7fc6c015cd3f4f83af4a261b89e7262d1477 Mon Sep 17 00:00:00 2001 From: chrislusf Date: Tue, 26 May 2015 10:29:49 -0700 Subject: [PATCH] Fix a possible index out of range error. Remove unnecessary caching. --- go/operation/lookup_vid_cache.go | 24 +++++++++++++------ go/weed/benchmark.go | 40 +++++++++++--------------------- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/go/operation/lookup_vid_cache.go b/go/operation/lookup_vid_cache.go index 72e7da924..ac4240102 100644 --- a/go/operation/lookup_vid_cache.go +++ b/go/operation/lookup_vid_cache.go @@ -4,6 +4,8 @@ import ( "errors" "strconv" "time" + + "github.com/chrislusf/seaweedfs/go/glog" ) type VidInfo struct { @@ -15,7 +17,11 @@ type VidCache struct { } func (vc *VidCache) Get(vid string) ([]Location, error) { - id, _ := strconv.Atoi(vid) + id, err := strconv.Atoi(vid) + if err != nil { + glog.V(1).Infof("Unknown volume id %s", vid) + return nil, err + } if 0 < id && id <= len(vc.cache) { if vc.cache[id-1].Locations == nil { return nil, errors.New("Not Set") @@ -28,14 +34,18 @@ func (vc *VidCache) Get(vid string) ([]Location, error) { return nil, errors.New("Not Found") } func (vc *VidCache) Set(vid string, locations []Location, duration time.Duration) { - id, _ := strconv.Atoi(vid) - if id >= len(vc.cache) { + id, err := strconv.Atoi(vid) + if err != nil { + glog.V(1).Infof("Unknown volume id %s", vid) + return + } + if id > len(vc.cache) { for i := id - len(vc.cache); i > 0; i-- { vc.cache = append(vc.cache, VidInfo{}) } } - - vc.cache[id-1].Locations = locations - vc.cache[id-1].NextRefreshTime = time.Now().Add(duration) - + if id > 0 { + vc.cache[id-1].Locations = locations + vc.cache[id-1].NextRefreshTime = time.Now().Add(duration) + } } diff --git a/go/weed/benchmark.go b/go/weed/benchmark.go index 0529a1e52..b63f0008e 100644 --- a/go/weed/benchmark.go +++ b/go/weed/benchmark.go @@ -34,8 +34,6 @@ type BenchmarkOptions struct { cpuprofile *string maxCpu *int secretKey *string - vid2server map[string]string //cache for vid locations - } var ( @@ -59,7 +57,6 @@ func init() { b.cpuprofile = cmdBenchmark.Flag.String("cpuprofile", "", "cpu profile output file") b.maxCpu = cmdBenchmark.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs") b.secretKey = cmdBenchmark.Flag.String("secure.secret", "", "secret to encrypt Json Web Token(JWT)") - b.vid2server = make(map[string]string) sharedBytes = make([]byte, 1024) } @@ -238,7 +235,6 @@ func writeFiles(idChan chan int, fileIdLineChan chan string, s *stat) { func readFiles(fileIdLineChan chan string, s *stat) { defer wait.Done() - masterLimitChan := make(chan bool, 1) for fid := range fileIdLineChan { if len(fid) == 0 { continue @@ -252,31 +248,21 @@ func readFiles(fileIdLineChan chan string, s *stat) { parts := strings.SplitN(fid, ",", 2) vid := parts[0] start := time.Now() - if server, ok := b.vid2server[vid]; !ok { - masterLimitChan <- true - if _, now_ok := b.vid2server[vid]; !now_ok { - if ret, err := operation.Lookup(*b.server, vid); err == nil { - if len(ret.Locations) > 0 { - server = ret.Locations[0].Url - b.vid2server[vid] = server - } - } - } - <-masterLimitChan - } - if server, ok := b.vid2server[vid]; ok { - url := "http://" + server + "/" + fid - if bytesRead, err := util.Get(url); err == nil { - s.completed++ - s.transferred += int64(len(bytesRead)) - readStats.addSample(time.Now().Sub(start)) - } else { - s.failed++ - fmt.Printf("Failed to read %s error:%v\n", url, err) - } - } else { + ret, err := operation.Lookup(*b.server, vid) + if err != nil || len(ret.Locations) == 0 { s.failed++ println("!!!! volume id ", vid, " location not found!!!!!") + continue + } + server := ret.Locations[rand.Intn(len(ret.Locations))].Url + url := "http://" + server + "/" + fid + if bytesRead, err := util.Get(url); err == nil { + s.completed++ + s.transferred += int64(len(bytesRead)) + readStats.addSample(time.Now().Sub(start)) + } else { + s.failed++ + fmt.Printf("Failed to read %s error:%v\n", url, err) } } }