seaweedfs/weed/server/filer_grpc_server.go

375 lines
11 KiB
Go
Raw Normal View History

2018-05-08 08:59:43 +00:00
package weed_server
import (
"context"
2018-05-27 18:52:26 +00:00
"fmt"
2018-05-27 06:59:56 +00:00
"os"
"path/filepath"
"strconv"
2018-05-27 18:52:26 +00:00
"time"
2018-05-27 06:59:56 +00:00
"github.com/seaweedfs/seaweedfs/weed/cluster"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/operation"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
"github.com/seaweedfs/seaweedfs/weed/util"
2018-05-08 08:59:43 +00:00
)
2018-05-10 06:18:02 +00:00
func (fs *FilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
2018-05-08 08:59:43 +00:00
glog.V(4).Infof("LookupDirectoryEntry %s", filepath.Join(req.Directory, req.Name))
2020-04-05 20:11:43 +00:00
entry, err := fs.filer.FindEntry(ctx, util.JoinPath(req.Directory, req.Name))
2020-03-08 01:01:39 +00:00
if err == filer_pb.ErrNotFound {
return &filer_pb.LookupDirectoryEntryResponse{}, err
2020-01-25 02:07:34 +00:00
}
2018-05-08 08:59:43 +00:00
if err != nil {
2020-01-25 01:26:03 +00:00
glog.V(3).Infof("LookupDirectoryEntry %s: %+v, ", filepath.Join(req.Directory, req.Name), err)
return nil, err
2018-05-08 08:59:43 +00:00
}
2018-05-10 06:18:02 +00:00
return &filer_pb.LookupDirectoryEntryResponse{
Entry: entry.ToProtoEntry(),
2018-05-08 08:59:43 +00:00
}, nil
}
2021-01-16 07:56:24 +00:00
func (fs *FilerServer) ListEntries(req *filer_pb.ListEntriesRequest, stream filer_pb.SeaweedFiler_ListEntriesServer) (err error) {
2018-05-08 08:59:43 +00:00
glog.V(4).Infof("ListEntries %v", req)
2018-07-22 08:14:36 +00:00
limit := int(req.Limit)
if limit == 0 {
limit = fs.option.DirListingLimit
2018-05-08 08:59:43 +00:00
}
2020-09-01 07:21:19 +00:00
paginationLimit := filer.PaginationSize
if limit < paginationLimit {
paginationLimit = limit
}
2018-07-22 08:14:36 +00:00
lastFileName := req.StartFromFileName
includeLastFile := req.InclusiveStartFrom
2021-01-16 07:56:24 +00:00
var listErr error
2018-07-22 08:14:36 +00:00
for limit > 0 {
2021-01-16 07:56:24 +00:00
var hasEntries bool
lastFileName, listErr = fs.filer.StreamListDirectoryEntries(stream.Context(), util.FullPath(req.Directory), lastFileName, includeLastFile, int64(paginationLimit), req.Prefix, "", "", func(entry *filer.Entry) bool {
2021-01-16 07:56:24 +00:00
hasEntries = true
if err = stream.Send(&filer_pb.ListEntriesResponse{
Entry: entry.ToProtoEntry(),
2019-12-13 08:22:37 +00:00
}); err != nil {
2021-01-16 07:56:24 +00:00
return false
2019-12-13 08:22:37 +00:00
}
2018-07-22 08:14:36 +00:00
limit--
if limit == 0 {
2021-01-16 07:56:24 +00:00
return false
}
2021-01-16 07:56:24 +00:00
return true
})
2021-01-16 07:56:24 +00:00
if listErr != nil {
return listErr
}
if err != nil {
return err
}
2021-01-16 07:56:24 +00:00
if !hasEntries {
return nil
}
2021-01-16 07:56:24 +00:00
includeLastFile = false
2018-05-08 08:59:43 +00:00
}
2019-12-13 08:22:37 +00:00
return nil
2018-05-08 08:59:43 +00:00
}
func (fs *FilerServer) LookupVolume(ctx context.Context, req *filer_pb.LookupVolumeRequest) (*filer_pb.LookupVolumeResponse, error) {
2018-05-08 08:59:43 +00:00
resp := &filer_pb.LookupVolumeResponse{
LocationsMap: make(map[string]*filer_pb.Locations),
2018-05-08 08:59:43 +00:00
}
for _, vidString := range req.VolumeIds {
vid, err := strconv.Atoi(vidString)
if err != nil {
2018-07-29 09:25:03 +00:00
glog.V(1).Infof("Unknown volume id %d", vid)
return nil, err
}
var locs []*filer_pb.Location
locations, found := fs.filer.MasterClient.GetLocations(uint32(vid))
if !found {
continue
}
for _, loc := range locations {
locs = append(locs, &filer_pb.Location{
Url: loc.Url,
PublicUrl: loc.PublicUrl,
GrpcPort: uint32(loc.GrpcPort),
DataCenter: loc.DataCenter,
})
}
resp.LocationsMap[vidString] = &filer_pb.Locations{
Locations: locs,
}
}
return resp, nil
2018-05-08 08:59:43 +00:00
}
func (fs *FilerServer) lookupFileId(fileId string) (targetUrls []string, err error) {
fid, err := needle.ParseFileIdFromString(fileId)
if err != nil {
return nil, err
}
locations, found := fs.filer.MasterClient.GetLocations(uint32(fid.VolumeId))
if !found || len(locations) == 0 {
return nil, fmt.Errorf("not found volume %d in %s", fid.VolumeId, fileId)
}
for _, loc := range locations {
targetUrls = append(targetUrls, fmt.Sprintf("http://%s/%s", loc.Url, fileId))
}
return
}
func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (resp *filer_pb.CreateEntryResponse, err error) {
2018-09-23 05:11:37 +00:00
2020-10-24 16:42:54 +00:00
glog.V(4).Infof("CreateEntry %v/%v", req.Directory, req.Entry.Name)
resp = &filer_pb.CreateEntryResponse{}
2020-11-16 00:58:48 +00:00
chunks, garbage, err2 := fs.cleanupChunks(util.Join(req.Directory, req.Entry.Name), nil, req.Entry)
if err2 != nil {
return &filer_pb.CreateEntryResponse{}, fmt.Errorf("CreateEntry cleanupChunks %s %s: %v", req.Directory, req.Entry.Name, err2)
}
2018-09-23 05:11:37 +00:00
so, err := fs.detectStorageOption(string(util.NewFullPath(req.Directory, req.Entry.Name)), "", "", 0, "", "", "", "")
if err != nil {
return nil, err
}
newEntry := filer.FromPbEntry(req.Directory, req.Entry)
newEntry.Chunks = chunks
newEntry.TtlSec = so.TtlSeconds
createErr := fs.filer.CreateEntry(ctx, newEntry, req.OExcl, req.IsFromOtherCluster, req.Signatures, req.SkipCheckParentDirectory)
if createErr == nil {
fs.filer.DeleteChunksNotRecursive(garbage)
2020-01-25 01:55:39 +00:00
} else {
glog.V(3).Infof("CreateEntry %s: %v", filepath.Join(req.Directory, req.Entry.Name), createErr)
resp.Error = createErr.Error()
}
return
}
func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
glog.V(4).Infof("UpdateEntry %v", req)
2020-04-05 20:11:43 +00:00
fullpath := util.Join(req.Directory, req.Entry.Name)
2020-03-23 07:01:34 +00:00
entry, err := fs.filer.FindEntry(ctx, util.FullPath(fullpath))
if err != nil {
return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("not found %s: %v", fullpath, err)
}
2020-11-16 00:58:48 +00:00
chunks, garbage, err2 := fs.cleanupChunks(fullpath, entry, req.Entry)
if err2 != nil {
return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("UpdateEntry cleanupChunks %s: %v", fullpath, err2)
}
newEntry := filer.FromPbEntry(req.Directory, req.Entry)
newEntry.Chunks = chunks
2018-05-23 10:08:46 +00:00
2020-09-01 07:21:19 +00:00
if filer.EqualEntry(entry, newEntry) {
2018-08-13 08:20:49 +00:00
return &filer_pb.UpdateEntryResponse{}, err
}
2019-03-15 22:55:34 +00:00
if err = fs.filer.UpdateEntry(ctx, entry, newEntry); err == nil {
fs.filer.DeleteChunksNotRecursive(garbage)
2020-09-24 10:06:44 +00:00
fs.filer.NotifyUpdateEvent(ctx, entry, newEntry, true, req.IsFromOtherCluster, req.Signatures)
2020-01-25 01:55:39 +00:00
} else {
glog.V(3).Infof("UpdateEntry %s: %v", filepath.Join(req.Directory, req.Entry.Name), err)
}
return &filer_pb.UpdateEntryResponse{}, err
}
2020-11-16 00:58:48 +00:00
func (fs *FilerServer) cleanupChunks(fullpath string, existingEntry *filer.Entry, newEntry *filer_pb.Entry) (chunks, garbage []*filer_pb.FileChunk, err error) {
// remove old chunks if not included in the new ones
if existingEntry != nil {
garbage, err = filer.MinusChunks(fs.lookupFileId, existingEntry.GetChunks(), newEntry.GetChunks())
if err != nil {
return newEntry.GetChunks(), nil, fmt.Errorf("MinusChunks: %v", err)
}
}
// files with manifest chunks are usually large and append only, skip calculating covered chunks
manifestChunks, nonManifestChunks := filer.SeparateManifestChunks(newEntry.GetChunks())
2020-09-01 07:21:19 +00:00
chunks, coveredChunks := filer.CompactFileChunks(fs.lookupFileId, nonManifestChunks)
garbage = append(garbage, coveredChunks...)
2020-09-24 10:06:44 +00:00
if newEntry.Attributes != nil {
so, _ := fs.detectStorageOption(fullpath,
"",
"",
2020-11-16 00:58:48 +00:00
newEntry.Attributes.TtlSec,
"",
2020-11-16 00:58:48 +00:00
"",
"",
"",
) // ignore readonly error for capacity needed to manifestize
2020-11-15 22:41:56 +00:00
chunks, err = filer.MaybeManifestize(fs.saveAsChunk(so), chunks)
2020-09-24 10:06:44 +00:00
if err != nil {
// not good, but should be ok
glog.V(0).Infof("MaybeManifestize: %v", err)
}
}
chunks = append(chunks, manifestChunks...)
return
}
2020-04-17 09:28:09 +00:00
func (fs *FilerServer) AppendToEntry(ctx context.Context, req *filer_pb.AppendToEntryRequest) (*filer_pb.AppendToEntryResponse, error) {
glog.V(4).Infof("AppendToEntry %v", req)
2020-04-17 09:28:09 +00:00
fullpath := util.NewFullPath(req.Directory, req.EntryName)
2023-06-26 05:07:23 +00:00
Squashed commit of the following: commit 482742514656e9b5a652acf7406740fbc55db13d Author: chrislu <chris.lu@gmail.com> Date: Sat Sep 16 15:05:38 2023 -0700 balancer works commit 3b50139f68d5f59961113cf8fd0b903a7294a6ca Author: chrislu <chris.lu@gmail.com> Date: Fri Sep 15 22:22:32 2023 -0700 comments commit 7f685ce7ba8853775e7c02c5b5c242d7920d62d3 Author: chrislu <chris.lu@gmail.com> Date: Fri Sep 15 22:20:05 2023 -0700 adjust APIs commit 436d99443b399082f75f4ceb7595e8b5ac0a8ba3 Author: chrislu <chris.lu@gmail.com> Date: Thu Sep 14 23:49:05 2023 -0700 receive broker stats commit b771fefa374fe237ff1317bbd03a9297a52191e3 Merge: 0a851ec00 890881037 Author: chrislu <chris.lu@gmail.com> Date: Wed Sep 13 00:03:47 2023 -0700 Merge branch 'master' into sub commit 0a851ec00b455c72b405503f6f1f41728b15962e Author: chrislu <chris.lu@gmail.com> Date: Sun Sep 10 22:01:25 2023 -0700 Create balancer.go commit 39941edc0bae3b9a4a2c3344caf494f7ab80a82a Author: chrislu <chris.lu@gmail.com> Date: Thu Sep 7 23:55:19 2023 -0700 add publisher shutdown commit 875f562779f239a140d1008732b5375c0e511e61 Author: chrislu <chris.lu@gmail.com> Date: Wed Sep 6 23:16:41 2023 -0700 server side send response at least once per second commit 984b6c54cf6b0defaa6e727ab5e36809411fe92c Author: chrislu <chris.lu@gmail.com> Date: Wed Sep 6 23:15:29 2023 -0700 ack interval 128 commit 2492a454997a59ffe00405e365290addc460078a Author: chrislu <chris.lu@gmail.com> Date: Wed Sep 6 22:39:46 2023 -0700 ack interval commit ba67e6ca2998e82eb23abf5c431bdf9a92e966ea Author: chrislu <chris.lu@gmail.com> Date: Mon Sep 4 21:43:50 2023 -0700 api for sub commit 9e4f98569898985ed285d8bb8a39b4ea5f095a98 Author: chrislu <chris.lu@gmail.com> Date: Mon Sep 4 21:43:30 2023 -0700 publish, benchmark commit cb470d44df2fed94ad8fd370b1c281cb126d373b Author: chrislu <chris.lu@gmail.com> Date: Fri Sep 1 00:36:51 2023 -0700 can pub and sub commit 1eb2da46d5d5a52c1012aa19ef31c1c8ed568d9e Author: chrislu <chris.lu@gmail.com> Date: Mon Aug 28 09:02:12 2023 -0700 connect and publish commit 504ae8383ac3a0838d31d04b31623872b5734b31 Author: chrislu <chris.lu@gmail.com> Date: Mon Aug 28 09:01:25 2023 -0700 protoc version commit dbcba75271a4617b5931c4779ca0f7c924369a8f Author: chrislu <chris.lu@gmail.com> Date: Sun Aug 27 18:59:04 2023 -0700 rename to lookup commit c9caf3311995290c36ed369b48fbbab23d6bc7b5 Author: chrislu <chris.lu@gmail.com> Date: Sun Aug 27 18:33:46 2023 -0700 move functions commit 4d6c18d86f07ee35625f207c94539727944a8776 Author: chrislu <chris.lu@gmail.com> Date: Sun Aug 27 17:50:59 2023 -0700 pub sub initial tests commit 4eb8e8624d8280d8aa4a227afc06bcacbfe732a7 Author: chrislu <chris.lu@gmail.com> Date: Sun Aug 27 13:14:39 2023 -0700 rename commit 19904566706e9dba502f1ffd1f6fdf0bf876e99c Author: chrislu <chris.lu@gmail.com> Date: Sun Aug 27 13:13:14 2023 -0700 sub commit 905911853dd5103496e8fc9b47934fa3a48da214 Author: chrislu <chris.lu@gmail.com> Date: Sat Aug 26 13:39:21 2023 -0700 adjust proto
2023-09-16 22:06:16 +00:00
lockClient := cluster.NewLockClient(fs.grpcDialOption, fs.option.Host)
lock := lockClient.NewLock(string(fullpath), string(fs.option.Host))
defer lock.StopLock()
2023-06-26 05:07:23 +00:00
2020-04-17 09:28:09 +00:00
var offset int64 = 0
2020-11-16 00:58:48 +00:00
entry, err := fs.filer.FindEntry(ctx, fullpath)
2020-04-17 09:28:09 +00:00
if err == filer_pb.ErrNotFound {
2020-09-01 07:21:19 +00:00
entry = &filer.Entry{
2020-04-17 09:28:09 +00:00
FullPath: fullpath,
2020-09-01 07:21:19 +00:00
Attr: filer.Attr{
2020-04-17 09:28:09 +00:00
Crtime: time.Now(),
Mtime: time.Now(),
Mode: os.FileMode(0644),
Uid: OS_UID,
Gid: OS_GID,
},
}
} else {
offset = int64(filer.TotalSize(entry.GetChunks()))
2020-04-17 09:28:09 +00:00
}
for _, chunk := range req.Chunks {
chunk.Offset = offset
offset += int64(chunk.Size)
}
entry.Chunks = append(entry.GetChunks(), req.Chunks...)
so, err := fs.detectStorageOption(string(fullpath), "", "", entry.TtlSec, "", "", "", "")
if err != nil {
glog.Warningf("detectStorageOption: %v", err)
return &filer_pb.AppendToEntryResponse{}, err
}
entry.Chunks, err = filer.MaybeManifestize(fs.saveAsChunk(so), entry.GetChunks())
if err != nil {
// not good, but should be ok
glog.V(0).Infof("MaybeManifestize: %v", err)
}
err = fs.filer.CreateEntry(context.Background(), entry, false, false, nil, false)
2020-04-17 09:28:09 +00:00
return &filer_pb.AppendToEntryResponse{}, err
}
2018-05-10 06:18:02 +00:00
func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
glog.V(4).Infof("DeleteEntry %v", req)
2020-09-09 18:21:23 +00:00
err = fs.filer.DeleteEntryMetaAndData(ctx, util.JoinPath(req.Directory, req.Name), req.IsRecursive, req.IgnoreRecursiveError, req.IsDeleteData, req.IsFromOtherCluster, req.Signatures)
2020-02-25 22:38:36 +00:00
resp = &filer_pb.DeleteEntryResponse{}
if err != nil && err != filer_pb.ErrNotFound {
2020-02-25 22:38:36 +00:00
resp.Error = err.Error()
}
return resp, nil
}
func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
if req.DiskType == "" {
req.DiskType = fs.option.DiskType
}
so, err := fs.detectStorageOption(req.Path, req.Collection, req.Replication, req.TtlSec, req.DiskType, req.DataCenter, req.Rack, req.DataNode)
if err != nil {
glog.V(3).Infof("AssignVolume: %v", err)
return &filer_pb.AssignVolumeResponse{Error: fmt.Sprintf("assign volume: %v", err)}, nil
}
2020-11-16 00:58:48 +00:00
assignRequest, altRequest := so.ToAssignRequests(int(req.Count))
assignResult, err := operation.Assign(fs.filer.GetMaster, fs.grpcDialOption, assignRequest, altRequest)
if err != nil {
2020-01-25 02:07:34 +00:00
glog.V(3).Infof("AssignVolume: %v", err)
return &filer_pb.AssignVolumeResponse{Error: fmt.Sprintf("assign volume: %v", err)}, nil
}
if assignResult.Error != "" {
2020-01-25 02:07:34 +00:00
glog.V(3).Infof("AssignVolume error: %v", assignResult.Error)
return &filer_pb.AssignVolumeResponse{Error: fmt.Sprintf("assign volume result: %v", assignResult.Error)}, nil
}
return &filer_pb.AssignVolumeResponse{
FileId: assignResult.Fid,
Count: int32(assignResult.Count),
Location: &filer_pb.Location{
Url: assignResult.Url,
PublicUrl: assignResult.PublicUrl,
GrpcPort: uint32(assignResult.GrpcPort),
},
Auth: string(assignResult.Auth),
2020-11-16 00:58:48 +00:00
Collection: so.Collection,
Replication: so.Replication,
}, nil
}
2020-10-15 17:52:17 +00:00
func (fs *FilerServer) CollectionList(ctx context.Context, req *filer_pb.CollectionListRequest) (resp *filer_pb.CollectionListResponse, err error) {
glog.V(4).Infof("CollectionList %v", req)
resp = &filer_pb.CollectionListResponse{}
err = fs.filer.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
2020-10-15 17:52:17 +00:00
masterResp, err := client.CollectionList(context.Background(), &master_pb.CollectionListRequest{
IncludeNormalVolumes: req.IncludeNormalVolumes,
IncludeEcVolumes: req.IncludeEcVolumes,
})
if err != nil {
return err
}
for _, c := range masterResp.Collections {
resp.Collections = append(resp.Collections, &filer_pb.Collection{Name: c.Name})
}
return nil
})
return
}
func (fs *FilerServer) DeleteCollection(ctx context.Context, req *filer_pb.DeleteCollectionRequest) (resp *filer_pb.DeleteCollectionResponse, err error) {
glog.V(4).Infof("DeleteCollection %v", req)
err = fs.filer.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
_, err := client.CollectionDelete(context.Background(), &master_pb.CollectionDeleteRequest{
2019-03-19 12:19:37 +00:00
Name: req.GetCollection(),
})
return err
})
return &filer_pb.DeleteCollectionResponse{}, err
}