filer_grpc_server.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. )
  16. func (fs *FilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
  17. entry, err := fs.filer.FindEntry(ctx, filer2.FullPath(filepath.ToSlash(filepath.Join(req.Directory, req.Name))))
  18. if err != nil {
  19. return nil, err
  20. }
  21. return &filer_pb.LookupDirectoryEntryResponse{
  22. Entry: &filer_pb.Entry{
  23. Name: req.Name,
  24. IsDirectory: entry.IsDirectory(),
  25. Attributes: filer2.EntryAttributeToPb(entry),
  26. Chunks: entry.Chunks,
  27. },
  28. }, nil
  29. }
  30. func (fs *FilerServer) ListEntries(ctx context.Context, req *filer_pb.ListEntriesRequest) (*filer_pb.ListEntriesResponse, error) {
  31. limit := int(req.Limit)
  32. if limit == 0 {
  33. limit = fs.option.DirListingLimit
  34. }
  35. paginationLimit := 1024
  36. if limit < paginationLimit {
  37. paginationLimit = limit
  38. }
  39. resp := &filer_pb.ListEntriesResponse{}
  40. lastFileName := req.StartFromFileName
  41. includeLastFile := req.InclusiveStartFrom
  42. for limit > 0 {
  43. entries, err := fs.filer.ListDirectoryEntries(ctx, filer2.FullPath(req.Directory), lastFileName, includeLastFile, paginationLimit)
  44. if err != nil {
  45. return nil, err
  46. }
  47. if len(entries) == 0 {
  48. return resp, nil
  49. }
  50. includeLastFile = false
  51. for _, entry := range entries {
  52. lastFileName = entry.Name()
  53. if req.Prefix != "" {
  54. if !strings.HasPrefix(entry.Name(), req.Prefix) {
  55. continue
  56. }
  57. }
  58. resp.Entries = append(resp.Entries, &filer_pb.Entry{
  59. Name: entry.Name(),
  60. IsDirectory: entry.IsDirectory(),
  61. Chunks: entry.Chunks,
  62. Attributes: filer2.EntryAttributeToPb(entry),
  63. })
  64. limit--
  65. if limit == 0 {
  66. return resp, nil
  67. }
  68. }
  69. if len(entries) < paginationLimit {
  70. break
  71. }
  72. }
  73. return resp, nil
  74. }
  75. func (fs *FilerServer) LookupVolume(ctx context.Context, req *filer_pb.LookupVolumeRequest) (*filer_pb.LookupVolumeResponse, error) {
  76. resp := &filer_pb.LookupVolumeResponse{
  77. LocationsMap: make(map[string]*filer_pb.Locations),
  78. }
  79. for _, vidString := range req.VolumeIds {
  80. vid, err := strconv.Atoi(vidString)
  81. if err != nil {
  82. glog.V(1).Infof("Unknown volume id %d", vid)
  83. return nil, err
  84. }
  85. var locs []*filer_pb.Location
  86. locations, found := fs.filer.MasterClient.GetLocations(uint32(vid))
  87. if !found {
  88. continue
  89. }
  90. for _, loc := range locations {
  91. locs = append(locs, &filer_pb.Location{
  92. Url: loc.Url,
  93. PublicUrl: loc.PublicUrl,
  94. })
  95. }
  96. resp.LocationsMap[vidString] = &filer_pb.Locations{
  97. Locations: locs,
  98. }
  99. }
  100. return resp, nil
  101. }
  102. func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (resp *filer_pb.CreateEntryResponse, err error) {
  103. fullpath := filer2.FullPath(filepath.ToSlash(filepath.Join(req.Directory, req.Entry.Name)))
  104. chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
  105. if req.Entry.Attributes == nil {
  106. return nil, fmt.Errorf("can not create entry with empty attributes")
  107. }
  108. err = fs.filer.CreateEntry(ctx, &filer2.Entry{
  109. FullPath: fullpath,
  110. Attr: filer2.PbToEntryAttribute(req.Entry.Attributes),
  111. Chunks: chunks,
  112. })
  113. if err == nil {
  114. fs.filer.DeleteChunks(fullpath, garbages)
  115. }
  116. return &filer_pb.CreateEntryResponse{}, err
  117. }
  118. func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
  119. fullpath := filepath.ToSlash(filepath.Join(req.Directory, req.Entry.Name))
  120. entry, err := fs.filer.FindEntry(ctx, filer2.FullPath(fullpath))
  121. if err != nil {
  122. return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("not found %s: %v", fullpath, err)
  123. }
  124. // remove old chunks if not included in the new ones
  125. unusedChunks := filer2.MinusChunks(entry.Chunks, req.Entry.Chunks)
  126. chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
  127. newEntry := &filer2.Entry{
  128. FullPath: filer2.FullPath(filepath.ToSlash(filepath.Join(req.Directory, req.Entry.Name))),
  129. Attr: entry.Attr,
  130. Chunks: chunks,
  131. }
  132. glog.V(3).Infof("updating %s: %+v, chunks %d: %v => %+v, chunks %d: %v",
  133. fullpath, entry.Attr, len(entry.Chunks), entry.Chunks,
  134. req.Entry.Attributes, len(req.Entry.Chunks), req.Entry.Chunks)
  135. if req.Entry.Attributes != nil {
  136. if req.Entry.Attributes.Mtime != 0 {
  137. newEntry.Attr.Mtime = time.Unix(req.Entry.Attributes.Mtime, 0)
  138. }
  139. if req.Entry.Attributes.FileMode != 0 {
  140. newEntry.Attr.Mode = os.FileMode(req.Entry.Attributes.FileMode)
  141. }
  142. newEntry.Attr.Uid = req.Entry.Attributes.Uid
  143. newEntry.Attr.Gid = req.Entry.Attributes.Gid
  144. newEntry.Attr.Mime = req.Entry.Attributes.Mime
  145. newEntry.Attr.UserName = req.Entry.Attributes.UserName
  146. newEntry.Attr.GroupNames = req.Entry.Attributes.GroupName
  147. }
  148. if filer2.EqualEntry(entry, newEntry) {
  149. return &filer_pb.UpdateEntryResponse{}, err
  150. }
  151. if err = fs.filer.UpdateEntry(ctx, entry, newEntry); err == nil {
  152. fs.filer.DeleteChunks(entry.FullPath, unusedChunks)
  153. fs.filer.DeleteChunks(entry.FullPath, garbages)
  154. }
  155. fs.filer.NotifyUpdateEvent(entry, newEntry, true)
  156. return &filer_pb.UpdateEntryResponse{}, err
  157. }
  158. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  159. err = fs.filer.DeleteEntryMetaAndData(ctx, filer2.FullPath(filepath.ToSlash(filepath.Join(req.Directory, req.Name))), req.IsRecursive, req.IgnoreRecursiveError, req.IsDeleteData)
  160. return &filer_pb.DeleteEntryResponse{}, err
  161. }
  162. func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
  163. ttlStr := ""
  164. if req.TtlSec > 0 {
  165. ttlStr = strconv.Itoa(int(req.TtlSec))
  166. }
  167. var altRequest *operation.VolumeAssignRequest
  168. dataCenter := req.DataCenter
  169. if dataCenter == "" {
  170. dataCenter = fs.option.DataCenter
  171. }
  172. assignRequest := &operation.VolumeAssignRequest{
  173. Count: uint64(req.Count),
  174. Replication: req.Replication,
  175. Collection: req.Collection,
  176. Ttl: ttlStr,
  177. DataCenter: dataCenter,
  178. }
  179. if dataCenter != "" {
  180. altRequest = &operation.VolumeAssignRequest{
  181. Count: uint64(req.Count),
  182. Replication: req.Replication,
  183. Collection: req.Collection,
  184. Ttl: ttlStr,
  185. DataCenter: "",
  186. }
  187. }
  188. assignResult, err := operation.Assign(fs.filer.GetMaster(), fs.grpcDialOption, assignRequest, altRequest)
  189. if err != nil {
  190. return nil, fmt.Errorf("assign volume: %v", err)
  191. }
  192. if assignResult.Error != "" {
  193. return nil, fmt.Errorf("assign volume result: %v", assignResult.Error)
  194. }
  195. return &filer_pb.AssignVolumeResponse{
  196. FileId: assignResult.Fid,
  197. Count: int32(assignResult.Count),
  198. Url: assignResult.Url,
  199. PublicUrl: assignResult.PublicUrl,
  200. Auth: string(assignResult.Auth),
  201. }, err
  202. }
  203. func (fs *FilerServer) DeleteCollection(ctx context.Context, req *filer_pb.DeleteCollectionRequest) (resp *filer_pb.DeleteCollectionResponse, err error) {
  204. err = fs.filer.MasterClient.WithClient(ctx, func(client master_pb.SeaweedClient) error {
  205. _, err := client.CollectionDelete(ctx, &master_pb.CollectionDeleteRequest{
  206. Name: req.GetCollection(),
  207. })
  208. return err
  209. })
  210. return &filer_pb.DeleteCollectionResponse{}, err
  211. }
  212. func (fs *FilerServer) Statistics(ctx context.Context, req *filer_pb.StatisticsRequest) (resp *filer_pb.StatisticsResponse, err error) {
  213. input := &master_pb.StatisticsRequest{
  214. Replication: req.Replication,
  215. Collection: req.Collection,
  216. Ttl: req.Ttl,
  217. }
  218. output, err := operation.Statistics(fs.filer.GetMaster(), fs.grpcDialOption, input)
  219. if err != nil {
  220. return nil, err
  221. }
  222. return &filer_pb.StatisticsResponse{
  223. TotalSize: output.TotalSize,
  224. UsedSize: output.UsedSize,
  225. FileCount: output.FileCount,
  226. }, nil
  227. }
  228. func (fs *FilerServer) GetFilerConfiguration(ctx context.Context, req *filer_pb.GetFilerConfigurationRequest) (resp *filer_pb.GetFilerConfigurationResponse, err error) {
  229. return &filer_pb.GetFilerConfigurationResponse{
  230. Masters: fs.option.Masters,
  231. Collection: fs.option.Collection,
  232. Replication: fs.option.DefaultReplication,
  233. MaxMb: uint32(fs.option.MaxMB),
  234. }, nil
  235. }