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

fix: finish remaining span when process close #135

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions storage/clickhousespanstore/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ func (pool *WriteWorkerPool) Work() {
finish := false
nextWorkerID := int32(1)
pendingSpanCount := 0

pool.done.Add(1)
defer pool.done.Done()

for {
// Initialize to zero, or update value from previous loop
numPendingSpans.Set(float64(pendingSpanCount))

pool.done.Add(1)
select {
case batch := <-pool.batches:
batchSize := len(batch)
Expand Down Expand Up @@ -103,7 +106,6 @@ func (pool *WriteWorkerPool) Work() {
pool.workers.CloseWorkers()
finish = true
}
pool.done.Done()

if finish {
break
Expand Down
29 changes: 22 additions & 7 deletions storage/clickhousespanstore/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,32 @@ func (w *SpanWriter) registerMetrics() {
func (w *SpanWriter) backgroundWriter(maxSpanCount int) {
pool := NewWorkerPool(&w.workerParams, maxSpanCount)
go pool.Work()
batch := make([]*model.Span, 0, w.size)

batch := make([]*model.Span, 0, w.size)
timer := time.After(w.workerParams.delay)
last := time.Now()

finishSpan := func() {
for len(w.spans) > 0 {
select {
case span := <-w.spans:
batch = append(batch, span)
if len(batch) >= cap(batch) {
pool.WriteBatch(batch)
}
default:
}
}
if len(batch) > 0 {
pool.WriteBatch(batch)
}
pool.Close()
}

w.done.Add(1)
defer w.done.Done()

for {
w.done.Add(1)

flush := false
finish := false
Expand Down Expand Up @@ -128,11 +147,7 @@ func (w *SpanWriter) backgroundWriter(maxSpanCount int) {
}

if finish {
pool.Close()
}
w.done.Done()

if finish {
finishSpan()
break
}
}
Expand Down