seaweedfs/weed/server/filer_grpc_server.go

270 lines
7.7 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"
"strings"
2018-05-27 18:52:26 +00:00
"time"
2018-05-27 06:59:56 +00:00
2018-05-14 06:56:16 +00:00
"github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/chrislusf/seaweedfs/weed/glog"
2018-05-27 18:52:26 +00:00
"github.com/chrislusf/seaweedfs/weed/operation"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
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
2019-03-15 22:55:34 +00:00
entry, err := fs.filer.FindEntry(ctx, filer2.FullPath(filepath.ToSlash(filepath.Join(req.Directory, req.Name))))
2018-05-08 08:59:43 +00:00
if err != nil {
return nil, fmt.Errorf("%s not found under %s: %v", req.Name, req.Directory, err)
2018-05-08 08:59:43 +00:00
}
2018-05-10 06:18:02 +00:00
return &filer_pb.LookupDirectoryEntryResponse{
Entry: &filer_pb.Entry{
2018-05-08 08:59:43 +00:00
Name: req.Name,
2018-05-14 06:56:16 +00:00
IsDirectory: entry.IsDirectory(),
Attributes: filer2.EntryAttributeToPb(entry),
Chunks: entry.Chunks,
2018-05-08 08:59:43 +00:00
},
}, nil
}
2018-05-10 06:18:02 +00:00
func (fs *FilerServer) ListEntries(ctx context.Context, req *filer_pb.ListEntriesRequest) (*filer_pb.ListEntriesResponse, error) {
2018-05-08 08:59:43 +00:00
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
}
2018-05-10 06:18:02 +00:00
resp := &filer_pb.ListEntriesResponse{}
2018-07-22 08:14:36 +00:00
lastFileName := req.StartFromFileName
includeLastFile := req.InclusiveStartFrom
for limit > 0 {
2019-03-15 22:55:34 +00:00
entries, err := fs.filer.ListDirectoryEntries(ctx, filer2.FullPath(req.Directory), lastFileName, includeLastFile, 1024)
2018-07-22 08:14:36 +00:00
if err != nil {
return nil, err
}
if len(entries) == 0 {
return resp, nil
}
includeLastFile = false
for _, entry := range entries {
lastFileName = entry.Name()
if req.Prefix != "" {
if !strings.HasPrefix(entry.Name(), req.Prefix) {
continue
}
}
resp.Entries = append(resp.Entries, &filer_pb.Entry{
Name: entry.Name(),
IsDirectory: entry.IsDirectory(),
Chunks: entry.Chunks,
Attributes: filer2.EntryAttributeToPb(entry),
})
limit--
}
if len(resp.Entries) < 1024 {
break
}
2018-05-08 08:59:43 +00:00
}
return resp, nil
}
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
for _, loc := range fs.filer.MasterClient.GetLocations(uint32(vid)) {
locs = append(locs, &filer_pb.Location{
Url: loc.Url,
PublicUrl: loc.PublicUrl,
})
}
resp.LocationsMap[vidString] = &filer_pb.Locations{
Locations: locs,
}
}
return resp, nil
2018-05-08 08:59:43 +00:00
}
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
fullpath := filer2.FullPath(filepath.ToSlash(filepath.Join(req.Directory, req.Entry.Name)))
2018-09-23 05:11:37 +00:00
chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
2019-04-16 16:55:37 +00:00
fs.filer.DeleteChunks(fullpath, garbages)
2018-09-23 05:11:37 +00:00
2018-12-03 06:57:59 +00:00
if req.Entry.Attributes == nil {
return nil, fmt.Errorf("can not create entry with empty attributes")
}
2019-03-15 22:55:34 +00:00
err = fs.filer.CreateEntry(ctx, &filer2.Entry{
2018-09-23 05:11:37 +00:00
FullPath: fullpath,
Attr: filer2.PbToEntryAttribute(req.Entry.Attributes),
2018-09-23 05:11:37 +00:00
Chunks: chunks,
})
if err == nil {
}
return &filer_pb.CreateEntryResponse{}, err
}
func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
fullpath := filepath.ToSlash(filepath.Join(req.Directory, req.Entry.Name))
2019-03-15 22:55:34 +00:00
entry, err := fs.filer.FindEntry(ctx, filer2.FullPath(fullpath))
if err != nil {
return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("not found %s: %v", fullpath, err)
}
// remove old chunks if not included in the new ones
unusedChunks := filer2.FindUnusedFileChunks(entry.Chunks, req.Entry.Chunks)
chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
2018-05-23 10:08:46 +00:00
newEntry := &filer2.Entry{
FullPath: filer2.FullPath(filepath.ToSlash(filepath.Join(req.Directory, req.Entry.Name))),
2018-05-23 10:08:46 +00:00
Attr: entry.Attr,
Chunks: chunks,
}
2018-05-23 10:08:46 +00:00
glog.V(3).Infof("updating %s: %+v, chunks %d: %v => %+v, chunks %d: %v",
fullpath, entry.Attr, len(entry.Chunks), entry.Chunks,
req.Entry.Attributes, len(req.Entry.Chunks), req.Entry.Chunks)
if req.Entry.Attributes != nil {
if req.Entry.Attributes.Mtime != 0 {
newEntry.Attr.Mtime = time.Unix(req.Entry.Attributes.Mtime, 0)
}
if req.Entry.Attributes.FileMode != 0 {
newEntry.Attr.Mode = os.FileMode(req.Entry.Attributes.FileMode)
}
newEntry.Attr.Uid = req.Entry.Attributes.Uid
newEntry.Attr.Gid = req.Entry.Attributes.Gid
newEntry.Attr.Mime = req.Entry.Attributes.Mime
newEntry.Attr.UserName = req.Entry.Attributes.UserName
newEntry.Attr.GroupNames = req.Entry.Attributes.GroupName
2018-05-23 10:08:46 +00:00
}
2018-08-13 08:20:49 +00:00
if filer2.EqualEntry(entry, newEntry) {
return &filer_pb.UpdateEntryResponse{}, err
}
2019-03-15 22:55:34 +00:00
if err = fs.filer.UpdateEntry(ctx, entry, newEntry); err == nil {
2019-04-16 16:55:37 +00:00
fs.filer.DeleteChunks(entry.FullPath, unusedChunks)
fs.filer.DeleteChunks(entry.FullPath, garbages)
}
2018-09-17 07:27:56 +00:00
fs.filer.NotifyUpdateEvent(entry, newEntry, true)
2018-08-13 08:20:49 +00:00
return &filer_pb.UpdateEntryResponse{}, 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) {
2019-03-15 22:26:09 +00:00
err = fs.filer.DeleteEntryMetaAndData(ctx, filer2.FullPath(filepath.ToSlash(filepath.Join(req.Directory, req.Name))), req.IsRecursive, req.IsDeleteData)
2018-05-14 07:17:22 +00:00
return &filer_pb.DeleteEntryResponse{}, err
2018-05-08 08:59:43 +00:00
}
func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
2018-06-12 06:13:33 +00:00
ttlStr := ""
if req.TtlSec > 0 {
ttlStr = strconv.Itoa(int(req.TtlSec))
}
2018-07-10 07:20:50 +00:00
var altRequest *operation.VolumeAssignRequest
dataCenter := req.DataCenter
2018-07-14 21:09:45 +00:00
if dataCenter == "" {
dataCenter = fs.option.DataCenter
}
assignRequest := &operation.VolumeAssignRequest{
Count: uint64(req.Count),
Replication: req.Replication,
Collection: req.Collection,
2018-06-12 06:13:33 +00:00
Ttl: ttlStr,
DataCenter: dataCenter,
}
if dataCenter != "" {
2018-07-10 07:20:50 +00:00
altRequest = &operation.VolumeAssignRequest{
Count: uint64(req.Count),
Replication: req.Replication,
Collection: req.Collection,
Ttl: ttlStr,
DataCenter: "",
}
}
2019-02-18 20:11:52 +00:00
assignResult, err := operation.Assign(fs.filer.GetMaster(), fs.grpcDialOption, assignRequest, altRequest)
if err != nil {
return nil, fmt.Errorf("assign volume: %v", err)
}
if assignResult.Error != "" {
return nil, fmt.Errorf("assign volume result: %v", assignResult.Error)
}
return &filer_pb.AssignVolumeResponse{
FileId: assignResult.Fid,
Count: int32(assignResult.Count),
Url: assignResult.Url,
PublicUrl: assignResult.PublicUrl,
2019-02-15 08:09:19 +00:00
Auth: string(assignResult.Auth),
}, err
}
func (fs *FilerServer) DeleteCollection(ctx context.Context, req *filer_pb.DeleteCollectionRequest) (resp *filer_pb.DeleteCollectionResponse, err error) {
2019-03-20 06:01:23 +00:00
err = fs.filer.MasterClient.WithClient(ctx, func(client master_pb.SeaweedClient) error {
2019-03-19 12:19:37 +00:00
_, err := client.CollectionDelete(ctx, &master_pb.CollectionDeleteRequest{
Name: req.GetCollection(),
})
return err
})
return &filer_pb.DeleteCollectionResponse{}, err
}
func (fs *FilerServer) Statistics(ctx context.Context, req *filer_pb.StatisticsRequest) (resp *filer_pb.StatisticsResponse, err error) {
input := &master_pb.StatisticsRequest{
Replication: req.Replication,
Collection: req.Collection,
Ttl: req.Ttl,
}
2019-02-18 20:11:52 +00:00
output, err := operation.Statistics(fs.filer.GetMaster(), fs.grpcDialOption, input)
if err != nil {
return nil, err
}
return &filer_pb.StatisticsResponse{
TotalSize: output.TotalSize,
UsedSize: output.UsedSize,
FileCount: output.FileCount,
}, nil
}