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.

131 lines
3.5 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
8 months ago
1 year ago
1 year ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/stats"
  7. "time"
  8. "github.com/seaweedfs/raft"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  10. "github.com/seaweedfs/seaweedfs/weed/security"
  11. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  12. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  13. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  14. "github.com/seaweedfs/seaweedfs/weed/topology"
  15. )
  16. func (ms *MasterServer) StreamAssign(server master_pb.Seaweed_StreamAssignServer) error {
  17. for {
  18. req, err := server.Recv()
  19. if err != nil {
  20. glog.Errorf("StreamAssign failed to receive: %v", err)
  21. return err
  22. }
  23. resp, err := ms.Assign(context.Background(), req)
  24. if err != nil {
  25. glog.Errorf("StreamAssign failed to assign: %v", err)
  26. return err
  27. }
  28. if err = server.Send(resp); err != nil {
  29. glog.Errorf("StreamAssign failed to send: %v", err)
  30. return err
  31. }
  32. }
  33. }
  34. func (ms *MasterServer) Assign(ctx context.Context, req *master_pb.AssignRequest) (*master_pb.AssignResponse, error) {
  35. if !ms.Topo.IsLeader() {
  36. return nil, raft.NotLeaderError
  37. }
  38. if req.Count == 0 {
  39. req.Count = 1
  40. }
  41. if req.Replication == "" {
  42. req.Replication = ms.option.DefaultReplicaPlacement
  43. }
  44. replicaPlacement, err := super_block.NewReplicaPlacementFromString(req.Replication)
  45. if err != nil {
  46. return nil, err
  47. }
  48. ttl, err := needle.ReadTTL(req.Ttl)
  49. if err != nil {
  50. return nil, err
  51. }
  52. diskType := types.ToDiskType(req.DiskType)
  53. option := &topology.VolumeGrowOption{
  54. Collection: req.Collection,
  55. ReplicaPlacement: replicaPlacement,
  56. Ttl: ttl,
  57. DiskType: diskType,
  58. Preallocate: ms.preallocateSize,
  59. DataCenter: req.DataCenter,
  60. Rack: req.Rack,
  61. DataNode: req.DataNode,
  62. MemoryMapMaxSizeMb: req.MemoryMapMaxSizeMb,
  63. }
  64. if !ms.Topo.DataCenterExists(option.DataCenter) {
  65. return nil, fmt.Errorf("data center %v not found in topology", option.DataCenter)
  66. }
  67. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
  68. vl.SetLastGrowCount(req.WritableVolumeCount)
  69. var (
  70. lastErr error
  71. maxTimeout = time.Second * 10
  72. startTime = time.Now()
  73. )
  74. for time.Now().Sub(startTime) < maxTimeout {
  75. fid, count, dnList, shouldGrow, err := ms.Topo.PickForWrite(req.Count, option, vl)
  76. if shouldGrow && !vl.HasGrowRequest() {
  77. if err != nil && ms.Topo.AvailableSpaceFor(option) <= 0 {
  78. err = fmt.Errorf("%s and no free volumes left for %s", err.Error(), option.String())
  79. }
  80. vl.AddGrowRequest()
  81. ms.volumeGrowthRequestChan <- &topology.VolumeGrowRequest{
  82. Option: option,
  83. Count: req.WritableVolumeCount,
  84. }
  85. }
  86. if err != nil {
  87. stats.MasterPickForWriteErrorCounter.Inc()
  88. lastErr = err
  89. time.Sleep(200 * time.Millisecond)
  90. continue
  91. }
  92. dn := dnList.Head()
  93. if dn == nil {
  94. continue
  95. }
  96. var replicas []*master_pb.Location
  97. for _, r := range dnList.Rest() {
  98. replicas = append(replicas, &master_pb.Location{
  99. Url: r.Url(),
  100. PublicUrl: r.PublicUrl,
  101. GrpcPort: uint32(r.GrpcPort),
  102. DataCenter: r.GetDataCenterId(),
  103. })
  104. }
  105. return &master_pb.AssignResponse{
  106. Fid: fid,
  107. Location: &master_pb.Location{
  108. Url: dn.Url(),
  109. PublicUrl: dn.PublicUrl,
  110. GrpcPort: uint32(dn.GrpcPort),
  111. DataCenter: dn.GetDataCenterId(),
  112. },
  113. Count: count,
  114. Auth: string(security.GenJwtForVolumeServer(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fid)),
  115. Replicas: replicas,
  116. }, nil
  117. }
  118. return nil, lastErr
  119. }