You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
6.7 KiB

7 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/operation"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "fmt"
  7. "github.com/chrislusf/seaweedfs/weed/filer2"
  8. "path/filepath"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "time"
  11. "os"
  12. )
  13. func (fs *FilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
  14. found, entry, err := fs.filer.FindEntry(filer2.FullPath(filepath.Join(req.Directory, req.Name)))
  15. if err != nil {
  16. return nil, err
  17. }
  18. if !found {
  19. return nil, fmt.Errorf("%s not found under %s", req.Name, req.Directory)
  20. }
  21. return &filer_pb.LookupDirectoryEntryResponse{
  22. Entry: &filer_pb.Entry{
  23. Name: req.Name,
  24. IsDirectory: entry.IsDirectory(),
  25. Chunks: entry.Chunks,
  26. },
  27. }, nil
  28. }
  29. func (fs *FilerServer) ListEntries(ctx context.Context, req *filer_pb.ListEntriesRequest) (*filer_pb.ListEntriesResponse, error) {
  30. entries, err := fs.filer.ListDirectoryEntries(filer2.FullPath(req.Directory), "", false, 1000)
  31. if err != nil {
  32. return nil, err
  33. }
  34. resp := &filer_pb.ListEntriesResponse{}
  35. for _, entry := range entries {
  36. resp.Entries = append(resp.Entries, &filer_pb.Entry{
  37. Name: entry.Name(),
  38. IsDirectory: entry.IsDirectory(),
  39. Chunks: entry.Chunks,
  40. Attributes: &filer_pb.FuseAttributes{
  41. FileSize: filer2.TotalSize(entry.Chunks),
  42. Mtime: entry.Mtime.Unix(),
  43. Gid: entry.Gid,
  44. Uid: entry.Uid,
  45. FileMode: uint32(entry.Mode),
  46. },
  47. })
  48. }
  49. return resp, nil
  50. }
  51. func (fs *FilerServer) GetEntryAttributes(ctx context.Context, req *filer_pb.GetEntryAttributesRequest) (*filer_pb.GetEntryAttributesResponse, error) {
  52. attributes := &filer_pb.FuseAttributes{}
  53. fullpath := filer2.NewFullPath(req.ParentDir, req.Name)
  54. found, entry, err := fs.filer.FindEntry(fullpath)
  55. if err != nil {
  56. return nil, err
  57. }
  58. if !found {
  59. attributes.FileSize = 0
  60. return nil, fmt.Errorf("file %s not found", fullpath)
  61. }
  62. attributes.FileSize = filer2.TotalSize(entry.Chunks)
  63. attributes.FileMode = uint32(entry.Mode)
  64. attributes.Uid = entry.Uid
  65. attributes.Gid = entry.Gid
  66. attributes.Mtime = entry.Mtime.Unix()
  67. glog.V(3).Infof("GetEntryAttributes %v size %d chunks %d: %+v", fullpath, attributes.FileSize, len(entry.Chunks), attributes)
  68. return &filer_pb.GetEntryAttributesResponse{
  69. Attributes: attributes,
  70. Chunks: entry.Chunks,
  71. }, nil
  72. }
  73. func (fs *FilerServer) LookupVolume(ctx context.Context, req *filer_pb.LookupVolumeRequest) (*filer_pb.LookupVolumeResponse, error) {
  74. lookupResult, err := operation.LookupVolumeIds(fs.getMasterNode(), req.VolumeIds)
  75. if err != nil {
  76. return nil, err
  77. }
  78. resp := &filer_pb.LookupVolumeResponse{
  79. LocationsMap: make(map[string]*filer_pb.Locations),
  80. }
  81. for vid, locations := range lookupResult {
  82. var locs []*filer_pb.Location
  83. for _, loc := range locations.Locations {
  84. locs = append(locs, &filer_pb.Location{
  85. Url: loc.Url,
  86. PublicUrl: loc.PublicUrl,
  87. })
  88. }
  89. resp.LocationsMap[vid] = &filer_pb.Locations{
  90. Locations: locs,
  91. }
  92. }
  93. return resp, nil
  94. }
  95. func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (resp *filer_pb.CreateEntryResponse, err error) {
  96. err = fs.filer.CreateEntry(&filer2.Entry{
  97. FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
  98. Attr: filer2.Attr{
  99. Mtime: time.Unix(req.Entry.Attributes.Mtime, 0),
  100. Crtime: time.Unix(req.Entry.Attributes.Mtime, 0),
  101. Mode: os.FileMode(req.Entry.Attributes.FileMode),
  102. Uid: req.Entry.Attributes.Uid,
  103. Gid: req.Entry.Attributes.Gid,
  104. },
  105. Chunks: req.Entry.Chunks,
  106. })
  107. if err == nil {
  108. }
  109. return &filer_pb.CreateEntryResponse{}, err
  110. }
  111. func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
  112. fullpath := filepath.Join(req.Directory, req.Entry.Name)
  113. found, entry, err := fs.filer.FindEntry(filer2.FullPath(fullpath))
  114. if err != nil {
  115. return &filer_pb.UpdateEntryResponse{}, err
  116. }
  117. if !found {
  118. return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("file not found: %s", fullpath)
  119. }
  120. // remove old chunks if not included in the new ones
  121. unusedChunks := filer2.FindUnusedFileChunks(entry.Chunks, req.Entry.Chunks)
  122. chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
  123. newEntry := &filer2.Entry{
  124. FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
  125. Attr: entry.Attr,
  126. Chunks: chunks,
  127. }
  128. glog.V(3).Infof("updating %s: %+v, chunks %d: %v => %+v, chunks %d: %v",
  129. fullpath, entry.Attr, len(entry.Chunks), entry.Chunks,
  130. req.Entry.Attributes, len(req.Entry.Chunks), req.Entry.Chunks)
  131. if req.Entry.Attributes != nil {
  132. if req.Entry.Attributes.Mtime != 0 {
  133. newEntry.Attr.Mtime = time.Unix(req.Entry.Attributes.Mtime, 0)
  134. }
  135. if req.Entry.Attributes.FileMode != 0 {
  136. newEntry.Attr.Mode = os.FileMode(req.Entry.Attributes.FileMode)
  137. }
  138. newEntry.Attr.Uid = req.Entry.Attributes.Uid
  139. newEntry.Attr.Gid = req.Entry.Attributes.Gid
  140. }
  141. if err = fs.filer.UpdateEntry(newEntry); err == nil {
  142. for _, garbage := range unusedChunks {
  143. glog.V(0).Infof("deleting %s old chunk: %v, [%d, %d)", fullpath, garbage.FileId, garbage.Offset, garbage.Offset+int64(garbage.Size))
  144. operation.DeleteFile(fs.master, garbage.FileId, fs.jwt(garbage.FileId))
  145. }
  146. for _, garbage := range garbages {
  147. glog.V(0).Infof("deleting %s garbage chunk: %v, [%d, %d)", fullpath, garbage.FileId, garbage.Offset, garbage.Offset+int64(garbage.Size))
  148. operation.DeleteFile(fs.master, garbage.FileId, fs.jwt(garbage.FileId))
  149. }
  150. }
  151. return &filer_pb.UpdateEntryResponse{}, err
  152. }
  153. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  154. entry, err := fs.filer.DeleteEntry(filer2.FullPath(filepath.Join(req.Directory, req.Name)))
  155. if err == nil {
  156. for _, chunk := range entry.Chunks {
  157. if err = operation.DeleteFile(fs.getMasterNode(), chunk.FileId, fs.jwt(chunk.FileId)); err != nil {
  158. glog.V(0).Infof("deleting file %s: %v", chunk.FileId, err)
  159. }
  160. }
  161. }
  162. return &filer_pb.DeleteEntryResponse{}, err
  163. }
  164. func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
  165. assignResult, err := operation.Assign(fs.master, &operation.VolumeAssignRequest{
  166. Count: uint64(req.Count),
  167. Replication: req.Replication,
  168. Collection: req.Collection,
  169. })
  170. if err != nil {
  171. return nil, fmt.Errorf("assign volume: %v", err)
  172. }
  173. if assignResult.Error != "" {
  174. return nil, fmt.Errorf("assign volume result: %v", assignResult.Error)
  175. }
  176. return &filer_pb.AssignVolumeResponse{
  177. FileId: assignResult.Fid,
  178. Count: int32(assignResult.Count),
  179. Url: assignResult.Url,
  180. PublicUrl: assignResult.PublicUrl,
  181. }, err
  182. }