featurebase/idk/internal/write.go
souhailanoor 2bdf1f5081
FB-1597: unifying idk and featurebase (#2160)
* unifying idk and featurebase: first pass

* resolved conflict with master for gitignore & dockerignore

* deleted binaries that were accidentally pushed to git

* combined gitlab jobs for idk & featurebase

* run go fmt for idk

* updated ssh env variable, and made docker password variable in gitlab env variables

* fixed typo assigning variable name

* trying to fix docker login error

* trying a different solution for docker password

* pass registry

* fixed docker login

* updated paths for idk

* exclude idk tests from featurebase test run

* fix vendor error

* update certificates

* grpc needs to be in version 1.38
genproto, which is imported by big query updates the grpc version to 1.47.0
grpc 1.47.0 causes etcd to deadlock when calling etcd.Close()
the fix is to have a replace in go.mod to specify a specific grpc version

* run go mod tidy

* go mod

* run go mod tidy

* exclude bigquery since it is causing issues and undo grpc replace in go.mod

* fix grpc version

* fix formatting error

* update formatting

* attempt to fix formatting

* update path for code coverage

* update to use current branch binaries, not master

* fix for building idk - path updates

* udpate path for binaries

* update job dependecies

* update docker idk tests to use the current branch registry

* update stages for jobs

* updated job dependencies

* not allow idk s3 dump to fail since it is a dependency for integration tests

* update dependecy for idk tests

* update paths for idk build and code coverage

* download featurebase binary from s3

* pass branch name to all setup scripts

* change to current branch instead of master

* updated sonarcloud

* sonarcloud fix and branch name fix

* trying to speed up pipeline run time

* update stage

* branch name fix + sonar cloud

* sonarcloud
2022-07-28 17:23:16 -05:00

111 lines
2.3 KiB
Go

package internal
import (
"context"
"net"
"strconv"
"time"
"github.com/pkg/errors"
segmentio "github.com/segmentio/kafka-go"
)
func CreateKafkaTopic(topic string, addr net.Addr) (err error) {
conn, err := segmentio.Dial(addr.Network(), addr.String())
if err != nil {
return err
}
defer func() {
if cerr := conn.Close(); cerr != nil && err == nil {
err = errors.Wrap(cerr, "closing kafka connection")
}
}()
controller, err := conn.Controller()
if err != nil {
return errors.Wrap(err, "finding kafka controller")
}
var controllerConn *segmentio.Conn
controllerConn, err = segmentio.Dial("tcp", net.JoinHostPort(controller.Host, strconv.Itoa(controller.Port)))
if err != nil {
return errors.Wrap(err, "connecting to kafka controller")
}
defer func() {
if cerr := controllerConn.Close(); cerr != nil && err == nil {
err = errors.Wrap(cerr, "closing kafka controller connection")
}
}()
err = controllerConn.CreateTopics(segmentio.TopicConfig{
Topic: topic,
NumPartitions: 64,
ReplicationFactor: 1,
})
if err != nil {
return err
}
return nil
}
func KafkaWriteWithBackoff(ctx context.Context, writer *segmentio.Writer, log func(string, ...interface{}), interval, maxInterval time.Duration, messages ...segmentio.Message) error {
var berr error
tries := 0
retry:
for {
tries++
err := writer.WriteMessages(ctx, messages...)
switch err := err.(type) {
case nil:
return nil
case segmentio.Error:
berr = err
if !err.Temporary() {
break retry
}
case segmentio.WriteErrors:
var remaining []segmentio.Message
for i, m := range messages {
switch err := err[i].(type) {
case nil:
continue
case segmentio.Error:
if err.Temporary() {
remaining = append(remaining, m)
continue
}
}
return errors.Wrap(err, "failed to deliver messages")
}
messages = remaining
berr = err
default:
if berr == nil || err != context.DeadlineExceeded {
berr = err
}
break retry
}
log("temporary write error: %v", err)
interval *= 2
if interval > maxInterval {
interval = maxInterval
}
timer := time.NewTimer(interval)
select {
case <-timer.C:
case <-ctx.Done():
timer.Stop()
break retry
}
}
return errors.Wrapf(berr, "failed to deliver messages after %d tries", tries)
}