Skip to content
Merged
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
4 changes: 1 addition & 3 deletions tsdb/index/tsi1/log_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,19 +421,17 @@ func (f *LogFile) TagValue(name, key, value []byte) TagValueElem {
// TagValueIterator returns a value iterator for a tag key.
func (f *LogFile) TagValueIterator(name, key []byte) TagValueIterator {
f.mu.RLock()
defer f.mu.RUnlock()

mm, ok := f.mms[string(name)]
if !ok {
f.mu.RUnlock()
return nil
}

tk, ok := mm.tagSet[string(key)]
if !ok {
f.mu.RUnlock()
return nil
}
f.mu.RUnlock()
return tk.TagValueIterator()
}

Expand Down
69 changes: 69 additions & 0 deletions tsdb/index/tsi1/log_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (
"github.com/influxdata/influxdb/models"
"github.com/influxdata/influxdb/pkg/bloom"
"github.com/influxdata/influxdb/pkg/slices"
th "github.com/influxdata/influxdb/pkg/testing/helper"
"github.com/influxdata/influxdb/tsdb"
"github.com/influxdata/influxdb/tsdb/index/tsi1"
"github.com/stretchr/testify/require"
)

// Ensure log file can append series.
Expand Down Expand Up @@ -396,6 +398,73 @@ func GenerateLogFile(sfile *tsdb.SeriesFile, measurementN, tagN, valueN int) (*L
return f, nil
}

// Ensure TagValueIterator is safe to call concurrently with AddSeriesList.
// Regression test for a "concurrent map iteration and map write" fatal
// introduced when LogFile.TagValueIterator released its RLock before the
// underlying logTagKey.tagValues map was snapshotted. This is able to
// consistently reproduce the fatal error introduced by #26372.
func TestLogFile_TagValueIterator_ConcurrentWrite(t *testing.T) {
Comment on lines +401 to +406
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

The regression test comment references "#26372", but the PR description/issue being addressed is #27343. This looks like a typo and may confuse future readers trying to trace the original regression; please update the reference to the correct issue/PR number.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The comment is correct.

sfile := MustOpenSeriesFile()
defer th.CheckedClose(t, sfile)()

f := MustOpenLogFile(sfile.SeriesFile)
defer th.CheckedClose(t, f)()
seriesSet := tsdb.NewSeriesIDSet()

// Seed the measurement/tag key so the iterator finds something.
_, err := f.AddSeriesList(seriesSet,
slices.StringsToBytes("cpu"),
[]models.Tags{models.NewTags(map[string]string{"host": "server-0"})},
notrack,
)
require.NoError(t, err)

done := make(chan struct{})
errCh := make(chan error, 1)

// Writer: continuously add new tag values for tag key "host" on "cpu",
// which mutates the logTagKey.tagValues map under f.mu.Lock().
go func() {
defer close(errCh)
for i := 0; ; i++ {
select {
case <-done:
return
default:
}
tags := models.NewTags(map[string]string{
"host": fmt.Sprintf("server-%d", i),
})
if _, err := f.AddSeriesList(seriesSet,
slices.StringsToBytes("cpu"),
[]models.Tags{tags},
notrack,
); err != nil {
errCh <- err
return
}
}
}()

// Reader: hammer TagValueIterator, which iterates the same map.
// With the buggy LogFile.TagValueIterator (RLock released before the
// snapshot loop runs in logTagKey.TagValueIterator), the Go runtime
// fatals with "concurrent map iteration and map write".
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
itr := f.TagValueIterator([]byte("cpu"), []byte("host"))
if itr == nil {
continue
}
for e := itr.Next(); e != nil; e = itr.Next() {
_ = e.Value()
}
}
close(done)

require.NoError(t, <-errCh)
}

func benchmarkLogFile_AddSeries(b *testing.B, measurementN, seriesKeyN, seriesValueN int) {
sfile := MustOpenSeriesFile()
defer sfile.Close()
Expand Down
Loading