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.

516 lines
15 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
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 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
5 years ago
4 years ago
5 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: &filer_pb.Entry{
  29. Name: req.Name,
  30. IsDirectory: entry.IsDirectory(),
  31. Attributes: filer.EntryAttributeToPb(entry),
  32. Chunks: entry.Chunks,
  33. Extended: entry.Extended,
  34. HardLinkId: entry.HardLinkId,
  35. HardLinkCounter: entry.HardLinkCounter,
  36. Content: entry.Content,
  37. },
  38. }, nil
  39. }
  40. func (fs *FilerServer) ListEntries(req *filer_pb.ListEntriesRequest, stream filer_pb.SeaweedFiler_ListEntriesServer) (err error) {
  41. glog.V(4).Infof("ListEntries %v", req)
  42. limit := int(req.Limit)
  43. if limit == 0 {
  44. limit = fs.option.DirListingLimit
  45. }
  46. paginationLimit := filer.PaginationSize
  47. if limit < paginationLimit {
  48. paginationLimit = limit
  49. }
  50. lastFileName := req.StartFromFileName
  51. includeLastFile := req.InclusiveStartFrom
  52. var listErr error
  53. for limit > 0 {
  54. var hasEntries bool
  55. lastFileName, listErr = fs.filer.StreamListDirectoryEntries(stream.Context(), util.FullPath(req.Directory), lastFileName, includeLastFile, int64(paginationLimit), req.Prefix, "", func(entry *filer.Entry) bool {
  56. hasEntries = true
  57. if err = stream.Send(&filer_pb.ListEntriesResponse{
  58. Entry: &filer_pb.Entry{
  59. Name: entry.Name(),
  60. IsDirectory: entry.IsDirectory(),
  61. Chunks: entry.Chunks,
  62. Attributes: filer.EntryAttributeToPb(entry),
  63. Extended: entry.Extended,
  64. HardLinkId: entry.HardLinkId,
  65. HardLinkCounter: entry.HardLinkCounter,
  66. Content: entry.Content,
  67. },
  68. }); err != nil {
  69. return false
  70. }
  71. limit--
  72. if limit == 0 {
  73. return false
  74. }
  75. return true
  76. })
  77. if listErr != nil {
  78. return listErr
  79. }
  80. if err != nil {
  81. return err
  82. }
  83. if !hasEntries {
  84. return nil
  85. }
  86. includeLastFile = false
  87. }
  88. return nil
  89. }
  90. func (fs *FilerServer) LookupVolume(ctx context.Context, req *filer_pb.LookupVolumeRequest) (*filer_pb.LookupVolumeResponse, error) {
  91. resp := &filer_pb.LookupVolumeResponse{
  92. LocationsMap: make(map[string]*filer_pb.Locations),
  93. }
  94. for _, vidString := range req.VolumeIds {
  95. vid, err := strconv.Atoi(vidString)
  96. if err != nil {
  97. glog.V(1).Infof("Unknown volume id %d", vid)
  98. return nil, err
  99. }
  100. var locs []*filer_pb.Location
  101. locations, found := fs.filer.MasterClient.GetLocations(uint32(vid))
  102. if !found {
  103. continue
  104. }
  105. for _, loc := range locations {
  106. locs = append(locs, &filer_pb.Location{
  107. Url: loc.Url,
  108. PublicUrl: loc.PublicUrl,
  109. })
  110. }
  111. resp.LocationsMap[vidString] = &filer_pb.Locations{
  112. Locations: locs,
  113. }
  114. }
  115. return resp, nil
  116. }
  117. func (fs *FilerServer) lookupFileId(fileId string) (targetUrls []string, err error) {
  118. fid, err := needle.ParseFileIdFromString(fileId)
  119. if err != nil {
  120. return nil, err
  121. }
  122. locations, found := fs.filer.MasterClient.GetLocations(uint32(fid.VolumeId))
  123. if !found || len(locations) == 0 {
  124. return nil, fmt.Errorf("not found volume %d in %s", fid.VolumeId, fileId)
  125. }
  126. for _, loc := range locations {
  127. targetUrls = append(targetUrls, fmt.Sprintf("http://%s/%s", loc.Url, fileId))
  128. }
  129. return
  130. }
  131. func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (resp *filer_pb.CreateEntryResponse, err error) {
  132. glog.V(4).Infof("CreateEntry %v/%v", req.Directory, req.Entry.Name)
  133. resp = &filer_pb.CreateEntryResponse{}
  134. chunks, garbage, err2 := fs.cleanupChunks(util.Join(req.Directory, req.Entry.Name), nil, req.Entry)
  135. if err2 != nil {
  136. return &filer_pb.CreateEntryResponse{}, fmt.Errorf("CreateEntry cleanupChunks %s %s: %v", req.Directory, req.Entry.Name, err2)
  137. }
  138. createErr := fs.filer.CreateEntry(ctx, &filer.Entry{
  139. FullPath: util.JoinPath(req.Directory, req.Entry.Name),
  140. Attr: filer.PbToEntryAttribute(req.Entry.Attributes),
  141. Chunks: chunks,
  142. Extended: req.Entry.Extended,
  143. HardLinkId: filer.HardLinkId(req.Entry.HardLinkId),
  144. HardLinkCounter: req.Entry.HardLinkCounter,
  145. Content: req.Entry.Content,
  146. }, req.OExcl, req.IsFromOtherCluster, req.Signatures)
  147. if createErr == nil {
  148. fs.filer.DeleteChunks(garbage)
  149. } else {
  150. glog.V(3).Infof("CreateEntry %s: %v", filepath.Join(req.Directory, req.Entry.Name), createErr)
  151. resp.Error = createErr.Error()
  152. }
  153. return
  154. }
  155. func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
  156. glog.V(4).Infof("UpdateEntry %v", req)
  157. fullpath := util.Join(req.Directory, req.Entry.Name)
  158. entry, err := fs.filer.FindEntry(ctx, util.FullPath(fullpath))
  159. if err != nil {
  160. return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("not found %s: %v", fullpath, err)
  161. }
  162. chunks, garbage, err2 := fs.cleanupChunks(fullpath, entry, req.Entry)
  163. if err2 != nil {
  164. return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("UpdateEntry cleanupChunks %s: %v", fullpath, err2)
  165. }
  166. newEntry := &filer.Entry{
  167. FullPath: util.JoinPath(req.Directory, req.Entry.Name),
  168. Attr: entry.Attr,
  169. Extended: req.Entry.Extended,
  170. Chunks: chunks,
  171. HardLinkId: filer.HardLinkId(req.Entry.HardLinkId),
  172. HardLinkCounter: req.Entry.HardLinkCounter,
  173. Content: req.Entry.Content,
  174. }
  175. glog.V(3).Infof("updating %s: %+v, chunks %d: %v => %+v, chunks %d: %v, extended: %v => %v",
  176. fullpath, entry.Attr, len(entry.Chunks), entry.Chunks,
  177. req.Entry.Attributes, len(req.Entry.Chunks), req.Entry.Chunks,
  178. entry.Extended, req.Entry.Extended)
  179. if req.Entry.Attributes != nil {
  180. if req.Entry.Attributes.Mtime != 0 {
  181. newEntry.Attr.Mtime = time.Unix(req.Entry.Attributes.Mtime, 0)
  182. }
  183. if req.Entry.Attributes.FileMode != 0 {
  184. newEntry.Attr.Mode = os.FileMode(req.Entry.Attributes.FileMode)
  185. }
  186. newEntry.Attr.Uid = req.Entry.Attributes.Uid
  187. newEntry.Attr.Gid = req.Entry.Attributes.Gid
  188. newEntry.Attr.Mime = req.Entry.Attributes.Mime
  189. newEntry.Attr.UserName = req.Entry.Attributes.UserName
  190. newEntry.Attr.GroupNames = req.Entry.Attributes.GroupName
  191. }
  192. if filer.EqualEntry(entry, newEntry) {
  193. return &filer_pb.UpdateEntryResponse{}, err
  194. }
  195. if err = fs.filer.UpdateEntry(ctx, entry, newEntry); err == nil {
  196. fs.filer.DeleteChunks(garbage)
  197. fs.filer.NotifyUpdateEvent(ctx, entry, newEntry, true, req.IsFromOtherCluster, req.Signatures)
  198. } else {
  199. glog.V(3).Infof("UpdateEntry %s: %v", filepath.Join(req.Directory, req.Entry.Name), err)
  200. }
  201. return &filer_pb.UpdateEntryResponse{}, err
  202. }
  203. func (fs *FilerServer) cleanupChunks(fullpath string, existingEntry *filer.Entry, newEntry *filer_pb.Entry) (chunks, garbage []*filer_pb.FileChunk, err error) {
  204. // remove old chunks if not included in the new ones
  205. if existingEntry != nil {
  206. garbage, err = filer.MinusChunks(fs.lookupFileId, existingEntry.Chunks, newEntry.Chunks)
  207. if err != nil {
  208. return newEntry.Chunks, nil, fmt.Errorf("MinusChunks: %v", err)
  209. }
  210. }
  211. // files with manifest chunks are usually large and append only, skip calculating covered chunks
  212. manifestChunks, nonManifestChunks := filer.SeparateManifestChunks(newEntry.Chunks)
  213. chunks, coveredChunks := filer.CompactFileChunks(fs.lookupFileId, nonManifestChunks)
  214. garbage = append(garbage, coveredChunks...)
  215. if newEntry.Attributes != nil {
  216. so := fs.detectStorageOption(fullpath,
  217. newEntry.Attributes.Collection,
  218. newEntry.Attributes.Replication,
  219. newEntry.Attributes.TtlSec,
  220. newEntry.Attributes.DiskType,
  221. "",
  222. "",
  223. )
  224. chunks, err = filer.MaybeManifestize(fs.saveAsChunk(so), chunks)
  225. if err != nil {
  226. // not good, but should be ok
  227. glog.V(0).Infof("MaybeManifestize: %v", err)
  228. }
  229. }
  230. chunks = append(chunks, manifestChunks...)
  231. return
  232. }
  233. func (fs *FilerServer) AppendToEntry(ctx context.Context, req *filer_pb.AppendToEntryRequest) (*filer_pb.AppendToEntryResponse, error) {
  234. glog.V(4).Infof("AppendToEntry %v", req)
  235. fullpath := util.NewFullPath(req.Directory, req.EntryName)
  236. var offset int64 = 0
  237. entry, err := fs.filer.FindEntry(ctx, fullpath)
  238. if err == filer_pb.ErrNotFound {
  239. entry = &filer.Entry{
  240. FullPath: fullpath,
  241. Attr: filer.Attr{
  242. Crtime: time.Now(),
  243. Mtime: time.Now(),
  244. Mode: os.FileMode(0644),
  245. Uid: OS_UID,
  246. Gid: OS_GID,
  247. },
  248. }
  249. } else {
  250. offset = int64(filer.TotalSize(entry.Chunks))
  251. }
  252. for _, chunk := range req.Chunks {
  253. chunk.Offset = offset
  254. offset += int64(chunk.Size)
  255. }
  256. entry.Chunks = append(entry.Chunks, req.Chunks...)
  257. so := fs.detectStorageOption(string(fullpath), entry.Collection, entry.Replication, entry.TtlSec, entry.DiskType, "", "")
  258. entry.Chunks, err = filer.MaybeManifestize(fs.saveAsChunk(so), entry.Chunks)
  259. if err != nil {
  260. // not good, but should be ok
  261. glog.V(0).Infof("MaybeManifestize: %v", err)
  262. }
  263. err = fs.filer.CreateEntry(context.Background(), entry, false, false, nil)
  264. return &filer_pb.AppendToEntryResponse{}, err
  265. }
  266. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  267. glog.V(4).Infof("DeleteEntry %v", req)
  268. err = fs.filer.DeleteEntryMetaAndData(ctx, util.JoinPath(req.Directory, req.Name), req.IsRecursive, req.IgnoreRecursiveError, req.IsDeleteData, req.IsFromOtherCluster, req.Signatures)
  269. resp = &filer_pb.DeleteEntryResponse{}
  270. if err != nil && err != filer_pb.ErrNotFound {
  271. resp.Error = err.Error()
  272. }
  273. return resp, nil
  274. }
  275. func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
  276. so := fs.detectStorageOption(req.Path, req.Collection, req.Replication, req.TtlSec, req.DiskType, req.DataCenter, req.Rack)
  277. assignRequest, altRequest := so.ToAssignRequests(int(req.Count))
  278. assignResult, err := operation.Assign(fs.filer.GetMaster(), fs.grpcDialOption, assignRequest, altRequest)
  279. if err != nil {
  280. glog.V(3).Infof("AssignVolume: %v", err)
  281. return &filer_pb.AssignVolumeResponse{Error: fmt.Sprintf("assign volume: %v", err)}, nil
  282. }
  283. if assignResult.Error != "" {
  284. glog.V(3).Infof("AssignVolume error: %v", assignResult.Error)
  285. return &filer_pb.AssignVolumeResponse{Error: fmt.Sprintf("assign volume result: %v", assignResult.Error)}, nil
  286. }
  287. return &filer_pb.AssignVolumeResponse{
  288. FileId: assignResult.Fid,
  289. Count: int32(assignResult.Count),
  290. Url: assignResult.Url,
  291. PublicUrl: assignResult.PublicUrl,
  292. Auth: string(assignResult.Auth),
  293. Collection: so.Collection,
  294. Replication: so.Replication,
  295. }, nil
  296. }
  297. func (fs *FilerServer) CollectionList(ctx context.Context, req *filer_pb.CollectionListRequest) (resp *filer_pb.CollectionListResponse, err error) {
  298. glog.V(4).Infof("CollectionList %v", req)
  299. resp = &filer_pb.CollectionListResponse{}
  300. err = fs.filer.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  301. masterResp, err := client.CollectionList(context.Background(), &master_pb.CollectionListRequest{
  302. IncludeNormalVolumes: req.IncludeNormalVolumes,
  303. IncludeEcVolumes: req.IncludeEcVolumes,
  304. })
  305. if err != nil {
  306. return err
  307. }
  308. for _, c := range masterResp.Collections {
  309. resp.Collections = append(resp.Collections, &filer_pb.Collection{Name: c.Name})
  310. }
  311. return nil
  312. })
  313. return
  314. }
  315. func (fs *FilerServer) DeleteCollection(ctx context.Context, req *filer_pb.DeleteCollectionRequest) (resp *filer_pb.DeleteCollectionResponse, err error) {
  316. glog.V(4).Infof("DeleteCollection %v", req)
  317. err = fs.filer.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  318. _, err := client.CollectionDelete(context.Background(), &master_pb.CollectionDeleteRequest{
  319. Name: req.GetCollection(),
  320. })
  321. return err
  322. })
  323. return &filer_pb.DeleteCollectionResponse{}, err
  324. }
  325. func (fs *FilerServer) Statistics(ctx context.Context, req *filer_pb.StatisticsRequest) (resp *filer_pb.StatisticsResponse, err error) {
  326. var output *master_pb.StatisticsResponse
  327. err = fs.filer.MasterClient.WithClient(func(masterClient master_pb.SeaweedClient) error {
  328. grpcResponse, grpcErr := masterClient.Statistics(context.Background(), &master_pb.StatisticsRequest{
  329. Replication: req.Replication,
  330. Collection: req.Collection,
  331. Ttl: req.Ttl,
  332. DiskType: req.DiskType,
  333. })
  334. if grpcErr != nil {
  335. return grpcErr
  336. }
  337. output = grpcResponse
  338. return nil
  339. })
  340. if err != nil {
  341. return nil, err
  342. }
  343. return &filer_pb.StatisticsResponse{
  344. TotalSize: output.TotalSize,
  345. UsedSize: output.UsedSize,
  346. FileCount: output.FileCount,
  347. }, nil
  348. }
  349. func (fs *FilerServer) GetFilerConfiguration(ctx context.Context, req *filer_pb.GetFilerConfigurationRequest) (resp *filer_pb.GetFilerConfigurationResponse, err error) {
  350. t := &filer_pb.GetFilerConfigurationResponse{
  351. Masters: fs.option.Masters,
  352. Collection: fs.option.Collection,
  353. Replication: fs.option.DefaultReplication,
  354. MaxMb: uint32(fs.option.MaxMB),
  355. DirBuckets: fs.filer.DirBucketsPath,
  356. Cipher: fs.filer.Cipher,
  357. Signature: fs.filer.Signature,
  358. MetricsAddress: fs.metricsAddress,
  359. MetricsIntervalSec: int32(fs.metricsIntervalSec),
  360. }
  361. glog.V(4).Infof("GetFilerConfiguration: %v", t)
  362. return t, nil
  363. }
  364. func (fs *FilerServer) KeepConnected(stream filer_pb.SeaweedFiler_KeepConnectedServer) error {
  365. req, err := stream.Recv()
  366. if err != nil {
  367. return err
  368. }
  369. clientName := fmt.Sprintf("%s:%d", req.Name, req.GrpcPort)
  370. m := make(map[string]bool)
  371. for _, tp := range req.Resources {
  372. m[tp] = true
  373. }
  374. fs.brokersLock.Lock()
  375. fs.brokers[clientName] = m
  376. glog.V(0).Infof("+ broker %v", clientName)
  377. fs.brokersLock.Unlock()
  378. defer func() {
  379. fs.brokersLock.Lock()
  380. delete(fs.brokers, clientName)
  381. glog.V(0).Infof("- broker %v: %v", clientName, err)
  382. fs.brokersLock.Unlock()
  383. }()
  384. for {
  385. if err := stream.Send(&filer_pb.KeepConnectedResponse{}); err != nil {
  386. glog.V(0).Infof("send broker %v: %+v", clientName, err)
  387. return err
  388. }
  389. // println("replied")
  390. if _, err := stream.Recv(); err != nil {
  391. glog.V(0).Infof("recv broker %v: %v", clientName, err)
  392. return err
  393. }
  394. // println("received")
  395. }
  396. }
  397. func (fs *FilerServer) LocateBroker(ctx context.Context, req *filer_pb.LocateBrokerRequest) (resp *filer_pb.LocateBrokerResponse, err error) {
  398. resp = &filer_pb.LocateBrokerResponse{}
  399. fs.brokersLock.Lock()
  400. defer fs.brokersLock.Unlock()
  401. var localBrokers []*filer_pb.LocateBrokerResponse_Resource
  402. for b, m := range fs.brokers {
  403. if _, found := m[req.Resource]; found {
  404. resp.Found = true
  405. resp.Resources = []*filer_pb.LocateBrokerResponse_Resource{
  406. {
  407. GrpcAddresses: b,
  408. ResourceCount: int32(len(m)),
  409. },
  410. }
  411. return
  412. }
  413. localBrokers = append(localBrokers, &filer_pb.LocateBrokerResponse_Resource{
  414. GrpcAddresses: b,
  415. ResourceCount: int32(len(m)),
  416. })
  417. }
  418. resp.Resources = localBrokers
  419. return resp, nil
  420. }