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.

195 lines
5.3 KiB

6 years ago
6 years ago
4 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/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. totalSize := ms.Topo.GetDiskUsages().GetMaxVolumeCount() * int64(ms.option.VolumeSizeLimitMB) * 1024 * 1024
  108. resp := &master_pb.StatisticsResponse{
  109. TotalSize: uint64(totalSize),
  110. UsedSize: stats.UsedSize,
  111. FileCount: stats.FileCount,
  112. }
  113. return resp, nil
  114. }
  115. func (ms *MasterServer) VolumeList(ctx context.Context, req *master_pb.VolumeListRequest) (*master_pb.VolumeListResponse, error) {
  116. if !ms.Topo.IsLeader() {
  117. return nil, raft.NotLeaderError
  118. }
  119. resp := &master_pb.VolumeListResponse{
  120. TopologyInfo: ms.Topo.ToTopologyInfo(),
  121. VolumeSizeLimitMb: uint64(ms.option.VolumeSizeLimitMB),
  122. }
  123. return resp, nil
  124. }
  125. func (ms *MasterServer) LookupEcVolume(ctx context.Context, req *master_pb.LookupEcVolumeRequest) (*master_pb.LookupEcVolumeResponse, error) {
  126. if !ms.Topo.IsLeader() {
  127. return nil, raft.NotLeaderError
  128. }
  129. resp := &master_pb.LookupEcVolumeResponse{}
  130. ecLocations, found := ms.Topo.LookupEcShards(needle.VolumeId(req.VolumeId))
  131. if !found {
  132. return resp, fmt.Errorf("ec volume %d not found", req.VolumeId)
  133. }
  134. resp.VolumeId = req.VolumeId
  135. for shardId, shardLocations := range ecLocations.Locations {
  136. var locations []*master_pb.Location
  137. for _, dn := range shardLocations {
  138. locations = append(locations, &master_pb.Location{
  139. Url: string(dn.Id()),
  140. PublicUrl: dn.PublicUrl,
  141. })
  142. }
  143. resp.ShardIdLocations = append(resp.ShardIdLocations, &master_pb.LookupEcVolumeResponse_EcShardIdLocation{
  144. ShardId: uint32(shardId),
  145. Locations: locations,
  146. })
  147. }
  148. return resp, nil
  149. }
  150. func (ms *MasterServer) VacuumVolume(ctx context.Context, req *master_pb.VacuumVolumeRequest) (*master_pb.VacuumVolumeResponse, error) {
  151. if !ms.Topo.IsLeader() {
  152. return nil, raft.NotLeaderError
  153. }
  154. resp := &master_pb.VacuumVolumeResponse{}
  155. ms.Topo.Vacuum(ms.grpcDialOption, float64(req.GarbageThreshold), ms.preallocateSize)
  156. return resp, nil
  157. }