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.

100 lines
2.3 KiB

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. go func() {
  28. for update := range updatesChan {
  29. if err := stream.Send(&messaging_pb.PublishResponse{
  30. Config: &messaging_pb.PublishResponse_ConfigMessage{
  31. PartitionCount: update,
  32. },
  33. }); err != nil {
  34. glog.V(0).Infof("err sending publish response: %v", err)
  35. return
  36. }
  37. }
  38. }()
  39. logBuffer := log_buffer.NewLogBuffer(time.Minute, func(startTime, stopTime time.Time, buf []byte) {
  40. //targetFile :=
  41. fmt.Sprintf("%s/%s/%s/%04d-%02d-%02d/%02d-%02d.part%02d",
  42. filer2.TopicsDir, namespace, topic,
  43. startTime.Year(), startTime.Month(), startTime.Day(), startTime.Hour(), startTime.Minute(),
  44. partition,
  45. )
  46. /*
  47. if err := f.appendToFile(targetFile, buf); err != nil {
  48. glog.V(0).Infof("log write failed %s: %v", targetFile, err)
  49. }
  50. */
  51. }, func() {
  52. // notify subscribers
  53. })
  54. for {
  55. in, err := stream.Recv()
  56. if err == io.EOF {
  57. return nil
  58. }
  59. if err != nil {
  60. return err
  61. }
  62. m := &messaging_pb.Message{
  63. Timestamp: time.Now().UnixNano(),
  64. Key: in.Data.Key,
  65. Value: in.Data.Value,
  66. Headers: in.Data.Headers,
  67. }
  68. data, err := proto.Marshal(m)
  69. if err != nil {
  70. glog.Errorf("marshall error: %v\n", err)
  71. continue
  72. }
  73. logBuffer.AddToBuffer(in.Data.Key, data)
  74. }
  75. }
  76. func (broker *MessageBroker) ConfigureTopic(c context.Context, request *messaging_pb.ConfigureTopicRequest) (*messaging_pb.ConfigureTopicResponse, error) {
  77. panic("implement me")
  78. }
  79. func (broker *MessageBroker) GetTopicConfiguration(c context.Context, request *messaging_pb.GetTopicConfigurationRequest) (*messaging_pb.GetTopicConfigurationResponse, error) {
  80. panic("implement me")
  81. }