Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kafka(ticdc): simplify the topic manager and fix store incorrect partition in the cache #9773

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cdc/sink/ddlsink/mq/kafka_ddl_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func NewKafkaDDLSink(
topic,
options.DeriveTopicConfig(),
adminClient,
tiflowutil.RoleOwner,
)
if err != nil {
return nil, errors.Trace(err)
Expand Down
3 changes: 0 additions & 3 deletions cdc/sink/ddlsink/mq/mq_ddl_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,6 @@ func (k *DDLSink) Close() {
if k.producer != nil {
k.producer.Close()
}
if k.topicManager != nil {
k.topicManager.Close()
}
if k.admin != nil {
k.admin.Close()
}
Expand Down
1 change: 1 addition & 0 deletions cdc/sink/dmlsink/mq/kafka_dml_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func NewKafkaDMLSink(
topic,
options.DeriveTopicConfig(),
adminClient,
tiflowutil.RoleProcessor,
)
if err != nil {
return nil, errors.Trace(err)
Expand Down
118 changes: 23 additions & 95 deletions cdc/sink/dmlsink/mq/manager/kafka_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,51 +25,36 @@ import (
cerror "github.com/pingcap/tiflow/pkg/errors"
"github.com/pingcap/tiflow/pkg/retry"
"github.com/pingcap/tiflow/pkg/sink/kafka"
"github.com/pingcap/tiflow/pkg/util"
"go.uber.org/zap"
)

const (
// metaRefreshInterval is the interval of refreshing metadata.
// We can't get the metadata too frequently, because it may cause
// the kafka cluster to be overloaded. Especially when there are
// many topics in the cluster or there are many TiCDC changefeeds.
metaRefreshInterval = 10 * time.Minute
)

// kafkaTopicManager is a manager for kafka topics.
type kafkaTopicManager struct {
changefeedID model.ChangeFeedID
role util.Role

admin kafka.ClusterAdminClient

cfg *kafka.AutoCreateTopicConfig

topics sync.Map

metaRefreshTicker *time.Ticker

// cancel is used to cancel the background goroutine.
cancel context.CancelFunc
}

// NewKafkaTopicManager creates a new topic manager.
func NewKafkaTopicManager(
ctx context.Context,
changefeedID model.ChangeFeedID,
admin kafka.ClusterAdminClient,
cfg *kafka.AutoCreateTopicConfig,
role util.Role,
) *kafkaTopicManager {
mgr := &kafkaTopicManager{
changefeedID: changefeedID,
admin: admin,
cfg: cfg,
metaRefreshTicker: time.NewTicker(metaRefreshInterval),
changefeedID: changefeedID,
role: role,
admin: admin,
cfg: cfg,
}

ctx, mgr.cancel = context.WithCancel(ctx)
// Background refresh metadata.
go mgr.backgroundRefreshMeta(ctx)

return mgr
}

Expand All @@ -92,26 +77,6 @@ func (m *kafkaTopicManager) GetPartitionNum(
return partitionNum, nil
}

func (m *kafkaTopicManager) backgroundRefreshMeta(ctx context.Context) {
for {
select {
case <-ctx.Done():
log.Info("Background refresh Kafka metadata goroutine exit.",
zap.String("namespace", m.changefeedID.Namespace),
zap.String("changefeed", m.changefeedID.ID),
)
return
case <-m.metaRefreshTicker.C:
// We ignore the error here, because the error may be caused by the
// network problem, and we can try to get the metadata next time.
topicPartitionNums, _ := m.fetchAllTopicsPartitionsNum(ctx)
for topic, partitionNum := range topicPartitionNums {
m.tryUpdatePartitionsAndLogging(topic, partitionNum)
}
}
}
}

// tryUpdatePartitionsAndLogging try to update the partitions of the topic.
func (m *kafkaTopicManager) tryUpdatePartitionsAndLogging(topic string, partitions int32) {
oldPartitions, ok := m.topics.Load(topic)
Expand Down Expand Up @@ -139,41 +104,6 @@ func (m *kafkaTopicManager) tryUpdatePartitionsAndLogging(topic string, partitio
}
}

// fetchAllTopicsPartitionsNum fetches all topics' partitions number.
// The error returned by this method could be a transient error that is fixable by the underlying logic.
// When handling this error, please be cautious.
// If you simply throw the error to the caller, it may impact the robustness of your program.
func (m *kafkaTopicManager) fetchAllTopicsPartitionsNum(
ctx context.Context,
) (map[string]int32, error) {
var topics []string
m.topics.Range(func(key, value any) bool {
topics = append(topics, key.(string))
return true
})

start := time.Now()
numPartitions, err := m.admin.GetTopicsPartitionsNum(ctx, topics)
if err != nil {
log.Warn(
"Kafka admin client describe topics failed",
zap.String("namespace", m.changefeedID.Namespace),
zap.String("changefeed", m.changefeedID.ID),
zap.Error(err),
zap.Duration("duration", time.Since(start)),
)
return nil, err
}

log.Info(
"Kafka admin client describe topics success",
zap.String("namespace", m.changefeedID.Namespace),
zap.String("changefeed", m.changefeedID.ID),
zap.Duration("duration", time.Since(start)))

return numPartitions, nil
}

// waitUntilTopicVisible is called after CreateTopic to make sure the topic
// can be safely written to. The reason is that it may take several seconds after
// CreateTopic returns success for all the brokers to become aware that the
Expand Down Expand Up @@ -254,41 +184,39 @@ func (m *kafkaTopicManager) createTopic(
zap.Int16("replicationFactor", m.cfg.ReplicationFactor),
zap.Duration("duration", time.Since(start)),
)
m.tryUpdatePartitionsAndLogging(topicName, m.cfg.PartitionNum)

return m.cfg.PartitionNum, nil
}

// CreateTopicAndWaitUntilVisible wraps createTopic and waitUntilTopicVisible together.
func (m *kafkaTopicManager) CreateTopicAndWaitUntilVisible(
ctx context.Context, topicName string,
) (int32, error) {
var (
partitionNum int32
err error
)
// If the topic is not in the cache, we try to get the metadata of the topic.
// ignoreTopicErr is set to true to ignore the error if the topic is not found,
// which means we should create the topic later.
topicDetails, err := m.admin.GetTopicsMeta(ctx, []string{topicName}, true)
if err != nil {
return 0, errors.Trace(err)
}
if detail, ok := topicDetails[topicName]; ok {
m.tryUpdatePartitionsAndLogging(topicName, detail.NumPartitions)
return detail.NumPartitions, nil
}

partitionNum, err := m.createTopic(ctx, topicName)
if err != nil {
return 0, errors.Trace(err)
}
details, ok := topicDetails[topicName]
partitionNum = details.NumPartitions
if !ok {
partitionNum, err = m.createTopic(ctx, topicName)
if err != nil {
return 0, errors.Trace(err)
}

err = m.waitUntilTopicVisible(ctx, topicName)
if err != nil {
return 0, errors.Trace(err)
err = m.waitUntilTopicVisible(ctx, topicName)
if err != nil {
return 0, errors.Trace(err)
}
}

m.tryUpdatePartitionsAndLogging(topicName, partitionNum)
return partitionNum, nil
}

// Close exits the background goroutine.
func (m *kafkaTopicManager) Close() {
m.cancel()
}
18 changes: 8 additions & 10 deletions cdc/sink/dmlsink/mq/manager/kafka_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/pingcap/tiflow/cdc/model"
"github.com/pingcap/tiflow/pkg/sink/kafka"
"github.com/pingcap/tiflow/pkg/util"
"github.com/stretchr/testify/require"
)

Expand All @@ -34,8 +35,7 @@ func TestPartitions(t *testing.T) {
}

ctx := context.Background()
manager := NewKafkaTopicManager(ctx, model.DefaultChangeFeedID("test"), adminClient, cfg)
defer manager.Close()
manager := NewKafkaTopicManager(model.DefaultChangeFeedID("test"), adminClient, cfg, util.RoleTester)

partitionsNum, err := manager.GetPartitionNum(
ctx,
Expand All @@ -56,8 +56,8 @@ func TestCreateTopic(t *testing.T) {
}

ctx := context.Background()
manager := NewKafkaTopicManager(ctx, model.DefaultChangeFeedID("test"), adminClient, cfg)
defer manager.Close()
manager := NewKafkaTopicManager(model.DefaultChangeFeedID("test"), adminClient, cfg, util.RoleTester)

partitionNum, err := manager.CreateTopicAndWaitUntilVisible(ctx, kafka.DefaultMockTopicName)
require.Nil(t, err)
require.Equal(t, int32(3), partitionNum)
Expand All @@ -71,8 +71,7 @@ func TestCreateTopic(t *testing.T) {

// Try to create a topic without auto create.
cfg.AutoCreate = false
manager = NewKafkaTopicManager(ctx, model.DefaultChangeFeedID("test"), adminClient, cfg)
defer manager.Close()
manager = NewKafkaTopicManager(model.DefaultChangeFeedID("test"), adminClient, cfg, util.RoleTester)
_, err = manager.CreateTopicAndWaitUntilVisible(ctx, "new-topic2")
require.Regexp(
t,
Expand All @@ -87,8 +86,8 @@ func TestCreateTopic(t *testing.T) {
PartitionNum: 2,
ReplicationFactor: 4,
}
manager = NewKafkaTopicManager(ctx, model.DefaultChangeFeedID("test"), adminClient, cfg)
defer manager.Close()

manager = NewKafkaTopicManager(model.DefaultChangeFeedID("test"), adminClient, cfg, util.RoleTester)
_, err = manager.CreateTopicAndWaitUntilVisible(ctx, "new-topic-failed")
require.Regexp(
t,
Expand All @@ -109,8 +108,7 @@ func TestCreateTopicWithDelay(t *testing.T) {
}

ctx := context.Background()
manager := NewKafkaTopicManager(ctx, model.DefaultChangeFeedID("test"), adminClient, cfg)
defer manager.Close()
manager := NewKafkaTopicManager(model.DefaultChangeFeedID("test"), adminClient, cfg, util.RoleTester)
partitionNum, err := manager.createTopic(ctx, "new_topic")
require.Nil(t, err)
err = adminClient.SetRemainingFetchesUntilTopicVisible("new_topic", 3)
Expand Down
2 changes: 0 additions & 2 deletions cdc/sink/dmlsink/mq/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,4 @@ type TopicManager interface {
GetPartitionNum(ctx context.Context, topic string) (int32, error)
// CreateTopicAndWaitUntilVisible creates the topic and wait for the topic completion.
CreateTopicAndWaitUntilVisible(ctx context.Context, topicName string) (int32, error)
// Close closes the topic manager.
Close()
}
4 changes: 0 additions & 4 deletions cdc/sink/dmlsink/mq/manager/pulsar_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ func (m *pulsarTopicManager) CreateTopicAndWaitUntilVisible(ctx context.Context,
return 0, nil
}

// Close
func (m *pulsarTopicManager) Close() {
}

// str2Pointer returns the pointer of the string.
func str2Pointer(str string) *string {
return &str
Expand Down
6 changes: 0 additions & 6 deletions cdc/sink/dmlsink/mq/mq_dml_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,6 @@ func (s *dmlSink) Close() {
}
s.wg.Wait()

s.alive.RLock()
if s.alive.topicManager != nil {
s.alive.topicManager.Close()
}
s.alive.RUnlock()

if s.adminClient != nil {
s.adminClient.Close()
}
Expand Down
6 changes: 3 additions & 3 deletions cdc/sink/util/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
cerror "github.com/pingcap/tiflow/pkg/errors"
"github.com/pingcap/tiflow/pkg/sink/codec/common"
"github.com/pingcap/tiflow/pkg/sink/kafka"
"github.com/pingcap/tiflow/pkg/util"
)

// GetTopic returns the topic name from the sink URI.
Expand Down Expand Up @@ -95,10 +96,9 @@ func GetTopicManagerAndTryCreateTopic(
topic string,
topicCfg *kafka.AutoCreateTopicConfig,
adminClient kafka.ClusterAdminClient,
role util.Role,
) (manager.TopicManager, error) {
topicManager := manager.NewKafkaTopicManager(
ctx, changefeedID, adminClient, topicCfg,
)
topicManager := manager.NewKafkaTopicManager(changefeedID, adminClient, topicCfg, role)

if _, err := topicManager.CreateTopicAndWaitUntilVisible(ctx, topic); err != nil {
return nil, cerror.WrapError(cerror.ErrKafkaCreateTopic, err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/sink/kafka/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,9 @@ func (a *saramaAdminClient) Close() {
zap.String("namespace", a.changefeed.Namespace),
zap.String("changefeed", a.changefeed.ID),
zap.Error(err))
return
}
log.Info("sarama admin client closed",
zap.String("namespace", a.changefeed.Namespace),
zap.String("changefeed", a.changefeed.ID))
}
Loading