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.

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