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.

103 lines
2.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package messaging
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "time"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/chrislusf/seaweedfs/weed/filer2"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
  11. "github.com/chrislusf/seaweedfs/weed/util/log_buffer"
  12. )
  13. func (broker *MessageBroker) Subscribe(server messaging_pb.SeaweedMessaging_SubscribeServer) error {
  14. panic("implement me")
  15. }
  16. func (broker *MessageBroker) Publish(stream messaging_pb.SeaweedMessaging_PublishServer) error {
  17. // process initial request
  18. in, err := stream.Recv()
  19. if err == io.EOF {
  20. return nil
  21. }
  22. if err != nil {
  23. return err
  24. }
  25. namespace, topic, partition := in.Init.Namespace, in.Init.Topic, in.Init.Partition
  26. updatesChan := make(chan int32)
  27. // TODO look it up
  28. topicConfig := &messaging_pb.TopicConfiguration{
  29. }
  30. go func() {
  31. for update := range updatesChan {
  32. if err := stream.Send(&messaging_pb.PublishResponse{
  33. Config: &messaging_pb.PublishResponse_ConfigMessage{
  34. PartitionCount: update,
  35. },
  36. }); err != nil {
  37. glog.V(0).Infof("err sending publish response: %v", err)
  38. return
  39. }
  40. }
  41. }()
  42. logBuffer := log_buffer.NewLogBuffer(time.Minute, func(startTime, stopTime time.Time, buf []byte) {
  43. targetFile := fmt.Sprintf(
  44. "%s/%s/%s/%04d-%02d-%02d/%02d-%02d.part%02d",
  45. filer2.TopicsDir, namespace, topic,
  46. startTime.Year(), startTime.Month(), startTime.Day(), startTime.Hour(), startTime.Minute(),
  47. partition,
  48. )
  49. if err := broker.appendToFile(targetFile, topicConfig, buf); err != nil {
  50. glog.V(0).Infof("log write failed %s: %v", targetFile, err)
  51. }
  52. }, func() {
  53. // notify subscribers
  54. })
  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. m := &messaging_pb.Message{
  64. Timestamp: time.Now().UnixNano(),
  65. Key: in.Data.Key,
  66. Value: in.Data.Value,
  67. Headers: in.Data.Headers,
  68. }
  69. data, err := proto.Marshal(m)
  70. if err != nil {
  71. glog.Errorf("marshall error: %v\n", err)
  72. continue
  73. }
  74. logBuffer.AddToBuffer(in.Data.Key, data)
  75. }
  76. }
  77. func (broker *MessageBroker) ConfigureTopic(c context.Context, request *messaging_pb.ConfigureTopicRequest) (*messaging_pb.ConfigureTopicResponse, error) {
  78. panic("implement me")
  79. }
  80. func (broker *MessageBroker) GetTopicConfiguration(c context.Context, request *messaging_pb.GetTopicConfigurationRequest) (*messaging_pb.GetTopicConfigurationResponse, error) {
  81. panic("implement me")
  82. }