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
6 changes: 6 additions & 0 deletions internal/stackql/primitivegraph/base_dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package primitivegraph
import (
"context"
"fmt"
"sync"

"github.com/stackql/stackql/internal/stackql/acid/binlog"
"github.com/stackql/stackql/internal/stackql/internal_data_transfer/internaldto"
Expand All @@ -24,6 +25,7 @@ type standardBasePrimitiveGraph struct {
g *simple.WeightedDirectedGraph
sorted []graph.Node
txnControlCounterSlice []internaldto.TxnControlCounters
txnControlCounterMutex sync.Mutex // guards txnControlCounterSlice; executors append concurrently
errGroup *errgroup.Group
errGroupCtx context.Context
containsView bool
Expand Down Expand Up @@ -100,10 +102,14 @@ func (pg *standardBasePrimitiveGraph) GetUndoLog() (binlog.LogEntry, bool) {
}

func (pg *standardBasePrimitiveGraph) AddTxnControlCounters(t internaldto.TxnControlCounters) {
pg.txnControlCounterMutex.Lock()
defer pg.txnControlCounterMutex.Unlock()
pg.txnControlCounterSlice = append(pg.txnControlCounterSlice, t)
}

func (pg *standardBasePrimitiveGraph) GetTxnControlCounterSlice() []internaldto.TxnControlCounters {
pg.txnControlCounterMutex.Lock()
defer pg.txnControlCounterMutex.Unlock()
return pg.txnControlCounterSlice
}

Expand Down
32 changes: 32 additions & 0 deletions internal/stackql/primitivegraph/base_dag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package primitivegraph_test

import (
"sync"
"testing"

"github.com/stackql/stackql/internal/stackql/internal_data_transfer/internaldto"
"github.com/stackql/stackql/internal/stackql/primitivegraph"
)

// Executors append txn control counters to the graph from inside the
// errgroup, so concurrent UNION arms race on the slice unless it is guarded.
// Run with -race to catch a regression; the length check catches lost writes.
func TestAddTxnControlCountersConcurrent(t *testing.T) {
const writers = 64
const perWriter = 100
holder := primitivegraph.NewPrimitiveGraphHolder(-1)
var wg sync.WaitGroup
for w := 0; w < writers; w++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
for i := 0; i < perWriter; i++ {
holder.AddTxnControlCounters(internaldto.NewTxnControlCountersFromVals(id, id, i, i))
}
}(w)
}
wg.Wait()
if got := len(holder.GetTxnControlCounterSlice()); got != writers*perWriter {
t.Fatalf("expected %d txn control counters, got %d", writers*perWriter, got)
}
}
Loading