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.

294 lines
8.2 KiB

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