-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1be2b75
commit 55ec72d
Showing
23 changed files
with
822 additions
and
162 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
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,76 @@ | ||
package pipeline | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
) | ||
|
||
type RetriableBatcher struct { | ||
outFn RetriableBatcherOutFn | ||
backoff backoff.BackOff | ||
batcher *Batcher | ||
onRetryError func(err error) | ||
} | ||
|
||
type RetriableBatcherOutFn func(*WorkerData, *Batch) error | ||
|
||
type BackoffOpts struct { | ||
MinRetention time.Duration | ||
Multiplier float64 | ||
AttemptNum uint64 | ||
} | ||
|
||
func NewRetriableBatcher(batcherOpts *BatcherOptions, batcherOutFn RetriableBatcherOutFn, opts BackoffOpts, onError func(err error)) *RetriableBatcher { | ||
boff := GetBackoff( | ||
opts.MinRetention, | ||
opts.Multiplier, | ||
opts.AttemptNum, | ||
) | ||
|
||
batcherBackoff := &RetriableBatcher{ | ||
outFn: batcherOutFn, | ||
backoff: boff, | ||
onRetryError: onError, | ||
} | ||
batcherBackoff.setBatcher(batcherOpts) | ||
return batcherBackoff | ||
} | ||
|
||
func (b *RetriableBatcher) setBatcher(batcherOpts *BatcherOptions) { | ||
batcherOpts.OutFn = b.Out | ||
b.batcher = NewBatcher(*batcherOpts) | ||
} | ||
|
||
func (b *RetriableBatcher) Out(data *WorkerData, batch *Batch) { | ||
b.backoff.Reset() | ||
|
||
err := backoff.Retry(func() error { | ||
return b.outFn(data, batch) | ||
}, b.backoff) | ||
|
||
if err != nil { | ||
b.onRetryError(err) | ||
} | ||
} | ||
|
||
func (b *RetriableBatcher) Start(ctx context.Context) { | ||
b.batcher.Start(ctx) | ||
} | ||
|
||
func (b *RetriableBatcher) Stop() { | ||
b.batcher.Stop() | ||
} | ||
|
||
func (b *RetriableBatcher) Add(event *Event) { | ||
b.batcher.Add(event) | ||
} | ||
|
||
func GetBackoff(minRetention time.Duration, multiplier float64, attemptNum uint64) backoff.BackOff { | ||
expBackoff := backoff.NewExponentialBackOff() | ||
expBackoff.InitialInterval = minRetention | ||
expBackoff.Multiplier = multiplier | ||
expBackoff.RandomizationFactor = 0.5 | ||
return backoff.WithMaxRetries(expBackoff, attemptNum) | ||
} |
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,62 @@ | ||
package pipeline | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/ozontech/file.d/metric" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/atomic" | ||
) | ||
|
||
func TestBackoff(t *testing.T) { | ||
errorCount := &atomic.Int32{} | ||
errorCountBefore := errorCount.Load() | ||
|
||
eventCount := &atomic.Int32{} | ||
eventCountBefore := eventCount.Load() | ||
|
||
errorFn := func(err error) { | ||
errorCount.Inc() | ||
} | ||
|
||
batcherBackoff := NewRetriableBatcher( | ||
&BatcherOptions{ | ||
MetricCtl: metric.NewCtl("", prometheus.NewRegistry()), | ||
}, | ||
func(workerData *WorkerData, batch *Batch) error { | ||
eventCount.Inc() | ||
return nil | ||
}, | ||
BackoffOpts{AttemptNum: 3}, | ||
errorFn, | ||
) | ||
|
||
batcherBackoff.Out(nil, nil) | ||
|
||
assert.Equal(t, errorCountBefore, errorCount.Load(), "wrong error count") | ||
assert.Equal(t, eventCountBefore+1, eventCount.Load(), "wrong event count") | ||
} | ||
|
||
func TestBackoffWithError(t *testing.T) { | ||
errorCount := &atomic.Int32{} | ||
prevValue := errorCount.Load() | ||
errorFn := func(err error) { | ||
errorCount.Inc() | ||
} | ||
|
||
batcherBackoff := NewRetriableBatcher( | ||
&BatcherOptions{ | ||
MetricCtl: metric.NewCtl("", prometheus.NewRegistry()), | ||
}, | ||
func(workerData *WorkerData, batch *Batch) error { | ||
return errors.New("some error") | ||
}, | ||
BackoffOpts{AttemptNum: 3}, | ||
errorFn, | ||
) | ||
|
||
batcherBackoff.Out(nil, nil) | ||
assert.Equal(t, prevValue+1, errorCount.Load(), "wrong error count") | ||
} |
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
Oops, something went wrong.