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

fix(gateway): wait for kafka topic creation #5359

Merged
merged 7 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion scheduler/pkg/kafka/config/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func setupSASLSSLAuthentication(config kafka.ConfigMap) error {

func withPasswordAuth(mechanism string, config kafka.ConfigMap) error {
// Set the SASL mechanism
config["security.protocol"] = "SASL_SSL"
config["security.protocol"] = tls.SecurityProtocolSASLSSL
config["sasl.mechanism"] = mechanism

// Set the SASL username and password
Expand Down
8 changes: 8 additions & 0 deletions scheduler/pkg/kafka/gateway/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ the Change License after the Change Date as each is defined in accordance with t

package gateway

import "time"

const (
HeaderKeyType = "seldon-infer-type"
HeaderValueJsonReq = "json/inferModelRequest"
HeaderValueJsonRes = "json/inferModelResponse"
HeaderValueProtoReq = "proto/InferModelRequest"
HeaderValueProtoRes = "proto/InferModelResponse"

// Topic creation retries
TopicCreateTimeout = time.Minute
TopicDescribeTimeout = time.Second
TopicDescribeMaxRetries = 60
TopicDescribeRetryDelay = time.Second
)
62 changes: 55 additions & 7 deletions scheduler/pkg/kafka/gateway/infer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"sync/atomic"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/confluentinc/confluent-kafka-go/v2/kafka"
"github.com/signalfx/splunk-otel-go/instrumentation/github.com/confluentinc/confluent-kafka-go/v2/kafka/splunkkafka"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -194,7 +195,10 @@ func (kc *InferKafkaHandler) GetNumModels() int {
func (kc *InferKafkaHandler) createTopics(topicNames []string) error {
logger := kc.logger.WithField("func", "createTopics")
if kc.adminClient == nil {
logger.Warnf("Can't create topics %v as no admin client", topicNames)
logger.Warnf("no kafka admin client, can't create any of the following topics: %v", topicNames)
lc525 marked this conversation as resolved.
Show resolved Hide resolved
// An error would typically be returned here, but a missing adminClient typically
// indicates we're running tests. Instead of failing tests, we return nil here.
// TODO: find a better way of mocking kafka
return nil
}
t1 := time.Now()
Expand All @@ -210,18 +214,62 @@ func (kc *InferKafkaHandler) createTopics(topicNames []string) error {
results, err := kc.adminClient.CreateTopics(
context.Background(),
topicSpecs,
kafka.SetAdminOperationTimeout(time.Minute),
kafka.SetAdminOperationTimeout(TopicCreateTimeout),
)
if err != nil {
return err
}

// Wait for topic creation
logFailure := func(err error, delay time.Duration) {
logger.WithError(err).Errorf("still waiting for all topics to be created...")
}

logger.Infof("waiting for kafka topic creation")
retryPolicy := backoff.WithMaxRetries(
backoff.NewConstantBackOff(TopicDescribeRetryDelay),
TopicDescribeMaxRetries,
)
err = backoff.RetryNotify(
func() error {
return kc.ensureTopicsExist(topicNames)
lc525 marked this conversation as resolved.
Show resolved Hide resolved
},
retryPolicy,
logFailure)

if err != nil {
logger.WithError(err).Errorf("some topics not created, giving up")
return err
} else {
logger.Infof("all topics created")
}

for _, result := range results {
logger.Debugf("Topic result for %s", result.String())
logger.Debugf("topic result for %s", result.String())
lc525 marked this conversation as resolved.
Show resolved Hide resolved
}

t2 := time.Now()
logger.Infof("Topic created in %d millis", t2.Sub(t1).Milliseconds())
logger.Debugf("kafka topics created in %d millis", t2.Sub(t1).Milliseconds())

return nil
}

func (kc *InferKafkaHandler) ensureTopicsExist(topicNames []string) error {
ctx, cancel := context.WithTimeout(context.Background(), TopicDescribeTimeout)
defer cancel()
topicsDescResult, err := kc.adminClient.DescribeTopics(
ctx,
kafka.NewTopicCollectionOfTopicNames(topicNames),
kafka.SetAdminOptionIncludeAuthorizedOperations(false))
if err != nil {
return err
}

for _, topicDescription := range topicsDescResult.TopicDescriptions {
if topicDescription.Error.Code() != kafka.ErrNoError {
return fmt.Errorf("topic description failure: %s", topicDescription.Error.Error())
}
}

return nil
}
Expand All @@ -241,7 +289,7 @@ func (kc *InferKafkaHandler) AddModel(modelName string) error {
kc.subscribedTopics[inputTopic] = true
err := kc.subscribeTopics()
if err != nil {
kc.logger.WithError(err).Warn("Failed to subscribe to topics")
kc.logger.WithError(err).Errorf("failed to subscribe to topics")
return nil
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should investigate more on why all of these errors are being swallowed in infer.go instead of being returned

}
return nil
Expand All @@ -255,7 +303,7 @@ func (kc *InferKafkaHandler) RemoveModel(modelName string) error {
if len(kc.subscribedTopics) > 0 {
err := kc.subscribeTopics()
if err != nil {
kc.logger.WithError(err).Errorf("Failed to subscribe to topics")
kc.logger.WithError(err).Errorf("failed to subscribe to topics")
return nil
}
}
Expand All @@ -276,7 +324,7 @@ func (kc *InferKafkaHandler) Serve() {
for run {
select {
case <-kc.done:
logger.Infof("Stopping consumer %s", kc.consumer.String())
logger.Infof("stopping consumer %s", kc.consumer.String())
kc.producerActive.Store(false)
run = false
default:
Expand Down
2 changes: 1 addition & 1 deletion scheduler/pkg/kafka/gateway/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (iw *InferWorker) produce(
for k, vs := range headers {
for _, v := range vs {
if !existsKafkaHeader(kafkaHeaders, k, v) {
logger.Infof("Adding header to kafka response %s:%s", k, v)
logger.Debugf("Adding header to kafka response %s:%s", k, v)
kafkaHeaders = append(kafkaHeaders, kafka.Header{Key: k, Value: []byte(v)})
}
}
Expand Down