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.

256 lines
7.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/filer2"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/operation"
  13. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  14. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. func (fs *FilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
  18. entry, err := fs.filer.FindEntry(filer2.FullPath(filepath.Join(req.Directory, req.Name)))
  19. if err != nil {
  20. return nil, fmt.Errorf("%s not found under %s: %v", req.Name, req.Directory, err)
  21. }
  22. return &filer_pb.LookupDirectoryEntryResponse{
  23. Entry: &filer_pb.Entry{
  24. Name: req.Name,
  25. IsDirectory: entry.IsDirectory(),
  26. Attributes: filer2.EntryAttributeToPb(entry),
  27. Chunks: entry.Chunks,
  28. },
  29. }, nil
  30. }
  31. func (fs *FilerServer) ListEntries(ctx context.Context, req *filer_pb.ListEntriesRequest) (*filer_pb.ListEntriesResponse, error) {
  32. limit := int(req.Limit)
  33. if limit == 0 {
  34. limit = fs.option.DirListingLimit
  35. }
  36. resp := &filer_pb.ListEntriesResponse{}
  37. lastFileName := req.StartFromFileName
  38. includeLastFile := req.InclusiveStartFrom
  39. for limit > 0 {
  40. entries, err := fs.filer.ListDirectoryEntries(filer2.FullPath(req.Directory), lastFileName, includeLastFile, limit)
  41. if err != nil {
  42. return nil, err
  43. }
  44. if len(entries) == 0 {
  45. return resp, nil
  46. }
  47. includeLastFile = false
  48. for _, entry := range entries {
  49. lastFileName = entry.Name()
  50. if req.Prefix != "" {
  51. if !strings.HasPrefix(entry.Name(), req.Prefix) {
  52. continue
  53. }
  54. }
  55. resp.Entries = append(resp.Entries, &filer_pb.Entry{
  56. Name: entry.Name(),
  57. IsDirectory: entry.IsDirectory(),
  58. Chunks: entry.Chunks,
  59. Attributes: filer2.EntryAttributeToPb(entry),
  60. })
  61. limit--
  62. }
  63. }
  64. return resp, nil
  65. }
  66. func (fs *FilerServer) LookupVolume(ctx context.Context, req *filer_pb.LookupVolumeRequest) (*filer_pb.LookupVolumeResponse, error) {
  67. resp := &filer_pb.LookupVolumeResponse{
  68. LocationsMap: make(map[string]*filer_pb.Locations),
  69. }
  70. for _, vidString := range req.VolumeIds {
  71. vid, err := strconv.Atoi(vidString)
  72. if err != nil {
  73. glog.V(1).Infof("Unknown volume id %d", vid)
  74. return nil, err
  75. }
  76. var locs []*filer_pb.Location
  77. for _, loc := range fs.filer.MasterClient.GetLocations(uint32(vid)) {
  78. locs = append(locs, &filer_pb.Location{
  79. Url: loc.Url,
  80. PublicUrl: loc.PublicUrl,
  81. })
  82. }
  83. resp.LocationsMap[vidString] = &filer_pb.Locations{
  84. Locations: locs,
  85. }
  86. }
  87. return resp, nil
  88. }
  89. func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (resp *filer_pb.CreateEntryResponse, err error) {
  90. fullpath := filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name))
  91. chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
  92. fs.filer.DeleteChunks(garbages)
  93. err = fs.filer.CreateEntry(&filer2.Entry{
  94. FullPath: fullpath,
  95. Attr: filer2.PbToEntryAttribute(req.Entry.Attributes),
  96. Chunks: chunks,
  97. })
  98. if err == nil {
  99. }
  100. return &filer_pb.CreateEntryResponse{}, err
  101. }
  102. func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
  103. fullpath := filepath.Join(req.Directory, req.Entry.Name)
  104. entry, err := fs.filer.FindEntry(filer2.FullPath(fullpath))
  105. if err != nil {
  106. return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("not found %s: %v", fullpath, err)
  107. }
  108. // remove old chunks if not included in the new ones
  109. unusedChunks := filer2.FindUnusedFileChunks(entry.Chunks, req.Entry.Chunks)
  110. chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
  111. newEntry := &filer2.Entry{
  112. FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
  113. Attr: entry.Attr,
  114. Chunks: chunks,
  115. }
  116. glog.V(3).Infof("updating %s: %+v, chunks %d: %v => %+v, chunks %d: %v",
  117. fullpath, entry.Attr, len(entry.Chunks), entry.Chunks,
  118. req.Entry.Attributes, len(req.Entry.Chunks), req.Entry.Chunks)
  119. if req.Entry.Attributes != nil {
  120. if req.Entry.Attributes.Mtime != 0 {
  121. newEntry.Attr.Mtime = time.Unix(req.Entry.Attributes.Mtime, 0)
  122. }
  123. if req.Entry.Attributes.FileMode != 0 {
  124. newEntry.Attr.Mode = os.FileMode(req.Entry.Attributes.FileMode)
  125. }
  126. newEntry.Attr.Uid = req.Entry.Attributes.Uid
  127. newEntry.Attr.Gid = req.Entry.Attributes.Gid
  128. newEntry.Attr.Mime = req.Entry.Attributes.Mime
  129. }
  130. if filer2.EqualEntry(entry, newEntry) {
  131. return &filer_pb.UpdateEntryResponse{}, err
  132. }
  133. if err = fs.filer.UpdateEntry(newEntry); err == nil {
  134. fs.filer.DeleteChunks(unusedChunks)
  135. fs.filer.DeleteChunks(garbages)
  136. }
  137. fs.filer.NotifyUpdateEvent(entry, newEntry, true)
  138. return &filer_pb.UpdateEntryResponse{}, err
  139. }
  140. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  141. err = fs.filer.DeleteEntryMetaAndData(filer2.FullPath(filepath.Join(req.Directory, req.Name)), req.IsRecursive, req.IsDeleteData)
  142. return &filer_pb.DeleteEntryResponse{}, err
  143. }
  144. func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
  145. ttlStr := ""
  146. if req.TtlSec > 0 {
  147. ttlStr = strconv.Itoa(int(req.TtlSec))
  148. }
  149. var altRequest *operation.VolumeAssignRequest
  150. dataCenter := req.DataCenter
  151. if dataCenter == "" {
  152. dataCenter = fs.option.DataCenter
  153. }
  154. assignRequest := &operation.VolumeAssignRequest{
  155. Count: uint64(req.Count),
  156. Replication: req.Replication,
  157. Collection: req.Collection,
  158. Ttl: ttlStr,
  159. DataCenter: dataCenter,
  160. }
  161. if dataCenter != "" {
  162. altRequest = &operation.VolumeAssignRequest{
  163. Count: uint64(req.Count),
  164. Replication: req.Replication,
  165. Collection: req.Collection,
  166. Ttl: ttlStr,
  167. DataCenter: "",
  168. }
  169. }
  170. assignResult, err := operation.Assign(fs.filer.GetMaster(), assignRequest, altRequest)
  171. if err != nil {
  172. return nil, fmt.Errorf("assign volume: %v", err)
  173. }
  174. if assignResult.Error != "" {
  175. return nil, fmt.Errorf("assign volume result: %v", assignResult.Error)
  176. }
  177. return &filer_pb.AssignVolumeResponse{
  178. FileId: assignResult.Fid,
  179. Count: int32(assignResult.Count),
  180. Url: assignResult.Url,
  181. PublicUrl: assignResult.PublicUrl,
  182. }, err
  183. }
  184. func (fs *FilerServer) DeleteCollection(ctx context.Context, req *filer_pb.DeleteCollectionRequest) (resp *filer_pb.DeleteCollectionResponse, err error) {
  185. for _, master := range fs.option.Masters {
  186. _, err = util.Get(fmt.Sprintf("http://%s/col/delete?collection=%s", master, req.Collection))
  187. }
  188. return &filer_pb.DeleteCollectionResponse{}, err
  189. }
  190. func (fs *FilerServer) Statistics(ctx context.Context, req *filer_pb.StatisticsRequest) (resp *filer_pb.StatisticsResponse, err error) {
  191. input := &master_pb.StatisticsRequest{
  192. Replication: req.Replication,
  193. Collection: req.Collection,
  194. Ttl: req.Ttl,
  195. }
  196. output, err := operation.Statistics(fs.filer.GetMaster(), input)
  197. if err != nil {
  198. return nil, err
  199. }
  200. return &filer_pb.StatisticsResponse{
  201. TotalSize: output.TotalSize,
  202. UsedSize: output.UsedSize,
  203. FileCount: output.FileCount,
  204. }, nil
  205. }