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.

308 lines
8.9 KiB

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