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.

192 lines
5.2 KiB

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