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

Ingester prevent writes to large traces even after flushing #1199

Merged
merged 5 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* [BUGFIX] Ensure that the admin client jsonnet has correct S3 bucket property. (@hedss)
* [BUGFIX] Publish tenant index age correctly for tenant index writers. [#1146](https://github.com/grafana/tempo/pull/1146) (@joe-elliott)
* [BUGFIX] Ingester startup panic `slice bounds out of range` [#1195](https://github.com/grafana/tempo/issues/1195) (@mdisibio)
* [BUGFIX] Prevent writes to large traces even after flushing to disk [#1199](https://github.com/grafana/tempo/pull/1199) (@mdisibio)
mdisibio marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think another solution that might cover more cases is returning an error from CombineTraces() as soon as the pieces sum up to > maxbytes. That will help fix compaction as well as querying.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, we can explore that in a separate PR. Let's continue collecting thoughts in #1133 and #976.


## v1.2.1 / 2021-11-15
* [BUGFIX] Fix defaults for MaxBytesPerTrace (ingester.max-bytes-per-trace) and MaxSearchBytesPerTrace (ingester.max-search-bytes-per-trace) [#1109](https://github.com/grafana/tempo/pull/1109) (@bitprocessor)
Expand Down
59 changes: 50 additions & 9 deletions modules/ingester/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ import (
"github.com/grafana/tempo/tempodb/wal"
)

type traceTooLargeError struct {
traceID common.ID
maxBytes, reqSize int
}

func newTraceTooLargeError(traceID common.ID, maxBytes, reqSize int) *traceTooLargeError {
return &traceTooLargeError{
traceID: traceID,
maxBytes: maxBytes,
reqSize: reqSize,
}
}

func (e traceTooLargeError) Error() string {
return fmt.Sprintf(
"%s max size of trace (%d) exceeded while adding %d bytes to trace %s",
overrides.ErrorPrefixTraceTooLarge, e.maxBytes, e.reqSize, hex.EncodeToString(e.traceID))
}

// Errors returned on Query.
var (
ErrTraceMissing = errors.New("Trace missing")
Expand Down Expand Up @@ -68,9 +87,10 @@ var (
)

type instance struct {
tracesMtx sync.Mutex
traces map[uint32]*trace
traceCount atomic.Int32
tracesMtx sync.Mutex
traces map[uint32]*trace
largeTraces map[uint32]int // maxBytes that trace exceeded
traceCount atomic.Int32

blocksMtx sync.RWMutex
headBlock *wal.AppendBlock
Expand Down Expand Up @@ -109,6 +129,7 @@ type searchLocalBlockEntry struct {
func newInstance(instanceID string, limiter *Limiter, writer tempodb.Writer, l *local.Backend) (*instance, error) {
i := &instance{
traces: map[uint32]*trace{},
largeTraces: map[uint32]int{},
searchAppendBlocks: map[*wal.AppendBlock]*searchStreamingBlockEntry{},
searchCompleteBlocks: map[*wal.LocalBlock]*searchLocalBlockEntry{},

Expand Down Expand Up @@ -140,9 +161,6 @@ func (i *instance) Push(ctx context.Context, req *tempopb.PushRequest) error {
return status.Errorf(codes.FailedPrecondition, "%s max live traces exceeded for tenant %s: %v", overrides.ErrorPrefixLiveTracesExceeded, i.instanceID, err)
}

i.tracesMtx.Lock()
defer i.tracesMtx.Unlock()

id, err := pushRequestTraceID(req)
joe-elliott marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
Expand All @@ -161,8 +179,7 @@ func (i *instance) Push(ctx context.Context, req *tempopb.PushRequest) error {
return err
}

trace := i.getOrCreateTrace(id)
return trace.Push(ctx, i.instanceID, buffer, nil)
return i.push(ctx, id, buffer, nil)
}

// PushBytes is used to push an unmarshalled tempopb.Trace to the instance
Expand All @@ -179,11 +196,29 @@ func (i *instance) PushBytes(ctx context.Context, id []byte, traceBytes []byte,
return status.Errorf(codes.FailedPrecondition, "%s max live traces exceeded for tenant %s: %v", overrides.ErrorPrefixLiveTracesExceeded, i.instanceID, err)
}

return i.push(ctx, id, traceBytes, searchData)
}

func (i *instance) push(ctx context.Context, id, traceBytes, searchData []byte) error {
i.tracesMtx.Lock()
defer i.tracesMtx.Unlock()

tkn := i.tokenForTraceID(id)

if maxBytes, ok := i.largeTraces[tkn]; ok {
return status.Errorf(codes.FailedPrecondition, (newTraceTooLargeError(id, maxBytes, len(traceBytes)).Error()))
}

trace := i.getOrCreateTrace(id)
return trace.Push(ctx, i.instanceID, traceBytes, searchData)
err := trace.Push(ctx, i.instanceID, traceBytes, searchData)
if err != nil {
if e, ok := err.(*traceTooLargeError); ok {
i.largeTraces[tkn] = trace.maxBytes
return status.Errorf(codes.FailedPrecondition, e.Error())
}
}

return err
}

func (i *instance) measureReceivedBytes(traceBytes []byte, searchData []byte) {
Expand Down Expand Up @@ -486,6 +521,12 @@ func (i *instance) tokenForTraceID(id []byte) uint32 {

// resetHeadBlock() should be called under lock
func (i *instance) resetHeadBlock() error {

// Clear large traces when cutting block
i.tracesMtx.Lock()
i.largeTraces = map[uint32]int{}
i.tracesMtx.Unlock()

oldHeadBlock := i.headBlock
var err error
newHeadBlock, err := i.writer.WAL().NewBlock(uuid.New(), i.instanceID, model.CurrentEncoding)
Expand Down
63 changes: 54 additions & 9 deletions modules/ingester/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"github.com/grafana/tempo/tempodb/wal"
)

const testTenantID = "fake"

type ringCountMock struct {
count int
}
Expand All @@ -45,7 +47,7 @@ func TestInstance(t *testing.T) {
ingester, _, _ := defaultIngester(t, tempDir)
request := test.MakeRequest(10, []byte{})

i, err := newInstance("fake", limiter, ingester.store, ingester.local)
i, err := newInstance(testTenantID, limiter, ingester.store, ingester.local)
assert.NoError(t, err, "unexpected error creating new instance")
err = i.Push(context.Background(), request)
assert.NoError(t, err)
Expand Down Expand Up @@ -94,7 +96,7 @@ func TestInstanceFind(t *testing.T) {
defer os.RemoveAll(tempDir)

ingester, _, _ := defaultIngester(t, tempDir)
i, err := newInstance("fake", limiter, ingester.store, ingester.local)
i, err := newInstance(testTenantID, limiter, ingester.store, ingester.local)
assert.NoError(t, err, "unexpected error creating new instance")

numTraces := 500
Expand Down Expand Up @@ -177,7 +179,7 @@ func TestInstanceDoesNotRace(t *testing.T) {

ingester, _, _ := defaultIngester(t, tempDir)

i, err := newInstance("fake", limiter, ingester.store, ingester.local)
i, err := newInstance(testTenantID, limiter, ingester.store, ingester.local)
assert.NoError(t, err, "unexpected error creating new instance")

end := make(chan struct{})
Expand Down Expand Up @@ -321,7 +323,7 @@ func TestInstanceLimits(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i, err := newInstance("fake", limiter, ingester.store, ingester.local)
i, err := newInstance(testTenantID, limiter, ingester.store, ingester.local)
require.NoError(t, err, "unexpected error creating new instance")

for j, push := range tt.pushes {
Expand Down Expand Up @@ -501,8 +503,6 @@ func TestInstanceCutBlockIfReady(t *testing.T) {
}

func TestInstanceMetrics(t *testing.T) {
tenantID := "fake"

limits, err := overrides.NewOverrides(overrides.Limits{})
assert.NoError(t, err, "unexpected error creating limits")
limiter := NewLimiter(limits, &ringCountMock{count: 1}, 1)
Expand All @@ -513,14 +513,14 @@ func TestInstanceMetrics(t *testing.T) {

ingester, _, _ := defaultIngester(t, tempDir)

i, err := newInstance(tenantID, limiter, ingester.store, ingester.local)
i, err := newInstance(testTenantID, limiter, ingester.store, ingester.local)
assert.NoError(t, err, "unexpected error creating new instance")

cutAndVerify := func(v int) {
err := i.CutCompleteTraces(0, true)
require.NoError(t, err)

liveTraces, err := test.GetGaugeVecValue(metricLiveTraces, tenantID)
liveTraces, err := test.GetGaugeVecValue(metricLiveTraces, testTenantID)
require.NoError(t, err)
require.Equal(t, v, int(liveTraces))
}
Expand All @@ -539,6 +539,51 @@ func TestInstanceMetrics(t *testing.T) {
cutAndVerify(0)
}

func TestInstanceFailsLargeTracesEvenAfterFlushing(t *testing.T) {
ctx := context.Background()
maxTraceBytes := 100
id := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}

tempDir, err := os.MkdirTemp("/tmp", "")
require.NoError(t, err)
defer os.RemoveAll(tempDir)

ingester, _, _ := defaultIngester(t, tempDir)

limits, err := overrides.NewOverrides(overrides.Limits{
MaxBytesPerTrace: maxTraceBytes,
})
require.NoError(t, err)
limiter := NewLimiter(limits, &ringCountMock{count: 1}, 1)

i, err := newInstance(testTenantID, limiter, ingester.store, ingester.local)
require.NoError(t, err)

pushFn := func(byteCount int) error {
return i.PushBytes(ctx, id, make([]byte, byteCount), nil)
}

// Fill up trace to max
err = pushFn(maxTraceBytes)
require.NoError(t, err)

// Pushing again fails
err = pushFn(3)
require.Contains(t, err.Error(), (newTraceTooLargeError(id, maxTraceBytes, 3)).Error())

// Pushing still fails after flush
err = i.CutCompleteTraces(0, true)
require.NoError(t, err)
err = pushFn(5)
require.Contains(t, err.Error(), (newTraceTooLargeError(id, maxTraceBytes, 5)).Error())

// Cut block and then pushing works again
_, err = i.CutBlockIfReady(0, 0, true)
require.NoError(t, err)
err = pushFn(maxTraceBytes)
require.NoError(t, err)
}

func defaultInstance(t require.TestingT, tmpDir string) *instance {
limits, err := overrides.NewOverrides(overrides.Limits{})
assert.NoError(t, err, "unexpected error creating limits")
Expand Down Expand Up @@ -570,7 +615,7 @@ func defaultInstance(t require.TestingT, tmpDir string) *instance {
}, log.NewNopLogger())
assert.NoError(t, err, "unexpected error creating store")

instance, err := newInstance("fake", limiter, s, l)
instance, err := newInstance(testTenantID, limiter, s, l)
assert.NoError(t, err, "unexpected error creating new instance")

return instance
Expand Down
7 changes: 2 additions & 5 deletions modules/ingester/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ package ingester

import (
"context"
"encoding/hex"
"time"

cortex_util "github.com/cortexproject/cortex/pkg/util/log"
"github.com/go-kit/log/level"
"github.com/gogo/status"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"google.golang.org/grpc/codes"

"github.com/grafana/tempo/modules/overrides"
"github.com/grafana/tempo/pkg/tempopb"
)

Expand Down Expand Up @@ -54,7 +50,8 @@ func (t *trace) Push(_ context.Context, instanceID string, trace []byte, searchD
if t.maxBytes != 0 {
reqSize := len(trace)
if t.currentBytes+reqSize > t.maxBytes {
return status.Errorf(codes.FailedPrecondition, "%s max size of trace (%d) exceeded while adding %d bytes to trace %s", overrides.ErrorPrefixTraceTooLarge, t.maxBytes, reqSize, hex.EncodeToString(t.traceID))
//return status.Errorf(codes.FailedPrecondition, "%s max size of trace (%d) exceeded while adding %d bytes to trace %s", overrides.ErrorPrefixTraceTooLarge, t.maxBytes, reqSize, hex.EncodeToString(t.traceID))
mdisibio marked this conversation as resolved.
Show resolved Hide resolved
return newTraceTooLargeError(t.traceID, t.maxBytes, reqSize)
}

t.currentBytes += reqSize
Expand Down