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.

126 lines
3.1 KiB

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