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.

262 lines
7.4 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. if req.Entry.Attributes == nil {
  94. return nil, fmt.Errorf("can not create entry with empty attributes")
  95. }
  96. err = fs.filer.CreateEntry(&filer2.Entry{
  97. FullPath: fullpath,
  98. Attr: filer2.PbToEntryAttribute(req.Entry.Attributes),
  99. Chunks: chunks,
  100. })
  101. if err == nil {
  102. }
  103. return &filer_pb.CreateEntryResponse{}, err
  104. }
  105. func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
  106. fullpath := filepath.Join(req.Directory, req.Entry.Name)
  107. entry, err := fs.filer.FindEntry(filer2.FullPath(fullpath))
  108. if err != nil {
  109. return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("not found %s: %v", fullpath, err)
  110. }
  111. // remove old chunks if not included in the new ones
  112. unusedChunks := filer2.FindUnusedFileChunks(entry.Chunks, req.Entry.Chunks)
  113. chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
  114. newEntry := &filer2.Entry{
  115. FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
  116. Attr: entry.Attr,
  117. Chunks: chunks,
  118. }
  119. glog.V(3).Infof("updating %s: %+v, chunks %d: %v => %+v, chunks %d: %v",
  120. fullpath, entry.Attr, len(entry.Chunks), entry.Chunks,
  121. req.Entry.Attributes, len(req.Entry.Chunks), req.Entry.Chunks)
  122. if req.Entry.Attributes != nil {
  123. if req.Entry.Attributes.Mtime != 0 {
  124. newEntry.Attr.Mtime = time.Unix(req.Entry.Attributes.Mtime, 0)
  125. }
  126. if req.Entry.Attributes.FileMode != 0 {
  127. newEntry.Attr.Mode = os.FileMode(req.Entry.Attributes.FileMode)
  128. }
  129. newEntry.Attr.Uid = req.Entry.Attributes.Uid
  130. newEntry.Attr.Gid = req.Entry.Attributes.Gid
  131. newEntry.Attr.Mime = req.Entry.Attributes.Mime
  132. newEntry.Attr.UserName = req.Entry.Attributes.UserName
  133. newEntry.Attr.GroupNames = req.Entry.Attributes.GroupName
  134. }
  135. if filer2.EqualEntry(entry, newEntry) {
  136. return &filer_pb.UpdateEntryResponse{}, err
  137. }
  138. if err = fs.filer.UpdateEntry(entry, newEntry); err == nil {
  139. fs.filer.DeleteChunks(unusedChunks)
  140. fs.filer.DeleteChunks(garbages)
  141. }
  142. fs.filer.NotifyUpdateEvent(entry, newEntry, true)
  143. return &filer_pb.UpdateEntryResponse{}, err
  144. }
  145. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  146. err = fs.filer.DeleteEntryMetaAndData(filer2.FullPath(filepath.Join(req.Directory, req.Name)), req.IsRecursive, req.IsDeleteData)
  147. return &filer_pb.DeleteEntryResponse{}, err
  148. }
  149. func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
  150. ttlStr := ""
  151. if req.TtlSec > 0 {
  152. ttlStr = strconv.Itoa(int(req.TtlSec))
  153. }
  154. var altRequest *operation.VolumeAssignRequest
  155. dataCenter := req.DataCenter
  156. if dataCenter == "" {
  157. dataCenter = fs.option.DataCenter
  158. }
  159. assignRequest := &operation.VolumeAssignRequest{
  160. Count: uint64(req.Count),
  161. Replication: req.Replication,
  162. Collection: req.Collection,
  163. Ttl: ttlStr,
  164. DataCenter: dataCenter,
  165. }
  166. if dataCenter != "" {
  167. altRequest = &operation.VolumeAssignRequest{
  168. Count: uint64(req.Count),
  169. Replication: req.Replication,
  170. Collection: req.Collection,
  171. Ttl: ttlStr,
  172. DataCenter: "",
  173. }
  174. }
  175. assignResult, err := operation.Assign(fs.filer.GetMaster(), assignRequest, altRequest)
  176. if err != nil {
  177. return nil, fmt.Errorf("assign volume: %v", err)
  178. }
  179. if assignResult.Error != "" {
  180. return nil, fmt.Errorf("assign volume result: %v", assignResult.Error)
  181. }
  182. return &filer_pb.AssignVolumeResponse{
  183. FileId: assignResult.Fid,
  184. Count: int32(assignResult.Count),
  185. Url: assignResult.Url,
  186. PublicUrl: assignResult.PublicUrl,
  187. }, err
  188. }
  189. func (fs *FilerServer) DeleteCollection(ctx context.Context, req *filer_pb.DeleteCollectionRequest) (resp *filer_pb.DeleteCollectionResponse, err error) {
  190. for _, master := range fs.option.Masters {
  191. _, err = util.Get(fmt.Sprintf("http://%s/col/delete?collection=%s", master, req.Collection))
  192. }
  193. return &filer_pb.DeleteCollectionResponse{}, err
  194. }
  195. func (fs *FilerServer) Statistics(ctx context.Context, req *filer_pb.StatisticsRequest) (resp *filer_pb.StatisticsResponse, err error) {
  196. input := &master_pb.StatisticsRequest{
  197. Replication: req.Replication,
  198. Collection: req.Collection,
  199. Ttl: req.Ttl,
  200. }
  201. output, err := operation.Statistics(fs.filer.GetMaster(), input)
  202. if err != nil {
  203. return nil, err
  204. }
  205. return &filer_pb.StatisticsResponse{
  206. TotalSize: output.TotalSize,
  207. UsedSize: output.UsedSize,
  208. FileCount: output.FileCount,
  209. }, nil
  210. }