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

sink(ticdc): return invalid topic expression in ErrKafkaInvalidTopicExpression (#11161) #11611

Open
wants to merge 2 commits into
base: release-7.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions cdc/sink/dmlsink/mq/dispatcher/topic/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ func (e Expression) Validate() error {
return nil
}

return errors.ErrKafkaInvalidTopicExpression.GenWithStackByArgs()
return errors.ErrKafkaInvalidTopicExpression.GenWithStackByArgs(e)
}

// ValidateForAvro checks whether topic pattern is {schema}_{table}, the only allowed
func (e Expression) ValidateForAvro() error {
if ok := avroTopicNameRE.MatchString(string(e)); !ok {
return errors.ErrKafkaInvalidTopicExpression.GenWithStackByArgs(
return errors.ErrKafkaInvalidTopicExpression.GenWithStackByArgs(e,
"topic rule for Avro must contain {schema} and {table}",
)
}
Expand Down
23 changes: 23 additions & 0 deletions cdc/sink/dmlsink/mq/dispatcher/topic/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ import (
"fmt"
"testing"

"github.com/pingcap/tiflow/pkg/errors"
"github.com/stretchr/testify/require"
)

func TestSubstituteTopicExpression(t *testing.T) {
t.Parallel()

cases := []struct {
name string
expression string
Expand Down Expand Up @@ -228,6 +231,8 @@ func TestSubstituteTopicExpression(t *testing.T) {
}

func TestSchemaOptional(t *testing.T) {
t.Parallel()

expression := "prefix_{table}"
topicExpr := Expression(expression)
err := topicExpr.Validate()
Expand All @@ -240,6 +245,8 @@ func TestSchemaOptional(t *testing.T) {
}

func TestTableOptional(t *testing.T) {
t.Parallel()

expression := "prefix_{schema}"
topicExpr := Expression(expression)
err := topicExpr.Validate()
Expand All @@ -251,6 +258,22 @@ func TestTableOptional(t *testing.T) {
require.Equal(t, topicName, "prefix_test")
}

func TestInvalidExpression(t *testing.T) {
t.Parallel()

invalidExpr := "%invalid{schema}"
topicExpr := Expression(invalidExpr)

err := topicExpr.Validate()
require.ErrorIs(t, err, errors.ErrKafkaInvalidTopicExpression)
require.ErrorContains(t, err, invalidExpr)

err = topicExpr.ValidateForAvro()
require.ErrorIs(t, err, errors.ErrKafkaInvalidTopicExpression)
require.ErrorContains(t, err, "Avro")
require.ErrorContains(t, err, invalidExpr)
}

// cmd: go test -run='^$' -bench '^(BenchmarkSubstitute)$' github.com/pingcap/tiflow/cdc/sink/dispatcher/topic
// goos: linux
// goarch: amd64
Expand Down
2 changes: 1 addition & 1 deletion errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ kafka send message failed

["CDC:ErrKafkaTopicExprInvalid"]
error = '''
invalid topic expression
invalid topic expression: %s
'''

["CDC:ErrLeaseExpired"]
Expand Down
2 changes: 1 addition & 1 deletion pkg/errors/cdc_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ var (
errors.RFCCodeText("CDC:ErrKafkaCreateTopic"),
)
ErrKafkaInvalidTopicExpression = errors.Normalize(
"invalid topic expression",
"invalid topic expression: %s ",
errors.RFCCodeText("CDC:ErrKafkaTopicExprInvalid"),
)
ErrKafkaConfigNotFound = errors.Normalize(
Expand Down
Loading