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.

140 lines
3.8 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 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. "strings"
  8. "time"
  9. "github.com/seaweedfs/raft"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  11. "github.com/seaweedfs/seaweedfs/weed/security"
  12. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  13. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  14. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  15. "github.com/seaweedfs/seaweedfs/weed/topology"
  16. )
  17. func (ms *MasterServer) StreamAssign(server master_pb.Seaweed_StreamAssignServer) error {
  18. for {
  19. req, err := server.Recv()
  20. if err != nil {
  21. glog.Errorf("StreamAssign failed to receive: %v", err)
  22. return err
  23. }
  24. resp, err := ms.Assign(context.Background(), req)
  25. if err != nil {
  26. glog.Errorf("StreamAssign failed to assign: %v", err)
  27. return err
  28. }
  29. if err = server.Send(resp); err != nil {
  30. glog.Errorf("StreamAssign failed to send: %v", err)
  31. return err
  32. }
  33. }
  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 := types.ToDiskType(req.DiskType)
  54. option := &topology.VolumeGrowOption{
  55. Collection: req.Collection,
  56. ReplicaPlacement: replicaPlacement,
  57. Ttl: ttl,
  58. DiskType: diskType,
  59. Preallocate: ms.preallocateSize,
  60. DataCenter: req.DataCenter,
  61. Rack: req.Rack,
  62. DataNode: req.DataNode,
  63. MemoryMapMaxSizeMb: req.MemoryMapMaxSizeMb,
  64. }
  65. if !ms.Topo.DataCenterExists(option.DataCenter) {
  66. return nil, fmt.Errorf("data center %v not found in topology", option.DataCenter)
  67. }
  68. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
  69. vl.SetLastGrowCount(req.WritableVolumeCount)
  70. var (
  71. lastErr error
  72. maxTimeout = time.Second * 10
  73. startTime = time.Now()
  74. )
  75. for time.Now().Sub(startTime) < maxTimeout {
  76. fid, count, dnList, shouldGrow, err := ms.Topo.PickForWrite(req.Count, option, vl)
  77. if shouldGrow && !vl.HasGrowRequest() {
  78. if err != nil && ms.Topo.AvailableSpaceFor(option) <= 0 {
  79. err = fmt.Errorf("%s and no free volumes left for %s", err.Error(), option.String())
  80. }
  81. vl.AddGrowRequest()
  82. ms.volumeGrowthRequestChan <- &topology.VolumeGrowRequest{
  83. Option: option,
  84. Count: req.WritableVolumeCount,
  85. Reason: "grpc assign",
  86. }
  87. }
  88. if err != nil {
  89. glog.V(1).Infof("assign %v %v: %v", req, option.String(), err)
  90. stats.MasterPickForWriteErrorCounter.Inc()
  91. lastErr = err
  92. if (req.DataCenter != "" || req.Rack != "") && strings.Contains(err.Error(), topology.NoWritableVolumes) {
  93. break
  94. }
  95. time.Sleep(200 * time.Millisecond)
  96. continue
  97. }
  98. dn := dnList.Head()
  99. if dn == nil {
  100. continue
  101. }
  102. var replicas []*master_pb.Location
  103. for _, r := range dnList.Rest() {
  104. replicas = append(replicas, &master_pb.Location{
  105. Url: r.Url(),
  106. PublicUrl: r.PublicUrl,
  107. GrpcPort: uint32(r.GrpcPort),
  108. DataCenter: r.GetDataCenterId(),
  109. })
  110. }
  111. return &master_pb.AssignResponse{
  112. Fid: fid,
  113. Location: &master_pb.Location{
  114. Url: dn.Url(),
  115. PublicUrl: dn.PublicUrl,
  116. GrpcPort: uint32(dn.GrpcPort),
  117. DataCenter: dn.GetDataCenterId(),
  118. },
  119. Count: count,
  120. Auth: string(security.GenJwtForVolumeServer(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fid)),
  121. Replicas: replicas,
  122. }, nil
  123. }
  124. if lastErr != nil {
  125. glog.V(0).Infof("assign %v %v: %v", req, option.String(), lastErr)
  126. }
  127. return nil, lastErr
  128. }