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

config: add producer option to configure transactions backoff duration #133

Merged
merged 2 commits into from
Feb 13, 2022
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
15 changes: 15 additions & 0 deletions pkg/kgo/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type cfg struct {
linger time.Duration
recordTimeout time.Duration
manualFlushing bool
txnBackoff time.Duration

partitioner Partitioner

Expand Down Expand Up @@ -460,6 +461,7 @@ func defaultCfg() cfg {
produceTimeout: 10 * time.Second,
recordRetries: math.MaxInt64, // effectively unbounded
partitioner: StickyKeyPartitioner(nil), // default to how Kafka partitions
txnBackoff: 20 * time.Millisecond,

//////////////
// consumer //
Expand Down Expand Up @@ -780,6 +782,19 @@ func WithHooks(hooks ...Hook) Opt {
return clientOpt{func(cfg *cfg) { cfg.hooks = append(cfg.hooks, hooks...) }}
}

// ConcurrentTransactionBackoff sets the backoff interval to use during
// transactional requests in case we encounter CONCURRENT_TRANSACTIONS error,
// overriding the default 20ms.
//
// Sometimes, when a client begins a transaction quickly enough after finishing
// a previous one, Kafka will return a CONCURRENT_TRANSACTIONS error. Clients
// are expected to backoff slightly and retry the operation. Lower backoffs may
// increase load on the brokers, while higher backoffs may increase transaction
// latency in clients.
func ConcurrentTransactionBackoff(backoff time.Duration) Opt {
return clientOpt{func(cfg *cfg) { cfg.txnBackoff = backoff }}
}

////////////////////////////
// PRODUCER CONFIGURATION //
////////////////////////////
Expand Down
5 changes: 3 additions & 2 deletions pkg/kgo/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,17 +631,18 @@ func (cl *Client) maybeRecoverProducerID() (necessary, did bool, err error) {
func (cl *Client) doWithConcurrentTransactions(name string, fn func() error) error {
start := time.Now()
tries := 0
backoff := cl.cfg.txnBackoff
start:
err := fn()
if err == kerr.ConcurrentTransactions && time.Since(start) < 10*time.Second {
tries++
cl.cfg.logger.Log(LogLevelInfo, fmt.Sprintf("%s failed with CONCURRENT_TRANSACTIONS, which may be because we ended a txn and began producing in a new txn too quickly; backing off and retrying", name),
"backoff", 100*time.Millisecond,
"backoff", backoff,
"since_request_tries_start", time.Since(start),
"tries", tries,
)
select {
case <-time.After(100 * time.Millisecond):
case <-time.After(backoff):
case <-cl.ctx.Done():
cl.cfg.logger.Log(LogLevelError, fmt.Sprintf("abandoning %s retry due to client ctx quitting", name))
return err
Expand Down