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"
|
|
|
|
|
2020-01-30 01:09:55 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"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
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
|
|
|
|
2020-01-30 01:09:55 +08:00
|
|
|
func (fs *FilerSource) Initialize(configuration util.Configuration, prefix string) error {
|
2018-09-17 15:27:56 +08:00
|
|
|
return fs.initialize(
|
2020-01-30 01:09:55 +08:00
|
|
|
configuration.GetString(prefix+"grpcAddress"),
|
|
|
|
configuration.GetString(prefix+"directory"),
|
2018-09-17 15:27:56 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-09-17 16:37:24 +08:00
|
|
|
func (fs *FilerSource) initialize(grpcAddress string, dir string) (err error) {
|
2018-09-17 15:27:56 +08:00
|
|
|
fs.grpcAddress = grpcAddress
|
2018-09-17 16:37:24 +08:00
|
|
|
fs.Dir = dir
|
2020-01-30 01:09:55 +08:00
|
|
|
fs.grpcDialOption = security.LoadClientTLS(util.GetViper(), "grpc.client")
|
2018-09-17 15:27:56 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-16 08:20:24 +08:00
|
|
|
func (fs *FilerSource) LookupFileId(ctx context.Context, part string) (fileUrl string, err error) {
|
2018-09-17 15:27:56 +08:00
|
|
|
|
|
|
|
vid2Locations := make(map[string]*filer_pb.Locations)
|
|
|
|
|
|
|
|
vid := volumeId(part)
|
|
|
|
|
2020-01-27 06:42:11 +08:00
|
|
|
err = fs.withFilerClient(ctx, fs.grpcDialOption, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
|
2018-09-17 15:27:56 +08:00
|
|
|
|
|
|
|
glog.V(4).Infof("read lookup volume id locations: %v", vid)
|
2019-03-16 08:20:24 +08:00
|
|
|
resp, err := client.LookupVolume(ctx, &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 "", 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 "", fmt.Errorf("LookupFileId locate volume id %s: %v", vid, err)
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
|
|
|
|
2018-10-04 14:36:52 +08:00
|
|
|
fileUrl = fmt.Sprintf("http://%s/%s", locations.Locations[0].Url, part)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-16 08:20:24 +08:00
|
|
|
func (fs *FilerSource) ReadPart(ctx context.Context, part string) (filename string, header http.Header, readCloser io.ReadCloser, err error) {
|
2018-10-04 14:36:52 +08:00
|
|
|
|
2019-03-16 08:20:24 +08:00
|
|
|
fileUrl, err := fs.LookupFileId(ctx, part)
|
2018-10-04 14:36:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, nil, err
|
|
|
|
}
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-09-21 16:54:29 +08:00
|
|
|
filename, header, readCloser, err = util.DownloadFile(fileUrl)
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2018-09-21 16:54:29 +08:00
|
|
|
return filename, header, readCloser, err
|
2018-09-17 15:27:56 +08:00
|
|
|
}
|
|
|
|
|
2020-01-27 06:42:11 +08:00
|
|
|
func (fs *FilerSource) withFilerClient(ctx context.Context, grpcDialOption grpc.DialOption, fn func(context.Context, filer_pb.SeaweedFilerClient) error) error {
|
2018-09-17 15:27:56 +08:00
|
|
|
|
2020-01-27 06:42:11 +08:00
|
|
|
return util.WithCachedGrpcClient(ctx, func(ctx2 context.Context, grpcConnection *grpc.ClientConn) error {
|
2019-04-06 11:31:58 +08:00
|
|
|
client := filer_pb.NewSeaweedFilerClient(grpcConnection)
|
2020-01-27 06:42:11 +08:00
|
|
|
return fn(ctx2, client)
|
2019-04-06 11:31:58 +08:00
|
|
|
}, fs.grpcAddress, fs.grpcDialOption)
|
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
|
|
|
|
}
|