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.

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