Skip to content

Commit c03df33

Browse files
committed
DoBatch preference to 4xx if error
Signed-off-by: Daniel Blando <[email protected]>
1 parent 575645b commit c03df33

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

pkg/distributor/distributor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ func TestPush_QuorumError(t *testing.T) {
554554
_, err := d.Push(ctx, request)
555555
status, ok := status.FromError(err)
556556
require.True(t, ok)
557-
require.True(t, status.Code() == 429 || status.Code() == 500)
557+
require.Equal(t, codes.Code(429), status.Code())
558558
}
559559

560560
// Simulating 1 error -> Should return 2xx

pkg/ring/batch.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,29 @@ type itemTracker struct {
3030
failed4xx atomic.Int32
3131
failed5xx atomic.Int32
3232
remaining atomic.Int32
33-
err atomic.Error
33+
err4xx atomic.Error
34+
err5xx atomic.Error
3435
}
3536

3637
func (i *itemTracker) recordError(err error) int32 {
37-
i.err.Store(err)
3838

3939
if status, ok := status.FromError(err); ok && status.Code()/100 == 4 {
40+
i.err4xx.Store(err)
4041
return i.failed4xx.Inc()
4142
}
4243

44+
i.err5xx.Store(err)
4345
return i.failed5xx.Inc()
4446
}
4547

48+
func (i *itemTracker) getError() error {
49+
if i.failed5xx.Load() > i.failed4xx.Load() {
50+
return i.err5xx.Load()
51+
}
52+
53+
return i.err4xx.Load()
54+
}
55+
4656
// DoBatch request against a set of keys in the ring, handling replication and
4757
// failures. For example if we want to write N items where they may all
4858
// hit different instances, and we want them all replicated R ways with
@@ -147,7 +157,7 @@ func (b *batchTracker) record(sampleTrackers []*itemTracker, err error) {
147157
// Ex: 5xx, _, 5xx -> return 5xx
148158
if errCount > int32(sampleTrackers[i].maxFailures) || sampleTrackers[i].remaining.Dec() == 0 {
149159
if b.rpcsFailed.Inc() == 1 {
150-
b.err <- err
160+
b.err <- sampleTrackers[i].getError()
151161
}
152162
}
153163
} else {
@@ -165,7 +175,7 @@ func (b *batchTracker) record(sampleTrackers []*itemTracker, err error) {
165175
// Ex: 4xx, 5xx, 2xx
166176
if sampleTrackers[i].remaining.Dec() == 0 {
167177
if b.rpcsFailed.Inc() == 1 {
168-
b.err <- sampleTrackers[i].err.Load()
178+
b.err <- sampleTrackers[i].getError()
169179
}
170180
}
171181
}

0 commit comments

Comments
 (0)