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.

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