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.

130 lines
3.5 KiB

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