From a8624c2e4f94dce96448f6f3b33fb0f71e1f07df Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 7 Oct 2020 22:49:04 -0700 Subject: read from alternative replica related to https://github.com/chrislusf/seaweedfs/issues/1512 --- weed/replication/source/filer_source.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'weed/replication/source/filer_source.go') diff --git a/weed/replication/source/filer_source.go b/weed/replication/source/filer_source.go index 9106ee98b..c3ef8835c 100644 --- a/weed/replication/source/filer_source.go +++ b/weed/replication/source/filer_source.go @@ -41,7 +41,7 @@ func (fs *FilerSource) DoInitialize(grpcAddress string, dir string) (err error) return nil } -func (fs *FilerSource) LookupFileId(part string) (fileUrl string, err error) { +func (fs *FilerSource) LookupFileId(part string) (fileUrls []string, err error) { vid2Locations := make(map[string]*filer_pb.Locations) @@ -64,29 +64,38 @@ func (fs *FilerSource) LookupFileId(part string) (fileUrl string, err error) { if err != nil { glog.V(1).Infof("LookupFileId volume id %s: %v", vid, err) - return "", fmt.Errorf("LookupFileId volume id %s: %v", vid, err) + return nil, fmt.Errorf("LookupFileId volume id %s: %v", vid, err) } locations := vid2Locations[vid] if locations == nil || len(locations.Locations) == 0 { glog.V(1).Infof("LookupFileId locate volume id %s: %v", vid, err) - return "", fmt.Errorf("LookupFileId locate volume id %s: %v", vid, err) + return nil, fmt.Errorf("LookupFileId locate volume id %s: %v", vid, err) } - fileUrl = fmt.Sprintf("http://%s/%s", locations.Locations[0].Url, part) + for _, loc := range locations.Locations { + fileUrls = append(fileUrls, fmt.Sprintf("http://%s/%s", loc.Url, part)) + } return } func (fs *FilerSource) ReadPart(part string) (filename string, header http.Header, resp *http.Response, err error) { - fileUrl, err := fs.LookupFileId(part) + fileUrls, err := fs.LookupFileId(part) if err != nil { return "", nil, nil, err } - filename, header, resp, err = util.DownloadFile(fileUrl) + for _, fileUrl := range fileUrls { + filename, header, resp, err = util.DownloadFile(fileUrl) + if err != nil { + glog.V(1).Infof("fail to read from %s: %v", fileUrl, err) + } else { + break + } + } return filename, header, resp, err } -- cgit v1.2.3