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.

455 lines
12 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/filer2"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/operation"
  13. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  14. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  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: filer2.EntryAttributeToPb(entry),
  32. Chunks: entry.Chunks,
  33. Extended: entry.Extended,
  34. },
  35. }, nil
  36. }
  37. func (fs *FilerServer) ListEntries(req *filer_pb.ListEntriesRequest, stream filer_pb.SeaweedFiler_ListEntriesServer) error {
  38. glog.V(4).Infof("ListEntries %v", req)
  39. limit := int(req.Limit)
  40. if limit == 0 {
  41. limit = fs.option.DirListingLimit
  42. }
  43. paginationLimit := filer2.PaginationSize
  44. if limit < paginationLimit {
  45. paginationLimit = limit
  46. }
  47. lastFileName := req.StartFromFileName
  48. includeLastFile := req.InclusiveStartFrom
  49. for limit > 0 {
  50. entries, err := fs.filer.ListDirectoryEntries(stream.Context(), util.FullPath(req.Directory), lastFileName, includeLastFile, paginationLimit)
  51. if err != nil {
  52. return err
  53. }
  54. if len(entries) == 0 {
  55. return nil
  56. }
  57. includeLastFile = false
  58. for _, entry := range entries {
  59. lastFileName = entry.Name()
  60. if req.Prefix != "" {
  61. if !strings.HasPrefix(entry.Name(), req.Prefix) {
  62. continue
  63. }
  64. }
  65. if err := stream.Send(&filer_pb.ListEntriesResponse{
  66. Entry: &filer_pb.Entry{
  67. Name: entry.Name(),
  68. IsDirectory: entry.IsDirectory(),
  69. Chunks: entry.Chunks,
  70. Attributes: filer2.EntryAttributeToPb(entry),
  71. Extended: entry.Extended,
  72. },
  73. }); err != nil {
  74. return err
  75. }
  76. limit--
  77. if limit == 0 {
  78. return nil
  79. }
  80. }
  81. if len(entries) < paginationLimit {
  82. break
  83. }
  84. }
  85. return nil
  86. }
  87. func (fs *FilerServer) LookupVolume(ctx context.Context, req *filer_pb.LookupVolumeRequest) (*filer_pb.LookupVolumeResponse, error) {
  88. resp := &filer_pb.LookupVolumeResponse{
  89. LocationsMap: make(map[string]*filer_pb.Locations),
  90. }
  91. for _, vidString := range req.VolumeIds {
  92. vid, err := strconv.Atoi(vidString)
  93. if err != nil {
  94. glog.V(1).Infof("Unknown volume id %d", vid)
  95. return nil, err
  96. }
  97. var locs []*filer_pb.Location
  98. locations, found := fs.filer.MasterClient.GetLocations(uint32(vid))
  99. if !found {
  100. continue
  101. }
  102. for _, loc := range locations {
  103. locs = append(locs, &filer_pb.Location{
  104. Url: loc.Url,
  105. PublicUrl: loc.PublicUrl,
  106. })
  107. }
  108. resp.LocationsMap[vidString] = &filer_pb.Locations{
  109. Locations: locs,
  110. }
  111. }
  112. return resp, nil
  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", req)
  116. resp = &filer_pb.CreateEntryResponse{}
  117. chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
  118. if req.Entry.Attributes == nil {
  119. glog.V(3).Infof("CreateEntry %s: nil attributes", filepath.Join(req.Directory, req.Entry.Name))
  120. resp.Error = fmt.Sprintf("can not create entry with empty attributes")
  121. return
  122. }
  123. createErr := fs.filer.CreateEntry(ctx, &filer2.Entry{
  124. FullPath: util.JoinPath(req.Directory, req.Entry.Name),
  125. Attr: filer2.PbToEntryAttribute(req.Entry.Attributes),
  126. Chunks: chunks,
  127. }, req.OExcl, req.IsFromOtherCluster)
  128. if createErr == nil {
  129. fs.filer.DeleteChunks(garbages)
  130. } else {
  131. glog.V(3).Infof("CreateEntry %s: %v", filepath.Join(req.Directory, req.Entry.Name), createErr)
  132. resp.Error = createErr.Error()
  133. }
  134. return
  135. }
  136. func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
  137. glog.V(4).Infof("UpdateEntry %v", req)
  138. fullpath := util.Join(req.Directory, req.Entry.Name)
  139. entry, err := fs.filer.FindEntry(ctx, util.FullPath(fullpath))
  140. if err != nil {
  141. return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("not found %s: %v", fullpath, err)
  142. }
  143. // remove old chunks if not included in the new ones
  144. unusedChunks := filer2.MinusChunks(entry.Chunks, req.Entry.Chunks)
  145. chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
  146. newEntry := &filer2.Entry{
  147. FullPath: util.JoinPath(req.Directory, req.Entry.Name),
  148. Attr: entry.Attr,
  149. Extended: req.Entry.Extended,
  150. Chunks: chunks,
  151. }
  152. glog.V(3).Infof("updating %s: %+v, chunks %d: %v => %+v, chunks %d: %v, extended: %v => %v",
  153. fullpath, entry.Attr, len(entry.Chunks), entry.Chunks,
  154. req.Entry.Attributes, len(req.Entry.Chunks), req.Entry.Chunks,
  155. entry.Extended, req.Entry.Extended)
  156. if req.Entry.Attributes != nil {
  157. if req.Entry.Attributes.Mtime != 0 {
  158. newEntry.Attr.Mtime = time.Unix(req.Entry.Attributes.Mtime, 0)
  159. }
  160. if req.Entry.Attributes.FileMode != 0 {
  161. newEntry.Attr.Mode = os.FileMode(req.Entry.Attributes.FileMode)
  162. }
  163. newEntry.Attr.Uid = req.Entry.Attributes.Uid
  164. newEntry.Attr.Gid = req.Entry.Attributes.Gid
  165. newEntry.Attr.Mime = req.Entry.Attributes.Mime
  166. newEntry.Attr.UserName = req.Entry.Attributes.UserName
  167. newEntry.Attr.GroupNames = req.Entry.Attributes.GroupName
  168. }
  169. if filer2.EqualEntry(entry, newEntry) {
  170. return &filer_pb.UpdateEntryResponse{}, err
  171. }
  172. if err = fs.filer.UpdateEntry(ctx, entry, newEntry); err == nil {
  173. fs.filer.DeleteChunks(unusedChunks)
  174. fs.filer.DeleteChunks(garbages)
  175. } else {
  176. glog.V(3).Infof("UpdateEntry %s: %v", filepath.Join(req.Directory, req.Entry.Name), err)
  177. }
  178. fs.filer.NotifyUpdateEvent(ctx, entry, newEntry, true, req.IsFromOtherCluster)
  179. return &filer_pb.UpdateEntryResponse{}, err
  180. }
  181. func (fs *FilerServer) AppendToEntry(ctx context.Context, req *filer_pb.AppendToEntryRequest) (*filer_pb.AppendToEntryResponse, error) {
  182. glog.V(4).Infof("AppendToEntry %v", req)
  183. fullpath := util.NewFullPath(req.Directory, req.EntryName)
  184. var offset int64 = 0
  185. entry, err := fs.filer.FindEntry(ctx, util.FullPath(fullpath))
  186. if err == filer_pb.ErrNotFound {
  187. entry = &filer2.Entry{
  188. FullPath: fullpath,
  189. Attr: filer2.Attr{
  190. Crtime: time.Now(),
  191. Mtime: time.Now(),
  192. Mode: os.FileMode(0644),
  193. Uid: OS_UID,
  194. Gid: OS_GID,
  195. },
  196. }
  197. } else {
  198. offset = int64(filer2.TotalSize(entry.Chunks))
  199. }
  200. for _, chunk := range req.Chunks {
  201. chunk.Offset = offset
  202. offset += int64(chunk.Size)
  203. }
  204. entry.Chunks = append(entry.Chunks, req.Chunks...)
  205. err = fs.filer.CreateEntry(context.Background(), entry, false, false)
  206. return &filer_pb.AppendToEntryResponse{}, err
  207. }
  208. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  209. glog.V(4).Infof("DeleteEntry %v", req)
  210. err = fs.filer.DeleteEntryMetaAndData(ctx, util.JoinPath(req.Directory, req.Name), req.IsRecursive, req.IgnoreRecursiveError, req.IsDeleteData, req.IsFromOtherCluster)
  211. resp = &filer_pb.DeleteEntryResponse{}
  212. if err != nil {
  213. resp.Error = err.Error()
  214. }
  215. return resp, nil
  216. }
  217. func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
  218. ttlStr := ""
  219. if req.TtlSec > 0 {
  220. ttlStr = strconv.Itoa(int(req.TtlSec))
  221. }
  222. collection, replication, _ := fs.detectCollection(req.ParentPath, req.Collection, req.Replication)
  223. var altRequest *operation.VolumeAssignRequest
  224. dataCenter := req.DataCenter
  225. if dataCenter == "" {
  226. dataCenter = fs.option.DataCenter
  227. }
  228. assignRequest := &operation.VolumeAssignRequest{
  229. Count: uint64(req.Count),
  230. Replication: replication,
  231. Collection: collection,
  232. Ttl: ttlStr,
  233. DataCenter: dataCenter,
  234. }
  235. if dataCenter != "" {
  236. altRequest = &operation.VolumeAssignRequest{
  237. Count: uint64(req.Count),
  238. Replication: replication,
  239. Collection: collection,
  240. Ttl: ttlStr,
  241. DataCenter: "",
  242. }
  243. }
  244. assignResult, err := operation.Assign(fs.filer.GetMaster(), fs.grpcDialOption, assignRequest, altRequest)
  245. if err != nil {
  246. glog.V(3).Infof("AssignVolume: %v", err)
  247. return &filer_pb.AssignVolumeResponse{Error: fmt.Sprintf("assign volume: %v", err)}, nil
  248. }
  249. if assignResult.Error != "" {
  250. glog.V(3).Infof("AssignVolume error: %v", assignResult.Error)
  251. return &filer_pb.AssignVolumeResponse{Error: fmt.Sprintf("assign volume result: %v", assignResult.Error)}, nil
  252. }
  253. return &filer_pb.AssignVolumeResponse{
  254. FileId: assignResult.Fid,
  255. Count: int32(assignResult.Count),
  256. Url: assignResult.Url,
  257. PublicUrl: assignResult.PublicUrl,
  258. Auth: string(assignResult.Auth),
  259. Collection: collection,
  260. Replication: replication,
  261. }, nil
  262. }
  263. func (fs *FilerServer) DeleteCollection(ctx context.Context, req *filer_pb.DeleteCollectionRequest) (resp *filer_pb.DeleteCollectionResponse, err error) {
  264. glog.V(4).Infof("DeleteCollection %v", req)
  265. err = fs.filer.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  266. _, err := client.CollectionDelete(context.Background(), &master_pb.CollectionDeleteRequest{
  267. Name: req.GetCollection(),
  268. })
  269. return err
  270. })
  271. return &filer_pb.DeleteCollectionResponse{}, err
  272. }
  273. func (fs *FilerServer) Statistics(ctx context.Context, req *filer_pb.StatisticsRequest) (resp *filer_pb.StatisticsResponse, err error) {
  274. var output *master_pb.StatisticsResponse
  275. err = fs.filer.MasterClient.WithClient(func(masterClient master_pb.SeaweedClient) error {
  276. grpcResponse, grpcErr := masterClient.Statistics(context.Background(), &master_pb.StatisticsRequest{
  277. Replication: req.Replication,
  278. Collection: req.Collection,
  279. Ttl: req.Ttl,
  280. })
  281. if grpcErr != nil {
  282. return grpcErr
  283. }
  284. output = grpcResponse
  285. return nil
  286. })
  287. if err != nil {
  288. return nil, err
  289. }
  290. return &filer_pb.StatisticsResponse{
  291. TotalSize: output.TotalSize,
  292. UsedSize: output.UsedSize,
  293. FileCount: output.FileCount,
  294. }, nil
  295. }
  296. func (fs *FilerServer) GetFilerConfiguration(ctx context.Context, req *filer_pb.GetFilerConfigurationRequest) (resp *filer_pb.GetFilerConfigurationResponse, err error) {
  297. t := &filer_pb.GetFilerConfigurationResponse{
  298. Masters: fs.option.Masters,
  299. Collection: fs.option.Collection,
  300. Replication: fs.option.DefaultReplication,
  301. MaxMb: uint32(fs.option.MaxMB),
  302. DirBuckets: fs.filer.DirBucketsPath,
  303. Cipher: fs.filer.Cipher,
  304. }
  305. glog.V(4).Infof("GetFilerConfiguration: %v", t)
  306. return t, nil
  307. }
  308. func (fs *FilerServer) KeepConnected(stream filer_pb.SeaweedFiler_KeepConnectedServer) error {
  309. req, err := stream.Recv()
  310. if err != nil {
  311. return err
  312. }
  313. clientName := fmt.Sprintf("%s:%d", req.Name, req.GrpcPort)
  314. m := make(map[string]bool)
  315. for _, tp := range req.Resources {
  316. m[tp] = true
  317. }
  318. fs.brokersLock.Lock()
  319. fs.brokers[clientName] = m
  320. glog.V(0).Infof("+ broker %v", clientName)
  321. fs.brokersLock.Unlock()
  322. defer func() {
  323. fs.brokersLock.Lock()
  324. delete(fs.brokers, clientName)
  325. glog.V(0).Infof("- broker %v: %v", clientName, err)
  326. fs.brokersLock.Unlock()
  327. }()
  328. for {
  329. if err := stream.Send(&filer_pb.KeepConnectedResponse{}); err != nil {
  330. glog.V(0).Infof("send broker %v: %+v", clientName, err)
  331. return err
  332. }
  333. // println("replied")
  334. if _, err := stream.Recv(); err != nil {
  335. glog.V(0).Infof("recv broker %v: %v", clientName, err)
  336. return err
  337. }
  338. // println("received")
  339. }
  340. }
  341. func (fs *FilerServer) LocateBroker(ctx context.Context, req *filer_pb.LocateBrokerRequest) (resp *filer_pb.LocateBrokerResponse, err error) {
  342. resp = &filer_pb.LocateBrokerResponse{}
  343. fs.brokersLock.Lock()
  344. defer fs.brokersLock.Unlock()
  345. var localBrokers []*filer_pb.LocateBrokerResponse_Resource
  346. for b, m := range fs.brokers {
  347. if _, found := m[req.Resource]; found {
  348. resp.Found = true
  349. resp.Resources = []*filer_pb.LocateBrokerResponse_Resource{
  350. {
  351. GrpcAddresses: b,
  352. ResourceCount: int32(len(m)),
  353. },
  354. }
  355. return
  356. }
  357. localBrokers = append(localBrokers, &filer_pb.LocateBrokerResponse_Resource{
  358. GrpcAddresses: b,
  359. ResourceCount: int32(len(m)),
  360. })
  361. }
  362. resp.Resources = localBrokers
  363. return resp, nil
  364. }