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.

235 lines
6.6 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) LookupVolume(ctx context.Context, req *filer_pb.LookupVolumeRequest) (*filer_pb.LookupVolumeResponse, error) {
  66. resp := &filer_pb.LookupVolumeResponse{
  67. LocationsMap: make(map[string]*filer_pb.Locations),
  68. }
  69. for _, vidString := range req.VolumeIds {
  70. vid, err := strconv.Atoi(vidString)
  71. if err != nil {
  72. glog.V(1).Infof("Unknown volume id %d", vid)
  73. return nil, err
  74. }
  75. var locs []*filer_pb.Location
  76. for _, loc := range fs.filer.MasterClient.GetLocations(uint32(vid)) {
  77. locs = append(locs, &filer_pb.Location{
  78. Url: loc.Url,
  79. PublicUrl: loc.PublicUrl,
  80. })
  81. }
  82. resp.LocationsMap[vidString] = &filer_pb.Locations{
  83. Locations: locs,
  84. }
  85. }
  86. return resp, nil
  87. }
  88. func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (resp *filer_pb.CreateEntryResponse, err error) {
  89. fullpath := filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name))
  90. chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
  91. fs.filer.DeleteChunks(garbages)
  92. err = fs.filer.CreateEntry(&filer2.Entry{
  93. FullPath: fullpath,
  94. Attr: filer2.PbToEntryAttribute(req.Entry.Attributes),
  95. Chunks: chunks,
  96. })
  97. if err == nil {
  98. }
  99. return &filer_pb.CreateEntryResponse{}, err
  100. }
  101. func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
  102. fullpath := filepath.Join(req.Directory, req.Entry.Name)
  103. entry, err := fs.filer.FindEntry(filer2.FullPath(fullpath))
  104. if err != nil {
  105. return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("not found %s: %v", fullpath, err)
  106. }
  107. // remove old chunks if not included in the new ones
  108. unusedChunks := filer2.FindUnusedFileChunks(entry.Chunks, req.Entry.Chunks)
  109. chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
  110. newEntry := &filer2.Entry{
  111. FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
  112. Attr: entry.Attr,
  113. Chunks: chunks,
  114. }
  115. glog.V(3).Infof("updating %s: %+v, chunks %d: %v => %+v, chunks %d: %v",
  116. fullpath, entry.Attr, len(entry.Chunks), entry.Chunks,
  117. req.Entry.Attributes, len(req.Entry.Chunks), req.Entry.Chunks)
  118. if req.Entry.Attributes != nil {
  119. if req.Entry.Attributes.Mtime != 0 {
  120. newEntry.Attr.Mtime = time.Unix(req.Entry.Attributes.Mtime, 0)
  121. }
  122. if req.Entry.Attributes.FileMode != 0 {
  123. newEntry.Attr.Mode = os.FileMode(req.Entry.Attributes.FileMode)
  124. }
  125. newEntry.Attr.Uid = req.Entry.Attributes.Uid
  126. newEntry.Attr.Gid = req.Entry.Attributes.Gid
  127. newEntry.Attr.Mime = req.Entry.Attributes.Mime
  128. }
  129. if filer2.EqualEntry(entry, newEntry) {
  130. return &filer_pb.UpdateEntryResponse{}, err
  131. }
  132. if err = fs.filer.UpdateEntry(newEntry); err == nil {
  133. fs.filer.DeleteChunks(unusedChunks)
  134. fs.filer.DeleteChunks(garbages)
  135. }
  136. fs.filer.NotifyUpdateEvent(entry, newEntry, true)
  137. return &filer_pb.UpdateEntryResponse{}, err
  138. }
  139. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  140. err = fs.filer.DeleteEntryMetaAndData(filer2.FullPath(filepath.Join(req.Directory, req.Name)), req.IsRecursive, req.IsDeleteData)
  141. return &filer_pb.DeleteEntryResponse{}, err
  142. }
  143. func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
  144. ttlStr := ""
  145. if req.TtlSec > 0 {
  146. ttlStr = strconv.Itoa(int(req.TtlSec))
  147. }
  148. var altRequest *operation.VolumeAssignRequest
  149. dataCenter := req.DataCenter
  150. if dataCenter == "" {
  151. dataCenter = fs.option.DataCenter
  152. }
  153. assignRequest := &operation.VolumeAssignRequest{
  154. Count: uint64(req.Count),
  155. Replication: req.Replication,
  156. Collection: req.Collection,
  157. Ttl: ttlStr,
  158. DataCenter: dataCenter,
  159. }
  160. if dataCenter != "" {
  161. altRequest = &operation.VolumeAssignRequest{
  162. Count: uint64(req.Count),
  163. Replication: req.Replication,
  164. Collection: req.Collection,
  165. Ttl: ttlStr,
  166. DataCenter: "",
  167. }
  168. }
  169. assignResult, err := operation.Assign(fs.filer.GetMaster(), assignRequest, altRequest)
  170. if err != nil {
  171. return nil, fmt.Errorf("assign volume: %v", err)
  172. }
  173. if assignResult.Error != "" {
  174. return nil, fmt.Errorf("assign volume result: %v", assignResult.Error)
  175. }
  176. return &filer_pb.AssignVolumeResponse{
  177. FileId: assignResult.Fid,
  178. Count: int32(assignResult.Count),
  179. Url: assignResult.Url,
  180. PublicUrl: assignResult.PublicUrl,
  181. }, err
  182. }
  183. func (fs *FilerServer) DeleteCollection(ctx context.Context, req *filer_pb.DeleteCollectionRequest) (resp *filer_pb.DeleteCollectionResponse, err error) {
  184. for _, master := range fs.option.Masters {
  185. _, err = util.Get(fmt.Sprintf("http://%s/col/delete?collection=%s", master, req.Collection))
  186. }
  187. return &filer_pb.DeleteCollectionResponse{}, err
  188. }