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.

278 lines
8.0 KiB

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