seaweedfs/weed/replication/source/filer_source.go

107 lines
2.4 KiB
Go
Raw Normal View History

2018-09-17 07:27:56 +00:00
package source
import (
"context"
2018-09-21 08:56:43 +00:00
"fmt"
"io"
2018-09-21 08:54:29 +00:00
"net/http"
2018-09-21 08:56:43 +00:00
"strings"
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/util"
2018-09-17 07:27:56 +00:00
)
type ReplicationSource interface {
ReadPart(part string) io.ReadCloser
}
type FilerSource struct {
grpcAddress string
2018-09-17 08:37:24 +00:00
Dir string
2018-09-17 07:27:56 +00:00
}
func (fs *FilerSource) Initialize(configuration util.Configuration) error {
return fs.initialize(
configuration.GetString("grpcAddress"),
configuration.GetString("directory"),
)
}
2018-09-17 08:37:24 +00:00
func (fs *FilerSource) initialize(grpcAddress string, dir string) (err error) {
2018-09-17 07:27:56 +00:00
fs.grpcAddress = grpcAddress
2018-09-17 08:37:24 +00:00
fs.Dir = dir
2018-09-17 07:27:56 +00:00
return nil
}
2018-10-04 06:36:52 +00:00
func (fs *FilerSource) LookupFileId(part string) (fileUrl string, err error) {
2018-09-17 07:27:56 +00:00
vid2Locations := make(map[string]*filer_pb.Locations)
vid := volumeId(part)
err = fs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
glog.V(4).Infof("read lookup volume id locations: %v", vid)
resp, err := client.LookupVolume(context.Background(), &filer_pb.LookupVolumeRequest{
VolumeIds: []string{vid},
})
if err != nil {
return err
}
vid2Locations = resp.LocationsMap
return nil
})
if err != nil {
2018-10-04 06:36:52 +00:00
glog.V(1).Infof("LookupFileId volume id %s: %v", vid, err)
return "", fmt.Errorf("LookupFileId volume id %s: %v", vid, err)
2018-09-17 07:27:56 +00:00
}
locations := vid2Locations[vid]
if locations == nil || len(locations.Locations) == 0 {
2018-10-04 06:36:52 +00:00
glog.V(1).Infof("LookupFileId locate volume id %s: %v", vid, err)
return "", fmt.Errorf("LookupFileId locate volume id %s: %v", vid, err)
2018-09-17 07:27:56 +00:00
}
2018-10-04 06:36:52 +00:00
fileUrl = fmt.Sprintf("http://%s/%s", locations.Locations[0].Url, part)
return
}
func (fs *FilerSource) ReadPart(part string) (filename string, header http.Header, readCloser io.ReadCloser, err error) {
fileUrl, err := fs.LookupFileId(part)
if err != nil {
return "", nil, nil, err
}
2018-09-17 07:27:56 +00:00
2018-09-21 08:54:29 +00:00
filename, header, readCloser, err = util.DownloadFile(fileUrl)
2018-09-17 07:27:56 +00:00
2018-09-21 08:54:29 +00:00
return filename, header, readCloser, err
2018-09-17 07:27:56 +00:00
}
func (fs *FilerSource) withFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
grpcConnection, err := util.GrpcDial(fs.grpcAddress)
if err != nil {
return fmt.Errorf("fail to dial %s: %v", fs.grpcAddress, err)
}
defer grpcConnection.Close()
client := filer_pb.NewSeaweedFilerClient(grpcConnection)
return fn(client)
}
func volumeId(fileId string) string {
lastCommaIndex := strings.LastIndex(fileId, ",")
if lastCommaIndex > 0 {
return fileId[:lastCommaIndex]
}
return fileId
}