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.

198 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"
  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, err := storage.ToDiskType(req.DiskType)
  54. if err != nil {
  55. return nil, err
  56. }
  57. option := &topology.VolumeGrowOption{
  58. Collection: req.Collection,
  59. ReplicaPlacement: replicaPlacement,
  60. Ttl: ttl,
  61. DiskType: diskType,
  62. Prealloacte: ms.preallocateSize,
  63. DataCenter: req.DataCenter,
  64. Rack: req.Rack,
  65. DataNode: req.DataNode,
  66. MemoryMapMaxSizeMb: req.MemoryMapMaxSizeMb,
  67. }
  68. if !ms.Topo.HasWritableVolume(option) {
  69. if ms.Topo.AvailableSpaceFor(option) <= 0 {
  70. return nil, fmt.Errorf("No free volumes left!")
  71. }
  72. ms.vgLock.Lock()
  73. if !ms.Topo.HasWritableVolume(option) {
  74. if _, err = ms.vg.AutomaticGrowByType(option, ms.grpcDialOption, ms.Topo, int(req.WritableVolumeCount)); err != nil {
  75. ms.vgLock.Unlock()
  76. return nil, fmt.Errorf("Cannot grow volume group! %v", err)
  77. }
  78. }
  79. ms.vgLock.Unlock()
  80. }
  81. fid, count, dn, err := ms.Topo.PickForWrite(req.Count, option)
  82. if err != nil {
  83. return nil, fmt.Errorf("%v", err)
  84. }
  85. return &master_pb.AssignResponse{
  86. Fid: fid,
  87. Url: dn.Url(),
  88. PublicUrl: dn.PublicUrl,
  89. Count: count,
  90. Auth: string(security.GenJwt(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fid)),
  91. }, nil
  92. }
  93. func (ms *MasterServer) Statistics(ctx context.Context, req *master_pb.StatisticsRequest) (*master_pb.StatisticsResponse, error) {
  94. if !ms.Topo.IsLeader() {
  95. return nil, raft.NotLeaderError
  96. }
  97. if req.Replication == "" {
  98. req.Replication = ms.option.DefaultReplicaPlacement
  99. }
  100. replicaPlacement, err := super_block.NewReplicaPlacementFromString(req.Replication)
  101. if err != nil {
  102. return nil, err
  103. }
  104. ttl, err := needle.ReadTTL(req.Ttl)
  105. if err != nil {
  106. return nil, err
  107. }
  108. volumeLayout := ms.Topo.GetVolumeLayout(req.Collection, replicaPlacement, ttl, storage.DiskType(req.DiskType))
  109. stats := volumeLayout.Stats()
  110. totalSize := (ms.Topo.GetMaxVolumeCount() + ms.Topo.GetMaxSsdVolumeCount()) * int64(ms.option.VolumeSizeLimitMB) * 1024 * 1024
  111. resp := &master_pb.StatisticsResponse{
  112. TotalSize: uint64(totalSize),
  113. UsedSize: stats.UsedSize,
  114. FileCount: stats.FileCount,
  115. }
  116. return resp, nil
  117. }
  118. func (ms *MasterServer) VolumeList(ctx context.Context, req *master_pb.VolumeListRequest) (*master_pb.VolumeListResponse, error) {
  119. if !ms.Topo.IsLeader() {
  120. return nil, raft.NotLeaderError
  121. }
  122. resp := &master_pb.VolumeListResponse{
  123. TopologyInfo: ms.Topo.ToTopologyInfo(),
  124. VolumeSizeLimitMb: uint64(ms.option.VolumeSizeLimitMB),
  125. }
  126. return resp, nil
  127. }
  128. func (ms *MasterServer) LookupEcVolume(ctx context.Context, req *master_pb.LookupEcVolumeRequest) (*master_pb.LookupEcVolumeResponse, error) {
  129. if !ms.Topo.IsLeader() {
  130. return nil, raft.NotLeaderError
  131. }
  132. resp := &master_pb.LookupEcVolumeResponse{}
  133. ecLocations, found := ms.Topo.LookupEcShards(needle.VolumeId(req.VolumeId))
  134. if !found {
  135. return resp, fmt.Errorf("ec volume %d not found", req.VolumeId)
  136. }
  137. resp.VolumeId = req.VolumeId
  138. for shardId, shardLocations := range ecLocations.Locations {
  139. var locations []*master_pb.Location
  140. for _, dn := range shardLocations {
  141. locations = append(locations, &master_pb.Location{
  142. Url: string(dn.Id()),
  143. PublicUrl: dn.PublicUrl,
  144. })
  145. }
  146. resp.ShardIdLocations = append(resp.ShardIdLocations, &master_pb.LookupEcVolumeResponse_EcShardIdLocation{
  147. ShardId: uint32(shardId),
  148. Locations: locations,
  149. })
  150. }
  151. return resp, nil
  152. }
  153. func (ms *MasterServer) VacuumVolume(ctx context.Context, req *master_pb.VacuumVolumeRequest) (*master_pb.VacuumVolumeResponse, error) {
  154. if !ms.Topo.IsLeader() {
  155. return nil, raft.NotLeaderError
  156. }
  157. resp := &master_pb.VacuumVolumeResponse{}
  158. ms.Topo.Vacuum(ms.grpcDialOption, float64(req.GarbageThreshold), ms.preallocateSize)
  159. return resp, nil
  160. }