seaweedfs/weed/replication/source/filer_source.go

138 lines
3.3 KiB
Go
Raw Normal View History

2018-09-17 15:27:56 +08:00
package source
import (
"context"
2018-09-21 16:56:43 +08:00
"fmt"
"io"
2018-09-21 16:54:29 +08:00
"net/http"
2018-09-21 16:56:43 +08:00
"strings"
"google.golang.org/grpc"
2020-03-04 16:39:47 +08:00
"github.com/chrislusf/seaweedfs/weed/pb"
"github.com/chrislusf/seaweedfs/weed/security"
2018-09-21 16:56:43 +08:00
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/util"
2018-09-17 15:27:56 +08:00
)
type ReplicationSource interface {
ReadPart(part string) io.ReadCloser
}
type FilerSource struct {
2019-02-19 04:11:52 +08:00
grpcAddress string
grpcDialOption grpc.DialOption
Dir string
address string
proxyByFiler bool
2018-09-17 15:27:56 +08:00
}
func (fs *FilerSource) Initialize(configuration util.Configuration, prefix string) error {
2020-09-10 02:21:23 +08:00
return fs.DoInitialize(
"",
configuration.GetString(prefix+"grpcAddress"),
configuration.GetString(prefix+"directory"),
false,
2018-09-17 15:27:56 +08:00
)
}
func (fs *FilerSource) DoInitialize(address, grpcAddress string, dir string, readChunkFromFiler bool) (err error) {
fs.address = address
if fs.address == "" {
fs.address = pb.GrpcAddressToServerAddress(grpcAddress)
}
2018-09-17 15:27:56 +08:00
fs.grpcAddress = grpcAddress
2018-09-17 16:37:24 +08:00
fs.Dir = dir
fs.grpcDialOption = security.LoadClientTLS(util.GetViper(), "grpc.client")
fs.proxyByFiler = readChunkFromFiler
2018-09-17 15:27:56 +08:00
return nil
}
func (fs *FilerSource) LookupFileId(part string) (fileUrls []string, err error) {
2018-09-17 15:27:56 +08:00
vid2Locations := make(map[string]*filer_pb.Locations)
vid := volumeId(part)
2020-04-30 04:26:02 +08:00
err = fs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
2018-09-17 15:27:56 +08:00
glog.V(4).Infof("read lookup volume id locations: %v", vid)
resp, err := client.LookupVolume(context.Background(), &filer_pb.LookupVolumeRequest{
2018-09-17 15:27:56 +08:00
VolumeIds: []string{vid},
})
if err != nil {
return err
}
vid2Locations = resp.LocationsMap
return nil
})
if err != nil {
2018-10-04 14:36:52 +08:00
glog.V(1).Infof("LookupFileId volume id %s: %v", vid, err)
return nil, fmt.Errorf("LookupFileId volume id %s: %v", vid, err)
2018-09-17 15:27:56 +08:00
}
locations := vid2Locations[vid]
if locations == nil || len(locations.Locations) == 0 {
2018-10-04 14:36:52 +08:00
glog.V(1).Infof("LookupFileId locate volume id %s: %v", vid, err)
return nil, fmt.Errorf("LookupFileId locate volume id %s: %v", vid, err)
2018-09-17 15:27:56 +08:00
}
for _, loc := range locations.Locations {
fileUrls = append(fileUrls, fmt.Sprintf("http://%s/%s", loc.Url, part))
}
2018-10-04 14:36:52 +08:00
return
}
func (fs *FilerSource) ReadPart(fileId string) (filename string, header http.Header, resp *http.Response, err error) {
if fs.proxyByFiler {
return util.DownloadFile("http://" + fs.address + "/?proxyChunkId=" + fileId)
}
2018-10-04 14:36:52 +08:00
fileUrls, err := fs.LookupFileId(fileId)
2018-10-04 14:36:52 +08:00
if err != nil {
return "", nil, nil, err
}
2018-09-17 15:27:56 +08:00
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
}
}
2018-09-17 15:27:56 +08:00
2020-09-09 18:53:09 +08:00
return filename, header, resp, err
2018-09-17 15:27:56 +08:00
}
2020-04-30 04:26:02 +08:00
var _ = filer_pb.FilerClient(&FilerSource{})
func (fs *FilerSource) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
2018-09-17 15:27:56 +08:00
2020-03-04 16:39:47 +08:00
return pb.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
2019-04-06 11:31:58 +08:00
client := filer_pb.NewSeaweedFilerClient(grpcConnection)
return fn(client)
2019-04-06 11:31:58 +08:00
}, fs.grpcAddress, fs.grpcDialOption)
2018-09-17 15:27:56 +08:00
}
2021-01-29 06:36:29 +08:00
func (fs *FilerSource) AdjustedUrl(location *filer_pb.Location) string {
return location.Url
}
2018-09-17 15:27:56 +08:00
func volumeId(fileId string) string {
lastCommaIndex := strings.LastIndex(fileId, ",")
if lastCommaIndex > 0 {
return fileId[:lastCommaIndex]
}
return fileId
}