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.

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