master_grpc_server_assign.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. Reason: "grpc assign",
  85. }
  86. }
  87. if err != nil {
  88. glog.V(1).Infof("assign %v %v: %v", req, option.String(), err)
  89. stats.MasterPickForWriteErrorCounter.Inc()
  90. lastErr = err
  91. time.Sleep(200 * time.Millisecond)
  92. continue
  93. }
  94. dn := dnList.Head()
  95. if dn == nil {
  96. continue
  97. }
  98. var replicas []*master_pb.Location
  99. for _, r := range dnList.Rest() {
  100. replicas = append(replicas, &master_pb.Location{
  101. Url: r.Url(),
  102. PublicUrl: r.PublicUrl,
  103. GrpcPort: uint32(r.GrpcPort),
  104. DataCenter: r.GetDataCenterId(),
  105. })
  106. }
  107. return &master_pb.AssignResponse{
  108. Fid: fid,
  109. Location: &master_pb.Location{
  110. Url: dn.Url(),
  111. PublicUrl: dn.PublicUrl,
  112. GrpcPort: uint32(dn.GrpcPort),
  113. DataCenter: dn.GetDataCenterId(),
  114. },
  115. Count: count,
  116. Auth: string(security.GenJwtForVolumeServer(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fid)),
  117. Replicas: replicas,
  118. }, nil
  119. }
  120. if lastErr != nil {
  121. glog.V(0).Infof("assign %v %v: %v", req, option.String(), lastErr)
  122. }
  123. return nil, lastErr
  124. }