master_grpc_server_assign.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. vl := ms.Topo.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl, option.DiskType)
  64. var (
  65. lastErr error
  66. maxTimeout = time.Second * 10
  67. startTime = time.Now()
  68. )
  69. for time.Now().Sub(startTime) < maxTimeout {
  70. fid, count, dnList, shouldGrow, err := ms.Topo.PickForWrite(req.Count, option, vl)
  71. if shouldGrow && !vl.HasGrowRequest() {
  72. // if picked volume is almost full, trigger a volume-grow request
  73. if ms.Topo.AvailableSpaceFor(option) <= 0 {
  74. return nil, fmt.Errorf("no free volumes left for " + option.String())
  75. }
  76. vl.AddGrowRequest()
  77. ms.volumeGrowthRequestChan <- &topology.VolumeGrowRequest{
  78. Option: option,
  79. Count: int(req.WritableVolumeCount),
  80. }
  81. }
  82. if err != nil {
  83. // glog.Warningf("PickForWrite %+v: %v", req, err)
  84. lastErr = err
  85. time.Sleep(200 * time.Millisecond)
  86. continue
  87. }
  88. dn := dnList.Head()
  89. if dn == nil {
  90. continue
  91. }
  92. var replicas []*master_pb.Location
  93. for _, r := range dnList.Rest() {
  94. replicas = append(replicas, &master_pb.Location{
  95. Url: r.Url(),
  96. PublicUrl: r.PublicUrl,
  97. GrpcPort: uint32(r.GrpcPort),
  98. DataCenter: r.GetDataCenterId(),
  99. })
  100. }
  101. return &master_pb.AssignResponse{
  102. Fid: fid,
  103. Location: &master_pb.Location{
  104. Url: dn.Url(),
  105. PublicUrl: dn.PublicUrl,
  106. GrpcPort: uint32(dn.GrpcPort),
  107. DataCenter: dn.GetDataCenterId(),
  108. },
  109. Count: count,
  110. Auth: string(security.GenJwtForVolumeServer(ms.guard.SigningKey, ms.guard.ExpiresAfterSec, fid)),
  111. Replicas: replicas,
  112. }, nil
  113. }
  114. return nil, lastErr
  115. }