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.

293 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. Chunks: chunks,
  134. }
  135. glog.V(3).Infof("updating %s: %+v, chunks %d: %v => %+v, chunks %d: %v",
  136. fullpath, entry.Attr, len(entry.Chunks), entry.Chunks,
  137. req.Entry.Attributes, len(req.Entry.Chunks), req.Entry.Chunks)
  138. if req.Entry.Attributes != nil {
  139. if req.Entry.Attributes.Mtime != 0 {
  140. newEntry.Attr.Mtime = time.Unix(req.Entry.Attributes.Mtime, 0)
  141. }
  142. if req.Entry.Attributes.FileMode != 0 {
  143. newEntry.Attr.Mode = os.FileMode(req.Entry.Attributes.FileMode)
  144. }
  145. newEntry.Attr.Uid = req.Entry.Attributes.Uid
  146. newEntry.Attr.Gid = req.Entry.Attributes.Gid
  147. newEntry.Attr.Mime = req.Entry.Attributes.Mime
  148. newEntry.Attr.UserName = req.Entry.Attributes.UserName
  149. newEntry.Attr.GroupNames = req.Entry.Attributes.GroupName
  150. }
  151. if filer2.EqualEntry(entry, newEntry) {
  152. return &filer_pb.UpdateEntryResponse{}, err
  153. }
  154. if err = fs.filer.UpdateEntry(ctx, entry, newEntry); err == nil {
  155. fs.filer.DeleteChunks(unusedChunks)
  156. fs.filer.DeleteChunks(garbages)
  157. }
  158. fs.filer.NotifyUpdateEvent(entry, newEntry, true)
  159. return &filer_pb.UpdateEntryResponse{}, err
  160. }
  161. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  162. err = fs.filer.DeleteEntryMetaAndData(ctx, filer2.FullPath(filepath.ToSlash(filepath.Join(req.Directory, req.Name))), req.IsRecursive, req.IgnoreRecursiveError, req.IsDeleteData)
  163. return &filer_pb.DeleteEntryResponse{}, err
  164. }
  165. func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
  166. ttlStr := ""
  167. if req.TtlSec > 0 {
  168. ttlStr = strconv.Itoa(int(req.TtlSec))
  169. }
  170. var altRequest *operation.VolumeAssignRequest
  171. dataCenter := req.DataCenter
  172. if dataCenter == "" {
  173. dataCenter = fs.option.DataCenter
  174. }
  175. assignRequest := &operation.VolumeAssignRequest{
  176. Count: uint64(req.Count),
  177. Replication: req.Replication,
  178. Collection: req.Collection,
  179. Ttl: ttlStr,
  180. DataCenter: dataCenter,
  181. }
  182. if dataCenter != "" {
  183. altRequest = &operation.VolumeAssignRequest{
  184. Count: uint64(req.Count),
  185. Replication: req.Replication,
  186. Collection: req.Collection,
  187. Ttl: ttlStr,
  188. DataCenter: "",
  189. }
  190. }
  191. assignResult, err := operation.Assign(fs.filer.GetMaster(), fs.grpcDialOption, assignRequest, altRequest)
  192. if err != nil {
  193. return nil, fmt.Errorf("assign volume: %v", err)
  194. }
  195. if assignResult.Error != "" {
  196. return nil, fmt.Errorf("assign volume result: %v", assignResult.Error)
  197. }
  198. return &filer_pb.AssignVolumeResponse{
  199. FileId: assignResult.Fid,
  200. Count: int32(assignResult.Count),
  201. Url: assignResult.Url,
  202. PublicUrl: assignResult.PublicUrl,
  203. Auth: string(assignResult.Auth),
  204. }, err
  205. }
  206. func (fs *FilerServer) DeleteCollection(ctx context.Context, req *filer_pb.DeleteCollectionRequest) (resp *filer_pb.DeleteCollectionResponse, err error) {
  207. err = fs.filer.MasterClient.WithClient(ctx, func(client master_pb.SeaweedClient) error {
  208. _, err := client.CollectionDelete(ctx, &master_pb.CollectionDeleteRequest{
  209. Name: req.GetCollection(),
  210. })
  211. return err
  212. })
  213. return &filer_pb.DeleteCollectionResponse{}, err
  214. }
  215. func (fs *FilerServer) Statistics(ctx context.Context, req *filer_pb.StatisticsRequest) (resp *filer_pb.StatisticsResponse, err error) {
  216. input := &master_pb.StatisticsRequest{
  217. Replication: req.Replication,
  218. Collection: req.Collection,
  219. Ttl: req.Ttl,
  220. }
  221. output, err := operation.Statistics(fs.filer.GetMaster(), fs.grpcDialOption, input)
  222. if err != nil {
  223. return nil, err
  224. }
  225. return &filer_pb.StatisticsResponse{
  226. TotalSize: output.TotalSize,
  227. UsedSize: output.UsedSize,
  228. FileCount: output.FileCount,
  229. }, nil
  230. }
  231. func (fs *FilerServer) GetFilerConfiguration(ctx context.Context, req *filer_pb.GetFilerConfigurationRequest) (resp *filer_pb.GetFilerConfigurationResponse, err error) {
  232. return &filer_pb.GetFilerConfigurationResponse{
  233. Masters: fs.option.Masters,
  234. Collection: fs.option.Collection,
  235. Replication: fs.option.DefaultReplication,
  236. MaxMb: uint32(fs.option.MaxMB),
  237. }, nil
  238. }