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

feat: client side load balancing for user transformations #5375

Merged
merged 8 commits into from
Jan 6, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/apache/pulsar-client-go v0.14.0
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/aws/aws-sdk-go v1.55.5
github.com/bufbuild/httplb v0.3.0
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/cenkalti/backoff/v4 v4.3.0
github.com/confluentinc/confluent-kafka-go/v2 v2.6.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdb
github.com/bsm/gomega v1.26.0/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/bufbuild/httplb v0.3.0 h1:sCMPD+89ydD3atcVareDsiv/kUT+pLHolENMoCGZJV8=
github.com/bufbuild/httplb v0.3.0/go.mod h1:qDNs7dSFxIhKi/DA/rCCPVzbQfHs1JVxPMl9EvrbL4Q=
github.com/buger/goterm v1.0.4 h1:Z9YvGmOih81P0FbVtEYTFF6YsSgxSUKEhf/f9bTMXbY=
github.com/buger/goterm v1.0.4/go.mod h1:HiFWV3xnkolgrBV3mY8m0X0Pumt4zg4QhbdOzQtB8tE=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
Expand Down
70 changes: 55 additions & 15 deletions processor/transformer/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"io"
"net"
"net/http"
"os"
"runtime/trace"
Expand All @@ -15,6 +16,8 @@ import (
"sync"
"time"

"github.com/bufbuild/httplb"
"github.com/bufbuild/httplb/resolver"
"github.com/cenkalti/backoff"
jsoniter "github.com/json-iterator/go"
"github.com/samber/lo"
Expand All @@ -26,6 +29,7 @@ import (
backendconfig "github.com/rudderlabs/rudder-server/backend-config"
"github.com/rudderlabs/rudder-server/processor/integrations"
"github.com/rudderlabs/rudder-server/utils/httputil"
"github.com/rudderlabs/rudder-server/utils/sysUtils"
"github.com/rudderlabs/rudder-server/utils/types"
warehouseutils "github.com/rudderlabs/rudder-server/warehouse/utils"
)
Expand Down Expand Up @@ -136,9 +140,9 @@ type Response struct {

type Opt func(*handle)

func WithClient(client *http.Client) Opt {
func WithClient(client HTTPDoer) Opt {
return func(s *handle) {
s.client = client
s.httpClient = client
}
}

Expand All @@ -149,6 +153,10 @@ type Transformer interface {
Validate(ctx context.Context, clientEvents []TransformerEvent, batchSize int) Response
}

type HTTPDoer interface {
Do(req *http.Request) (*http.Response, error)
}

// handle is the handle for this class
type handle struct {
sentStat stats.Measurement
Expand All @@ -159,7 +167,7 @@ type handle struct {
logger logger.Logger
stat stats.Stats

client *http.Client
httpClient HTTPDoer

guardConcurrency chan struct{}

Expand Down Expand Up @@ -211,16 +219,41 @@ func NewTransformer(conf *config.Config, log logger.Logger, stat stats.Stats, op

trans.guardConcurrency = make(chan struct{}, trans.config.maxConcurrency)

if trans.client == nil {
trans.client = &http.Client{
Transport: &http.Transport{
DisableKeepAlives: trans.config.disableKeepAlives,
MaxConnsPerHost: trans.config.maxHTTPConnections,
MaxIdleConnsPerHost: trans.config.maxHTTPIdleConnections,
IdleConnTimeout: trans.config.maxIdleConnDuration,
},
Timeout: trans.config.timeoutDuration,
}
clientType := conf.GetString("Transformer.Client.type", "stdlib")

transport := &http.Transport{
DisableKeepAlives: trans.config.disableKeepAlives,
MaxConnsPerHost: trans.config.maxHTTPConnections,
MaxIdleConnsPerHost: trans.config.maxHTTPIdleConnections,
IdleConnTimeout: trans.config.maxIdleConnDuration,
}
client := &http.Client{
Transport: transport,
Timeout: trans.config.timeoutDuration,
}

switch clientType {
case "stdlib":
trans.httpClient = client
case "recycled":
trans.httpClient = sysUtils.NewRecycledHTTPClient(func() *http.Client {
return client
}, config.GetDuration("Transformer.Client.ttl", 120, time.Second))
case "httplb":
trans.httpClient = httplb.NewClient(
httplb.WithTransport("http", &HTTPLBTransport{
Transport: transport,
}),
httplb.WithResolver(
resolver.NewDNSResolver(
net.DefaultResolver,
resolver.PreferIPv6,
config.GetDuration("Transformer.Client.ttl", 120, time.Second), // TTL value
),
),
)
default:
panic(fmt.Sprintf("unknown transformer client type: %s", clientType))
}

for _, opt := range opts {
Expand All @@ -245,6 +278,14 @@ func (trans *handle) Validate(ctx context.Context, clientEvents []TransformerEve
return trans.transform(ctx, clientEvents, trans.trackingPlanValidationURL(), batchSize, trackingPlanValidationStage)
}

type HTTPLBTransport struct {
*http.Transport
}

func (t *HTTPLBTransport) NewRoundTripper(scheme, target string, config httplb.TransportConfig) httplb.RoundTripperResult {
return httplb.RoundTripperResult{RoundTripper: t.Transport, Close: t.CloseIdleConnections}
}

func (trans *handle) transform(
ctx context.Context,
clientEvents []TransformerEvent,
Expand Down Expand Up @@ -474,7 +515,7 @@ func (trans *handle) doPost(ctx context.Context, rawJSON []byte, url, stage stri
// Header to let transformer know that the client understands event filter code
req.Header.Set("X-Feature-Filter-Code", "?1")

resp, reqErr = trans.client.Do(req)
resp, reqErr = trans.httpClient.Do(req)
})
trans.stat.NewTaggedStat("processor.transformer_request_time", stats.TimerType, tags).SendTiming(time.Since(requestStartTime))
if reqErr != nil {
Expand All @@ -495,7 +536,6 @@ func (trans *handle) doPost(ctx context.Context, rawJSON []byte, url, stage stri
retryCount++
trans.logger.Warnn(
"JS HTTP connection error",
logger.NewStringField("URL", url),
logger.NewErrorField(err),
logger.NewIntField("attempts", int64(retryCount)),
)
Expand Down
Loading
Loading