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.

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