filer_grpc_server.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "time"
  9. "github.com/seaweedfs/seaweedfs/weed/filer"
  10. "github.com/seaweedfs/seaweedfs/weed/glog"
  11. "github.com/seaweedfs/seaweedfs/weed/operation"
  12. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  13. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  14. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  15. "github.com/seaweedfs/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. DataCenter: loc.DataCenter,
  93. })
  94. }
  95. resp.LocationsMap[vidString] = &filer_pb.Locations{
  96. Locations: locs,
  97. }
  98. }
  99. return resp, nil
  100. }
  101. func (fs *FilerServer) lookupFileId(fileId string) (targetUrls []string, err error) {
  102. fid, err := needle.ParseFileIdFromString(fileId)
  103. if err != nil {
  104. return nil, err
  105. }
  106. locations, found := fs.filer.MasterClient.GetLocations(uint32(fid.VolumeId))
  107. if !found || len(locations) == 0 {
  108. return nil, fmt.Errorf("not found volume %d in %s", fid.VolumeId, fileId)
  109. }
  110. for _, loc := range locations {
  111. targetUrls = append(targetUrls, fmt.Sprintf("http://%s/%s", loc.Url, fileId))
  112. }
  113. return
  114. }
  115. func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (resp *filer_pb.CreateEntryResponse, err error) {
  116. glog.V(4).Infof("CreateEntry %v/%v", req.Directory, req.Entry.Name)
  117. resp = &filer_pb.CreateEntryResponse{}
  118. chunks, garbage, err2 := fs.cleanupChunks(util.Join(req.Directory, req.Entry.Name), nil, req.Entry)
  119. if err2 != nil {
  120. return &filer_pb.CreateEntryResponse{}, fmt.Errorf("CreateEntry cleanupChunks %s %s: %v", req.Directory, req.Entry.Name, err2)
  121. }
  122. so, err := fs.detectStorageOption(string(util.NewFullPath(req.Directory, req.Entry.Name)), "", "", 0, "", "", "", "")
  123. if err != nil {
  124. return nil, err
  125. }
  126. newEntry := filer.FromPbEntry(req.Directory, req.Entry)
  127. newEntry.Chunks = chunks
  128. newEntry.TtlSec = so.TtlSeconds
  129. createErr := fs.filer.CreateEntry(ctx, newEntry, req.OExcl, req.IsFromOtherCluster, req.Signatures, req.SkipCheckParentDirectory)
  130. if createErr == nil {
  131. fs.filer.DeleteChunks(garbage)
  132. } else {
  133. glog.V(3).Infof("CreateEntry %s: %v", filepath.Join(req.Directory, req.Entry.Name), createErr)
  134. resp.Error = createErr.Error()
  135. }
  136. return
  137. }
  138. func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
  139. glog.V(4).Infof("UpdateEntry %v", req)
  140. fullpath := util.Join(req.Directory, req.Entry.Name)
  141. entry, err := fs.filer.FindEntry(ctx, util.FullPath(fullpath))
  142. if err != nil {
  143. return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("not found %s: %v", fullpath, err)
  144. }
  145. chunks, garbage, err2 := fs.cleanupChunks(fullpath, entry, req.Entry)
  146. if err2 != nil {
  147. return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("UpdateEntry cleanupChunks %s: %v", fullpath, err2)
  148. }
  149. newEntry := filer.FromPbEntry(req.Directory, req.Entry)
  150. newEntry.Chunks = chunks
  151. if filer.EqualEntry(entry, newEntry) {
  152. return &filer_pb.UpdateEntryResponse{}, err
  153. }
  154. if err = fs.filer.UpdateEntry(ctx, entry, newEntry); err == nil {
  155. fs.filer.DeleteChunks(garbage)
  156. fs.filer.NotifyUpdateEvent(ctx, entry, newEntry, true, req.IsFromOtherCluster, req.Signatures)
  157. } else {
  158. glog.V(3).Infof("UpdateEntry %s: %v", filepath.Join(req.Directory, req.Entry.Name), err)
  159. }
  160. return &filer_pb.UpdateEntryResponse{}, err
  161. }
  162. func (fs *FilerServer) cleanupChunks(fullpath string, existingEntry *filer.Entry, newEntry *filer_pb.Entry) (chunks, garbage []*filer_pb.FileChunk, err error) {
  163. // remove old chunks if not included in the new ones
  164. if existingEntry != nil {
  165. garbage, err = filer.MinusChunks(fs.lookupFileId, existingEntry.GetChunks(), newEntry.GetChunks())
  166. if err != nil {
  167. return newEntry.GetChunks(), nil, fmt.Errorf("MinusChunks: %v", err)
  168. }
  169. }
  170. // files with manifest chunks are usually large and append only, skip calculating covered chunks
  171. manifestChunks, nonManifestChunks := filer.SeparateManifestChunks(newEntry.GetChunks())
  172. chunks, coveredChunks := filer.CompactFileChunks(fs.lookupFileId, nonManifestChunks)
  173. garbage = append(garbage, coveredChunks...)
  174. if newEntry.Attributes != nil {
  175. so, _ := fs.detectStorageOption(fullpath,
  176. "",
  177. "",
  178. newEntry.Attributes.TtlSec,
  179. "",
  180. "",
  181. "",
  182. "",
  183. ) // ignore readonly error for capacity needed to manifestize
  184. chunks, err = filer.MaybeManifestize(fs.saveAsChunk(so), chunks)
  185. if err != nil {
  186. // not good, but should be ok
  187. glog.V(0).Infof("MaybeManifestize: %v", err)
  188. }
  189. }
  190. chunks = append(chunks, manifestChunks...)
  191. return
  192. }
  193. func (fs *FilerServer) AppendToEntry(ctx context.Context, req *filer_pb.AppendToEntryRequest) (*filer_pb.AppendToEntryResponse, error) {
  194. glog.V(4).Infof("AppendToEntry %v", req)
  195. fullpath := util.NewFullPath(req.Directory, req.EntryName)
  196. var offset int64 = 0
  197. entry, err := fs.filer.FindEntry(ctx, fullpath)
  198. if err == filer_pb.ErrNotFound {
  199. entry = &filer.Entry{
  200. FullPath: fullpath,
  201. Attr: filer.Attr{
  202. Crtime: time.Now(),
  203. Mtime: time.Now(),
  204. Mode: os.FileMode(0644),
  205. Uid: OS_UID,
  206. Gid: OS_GID,
  207. },
  208. }
  209. } else {
  210. offset = int64(filer.TotalSize(entry.GetChunks()))
  211. }
  212. for _, chunk := range req.Chunks {
  213. chunk.Offset = offset
  214. offset += int64(chunk.Size)
  215. }
  216. entry.Chunks = append(entry.GetChunks(), req.Chunks...)
  217. so, err := fs.detectStorageOption(string(fullpath), "", "", entry.TtlSec, "", "", "", "")
  218. if err != nil {
  219. glog.Warningf("detectStorageOption: %v", err)
  220. return &filer_pb.AppendToEntryResponse{}, err
  221. }
  222. entry.Chunks, err = filer.MaybeManifestize(fs.saveAsChunk(so), entry.GetChunks())
  223. if err != nil {
  224. // not good, but should be ok
  225. glog.V(0).Infof("MaybeManifestize: %v", err)
  226. }
  227. err = fs.filer.CreateEntry(context.Background(), entry, false, false, nil, false)
  228. return &filer_pb.AppendToEntryResponse{}, err
  229. }
  230. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  231. glog.V(4).Infof("DeleteEntry %v", req)
  232. err = fs.filer.DeleteEntryMetaAndData(ctx, util.JoinPath(req.Directory, req.Name), req.IsRecursive, req.IgnoreRecursiveError, req.IsDeleteData, req.IsFromOtherCluster, req.Signatures)
  233. resp = &filer_pb.DeleteEntryResponse{}
  234. if err != nil && err != filer_pb.ErrNotFound {
  235. resp.Error = err.Error()
  236. }
  237. return resp, nil
  238. }
  239. func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
  240. so, err := fs.detectStorageOption(req.Path, req.Collection, req.Replication, req.TtlSec, req.DiskType, req.DataCenter, req.Rack, req.DataNode)
  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. assignRequest, altRequest := so.ToAssignRequests(int(req.Count))
  246. assignResult, err := operation.Assign(fs.filer.GetMaster, fs.grpcDialOption, assignRequest, altRequest)
  247. if err != nil {
  248. glog.V(3).Infof("AssignVolume: %v", err)
  249. return &filer_pb.AssignVolumeResponse{Error: fmt.Sprintf("assign volume: %v", err)}, nil
  250. }
  251. if assignResult.Error != "" {
  252. glog.V(3).Infof("AssignVolume error: %v", assignResult.Error)
  253. return &filer_pb.AssignVolumeResponse{Error: fmt.Sprintf("assign volume result: %v", assignResult.Error)}, nil
  254. }
  255. return &filer_pb.AssignVolumeResponse{
  256. FileId: assignResult.Fid,
  257. Count: int32(assignResult.Count),
  258. Location: &filer_pb.Location{
  259. Url: assignResult.Url,
  260. PublicUrl: assignResult.PublicUrl,
  261. GrpcPort: uint32(assignResult.GrpcPort),
  262. },
  263. Auth: string(assignResult.Auth),
  264. Collection: so.Collection,
  265. Replication: so.Replication,
  266. }, nil
  267. }
  268. func (fs *FilerServer) CollectionList(ctx context.Context, req *filer_pb.CollectionListRequest) (resp *filer_pb.CollectionListResponse, err error) {
  269. glog.V(4).Infof("CollectionList %v", req)
  270. resp = &filer_pb.CollectionListResponse{}
  271. err = fs.filer.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  272. masterResp, err := client.CollectionList(context.Background(), &master_pb.CollectionListRequest{
  273. IncludeNormalVolumes: req.IncludeNormalVolumes,
  274. IncludeEcVolumes: req.IncludeEcVolumes,
  275. })
  276. if err != nil {
  277. return err
  278. }
  279. for _, c := range masterResp.Collections {
  280. resp.Collections = append(resp.Collections, &filer_pb.Collection{Name: c.Name})
  281. }
  282. return nil
  283. })
  284. return
  285. }
  286. func (fs *FilerServer) DeleteCollection(ctx context.Context, req *filer_pb.DeleteCollectionRequest) (resp *filer_pb.DeleteCollectionResponse, err error) {
  287. glog.V(4).Infof("DeleteCollection %v", req)
  288. err = fs.filer.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  289. _, err := client.CollectionDelete(context.Background(), &master_pb.CollectionDeleteRequest{
  290. Name: req.GetCollection(),
  291. })
  292. return err
  293. })
  294. return &filer_pb.DeleteCollectionResponse{}, err
  295. }