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.

89 lines
2.3 KiB

2 years ago
2 years ago
  1. package broker
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/golang/protobuf/jsonpb"
  6. "github.com/seaweedfs/seaweedfs/weed/filer"
  7. "github.com/seaweedfs/seaweedfs/weed/mq"
  8. "github.com/seaweedfs/seaweedfs/weed/pb"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  11. "time"
  12. )
  13. func (broker *MessageQueueBroker) checkSegmentOnFiler(segment *mq.Segment) (brokers []pb.ServerAddress, err error) {
  14. info, found, err := broker.readSegmentOnFiler(segment)
  15. if err != nil {
  16. return
  17. }
  18. if !found {
  19. return
  20. }
  21. for _, b := range info.Brokers {
  22. brokers = append(brokers, pb.ServerAddress(b))
  23. }
  24. return
  25. }
  26. func (broker *MessageQueueBroker) saveSegmentBrokersOnFiler(segment *mq.Segment, brokers []pb.ServerAddress) (err error) {
  27. var nodes []string
  28. for _, b := range brokers {
  29. nodes = append(nodes, string(b))
  30. }
  31. broker.saveSegmentToFiler(segment, &mq_pb.SegmentInfo{
  32. Segment: segment.ToPbSegment(),
  33. StartTsNs: time.Now().UnixNano(),
  34. Brokers: nodes,
  35. StopTsNs: 0,
  36. PreviousSegments: nil,
  37. NextSegments: nil,
  38. })
  39. return
  40. }
  41. func (broker *MessageQueueBroker) readSegmentOnFiler(segment *mq.Segment) (info *mq_pb.SegmentInfo, found bool, err error) {
  42. dir, name := segment.DirAndName()
  43. found, err = filer_pb.Exists(broker, dir, name, false)
  44. if !found || err != nil {
  45. return
  46. }
  47. err = pb.WithFilerClient(false, broker.GetFiler(), broker.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  48. // read filer conf first
  49. data, err := filer.ReadInsideFiler(client, dir, name)
  50. if err != nil {
  51. return fmt.Errorf("ReadEntry: %v", err)
  52. }
  53. // parse into filer conf object
  54. info = &mq_pb.SegmentInfo{}
  55. if err = jsonpb.Unmarshal(bytes.NewReader(data), info); err != nil {
  56. return err
  57. }
  58. found = true
  59. return nil
  60. })
  61. return
  62. }
  63. func (broker *MessageQueueBroker) saveSegmentToFiler(segment *mq.Segment, info *mq_pb.SegmentInfo) (err error) {
  64. dir, name := segment.DirAndName()
  65. var buf bytes.Buffer
  66. filer.ProtoToText(&buf, info)
  67. err = pb.WithFilerClient(false, broker.GetFiler(), broker.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  68. // read filer conf first
  69. err := filer.SaveInsideFiler(client, dir, name, buf.Bytes())
  70. if err != nil {
  71. return fmt.Errorf("save segment info: %v", err)
  72. }
  73. return nil
  74. })
  75. return
  76. }