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.

142 lines
3.6 KiB

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