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.

368 lines
11 KiB

7 years ago
7 years ago
7 years ago
7 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
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/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. if req.DiskType == "" {
  241. req.DiskType = fs.option.DiskType
  242. }
  243. so, err := fs.detectStorageOption(req.Path, req.Collection, req.Replication, req.TtlSec, req.DiskType, req.DataCenter, req.Rack, req.DataNode)
  244. if err != nil {
  245. glog.V(3).Infof("AssignVolume: %v", err)
  246. return &filer_pb.AssignVolumeResponse{Error: fmt.Sprintf("assign volume: %v", err)}, nil
  247. }
  248. assignRequest, altRequest := so.ToAssignRequests(int(req.Count))
  249. assignResult, err := operation.Assign(fs.filer.GetMaster, fs.grpcDialOption, assignRequest, altRequest)
  250. if err != nil {
  251. glog.V(3).Infof("AssignVolume: %v", err)
  252. return &filer_pb.AssignVolumeResponse{Error: fmt.Sprintf("assign volume: %v", err)}, nil
  253. }
  254. if assignResult.Error != "" {
  255. glog.V(3).Infof("AssignVolume error: %v", assignResult.Error)
  256. return &filer_pb.AssignVolumeResponse{Error: fmt.Sprintf("assign volume result: %v", assignResult.Error)}, nil
  257. }
  258. return &filer_pb.AssignVolumeResponse{
  259. FileId: assignResult.Fid,
  260. Count: int32(assignResult.Count),
  261. Location: &filer_pb.Location{
  262. Url: assignResult.Url,
  263. PublicUrl: assignResult.PublicUrl,
  264. GrpcPort: uint32(assignResult.GrpcPort),
  265. },
  266. Auth: string(assignResult.Auth),
  267. Collection: so.Collection,
  268. Replication: so.Replication,
  269. }, nil
  270. }
  271. func (fs *FilerServer) CollectionList(ctx context.Context, req *filer_pb.CollectionListRequest) (resp *filer_pb.CollectionListResponse, err error) {
  272. glog.V(4).Infof("CollectionList %v", req)
  273. resp = &filer_pb.CollectionListResponse{}
  274. err = fs.filer.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  275. masterResp, err := client.CollectionList(context.Background(), &master_pb.CollectionListRequest{
  276. IncludeNormalVolumes: req.IncludeNormalVolumes,
  277. IncludeEcVolumes: req.IncludeEcVolumes,
  278. })
  279. if err != nil {
  280. return err
  281. }
  282. for _, c := range masterResp.Collections {
  283. resp.Collections = append(resp.Collections, &filer_pb.Collection{Name: c.Name})
  284. }
  285. return nil
  286. })
  287. return
  288. }
  289. func (fs *FilerServer) DeleteCollection(ctx context.Context, req *filer_pb.DeleteCollectionRequest) (resp *filer_pb.DeleteCollectionResponse, err error) {
  290. glog.V(4).Infof("DeleteCollection %v", req)
  291. err = fs.filer.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  292. _, err := client.CollectionDelete(context.Background(), &master_pb.CollectionDeleteRequest{
  293. Name: req.GetCollection(),
  294. })
  295. return err
  296. })
  297. return &filer_pb.DeleteCollectionResponse{}, err
  298. }