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.

223 lines
6.9 KiB

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