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.4 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/glog"
  6. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  7. "github.com/chrislusf/raft"
  8. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  9. "github.com/chrislusf/seaweedfs/weed/security"
  10. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  11. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  12. "github.com/chrislusf/seaweedfs/weed/topology"
  13. )
  14. func (ms *MasterServer) LookupVolume(ctx context.Context, req *master_pb.LookupVolumeRequest) (*master_pb.LookupVolumeResponse, error) {
  15. if !ms.Topo.IsLeader() {
  16. return nil, raft.NotLeaderError
  17. }
  18. resp := &master_pb.LookupVolumeResponse{}
  19. volumeLocations := ms.lookupVolumeId(req.VolumeIds, req.Collection)
  20. for _, result := range volumeLocations {
  21. var locations []*master_pb.Location
  22. for _, loc := range result.Locations {
  23. locations = append(locations, &master_pb.Location{
  24. Url: loc.Url,
  25. PublicUrl: loc.PublicUrl,
  26. })
  27. }
  28. resp.VolumeIdLocations = append(resp.VolumeIdLocations, &master_pb.LookupVolumeResponse_VolumeIdLocation{
  29. VolumeId: result.VolumeId,
  30. Locations: locations,
  31. Error: result.Error,
  32. })
  33. }
  34. return resp, nil
  35. }
  36. func (ms *MasterServer) Assign(ctx context.Context, req *master_pb.AssignRequest) (*master_pb.AssignResponse, error) {
  37. if !ms.Topo.IsLeader() {
  38. return nil, raft.NotLeaderError
  39. }
  40. if req.Count == 0 {
  41. req.Count = 1
  42. }
  43. if req.Replication == "" {
  44. req.Replication = ms.option.DefaultReplicaPlacement
  45. }
  46. replicaPlacement, err := super_block.NewReplicaPlacementFromString(req.Replication)
  47. if err != nil {
  48. return nil, err
  49. }
  50. ttl, err := needle.ReadTTL(req.Ttl)
  51. if err != nil {
  52. return nil, err
  53. }
  54. option := &topology.VolumeGrowOption{
  55. Collection: req.Collection,
  56. ReplicaPlacement: replicaPlacement,
  57. Ttl: ttl,
  58. Prealloacte: ms.preallocateSize,
  59. DataCenter: req.DataCenter,
  60. Rack: req.Rack,
  61. DataNode: req.DataNode,
  62. MemoryMapMaxSizeMb: req.MemoryMapMaxSizeMb,
  63. }
  64. if !ms.Topo.HasWritableVolume(option) {
  65. if ms.Topo.FreeSpace() <= 0 {
  66. return nil, fmt.Errorf("No free volumes left!")
  67. }
  68. ms.vgLock.Lock()
  69. if !ms.Topo.HasWritableVolume(option) {
  70. if _, err = ms.vg.AutomaticGrowByType(option, ms.grpcDialOption, ms.Topo, int(req.WritableVolumeCount)); err != nil {
  71. ms.vgLock.Unlock()
  72. return nil, fmt.Errorf("Cannot grow volume group! %v", err)
  73. }
  74. }
  75. ms.vgLock.Unlock()
  76. }
  77. fid, count, dn, err := ms.Topo.PickForWrite(req.Count, option)
  78. if err != nil {
  79. return nil, fmt.Errorf("%v", err)
  80. }
  81. return &master_pb.AssignResponse{
  82. Fid: fid,
  83. Url: dn.Url(),
  84. PublicUrl: dn.PublicUrl,
  85. Count: count,
  86. Auth: string(security.GenJwt(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fid)),
  87. }, nil
  88. }
  89. func (ms *MasterServer) Statistics(ctx context.Context, req *master_pb.StatisticsRequest) (*master_pb.StatisticsResponse, error) {
  90. if !ms.Topo.IsLeader() {
  91. return nil, raft.NotLeaderError
  92. }
  93. if req.Replication == "" {
  94. req.Replication = ms.option.DefaultReplicaPlacement
  95. }
  96. replicaPlacement, err := super_block.NewReplicaPlacementFromString(req.Replication)
  97. if err != nil {
  98. return nil, err
  99. }
  100. ttl, err := needle.ReadTTL(req.Ttl)
  101. if err != nil {
  102. return nil, err
  103. }
  104. volumeLayout := ms.Topo.GetVolumeLayout(req.Collection, replicaPlacement, ttl)
  105. stats := volumeLayout.Stats()
  106. totalSize := ms.Topo.GetMaxVolumeCount() * int64(ms.option.VolumeSizeLimitMB) * 1024 * 1024
  107. resp := &master_pb.StatisticsResponse{
  108. TotalSize: uint64(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) GetMasterConfiguration(ctx context.Context, req *master_pb.GetMasterConfigurationRequest) (*master_pb.GetMasterConfigurationResponse, error) {
  150. glog.V(0).Infof("master sends metrics to %s every %d seconds", ms.option.MetricsAddress, ms.option.MetricsIntervalSec)
  151. resp := &master_pb.GetMasterConfigurationResponse{
  152. MetricsAddress: ms.option.MetricsAddress,
  153. MetricsIntervalSeconds: uint32(ms.option.MetricsIntervalSec),
  154. StorageBackends: backend.ToPbStorageBackends(),
  155. }
  156. return resp, nil
  157. }