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.

84 lines
3.2 KiB

12 months ago
12 months ago
12 months ago
12 months ago
  1. package broker
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/filer"
  6. "github.com/seaweedfs/seaweedfs/weed/glog"
  7. "github.com/seaweedfs/seaweedfs/weed/mq/pub_balancer"
  8. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  11. jsonpb "google.golang.org/protobuf/encoding/protojson"
  12. )
  13. func (b *MessageQueueBroker) saveTopicConfToFiler(t *mq_pb.Topic, conf *mq_pb.ConfigureTopicResponse) error {
  14. glog.V(0).Infof("save conf for topic %v to filer", t)
  15. // save the topic configuration on filer
  16. topicDir := fmt.Sprintf("%s/%s/%s", filer.TopicsDir, t.Namespace, t.Name)
  17. if err := b.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  18. var buf bytes.Buffer
  19. filer.ProtoToText(&buf, conf)
  20. return filer.SaveInsideFiler(client, topicDir, "topic.conf", buf.Bytes())
  21. }); err != nil {
  22. return fmt.Errorf("save topic to %s: %v", topicDir, err)
  23. }
  24. return nil
  25. }
  26. // readTopicConfFromFiler reads the topic configuration from filer
  27. // this should only be run in broker leader, to ensure correct active broker list.
  28. func (b *MessageQueueBroker) readTopicConfFromFiler(t topic.Topic) (conf *mq_pb.ConfigureTopicResponse, err error) {
  29. glog.V(0).Infof("load conf for topic %v from filer", t)
  30. topicDir := fmt.Sprintf("%s/%s/%s", filer.TopicsDir, t.Namespace, t.Name)
  31. if err = b.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  32. data, err := filer.ReadInsideFiler(client, topicDir, "topic.conf")
  33. if err != nil {
  34. return fmt.Errorf("read topic %v partition %v conf: %v", t, err)
  35. }
  36. // parse into filer conf object
  37. conf = &mq_pb.ConfigureTopicResponse{}
  38. if err = jsonpb.Unmarshal(data, conf); err != nil {
  39. return fmt.Errorf("unmarshal topic %v conf: %v", t, err)
  40. }
  41. return nil
  42. }); err != nil {
  43. return nil, err
  44. }
  45. return conf, nil
  46. }
  47. func (b *MessageQueueBroker) genLocalPartitionFromFiler(t topic.Topic, partition topic.Partition) (localPartition *topic.LocalPartition, err error) {
  48. self := b.option.BrokerAddress()
  49. conf, err := b.readTopicConfFromFiler(t)
  50. if err != nil {
  51. return nil, err
  52. }
  53. for _, assignment := range conf.BrokerPartitionAssignments {
  54. if assignment.LeaderBroker == string(self) && partition.Equals(topic.FromPbPartition(assignment.Partition)) {
  55. localPartition = topic.FromPbBrokerPartitionAssignment(b.option.BrokerAddress(), partition, assignment, b.genLogFlushFunc(t, assignment.Partition), b.genLogOnDiskReadFunc(t, assignment.Partition))
  56. b.localTopicManager.AddTopicPartition(t, localPartition)
  57. break
  58. }
  59. }
  60. return localPartition, nil
  61. }
  62. func (b *MessageQueueBroker) ensureTopicActiveAssignments(t topic.Topic, conf *mq_pb.ConfigureTopicResponse) (err error) {
  63. // also fix assignee broker if invalid
  64. addedAssignments, updatedAssignments := pub_balancer.EnsureAssignmentsToActiveBrokers(b.Balancer.Brokers, conf.BrokerPartitionAssignments)
  65. if len(addedAssignments) > 0 || len(updatedAssignments) > 0 {
  66. glog.V(0).Infof("topic %v partition assignments added: %v updated: %v", t, addedAssignments, updatedAssignments)
  67. if err = b.saveTopicConfToFiler(t.ToPbTopic(), conf); err != nil {
  68. return err
  69. }
  70. }
  71. return err
  72. }