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.

90 lines
1.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package broker
  2. import (
  3. "io"
  4. "github.com/golang/protobuf/proto"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
  7. )
  8. func (broker *MessageBroker) Publish(stream messaging_pb.SeaweedMessaging_PublishServer) error {
  9. // process initial request
  10. in, err := stream.Recv()
  11. if err == io.EOF {
  12. return nil
  13. }
  14. if err != nil {
  15. return err
  16. }
  17. // TODO look it up
  18. topicConfig := &messaging_pb.TopicConfiguration{
  19. IsTransient: true,
  20. }
  21. // send init response
  22. initResponse := &messaging_pb.PublishResponse{
  23. Config: nil,
  24. Redirect: nil,
  25. }
  26. err = stream.Send(initResponse)
  27. if err != nil {
  28. return err
  29. }
  30. if initResponse.Redirect != nil {
  31. return nil
  32. }
  33. // get lock
  34. tp := TopicPartition{
  35. Namespace: in.Init.Namespace,
  36. Topic: in.Init.Topic,
  37. Partition: in.Init.Partition,
  38. }
  39. tl := broker.topicLocks.RequestLock(tp, topicConfig, true)
  40. defer broker.topicLocks.ReleaseLock(tp, true)
  41. updatesChan := make(chan int32)
  42. go func() {
  43. for update := range updatesChan {
  44. if err := stream.Send(&messaging_pb.PublishResponse{
  45. Config: &messaging_pb.PublishResponse_ConfigMessage{
  46. PartitionCount: update,
  47. },
  48. }); err != nil {
  49. glog.V(0).Infof("err sending publish response: %v", err)
  50. return
  51. }
  52. }
  53. }()
  54. // process each message
  55. for {
  56. in, err := stream.Recv()
  57. if err == io.EOF {
  58. return nil
  59. }
  60. if err != nil {
  61. return err
  62. }
  63. if in.Data == nil {
  64. continue
  65. }
  66. // fmt.Printf("received: %d : %s\n", len(in.Data.Value), string(in.Data.Value))
  67. data, err := proto.Marshal(in.Data)
  68. if err != nil {
  69. glog.Errorf("marshall error: %v\n", err)
  70. continue
  71. }
  72. tl.logBuffer.AddToBuffer(in.Data.Key, data)
  73. }
  74. }