master_grpc_server_volume.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/raft"
  6. "github.com/chrislusf/seaweedfs/weed/storage/types"
  7. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  8. "github.com/chrislusf/seaweedfs/weed/security"
  9. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  10. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  11. "github.com/chrislusf/seaweedfs/weed/topology"
  12. )
  13. func (ms *MasterServer) LookupVolume(ctx context.Context, req *master_pb.LookupVolumeRequest) (*master_pb.LookupVolumeResponse, error) {
  14. if !ms.Topo.IsLeader() {
  15. return nil, raft.NotLeaderError
  16. }
  17. resp := &master_pb.LookupVolumeResponse{}
  18. volumeLocations := ms.lookupVolumeId(req.VolumeIds, req.Collection)
  19. for _, result := range volumeLocations {
  20. var locations []*master_pb.Location
  21. for _, loc := range result.Locations {
  22. locations = append(locations, &master_pb.Location{
  23. Url: loc.Url,
  24. PublicUrl: loc.PublicUrl,
  25. })
  26. }
  27. resp.VolumeIdLocations = append(resp.VolumeIdLocations, &master_pb.LookupVolumeResponse_VolumeIdLocation{
  28. VolumeId: result.VolumeId,
  29. Locations: locations,
  30. Error: result.Error,
  31. })
  32. }
  33. return resp, nil
  34. }
  35. func (ms *MasterServer) Assign(ctx context.Context, req *master_pb.AssignRequest) (*master_pb.AssignResponse, error) {
  36. if !ms.Topo.IsLeader() {
  37. return nil, raft.NotLeaderError
  38. }
  39. if req.Count == 0 {
  40. req.Count = 1
  41. }
  42. if req.Replication == "" {
  43. req.Replication = ms.option.DefaultReplicaPlacement
  44. }
  45. replicaPlacement, err := super_block.NewReplicaPlacementFromString(req.Replication)
  46. if err != nil {
  47. return nil, err
  48. }
  49. ttl, err := needle.ReadTTL(req.Ttl)
  50. if err != nil {
  51. return nil, err
  52. }
  53. diskType := types.ToDiskType(req.DiskType)
  54. option := &topology.VolumeGrowOption{
  55. Collection: req.Collection,
  56. ReplicaPlacement: replicaPlacement,
  57. Ttl: ttl,
  58. DiskType: diskType,
  59. Prealloacte: ms.preallocateSize,
  60. DataCenter: req.DataCenter,
  61. Rack: req.Rack,
  62. DataNode: req.DataNode,
  63. MemoryMapMaxSizeMb: req.MemoryMapMaxSizeMb,
  64. }
  65. if !ms.Topo.HasWritableVolume(option) {
  66. if ms.Topo.AvailableSpaceFor(option) <= 0 {
  67. return nil, fmt.Errorf("no free volumes left for " + option.String())
  68. }
  69. ms.vgLock.Lock()
  70. if !ms.Topo.HasWritableVolume(option) {
  71. if _, err = ms.vg.AutomaticGrowByType(option, ms.grpcDialOption, ms.Topo, int(req.WritableVolumeCount)); err != nil {
  72. ms.vgLock.Unlock()
  73. return nil, fmt.Errorf("Cannot grow volume group! %v", err)
  74. }
  75. }
  76. ms.vgLock.Unlock()
  77. }
  78. fid, count, dn, err := ms.Topo.PickForWrite(req.Count, option)
  79. if err != nil {
  80. return nil, fmt.Errorf("%v", err)
  81. }
  82. return &master_pb.AssignResponse{
  83. Fid: fid,
  84. Url: dn.Url(),
  85. PublicUrl: dn.PublicUrl,
  86. Count: count,
  87. Auth: string(security.GenJwt(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fid)),
  88. }, nil
  89. }
  90. func (ms *MasterServer) Statistics(ctx context.Context, req *master_pb.StatisticsRequest) (*master_pb.StatisticsResponse, error) {
  91. if !ms.Topo.IsLeader() {
  92. return nil, raft.NotLeaderError
  93. }
  94. if req.Replication == "" {
  95. req.Replication = ms.option.DefaultReplicaPlacement
  96. }
  97. replicaPlacement, err := super_block.NewReplicaPlacementFromString(req.Replication)
  98. if err != nil {
  99. return nil, err
  100. }
  101. ttl, err := needle.ReadTTL(req.Ttl)
  102. if err != nil {
  103. return nil, err
  104. }
  105. volumeLayout := ms.Topo.GetVolumeLayout(req.Collection, replicaPlacement, ttl, types.ToDiskType(req.DiskType))
  106. stats := volumeLayout.Stats()
  107. resp := &master_pb.StatisticsResponse{
  108. TotalSize: stats.TotalSize,
  109. UsedSize: stats.UsedSize,
  110. FileCount: stats.FileCount,
  111. }
  112. return resp, nil
  113. }
  114. func (ms *MasterServer) VolumeList(ctx context.Context, req *master_pb.VolumeListRequest) (*master_pb.VolumeListResponse, error) {
  115. if !ms.Topo.IsLeader() {
  116. return nil, raft.NotLeaderError
  117. }
  118. resp := &master_pb.VolumeListResponse{
  119. TopologyInfo: ms.Topo.ToTopologyInfo(),
  120. VolumeSizeLimitMb: uint64(ms.option.VolumeSizeLimitMB),
  121. }
  122. return resp, nil
  123. }
  124. func (ms *MasterServer) LookupEcVolume(ctx context.Context, req *master_pb.LookupEcVolumeRequest) (*master_pb.LookupEcVolumeResponse, error) {
  125. if !ms.Topo.IsLeader() {
  126. return nil, raft.NotLeaderError
  127. }
  128. resp := &master_pb.LookupEcVolumeResponse{}
  129. ecLocations, found := ms.Topo.LookupEcShards(needle.VolumeId(req.VolumeId))
  130. if !found {
  131. return resp, fmt.Errorf("ec volume %d not found", req.VolumeId)
  132. }
  133. resp.VolumeId = req.VolumeId
  134. for shardId, shardLocations := range ecLocations.Locations {
  135. var locations []*master_pb.Location
  136. for _, dn := range shardLocations {
  137. locations = append(locations, &master_pb.Location{
  138. Url: string(dn.Id()),
  139. PublicUrl: dn.PublicUrl,
  140. })
  141. }
  142. resp.ShardIdLocations = append(resp.ShardIdLocations, &master_pb.LookupEcVolumeResponse_EcShardIdLocation{
  143. ShardId: uint32(shardId),
  144. Locations: locations,
  145. })
  146. }
  147. return resp, nil
  148. }
  149. func (ms *MasterServer) VacuumVolume(ctx context.Context, req *master_pb.VacuumVolumeRequest) (*master_pb.VacuumVolumeResponse, error) {
  150. if !ms.Topo.IsLeader() {
  151. return nil, raft.NotLeaderError
  152. }
  153. resp := &master_pb.VacuumVolumeResponse{}
  154. ms.Topo.Vacuum(ms.grpcDialOption, float64(req.GarbageThreshold), ms.preallocateSize)
  155. return resp, nil
  156. }