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"
|
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"
|
2018-06-12 06:13:33 +00:00
|
|
|
"strconv"
|
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
|
|
|
|
2018-05-26 10:49:46 +00:00
|
|
|
entry, err := fs.filer.FindEntry(filer2.FullPath(filepath.Join(req.Directory, req.Name)))
|
2018-05-08 08:59:43 +00:00
|
|
|
if err != nil {
|
2018-05-26 10:49:46 +00:00
|
|
|
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(),
|
2018-06-10 23:57:32 +00:00
|
|
|
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-05-14 06:56:16 +00:00
|
|
|
entries, err := fs.filer.ListDirectoryEntries(filer2.FullPath(req.Directory), "", false, 1000)
|
2018-05-08 08:59:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-10 06:18:02 +00:00
|
|
|
resp := &filer_pb.ListEntriesResponse{}
|
2018-05-14 06:56:16 +00:00
|
|
|
for _, entry := range entries {
|
2018-05-14 09:02:17 +00:00
|
|
|
|
2018-05-10 06:18:02 +00:00
|
|
|
resp.Entries = append(resp.Entries, &filer_pb.Entry{
|
2018-05-14 06:56:16 +00:00
|
|
|
Name: entry.Name(),
|
|
|
|
IsDirectory: entry.IsDirectory(),
|
2018-05-16 07:08:44 +00:00
|
|
|
Chunks: entry.Chunks,
|
2018-06-10 23:57:32 +00:00
|
|
|
Attributes: filer2.EntryAttributeToPb(entry),
|
2018-05-08 08:59:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-05-21 08:25:30 +00:00
|
|
|
func (fs *FilerServer) GetEntryAttributes(ctx context.Context, req *filer_pb.GetEntryAttributesRequest) (*filer_pb.GetEntryAttributesResponse, error) {
|
2018-05-08 08:59:43 +00:00
|
|
|
|
2018-05-21 08:25:30 +00:00
|
|
|
fullpath := filer2.NewFullPath(req.ParentDir, req.Name)
|
|
|
|
|
2018-05-26 10:49:46 +00:00
|
|
|
entry, err := fs.filer.FindEntry(fullpath)
|
2018-05-08 08:59:43 +00:00
|
|
|
if err != nil {
|
2018-05-26 10:49:46 +00:00
|
|
|
return nil, fmt.Errorf("FindEntry %s: %v", fullpath, err)
|
2018-05-08 08:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:57:32 +00:00
|
|
|
attributes := filer2.EntryAttributeToPb(entry)
|
2018-05-22 11:31:44 +00:00
|
|
|
|
2018-05-23 10:08:46 +00:00
|
|
|
glog.V(3).Infof("GetEntryAttributes %v size %d chunks %d: %+v", fullpath, attributes.FileSize, len(entry.Chunks), attributes)
|
2018-05-21 08:25:30 +00:00
|
|
|
|
|
|
|
return &filer_pb.GetEntryAttributesResponse{
|
2018-05-08 08:59:43 +00:00
|
|
|
Attributes: attributes,
|
2018-05-22 11:31:44 +00:00
|
|
|
Chunks: entry.Chunks,
|
2018-05-08 08:59:43 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-05-24 08:22:37 +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
|
|
|
|
2018-06-01 07:39:39 +00:00
|
|
|
lookupResult, err := operation.LookupVolumeIds(fs.filer.GetMaster(), req.VolumeIds)
|
2018-05-08 08:59:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-24 08:22:37 +00:00
|
|
|
|
|
|
|
resp := &filer_pb.LookupVolumeResponse{
|
|
|
|
LocationsMap: make(map[string]*filer_pb.Locations),
|
2018-05-08 08:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 08:22:37 +00:00
|
|
|
for vid, locations := range lookupResult {
|
|
|
|
var locs []*filer_pb.Location
|
|
|
|
for _, loc := range locations.Locations {
|
|
|
|
locs = append(locs, &filer_pb.Location{
|
|
|
|
Url: loc.Url,
|
|
|
|
PublicUrl: loc.PublicUrl,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
resp.LocationsMap[vid] = &filer_pb.Locations{
|
|
|
|
Locations: locs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
2018-05-08 08:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 07:08:44 +00:00
|
|
|
func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (resp *filer_pb.CreateEntryResponse, err error) {
|
|
|
|
err = fs.filer.CreateEntry(&filer2.Entry{
|
|
|
|
FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
|
2018-06-10 23:57:32 +00:00
|
|
|
Attr: filer2.PbToEntryAttribute(req.Entry.Attributes),
|
|
|
|
Chunks: req.Entry.Chunks,
|
2018-05-16 07:08:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
}
|
|
|
|
|
|
|
|
return &filer_pb.CreateEntryResponse{}, err
|
|
|
|
}
|
|
|
|
|
2018-05-22 10:26:38 +00:00
|
|
|
func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
|
2018-05-21 07:00:28 +00:00
|
|
|
|
|
|
|
fullpath := filepath.Join(req.Directory, req.Entry.Name)
|
2018-05-26 10:49:46 +00:00
|
|
|
entry, err := fs.filer.FindEntry(filer2.FullPath(fullpath))
|
2018-05-21 07:00:28 +00:00
|
|
|
if err != nil {
|
2018-05-26 10:49:46 +00:00
|
|
|
return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("not found %s: %v", fullpath, err)
|
2018-05-21 07:00:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 10:26:38 +00:00
|
|
|
// remove old chunks if not included in the new ones
|
|
|
|
unusedChunks := filer2.FindUnusedFileChunks(entry.Chunks, req.Entry.Chunks)
|
2018-05-21 07:00:28 +00:00
|
|
|
|
2018-05-22 10:26:38 +00:00
|
|
|
chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
|
2018-05-21 07:00:28 +00:00
|
|
|
|
2018-05-23 10:08:46 +00:00
|
|
|
newEntry := &filer2.Entry{
|
2018-05-22 10:26:38 +00:00
|
|
|
FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
|
2018-05-23 10:08:46 +00:00
|
|
|
Attr: entry.Attr,
|
|
|
|
Chunks: chunks,
|
|
|
|
}
|
2018-05-16 07:08:44 +00:00
|
|
|
|
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
|
2018-05-31 03:24:57 +00:00
|
|
|
newEntry.Attr.Mime = req.Entry.Attributes.Mime
|
2018-05-23 10:08:46 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = fs.filer.UpdateEntry(newEntry); err == nil {
|
2018-05-22 10:26:38 +00:00
|
|
|
for _, garbage := range unusedChunks {
|
2018-05-21 07:00:28 +00:00
|
|
|
glog.V(0).Infof("deleting %s old chunk: %v, [%d, %d)", fullpath, garbage.FileId, garbage.Offset, garbage.Offset+int64(garbage.Size))
|
2018-06-01 07:39:39 +00:00
|
|
|
operation.DeleteFile(fs.filer.GetMaster(), garbage.FileId, fs.jwt(garbage.FileId))
|
2018-05-21 07:00:28 +00:00
|
|
|
}
|
2018-05-22 10:26:38 +00:00
|
|
|
for _, garbage := range garbages {
|
|
|
|
glog.V(0).Infof("deleting %s garbage chunk: %v, [%d, %d)", fullpath, garbage.FileId, garbage.Offset, garbage.Offset+int64(garbage.Size))
|
2018-06-01 07:39:39 +00:00
|
|
|
operation.DeleteFile(fs.filer.GetMaster(), garbage.FileId, fs.jwt(garbage.FileId))
|
2018-05-22 10:26:38 +00:00
|
|
|
}
|
2018-05-21 07:00:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 10:26:38 +00:00
|
|
|
return &filer_pb.UpdateEntryResponse{}, err
|
2018-05-16 07:08:44 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2018-06-07 05:11:01 +00:00
|
|
|
err = fs.filer.DeleteEntryMetaAndData(filer2.FullPath(filepath.Join(req.Directory, req.Name)), req.IsDeleteData)
|
2018-05-14 07:17:22 +00:00
|
|
|
return &filer_pb.DeleteEntryResponse{}, err
|
2018-05-08 08:59:43 +00:00
|
|
|
}
|
2018-05-16 07:08:44 +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-06-01 07:39:39 +00:00
|
|
|
assignResult, err := operation.Assign(fs.filer.GetMaster(), &operation.VolumeAssignRequest{
|
2018-05-16 07:08:44 +00:00
|
|
|
Count: uint64(req.Count),
|
|
|
|
Replication: req.Replication,
|
|
|
|
Collection: req.Collection,
|
2018-06-12 06:13:33 +00:00
|
|
|
Ttl: ttlStr,
|
2018-05-16 07:08:44 +00:00
|
|
|
})
|
|
|
|
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,
|
|
|
|
}, err
|
|
|
|
}
|