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.

112 lines
2.9 KiB

6 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  6. "github.com/chrislusf/seaweedfs/weed/storage"
  7. "github.com/chrislusf/seaweedfs/weed/topology"
  8. )
  9. func (ms *MasterServer) LookupVolume(ctx context.Context, req *master_pb.LookupVolumeRequest) (*master_pb.LookupVolumeResponse, error) {
  10. resp := &master_pb.LookupVolumeResponse{}
  11. volumeLocations := ms.lookupVolumeId(req.VolumeIds, req.Collection)
  12. for _, result := range volumeLocations {
  13. var locations []*master_pb.Location
  14. for _, loc := range result.Locations {
  15. locations = append(locations, &master_pb.Location{
  16. Url: loc.Url,
  17. PublicUrl: loc.PublicUrl,
  18. })
  19. }
  20. resp.VolumeIdLocations = append(resp.VolumeIdLocations, &master_pb.LookupVolumeResponse_VolumeIdLocation{
  21. VolumeId: result.VolumeId,
  22. Locations: locations,
  23. Error: result.Error,
  24. })
  25. }
  26. return resp, nil
  27. }
  28. func (ms *MasterServer) Assign(ctx context.Context, req *master_pb.AssignRequest) (*master_pb.AssignResponse, error) {
  29. if req.Count == 0 {
  30. req.Count = 1
  31. }
  32. if req.Replication == "" {
  33. req.Replication = ms.defaultReplicaPlacement
  34. }
  35. replicaPlacement, err := storage.NewReplicaPlacementFromString(req.Replication)
  36. if err != nil {
  37. return nil, err
  38. }
  39. ttl, err := storage.ReadTTL(req.Ttl)
  40. if err != nil {
  41. return nil, err
  42. }
  43. option := &topology.VolumeGrowOption{
  44. Collection: req.Collection,
  45. ReplicaPlacement: replicaPlacement,
  46. Ttl: ttl,
  47. Prealloacte: ms.preallocate,
  48. DataCenter: req.DataCenter,
  49. Rack: req.Rack,
  50. DataNode: req.DataNode,
  51. }
  52. if !ms.Topo.HasWritableVolume(option) {
  53. if ms.Topo.FreeSpace() <= 0 {
  54. return nil, fmt.Errorf("No free volumes left!")
  55. }
  56. ms.vgLock.Lock()
  57. if !ms.Topo.HasWritableVolume(option) {
  58. if _, err = ms.vg.AutomaticGrowByType(option, ms.Topo); err != nil {
  59. ms.vgLock.Unlock()
  60. return nil, fmt.Errorf("Cannot grow volume group! %v", err)
  61. }
  62. }
  63. ms.vgLock.Unlock()
  64. }
  65. fid, count, dn, err := ms.Topo.PickForWrite(req.Count, option)
  66. if err != nil {
  67. return nil, fmt.Errorf("%v", err)
  68. }
  69. return &master_pb.AssignResponse{
  70. Fid: fid,
  71. Url: dn.Url(),
  72. PublicUrl: dn.PublicUrl,
  73. Count: count,
  74. }, nil
  75. }
  76. func (ms *MasterServer) Statistics(ctx context.Context, req *master_pb.StatisticsRequest) (*master_pb.StatisticsResponse, error) {
  77. if req.Replication == "" {
  78. req.Replication = ms.defaultReplicaPlacement
  79. }
  80. replicaPlacement, err := storage.NewReplicaPlacementFromString(req.Replication)
  81. if err != nil {
  82. return nil, err
  83. }
  84. ttl, err := storage.ReadTTL(req.Ttl)
  85. if err != nil {
  86. return nil, err
  87. }
  88. volumeLayout := ms.Topo.GetVolumeLayout(req.Collection, replicaPlacement, ttl)
  89. stats := volumeLayout.Stats()
  90. resp := &master_pb.StatisticsResponse{
  91. TotalSize: stats.TotalSize,
  92. UsedSize: stats.UsedSize,
  93. FileCount: stats.FileCount,
  94. }
  95. return resp, nil
  96. }