broker_topic_conf_read_write.go 2.6 KB

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