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.

214 lines
6.7 KiB

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