SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.
Go blob-storagecloud-drivedistributed-file-systemdistributed-storageerasure-codingfusehadoop-hdfshdfskuberneteslancelancedbobject-storageposixs3s3-storageseaweedfstiered-file-systemvector-search
add "weed filer.cat" to read files directly from volume servers
Chris Lu2b7685464139
diff --git a/weed/command/command.go b/weed/command/command.goindex 0df22b5756..8f9cec087e 100644--- a/weed/command/command.go+++ b/weed/command/command.go@@ -15,6 +15,7 @@ var Commands = []*Command{cmdDownload,cmdExport,cmdFiler,+ cmdFilerCat,cmdFilerReplicate,cmdFilerSynchronize,cmdFix,diff --git a/weed/command/filer_cat.go b/weed/command/filer_cat.gonew file mode 100644index 0000000000..a46098b041--- /dev/null+++ b/weed/command/filer_cat.go@@ -0,0 +1,118 @@+package command++import (+ "context"+ "fmt"+ "github.com/chrislusf/seaweedfs/weed/filer"+ "github.com/chrislusf/seaweedfs/weed/pb"+ "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"+ "github.com/chrislusf/seaweedfs/weed/wdclient"+ "google.golang.org/grpc"+ "math"+ "net/url"+ "os"+ "strings"++ "github.com/chrislusf/seaweedfs/weed/security"+ "github.com/chrislusf/seaweedfs/weed/util"+)++var (+ filerCat FilerCatOptions+)++type FilerCatOptions struct {+ grpcDialOption grpc.DialOption+ filerAddress string+ filerClient filer_pb.SeaweedFilerClient+ output *string+}++func (fco *FilerCatOptions) GetLookupFileIdFunction() wdclient.LookupFileIdFunctionType {+ return func(fileId string) (targetUrls []string, err error) {+ vid := filer.VolumeId(fileId)+ resp, err := fco.filerClient.LookupVolume(context.Background(), &filer_pb.LookupVolumeRequest{+ VolumeIds: []string{vid},+ })+ if err != nil {+ return nil, err+ }+ locations := resp.LocationsMap[vid]+ for _, loc := range locations.Locations {+ targetUrls = append(targetUrls, fmt.Sprintf("http://%s/%s", loc.Url, fileId))+ }+ return+ }+}++func init() {+ cmdFilerCat.Run = runFilerCat // break init cycle+ filerCat.output = cmdFilerCat.Flag.String("o", "", "write to file instead of stdout")+}++var cmdFilerCat = &Command{+ UsageLine: "filer.cat [-o <file>] http://localhost:8888/path/to/file",+ Short: "copy one file to local",+ Long: `read one file to stdout or write to a file++`,+}++func runFilerCat(cmd *Command, args []string) bool {++ util.LoadConfiguration("security", false)++ if len(args) == 0 {+ return false+ }+ filerSource := args[len(args)-1]++ filerUrl, err := url.Parse(filerSource)+ if err != nil {+ fmt.Printf("The last argument should be a URL on filer: %v\n", err)+ return false+ }+ urlPath := filerUrl.Path+ if strings.HasSuffix(urlPath, "/") {+ fmt.Printf("The last argument should be a file: %v\n", err)+ return false+ }++ filerCat.filerAddress = filerUrl.Host+ filerCat.grpcDialOption = security.LoadClientTLS(util.GetViper(), "grpc.client")++ dir, name := util.FullPath(urlPath).DirAndName()++ writer := os.Stdout+ if *filerCat.output != "" {++ fmt.Printf("saving %s to %s\n", filerSource, *filerCat.output)++ f, err := os.OpenFile(*filerCat.output, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)+ if err != nil {+ fmt.Printf("open file %s: %v\n", *filerCat.output, err)+ return false+ }+ defer f.Close()+ writer = f+ }++ pb.WithFilerClient(filerCat.filerAddress, filerCat.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {++ request := &filer_pb.LookupDirectoryEntryRequest{+ Name: name,+ Directory: dir,+ }+ respLookupEntry, err := filer_pb.LookupEntry(client, request)+ if err != nil {+ return err+ }++ filerCat.filerClient = client++ return filer.StreamContent(&filerCat, writer, respLookupEntry.Entry.Chunks, 0, math.MaxInt64)++ })++ return true+}diff --git a/weed/command/filer_copy.go b/weed/command/filer_copy.goindex 93248f3575..b95df696c5 100644--- a/weed/command/filer_copy.go+++ b/weed/command/filer_copy.go@@ -92,7 +92,7 @@ func runCopy(cmd *Command, args []string) bool {}urlPath := filerUrl.Pathif !strings.HasSuffix(urlPath, "/") {- fmt.Printf("The last argument should be a folder and end with \"/\": %v\n", err)+ fmt.Printf("The last argument should be a folder and end with \"/\"\n")return false}diff --git a/weed/filer/filechunk_manifest.go b/weed/filer/filechunk_manifest.goindex f5ab36d375..845bfaec19 100644--- a/weed/filer/filechunk_manifest.go+++ b/weed/filer/filechunk_manifest.go@@ -3,6 +3,7 @@ package filerimport ("bytes""fmt"+ "github.com/chrislusf/seaweedfs/weed/wdclient""io""math""time"@@ -38,7 +39,7 @@ func SeparateManifestChunks(chunks []*filer_pb.FileChunk) (manifestChunks, nonMareturn}-func ResolveChunkManifest(lookupFileIdFn LookupFileIdFunctionType, chunks []*filer_pb.FileChunk) (dataChunks, manifestChunks []*filer_pb.FileChunk, manifestResolveErr error) {+func ResolveChunkManifest(lookupFileIdFn wdclient.LookupFileIdFunctionType, chunks []*filer_pb.FileChunk) (dataChunks, manifestChunks []*filer_pb.FileChunk, manifestResolveErr error) {// TODO maybe parallel thisfor _, chunk := range chunks {if !chunk.IsChunkManifest {@@ -63,7 +64,7 @@ func ResolveChunkManifest(lookupFileIdFn LookupFileIdFunctionType, chunks []*filreturn}-func ResolveOneChunkManifest(lookupFileIdFn LookupFileIdFunctionType, chunk *filer_pb.FileChunk) (dataChunks []*filer_pb.FileChunk, manifestResolveErr error) {+func ResolveOneChunkManifest(lookupFileIdFn wdclient.LookupFileIdFunctionType, chunk *filer_pb.FileChunk) (dataChunks []*filer_pb.FileChunk, manifestResolveErr error) {if !chunk.IsChunkManifest {return}@@ -84,7 +85,7 @@ func ResolveOneChunkManifest(lookupFileIdFn LookupFileIdFunctionType, chunk *fil}// TODO fetch from cache for weed mount?-func fetchChunk(lookupFileIdFn LookupFileIdFunctionType, fileId string, cipherKey []byte, isGzipped bool) ([]byte, error) {+func fetchChunk(lookupFileIdFn wdclient.LookupFileIdFunctionType, fileId string, cipherKey []byte, isGzipped bool) ([]byte, error) {urlStrings, err := lookupFileIdFn(fileId)if err != nil {glog.Errorf("operation LookupFileId %s failed, err: %v", fileId, err)diff --git a/weed/filer/filechunks.go b/weed/filer/filechunks.goindex c75a35f79c..68f308a515 100644--- a/weed/filer/filechunks.go+++ b/weed/filer/filechunks.go@@ -4,6 +4,7 @@ import ("bytes""encoding/hex""fmt"+ "github.com/chrislusf/seaweedfs/weed/wdclient""math""sort""sync"@@ -52,7 +53,7 @@ func ETagChunks(chunks []*filer_pb.FileChunk) (etag string) {return fmt.Sprintf("%x-%d", util.Md5(bytes.Join(md5_digests, nil)), len(chunks))}-func CompactFileChunks(lookupFileIdFn LookupFileIdFunctionType, chunks []*filer_pb.FileChunk) (compacted, garbage []*filer_pb.FileChunk) {+func CompactFileChunks(lookupFileIdFn wdclient.LookupFileIdFunctionType, chunks []*filer_pb.FileChunk) (compacted, garbage []*filer_pb.FileChunk) {visibles, _ := NonOverlappingVisibleIntervals(lookupFileIdFn, chunks)@@ -71,7 +72,7 @@ func CompactFileChunks(lookupFileIdFn LookupFileIdFunctionType, chunks []*filer_return}-func MinusChunks(lookupFileIdFn LookupFileIdFunctionType, as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk, err error) {+func MinusChunks(lookupFileIdFn wdclient.LookupFileIdFunctionType, as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk, err error) {aData, aMeta, aErr := ResolveChunkManifest(lookupFileIdFn, as)if aErr != nil {@@ -116,7 +117,7 @@ func (cv *ChunkView) IsFullChunk() bool {return cv.Size == cv.ChunkSize}-func ViewFromChunks(lookupFileIdFn LookupFileIdFunctionType, chunks []*filer_pb.FileChunk, offset int64, size int64) (views []*ChunkView) {+func ViewFromChunks(lookupFileIdFn wdclient.LookupFileIdFunctionType, chunks []*filer_pb.FileChunk, offset int64, size int64) (views []*ChunkView) {visibles, _ := NonOverlappingVisibleIntervals(lookupFileIdFn, chunks)@@ -222,7 +223,7 @@ func MergeIntoVisibles(visibles []VisibleInterval, chunk *filer_pb.FileChunk) (n// NonOverlappingVisibleIntervals translates the file chunk into VisibleInterval in memory// If the file chunk content is a chunk manifest-func NonOverlappingVisibleIntervals(lookupFileIdFn LookupFileIdFunctionType, chunks []*filer_pb.FileChunk) (visibles []VisibleInterval, err error) {+func NonOverlappingVisibleIntervals(lookupFileIdFn wdclient.LookupFileIdFunctionType, chunks []*filer_pb.FileChunk) (visibles []VisibleInterval, err error) {chunks, _, err = ResolveChunkManifest(lookupFileIdFn, chunks)diff --git a/weed/filer/reader_at.go b/weed/filer/reader_at.goindex 6193dbd453..1f594a1bf3 100644--- a/weed/filer/reader_at.go+++ b/weed/filer/reader_at.go@@ -18,7 +18,7 @@ import (type ChunkReadAt struct {masterClient *wdclient.MasterClientchunkViews []*ChunkView- lookupFileId LookupFileIdFunctionType+ lookupFileId wdclient.LookupFileIdFunctionTypereaderLock sync.MutexfileSize int64@@ -31,9 +31,7 @@ type ChunkReadAt struct {var _ = io.ReaderAt(&ChunkReadAt{})var _ = io.Closer(&ChunkReadAt{})-type LookupFileIdFunctionType func(fileId string) (targetUrls []string, err error)--func LookupFn(filerClient filer_pb.FilerClient) LookupFileIdFunctionType {+func LookupFn(filerClient filer_pb.FilerClient) wdclient.LookupFileIdFunctionType {vidCache := make(map[string]*filer_pb.Locations)var vicCacheLock sync.RWMutexdiff --git a/weed/filer/stream.go b/weed/filer/stream.goindex cffdc8303b..f0042a0ffd 100644--- a/weed/filer/stream.go+++ b/weed/filer/stream.go@@ -13,16 +13,16 @@ import ("github.com/chrislusf/seaweedfs/weed/wdclient")-func StreamContent(masterClient *wdclient.MasterClient, w io.Writer, chunks []*filer_pb.FileChunk, offset int64, size int64) error {+func StreamContent(masterClient wdclient.HasLookupFileIdFunction, w io.Writer, chunks []*filer_pb.FileChunk, offset int64, size int64) error {// fmt.Printf("start to stream content for chunks: %+v\n", chunks)- chunkViews := ViewFromChunks(masterClient.LookupFileId, chunks, offset, size)+ chunkViews := ViewFromChunks(masterClient.GetLookupFileIdFunction(), chunks, offset, size)fileId2Url := make(map[string][]string)for _, chunkView := range chunkViews {- urlStrings, err := masterClient.LookupFileId(chunkView.FileId)+ urlStrings, err := masterClient.GetLookupFileIdFunction()(chunkView.FileId)if err != nil {glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)return err@@ -86,7 +86,7 @@ type ChunkStreamReader struct {bufferOffset int64bufferPos intchunkIndex int- lookupFileId LookupFileIdFunctionType+ lookupFileId wdclient.LookupFileIdFunctionType}var _ = io.ReadSeeker(&ChunkStreamReader{})diff --git a/weed/replication/sink/filersink/filer_sink.go b/weed/replication/sink/filersink/filer_sink.goindex a58c8f296e..9c0e4176f9 100644--- a/weed/replication/sink/filersink/filer_sink.go+++ b/weed/replication/sink/filersink/filer_sink.go@@ -3,6 +3,7 @@ package filersinkimport ("context""fmt"+ "github.com/chrislusf/seaweedfs/weed/wdclient""google.golang.org/grpc"@@ -206,7 +207,7 @@ func (fs *FilerSink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParent})}-func compareChunks(lookupFileIdFn filer.LookupFileIdFunctionType, oldEntry, newEntry *filer_pb.Entry) (deletedChunks, newChunks []*filer_pb.FileChunk, err error) {+func compareChunks(lookupFileIdFn wdclient.LookupFileIdFunctionType, oldEntry, newEntry *filer_pb.Entry) (deletedChunks, newChunks []*filer_pb.FileChunk, err error) {aData, aMeta, aErr := filer.ResolveChunkManifest(lookupFileIdFn, oldEntry.Chunks)if aErr != nil {return nil, nil, aErrdiff --git a/weed/wdclient/vid_map.go b/weed/wdclient/vid_map.goindex 773da01915..271baa1325 100644--- a/weed/wdclient/vid_map.go+++ b/weed/wdclient/vid_map.go@@ -15,6 +15,12 @@ const (maxCursorIndex = 4096)+type HasLookupFileIdFunction interface {+ GetLookupFileIdFunction() LookupFileIdFunctionType+}++type LookupFileIdFunctionType func(fileId string) (targetUrls []string, err error)+type Location struct {Url string `json:"url,omitempty"`PublicUrl string `json:"publicUrl,omitempty"`@@ -67,6 +73,10 @@ func (vc *vidMap) LookupVolumeServerUrl(vid string) (serverUrls []string, err erreturn}+func (vc *vidMap) GetLookupFileIdFunction() LookupFileIdFunctionType {+ return vc.LookupFileId+}+func (vc *vidMap) LookupFileId(fileId string) (fullUrls []string, err error) {parts := strings.Split(fileId, ",")if len(parts) != 2 {