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.

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