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.

248 lines
7.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/filer2"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. "strconv"
  14. "strings"
  15. )
  16. func (fs *FilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
  17. entry, err := fs.filer.FindEntry(filer2.FullPath(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(filer2.FullPath(req.Directory), lastFileName, includeLastFile, limit)
  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. }
  63. return resp, nil
  64. }
  65. func (fs *FilerServer) GetEntryAttributes(ctx context.Context, req *filer_pb.GetEntryAttributesRequest) (*filer_pb.GetEntryAttributesResponse, error) {
  66. fullpath := filer2.NewFullPath(req.ParentDir, req.Name)
  67. entry, err := fs.filer.FindEntry(fullpath)
  68. if err != nil {
  69. return nil, fmt.Errorf("FindEntry %s: %v", fullpath, err)
  70. }
  71. attributes := filer2.EntryAttributeToPb(entry)
  72. glog.V(3).Infof("GetEntryAttributes %v size %d chunks %d: %+v", fullpath, attributes.FileSize, len(entry.Chunks), attributes)
  73. return &filer_pb.GetEntryAttributesResponse{
  74. Attributes: attributes,
  75. Chunks: entry.Chunks,
  76. }, nil
  77. }
  78. func (fs *FilerServer) LookupVolume(ctx context.Context, req *filer_pb.LookupVolumeRequest) (*filer_pb.LookupVolumeResponse, error) {
  79. lookupResult, err := operation.LookupVolumeIds(fs.filer.GetMaster(), req.VolumeIds)
  80. if err != nil {
  81. return nil, err
  82. }
  83. resp := &filer_pb.LookupVolumeResponse{
  84. LocationsMap: make(map[string]*filer_pb.Locations),
  85. }
  86. for vid, locations := range lookupResult {
  87. var locs []*filer_pb.Location
  88. for _, loc := range locations.Locations {
  89. locs = append(locs, &filer_pb.Location{
  90. Url: loc.Url,
  91. PublicUrl: loc.PublicUrl,
  92. })
  93. }
  94. resp.LocationsMap[vid] = &filer_pb.Locations{
  95. Locations: locs,
  96. }
  97. }
  98. return resp, nil
  99. }
  100. func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (resp *filer_pb.CreateEntryResponse, err error) {
  101. err = fs.filer.CreateEntry(&filer2.Entry{
  102. FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
  103. Attr: filer2.PbToEntryAttribute(req.Entry.Attributes),
  104. Chunks: req.Entry.Chunks,
  105. })
  106. if err == nil {
  107. }
  108. return &filer_pb.CreateEntryResponse{}, err
  109. }
  110. func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
  111. fullpath := filepath.Join(req.Directory, req.Entry.Name)
  112. entry, err := fs.filer.FindEntry(filer2.FullPath(fullpath))
  113. if err != nil {
  114. return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("not found %s: %v", fullpath, err)
  115. }
  116. // remove old chunks if not included in the new ones
  117. unusedChunks := filer2.FindUnusedFileChunks(entry.Chunks, req.Entry.Chunks)
  118. chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
  119. newEntry := &filer2.Entry{
  120. FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
  121. Attr: entry.Attr,
  122. Chunks: chunks,
  123. }
  124. glog.V(3).Infof("updating %s: %+v, chunks %d: %v => %+v, chunks %d: %v",
  125. fullpath, entry.Attr, len(entry.Chunks), entry.Chunks,
  126. req.Entry.Attributes, len(req.Entry.Chunks), req.Entry.Chunks)
  127. if req.Entry.Attributes != nil {
  128. if req.Entry.Attributes.Mtime != 0 {
  129. newEntry.Attr.Mtime = time.Unix(req.Entry.Attributes.Mtime, 0)
  130. }
  131. if req.Entry.Attributes.FileMode != 0 {
  132. newEntry.Attr.Mode = os.FileMode(req.Entry.Attributes.FileMode)
  133. }
  134. newEntry.Attr.Uid = req.Entry.Attributes.Uid
  135. newEntry.Attr.Gid = req.Entry.Attributes.Gid
  136. newEntry.Attr.Mime = req.Entry.Attributes.Mime
  137. }
  138. if err = fs.filer.UpdateEntry(newEntry); err == nil {
  139. for _, garbage := range unusedChunks {
  140. glog.V(0).Infof("deleting %s old chunk: %v, [%d, %d)", fullpath, garbage.FileId, garbage.Offset, garbage.Offset+int64(garbage.Size))
  141. operation.DeleteFile(fs.filer.GetMaster(), garbage.FileId, fs.jwt(garbage.FileId))
  142. }
  143. for _, garbage := range garbages {
  144. glog.V(0).Infof("deleting %s garbage chunk: %v, [%d, %d)", fullpath, garbage.FileId, garbage.Offset, garbage.Offset+int64(garbage.Size))
  145. operation.DeleteFile(fs.filer.GetMaster(), garbage.FileId, fs.jwt(garbage.FileId))
  146. }
  147. }
  148. return &filer_pb.UpdateEntryResponse{}, err
  149. }
  150. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  151. err = fs.filer.DeleteEntryMetaAndData(filer2.FullPath(filepath.Join(req.Directory, req.Name)), req.IsRecursive, req.IsDeleteData)
  152. return &filer_pb.DeleteEntryResponse{}, err
  153. }
  154. func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
  155. ttlStr := ""
  156. if req.TtlSec > 0 {
  157. ttlStr = strconv.Itoa(int(req.TtlSec))
  158. }
  159. var altRequest *operation.VolumeAssignRequest
  160. dataCenter := req.DataCenter
  161. if dataCenter == "" {
  162. dataCenter = fs.option.DataCenter
  163. }
  164. assignRequest := &operation.VolumeAssignRequest{
  165. Count: uint64(req.Count),
  166. Replication: req.Replication,
  167. Collection: req.Collection,
  168. Ttl: ttlStr,
  169. DataCenter: dataCenter,
  170. }
  171. if dataCenter != "" {
  172. altRequest = &operation.VolumeAssignRequest{
  173. Count: uint64(req.Count),
  174. Replication: req.Replication,
  175. Collection: req.Collection,
  176. Ttl: ttlStr,
  177. DataCenter: "",
  178. }
  179. }
  180. assignResult, err := operation.Assign(fs.filer.GetMaster(), assignRequest, altRequest)
  181. if err != nil {
  182. return nil, fmt.Errorf("assign volume: %v", err)
  183. }
  184. if assignResult.Error != "" {
  185. return nil, fmt.Errorf("assign volume result: %v", assignResult.Error)
  186. }
  187. return &filer_pb.AssignVolumeResponse{
  188. FileId: assignResult.Fid,
  189. Count: int32(assignResult.Count),
  190. Url: assignResult.Url,
  191. PublicUrl: assignResult.PublicUrl,
  192. }, err
  193. }
  194. func (fs *FilerServer) DeleteCollection(ctx context.Context, req *filer_pb.DeleteCollectionRequest) (resp *filer_pb.DeleteCollectionResponse, err error) {
  195. for _, master := range fs.option.Masters {
  196. _, err = util.Get(fmt.Sprintf("http://%s/col/delete?collection=%s", master, req.Collection))
  197. }
  198. return &filer_pb.DeleteCollectionResponse{}, err
  199. }