Skip to content

feat: Add WaitWithTimeout to Partition and WaitGroupTimeout #26294

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

Draft
wants to merge 11 commits into
base: master-1.x
Choose a base branch
from
25 changes: 25 additions & 0 deletions pkg/wg_timeout/wg_timeout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package wg_timeout

import (
"sync"
"time"
)

func WaitGroupTimeout(wg *sync.WaitGroup, timeout time.Duration, emitter func()) {
breakoutChan := make(chan struct{})

go func(c chan struct{}) {
defer close(c)
wg.Wait()
}(breakoutChan)

timer := time.NewTimer(timeout)
defer timer.Stop()

select {
case <-breakoutChan:
return
case <-timer.C:
emitter()
}
}
7 changes: 7 additions & 0 deletions tsdb/index/tsi1/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,16 @@ func (p *Partition) CurrentCompactionN() int {
// Wait will block until all compactions are finished.
// Must only be called while they are disabled.
func (p *Partition) Wait() {
start := time.Now()
duration := 24 * time.Hour
ticker := time.NewTicker(10 * time.Millisecond)
defer ticker.Stop()
for {
if time.Since(start) > duration {
p.logger.Debug("timed out waiting for compaction to finish", zap.Duration("duration", duration), zap.String("path", p.path), zap.String("id", p.id))
// Reset start time to Now() so it will capture in the next 24 hours given this is still hanging
start = time.Now()
}
if p.CurrentCompactionN() == 0 {
return
}
Expand Down
5 changes: 4 additions & 1 deletion tsdb/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/influxdata/influxdb/pkg/file"
"github.com/influxdata/influxdb/pkg/limiter"
"github.com/influxdata/influxdb/pkg/slices"
"github.com/influxdata/influxdb/pkg/wg_timeout"
"github.com/influxdata/influxdb/query"
internal "github.com/influxdata/influxdb/tsdb/internal"
"github.com/influxdata/influxql"
Expand Down Expand Up @@ -1814,7 +1815,9 @@ func (fs *MeasurementFieldSet) SetMeasurementFieldSetWriter(queueLength int, log
func (fscm *measurementFieldSetChangeMgr) Close() {
if fscm != nil {
close(fscm.writeRequests)
fscm.wg.Wait()
wg_timeout.WaitGroupTimeout(&fscm.wg, 24*time.Hour, func() {
fscm.logger.Debug("timed out waiting for measurementFieldSetChangeMgr to close", zap.String("changeFilePath", fscm.changeFilePath))
})
}
}

Expand Down