-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from Trendyol/feature/verify-topic
feat: add verify topic feature
- Loading branch information
Showing
7 changed files
with
205 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,6 +141,7 @@ func StartAPI(cfg kafka.Config, metricCollectors ...prometheus.Collector) { | |
| `consumer.groupId` | Exception consumer group id | | exception-consumer-group | | ||
| `consumer.maxRetry` | Maximum retry value for attempting to retry a message | 3 | | | ||
| `consumer.concurrency` | Number of goroutines used at listeners | 1 | | | ||
| `consumer.verifyTopicOnStartup` | it checks existence of the given retry topic on the kafka cluster. | false | | | ||
| `consumer.minBytes` | [see doc](https://pkg.go.dev/github.com/segmentio/[email protected]#ReaderConfig.MinBytes) | 1 | | | ||
| `consumer.maxBytes` | [see doc](https://pkg.go.dev/github.com/segmentio/[email protected]#ReaderConfig.MaxBytes) | 1 MB | | | ||
| `consumer.maxWait` | [see doc](https://pkg.go.dev/github.com/segmentio/[email protected]#ReaderConfig.MaxWait) | 10s | | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/Trendyol/kafka-cronsumer/pkg/kafka" | ||
segmentio "github.com/segmentio/kafka-go" | ||
) | ||
|
||
type kafkaClient interface { | ||
Metadata(ctx context.Context, req *segmentio.MetadataRequest) (*segmentio.MetadataResponse, error) | ||
GetClient() *segmentio.Client | ||
} | ||
|
||
type client struct { | ||
*segmentio.Client | ||
} | ||
|
||
func NewKafkaClient(cfg *kafka.Config) (kafkaClient, error) { | ||
kc := client{ | ||
Client: &segmentio.Client{ | ||
Addr: segmentio.TCP(cfg.Brokers...), | ||
}, | ||
} | ||
|
||
transport := &segmentio.Transport{ | ||
MetadataTopics: []string{cfg.Consumer.Topic}, | ||
} | ||
|
||
if cfg.SASL.Enabled { | ||
transport.TLS = NewTLSConfig(cfg.SASL) | ||
transport.SASL = Mechanism(cfg.SASL) | ||
} | ||
|
||
kc.Transport = transport | ||
return &kc, nil | ||
} | ||
|
||
func (c *client) GetClient() *segmentio.Client { | ||
return c.Client | ||
} | ||
|
||
func VerifyTopics(client kafkaClient, topics ...string) (bool, error) { | ||
metadata, err := client.Metadata(context.Background(), &segmentio.MetadataRequest{ | ||
Topics: topics, | ||
}) | ||
if err != nil { | ||
return false, fmt.Errorf("error when during verifyTopics metadata request %w", err) | ||
} | ||
return checkTopicsWithinMetadata(metadata, topics) | ||
} | ||
|
||
func checkTopicsWithinMetadata(metadata *segmentio.MetadataResponse, topics []string) (bool, error) { | ||
metadataTopics := make(map[string]struct{}, len(metadata.Topics)) | ||
for _, topic := range metadata.Topics { | ||
if topic.Error != nil { | ||
continue | ||
} | ||
metadataTopics[topic.Name] = struct{}{} | ||
} | ||
|
||
for _, topic := range topics { | ||
if _, exist := metadataTopics[topic]; !exist { | ||
return false, nil | ||
} | ||
} | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/Trendyol/kafka-cronsumer/pkg/kafka" | ||
segmentio "github.com/segmentio/kafka-go" | ||
) | ||
|
||
type mockKafkaClientWrapper struct { | ||
wantErr bool | ||
wantExistTopic bool | ||
} | ||
|
||
func (m mockKafkaClientWrapper) GetClient() *segmentio.Client { | ||
return &segmentio.Client{} | ||
} | ||
|
||
func (m mockKafkaClientWrapper) Metadata(_ context.Context, _ *segmentio.MetadataRequest) (*segmentio.MetadataResponse, error) { | ||
if m.wantErr { | ||
return nil, errors.New("metadataReqErr") | ||
} | ||
|
||
if !m.wantExistTopic { | ||
return &segmentio.MetadataResponse{ | ||
Topics: []segmentio.Topic{ | ||
{Name: "topic1", Error: segmentio.UnknownTopicOrPartition}, | ||
{Name: "topic2", Error: nil}, | ||
}, | ||
}, nil | ||
} | ||
|
||
return &segmentio.MetadataResponse{ | ||
Topics: []segmentio.Topic{ | ||
{Name: "topic1", Error: nil}, | ||
{Name: "topic2", Error: nil}, | ||
}, | ||
}, nil | ||
} | ||
|
||
func Test_kafkaClientWrapper_VerifyTopics(t *testing.T) { | ||
t.Run("Should_Return_Error_When_Metadata_Request_Has_Failed", func(t *testing.T) { | ||
// Given | ||
mockClient := mockKafkaClientWrapper{wantErr: true} | ||
|
||
// When | ||
_, err := VerifyTopics(mockClient, "topic1") | ||
|
||
// Then | ||
if err == nil { | ||
t.Error("metadata request must be failed!") | ||
} | ||
}) | ||
t.Run("Should_Return_False_When_Given_Topic_Does_Not_Exist", func(t *testing.T) { | ||
// Given | ||
mockClient := mockKafkaClientWrapper{wantExistTopic: false} | ||
|
||
// When | ||
exist, err := VerifyTopics(mockClient, "topic1") | ||
|
||
// Then | ||
if exist { | ||
t.Errorf("topic %s must not exist", "topic1") | ||
} | ||
if err != nil { | ||
t.Error("err must be nil") | ||
} | ||
}) | ||
t.Run("Should_Return_True_When_Given_Topic_Exist", func(t *testing.T) { | ||
// Given | ||
mockClient := mockKafkaClientWrapper{wantExistTopic: true} | ||
|
||
// When | ||
exist, err := VerifyTopics(mockClient, "topic1") | ||
|
||
// Then | ||
if !exist { | ||
t.Errorf("topic %s must exist", "topic1") | ||
} | ||
if err != nil { | ||
t.Error("err must be nil") | ||
} | ||
}) | ||
} | ||
|
||
func Test_newKafkaClient(t *testing.T) { | ||
// Given | ||
cfg := &kafka.Config{Brokers: []string{"127.0.0.1:9092"}, Consumer: kafka.ConsumerConfig{Topic: "topic"}} | ||
|
||
// When | ||
sut, err := NewKafkaClient(cfg) | ||
|
||
// Then | ||
if sut.GetClient().Addr.String() != "127.0.0.1:9092" { | ||
t.Errorf("broker address must be 127.0.0.1:9092") | ||
} | ||
if err != nil { | ||
t.Errorf("err must be nil") | ||
} | ||
} | ||
|
||
func Test_kClient_GetClient(t *testing.T) { | ||
// Given | ||
mockClient := mockKafkaClientWrapper{} | ||
|
||
// When | ||
sut := mockClient.GetClient() | ||
|
||
// Then | ||
if sut == nil { | ||
t.Error("client must not be nil") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters