diff --git a/tsdb/engine/wal/wal.go b/tsdb/engine/wal/wal.go index 238183ea24..766ca628fc 100644 --- a/tsdb/engine/wal/wal.go +++ b/tsdb/engine/wal/wal.go @@ -1299,7 +1299,9 @@ func (p *Partition) addToCache(key, data []byte, timestamp int64) { } // Determine if we'll need to sort the values for this key later - entry.isDirtySort = bytes.Compare(entry.points[len(entry.points)-1][0:8], v[0:8]) != -1 + if !entry.isDirtySort { // don't bother if we already know it has to be sorted + entry.isDirtySort = bytes.Compare(entry.points[len(entry.points)-1][0:8], v[0:8]) != -1 + } entry.points = append(entry.points, v) entry.size += len(v) } diff --git a/tsdb/engine/wal/wal_test.go b/tsdb/engine/wal/wal_test.go index 2edcd32b26..e9aaab887f 100644 --- a/tsdb/engine/wal/wal_test.go +++ b/tsdb/engine/wal/wal_test.go @@ -825,6 +825,51 @@ func TestWAL_QueryDuringCompaction(t *testing.T) { verify() } +func TestWAL_PointsSorted(t *testing.T) { + log := openTestWAL() + defer log.Close() + defer os.RemoveAll(log.path) + + if err := log.Open(); err != nil { + t.Fatalf("couldn't open wal: %s", err.Error()) + } + + codec := tsdb.NewFieldCodec(map[string]*tsdb.Field{ + "value": { + ID: uint8(1), + Name: "value", + Type: influxql.Float, + }, + }) + + // test that we can write to two different series + p1 := parsePoint("cpu,host=A value=1.1 1", codec) + p2 := parsePoint("cpu,host=A value=4.4 4", codec) + p3 := parsePoint("cpu,host=A value=2.2 2", codec) + p4 := parsePoint("cpu,host=A value=6.6 6", codec) + if err := log.WritePoints([]tsdb.Point{p1, p2, p3, p4}, nil, nil); err != nil { + t.Fatalf("failed to write points: %s", err.Error()) + } + + c := log.Cursor("cpu,host=A") + k, _ := c.Next() + if btou64(k) != 1 { + t.Fatal("points out of order") + } + k, _ = c.Next() + if btou64(k) != 2 { + t.Fatal("points out of order") + } + k, _ = c.Next() + if btou64(k) != 4 { + t.Fatal("points out of order") + } + k, _ = c.Next() + if btou64(k) != 6 { + t.Fatal("points out of order") + } +} + // test that partitions get compacted and flushed when number of series hits compaction threshold // test that partitions get compacted and flushed when a single series hits the compaction threshold // test that writes slow down when the partition size threshold is hit