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.

186 lines
4.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
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.option.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.preallocateSize,
  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, ms.guard.ExpiresAfterSec, 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.option.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. VolumeSizeLimitMb: uint64(ms.option.VolumeSizeLimitMB),
  117. }
  118. return resp, nil
  119. }
  120. func (ms *MasterServer) LookupEcVolume(ctx context.Context, req *master_pb.LookupEcVolumeRequest) (*master_pb.LookupEcVolumeResponse, error) {
  121. if !ms.Topo.IsLeader() {
  122. return nil, raft.NotLeaderError
  123. }
  124. resp := &master_pb.LookupEcVolumeResponse{}
  125. ecLocations, found := ms.Topo.LookupEcShards(needle.VolumeId(req.VolumeId))
  126. if !found {
  127. return resp, fmt.Errorf("ec volume %d not found", req.VolumeId)
  128. }
  129. resp.VolumeId = req.VolumeId
  130. for shardId, shardLocations := range ecLocations.Locations {
  131. var locations []*master_pb.Location
  132. for _, dn := range shardLocations {
  133. locations = append(locations, &master_pb.Location{
  134. Url: string(dn.Id()),
  135. PublicUrl: dn.PublicUrl,
  136. })
  137. }
  138. resp.ShardIdLocations = append(resp.ShardIdLocations, &master_pb.LookupEcVolumeResponse_EcShardIdLocation{
  139. ShardId: uint32(shardId),
  140. Locations: locations,
  141. })
  142. }
  143. return resp, nil
  144. }
  145. func (ms *MasterServer) GetMasterConfiguration(ctx context.Context, req *master_pb.GetMasterConfigurationRequest) (*master_pb.GetMasterConfigurationResponse, error) {
  146. resp := &master_pb.GetMasterConfigurationResponse{
  147. MetricsAddress: ms.option.MetricsAddress,
  148. MetricsIntervalSeconds: uint32(ms.option.MetricsIntervalSec),
  149. }
  150. return resp, nil
  151. }