broker_topic_conf_read_write.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package broker
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/mq/logstore"
  6. "github.com/seaweedfs/seaweedfs/weed/mq/pub_balancer"
  7. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  9. )
  10. func (b *MessageQueueBroker) GetOrGenerateLocalPartition(t topic.Topic, partition topic.Partition) (localTopicPartition *topic.LocalPartition, getOrGenError error) {
  11. // get or generate a local partition
  12. conf, readConfErr := b.fca.ReadTopicConfFromFiler(t)
  13. if readConfErr != nil {
  14. glog.Errorf("topic %v not found: %v", t, readConfErr)
  15. return nil, fmt.Errorf("topic %v not found: %v", t, readConfErr)
  16. }
  17. localTopicPartition, _, getOrGenError = b.doGetOrGenLocalPartition(t, partition, conf)
  18. if getOrGenError != nil {
  19. glog.Errorf("topic %v partition %v not setup: %v", t, partition, getOrGenError)
  20. return nil, fmt.Errorf("topic %v partition %v not setup: %v", t, partition, getOrGenError)
  21. }
  22. return localTopicPartition, nil
  23. }
  24. func (b *MessageQueueBroker) doGetOrGenLocalPartition(t topic.Topic, partition topic.Partition, conf *mq_pb.ConfigureTopicResponse) (localPartition *topic.LocalPartition, isGenerated bool, err error) {
  25. b.accessLock.Lock()
  26. defer b.accessLock.Unlock()
  27. if localPartition = b.localTopicManager.GetLocalPartition(t, partition); localPartition == nil {
  28. localPartition, isGenerated, err = b.genLocalPartitionFromFiler(t, partition, conf)
  29. if err != nil {
  30. return nil, false, err
  31. }
  32. }
  33. return localPartition, isGenerated, nil
  34. }
  35. func (b *MessageQueueBroker) genLocalPartitionFromFiler(t topic.Topic, partition topic.Partition, conf *mq_pb.ConfigureTopicResponse) (localPartition *topic.LocalPartition, isGenerated bool, err error) {
  36. self := b.option.BrokerAddress()
  37. for _, assignment := range conf.BrokerPartitionAssignments {
  38. if assignment.LeaderBroker == string(self) && partition.Equals(topic.FromPbPartition(assignment.Partition)) {
  39. localPartition = topic.NewLocalPartition(partition, b.genLogFlushFunc(t, partition), logstore.GenMergedReadFunc(b, t, partition))
  40. b.localTopicManager.AddLocalPartition(t, localPartition)
  41. isGenerated = true
  42. break
  43. }
  44. }
  45. return localPartition, isGenerated, nil
  46. }
  47. func (b *MessageQueueBroker) ensureTopicActiveAssignments(t topic.Topic, conf *mq_pb.ConfigureTopicResponse) (err error) {
  48. // also fix assignee broker if invalid
  49. hasChanges := pub_balancer.EnsureAssignmentsToActiveBrokers(b.PubBalancer.Brokers, 1, conf.BrokerPartitionAssignments)
  50. if hasChanges {
  51. glog.V(0).Infof("topic %v partition updated assignments: %v", t, conf.BrokerPartitionAssignments)
  52. if err = b.fca.SaveTopicConfToFiler(t, conf); err != nil {
  53. return err
  54. }
  55. }
  56. return err
  57. }