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.

358 lines
11 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
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
6 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/filer"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/operation"
  12. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  13. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  14. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. func (fs *FilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
  18. glog.V(4).Infof("LookupDirectoryEntry %s", filepath.Join(req.Directory, req.Name))
  19. entry, err := fs.filer.FindEntry(ctx, util.JoinPath(req.Directory, req.Name))
  20. if err == filer_pb.ErrNotFound {
  21. return &filer_pb.LookupDirectoryEntryResponse{}, err
  22. }
  23. if err != nil {
  24. glog.V(3).Infof("LookupDirectoryEntry %s: %+v, ", filepath.Join(req.Directory, req.Name), err)
  25. return nil, err
  26. }
  27. return &filer_pb.LookupDirectoryEntryResponse{
  28. Entry: entry.ToProtoEntry(),
  29. }, nil
  30. }
  31. func (fs *FilerServer) ListEntries(req *filer_pb.ListEntriesRequest, stream filer_pb.SeaweedFiler_ListEntriesServer) (err error) {
  32. glog.V(4).Infof("ListEntries %v", req)
  33. limit := int(req.Limit)
  34. if limit == 0 {
  35. limit = fs.option.DirListingLimit
  36. }
  37. paginationLimit := filer.PaginationSize
  38. if limit < paginationLimit {
  39. paginationLimit = limit
  40. }
  41. lastFileName := req.StartFromFileName
  42. includeLastFile := req.InclusiveStartFrom
  43. var listErr error
  44. for limit > 0 {
  45. var hasEntries bool
  46. lastFileName, listErr = fs.filer.StreamListDirectoryEntries(stream.Context(), util.FullPath(req.Directory), lastFileName, includeLastFile, int64(paginationLimit), req.Prefix, "", "", func(entry *filer.Entry) bool {
  47. hasEntries = true
  48. if err = stream.Send(&filer_pb.ListEntriesResponse{
  49. Entry: entry.ToProtoEntry(),
  50. }); err != nil {
  51. return false
  52. }
  53. limit--
  54. if limit == 0 {
  55. return false
  56. }
  57. return true
  58. })
  59. if listErr != nil {
  60. return listErr
  61. }
  62. if err != nil {
  63. return err
  64. }
  65. if !hasEntries {
  66. return nil
  67. }
  68. includeLastFile = false
  69. }
  70. return nil
  71. }
  72. func (fs *FilerServer) LookupVolume(ctx context.Context, req *filer_pb.LookupVolumeRequest) (*filer_pb.LookupVolumeResponse, error) {
  73. resp := &filer_pb.LookupVolumeResponse{
  74. LocationsMap: make(map[string]*filer_pb.Locations),
  75. }
  76. for _, vidString := range req.VolumeIds {
  77. vid, err := strconv.Atoi(vidString)
  78. if err != nil {
  79. glog.V(1).Infof("Unknown volume id %d", vid)
  80. return nil, err
  81. }
  82. var locs []*filer_pb.Location
  83. locations, found := fs.filer.MasterClient.GetLocations(uint32(vid))
  84. if !found {
  85. continue
  86. }
  87. for _, loc := range locations {
  88. locs = append(locs, &filer_pb.Location{
  89. Url: loc.Url,
  90. PublicUrl: loc.PublicUrl,
  91. GrpcPort: uint32(loc.GrpcPort),
  92. })
  93. }
  94. resp.LocationsMap[vidString] = &filer_pb.Locations{
  95. Locations: locs,
  96. }
  97. }
  98. return resp, nil
  99. }
  100. func (fs *FilerServer) lookupFileId(fileId string) (targetUrls []string, err error) {
  101. fid, err := needle.ParseFileIdFromString(fileId)
  102. if err != nil {
  103. return nil, err
  104. }
  105. locations, found := fs.filer.MasterClient.GetLocations(uint32(fid.VolumeId))
  106. if !found || len(locations) == 0 {
  107. return nil, fmt.Errorf("not found volume %d in %s", fid.VolumeId, fileId)
  108. }
  109. for _, loc := range locations {
  110. targetUrls = append(targetUrls, fmt.Sprintf("http://%s/%s", loc.Url, fileId))
  111. }
  112. return
  113. }
  114. func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (resp *filer_pb.CreateEntryResponse, err error) {
  115. glog.V(4).Infof("CreateEntry %v/%v", req.Directory, req.Entry.Name)
  116. resp = &filer_pb.CreateEntryResponse{}
  117. chunks, garbage, err2 := fs.cleanupChunks(util.Join(req.Directory, req.Entry.Name), nil, req.Entry)
  118. if err2 != nil {
  119. return &filer_pb.CreateEntryResponse{}, fmt.Errorf("CreateEntry cleanupChunks %s %s: %v", req.Directory, req.Entry.Name, err2)
  120. }
  121. newEntry := filer.FromPbEntry(req.Directory, req.Entry)
  122. newEntry.Chunks = chunks
  123. createErr := fs.filer.CreateEntry(ctx, newEntry, req.OExcl, req.IsFromOtherCluster, req.Signatures, req.SkipCheckParentDirectory)
  124. if createErr == nil {
  125. fs.filer.DeleteChunks(garbage)
  126. } else {
  127. glog.V(3).Infof("CreateEntry %s: %v", filepath.Join(req.Directory, req.Entry.Name), createErr)
  128. resp.Error = createErr.Error()
  129. }
  130. return
  131. }
  132. func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
  133. glog.V(4).Infof("UpdateEntry %v", req)
  134. fullpath := util.Join(req.Directory, req.Entry.Name)
  135. entry, err := fs.filer.FindEntry(ctx, util.FullPath(fullpath))
  136. if err != nil {
  137. return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("not found %s: %v", fullpath, err)
  138. }
  139. chunks, garbage, err2 := fs.cleanupChunks(fullpath, entry, req.Entry)
  140. if err2 != nil {
  141. return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("UpdateEntry cleanupChunks %s: %v", fullpath, err2)
  142. }
  143. newEntry := filer.FromPbEntry(req.Directory, req.Entry)
  144. newEntry.Chunks = chunks
  145. if filer.EqualEntry(entry, newEntry) {
  146. return &filer_pb.UpdateEntryResponse{}, err
  147. }
  148. if err = fs.filer.UpdateEntry(ctx, entry, newEntry); err == nil {
  149. fs.filer.DeleteChunks(garbage)
  150. fs.filer.NotifyUpdateEvent(ctx, entry, newEntry, true, req.IsFromOtherCluster, req.Signatures)
  151. } else {
  152. glog.V(3).Infof("UpdateEntry %s: %v", filepath.Join(req.Directory, req.Entry.Name), err)
  153. }
  154. return &filer_pb.UpdateEntryResponse{}, err
  155. }
  156. func (fs *FilerServer) cleanupChunks(fullpath string, existingEntry *filer.Entry, newEntry *filer_pb.Entry) (chunks, garbage []*filer_pb.FileChunk, err error) {
  157. // remove old chunks if not included in the new ones
  158. if existingEntry != nil {
  159. garbage, err = filer.MinusChunks(fs.lookupFileId, existingEntry.Chunks, newEntry.Chunks)
  160. if err != nil {
  161. return newEntry.Chunks, nil, fmt.Errorf("MinusChunks: %v", err)
  162. }
  163. }
  164. // files with manifest chunks are usually large and append only, skip calculating covered chunks
  165. manifestChunks, nonManifestChunks := filer.SeparateManifestChunks(newEntry.Chunks)
  166. chunks, coveredChunks := filer.CompactFileChunks(fs.lookupFileId, nonManifestChunks)
  167. garbage = append(garbage, coveredChunks...)
  168. if newEntry.Attributes != nil {
  169. so, _ := fs.detectStorageOption(fullpath,
  170. newEntry.Attributes.Collection,
  171. newEntry.Attributes.Replication,
  172. newEntry.Attributes.TtlSec,
  173. newEntry.Attributes.DiskType,
  174. "",
  175. "",
  176. "",
  177. ) // ignore readonly error for capacity needed to manifestize
  178. chunks, err = filer.MaybeManifestize(fs.saveAsChunk(so), chunks)
  179. if err != nil {
  180. // not good, but should be ok
  181. glog.V(0).Infof("MaybeManifestize: %v", err)
  182. }
  183. }
  184. chunks = append(chunks, manifestChunks...)
  185. return
  186. }
  187. func (fs *FilerServer) AppendToEntry(ctx context.Context, req *filer_pb.AppendToEntryRequest) (*filer_pb.AppendToEntryResponse, error) {
  188. glog.V(4).Infof("AppendToEntry %v", req)
  189. fullpath := util.NewFullPath(req.Directory, req.EntryName)
  190. var offset int64 = 0
  191. entry, err := fs.filer.FindEntry(ctx, fullpath)
  192. if err == filer_pb.ErrNotFound {
  193. entry = &filer.Entry{
  194. FullPath: fullpath,
  195. Attr: filer.Attr{
  196. Crtime: time.Now(),
  197. Mtime: time.Now(),
  198. Mode: os.FileMode(0644),
  199. Uid: OS_UID,
  200. Gid: OS_GID,
  201. },
  202. }
  203. } else {
  204. offset = int64(filer.TotalSize(entry.Chunks))
  205. }
  206. for _, chunk := range req.Chunks {
  207. chunk.Offset = offset
  208. offset += int64(chunk.Size)
  209. }
  210. entry.Chunks = append(entry.Chunks, req.Chunks...)
  211. so, err := fs.detectStorageOption(string(fullpath), entry.Collection, entry.Replication, entry.TtlSec, entry.DiskType, "", "", "")
  212. if err != nil {
  213. glog.Warningf("detectStorageOption: %v", err)
  214. return &filer_pb.AppendToEntryResponse{}, err
  215. }
  216. entry.Chunks, err = filer.MaybeManifestize(fs.saveAsChunk(so), entry.Chunks)
  217. if err != nil {
  218. // not good, but should be ok
  219. glog.V(0).Infof("MaybeManifestize: %v", err)
  220. }
  221. err = fs.filer.CreateEntry(context.Background(), entry, false, false, nil, false)
  222. return &filer_pb.AppendToEntryResponse{}, err
  223. }
  224. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  225. glog.V(4).Infof("DeleteEntry %v", req)
  226. err = fs.filer.DeleteEntryMetaAndData(ctx, util.JoinPath(req.Directory, req.Name), req.IsRecursive, req.IgnoreRecursiveError, req.IsDeleteData, req.IsFromOtherCluster, req.Signatures)
  227. resp = &filer_pb.DeleteEntryResponse{}
  228. if err != nil && err != filer_pb.ErrNotFound {
  229. resp.Error = err.Error()
  230. }
  231. return resp, nil
  232. }
  233. func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
  234. so, err := fs.detectStorageOption(req.Path, req.Collection, req.Replication, req.TtlSec, req.DiskType, req.DataCenter, req.Rack, req.DataNode)
  235. if err != nil {
  236. glog.V(3).Infof("AssignVolume: %v", err)
  237. return &filer_pb.AssignVolumeResponse{Error: fmt.Sprintf("assign volume: %v", err)}, nil
  238. }
  239. assignRequest, altRequest := so.ToAssignRequests(int(req.Count))
  240. assignResult, err := operation.Assign(fs.filer.GetMaster, fs.grpcDialOption, assignRequest, altRequest)
  241. if err != nil {
  242. glog.V(3).Infof("AssignVolume: %v", err)
  243. return &filer_pb.AssignVolumeResponse{Error: fmt.Sprintf("assign volume: %v", err)}, nil
  244. }
  245. if assignResult.Error != "" {
  246. glog.V(3).Infof("AssignVolume error: %v", assignResult.Error)
  247. return &filer_pb.AssignVolumeResponse{Error: fmt.Sprintf("assign volume result: %v", assignResult.Error)}, nil
  248. }
  249. return &filer_pb.AssignVolumeResponse{
  250. FileId: assignResult.Fid,
  251. Count: int32(assignResult.Count),
  252. Location: &filer_pb.Location{
  253. Url: assignResult.Url,
  254. PublicUrl: assignResult.PublicUrl,
  255. GrpcPort: uint32(assignResult.GrpcPort),
  256. },
  257. Auth: string(assignResult.Auth),
  258. Collection: so.Collection,
  259. Replication: so.Replication,
  260. }, nil
  261. }
  262. func (fs *FilerServer) CollectionList(ctx context.Context, req *filer_pb.CollectionListRequest) (resp *filer_pb.CollectionListResponse, err error) {
  263. glog.V(4).Infof("CollectionList %v", req)
  264. resp = &filer_pb.CollectionListResponse{}
  265. err = fs.filer.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  266. masterResp, err := client.CollectionList(context.Background(), &master_pb.CollectionListRequest{
  267. IncludeNormalVolumes: req.IncludeNormalVolumes,
  268. IncludeEcVolumes: req.IncludeEcVolumes,
  269. })
  270. if err != nil {
  271. return err
  272. }
  273. for _, c := range masterResp.Collections {
  274. resp.Collections = append(resp.Collections, &filer_pb.Collection{Name: c.Name})
  275. }
  276. return nil
  277. })
  278. return
  279. }
  280. func (fs *FilerServer) DeleteCollection(ctx context.Context, req *filer_pb.DeleteCollectionRequest) (resp *filer_pb.DeleteCollectionResponse, err error) {
  281. glog.V(4).Infof("DeleteCollection %v", req)
  282. err = fs.filer.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  283. _, err := client.CollectionDelete(context.Background(), &master_pb.CollectionDeleteRequest{
  284. Name: req.GetCollection(),
  285. })
  286. return err
  287. })
  288. return &filer_pb.DeleteCollectionResponse{}, err
  289. }