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

Fixes write benchmarks #111

Merged
merged 8 commits into from
Jul 4, 2022
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
18 changes: 12 additions & 6 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ type TableBlock struct {
}

type tableMetrics struct {
blockRotated prometheus.Counter
granulesCreated prometheus.Counter
granulesSplits prometheus.Counter
rowsInserted prometheus.Counter
zeroRowsInserted prometheus.Counter
rowInsertSize prometheus.Histogram
blockRotated prometheus.Counter
granulesCreated prometheus.Counter
granulesSplits prometheus.Counter
rowsInserted prometheus.Counter
zeroRowsInserted prometheus.Counter
granulesCompactionAborted prometheus.Counter
rowInsertSize prometheus.Histogram
}

func newTable(
Expand Down Expand Up @@ -134,6 +135,10 @@ func newTable(
Name: "granules_splits",
Help: "Number of granules splits executed.",
}),
granulesCompactionAborted: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "granules_compaction_aborted",
Help: "Number of aborted granules compaction.",
}),
rowsInserted: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "rows_inserted",
Help: "Number of rows inserted into table.",
Expand Down Expand Up @@ -1003,6 +1008,7 @@ func addPartToGranule(granules []*Granule, p *Part) error {

// abort a compaction transaction.
func (t *TableBlock) abort(granule *Granule) {
t.table.metrics.granulesCompactionAborted.Inc()
for {
if granule.metadata.pruned.CAS(1, 0) { // unmark pruned, so that we can compact it in the future
return
Expand Down
198 changes: 108 additions & 90 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/go-kit/log/level"
"github.com/google/btree"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/segmentio/parquet-go"
"github.com/stretchr/testify/require"
"github.com/thanos-io/objstore/filesystem"
Expand Down Expand Up @@ -579,94 +581,110 @@ func Test_Table_Concurrency(t *testing.T) {
}
}

//func Benchmark_Table_Insert_10Rows_10Iter_10Writers(b *testing.B) {
// benchmarkTableInserts(b, 10, 10, 10)
//}
//
//func Benchmark_Table_Insert_100Row_100Iter_100Writers(b *testing.B) {
// benchmarkTableInserts(b, 100, 100, 100)
//}
//
//func benchmarkTableInserts(b *testing.B, rows, iterations, writers int) {
// config := NewTableConfig(
// dynparquet.NewSampleSchema(),
// 2<<13,
// )
//
// c := New(nil)
// db := c.DB("test")
// generateRows := func(id string, n int) *dynparquet.Buffer {
// rows := make(dynparquet.Samples, 0, n)
// for i := 0; i < n; i++ {
// rows = append(rows, dynparquet.Sample{
// Labels: []dynparquet.Label{ // TODO would be nice to not have all the same column
// {Name: "label1", Value: id},
// {Name: "label2", Value: "value2"},
// },
// Stacktrace: []uuid.UUID{
// {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
// {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2},
// },
// Timestamp: rand.Int63(),
// Value: int64(i),
// })
// }
//
// buf, err := rows.ToBuffer(config.schema)
// require.NoError(b, err)
//
// buf.Sort()
// return buf
// }
//
// // Pre-generate all rows we're inserting
// inserts := make(map[string]*dynparquet.Buffer, writers)
// for i := 0; i < writers; i++ {
// id := uuid.New().String()
// inserts[id] = generateRows(id, rows*iterations)
// }
//
// b.ResetTimer()
//
// for i := 0; i < b.N; i++ {
//
// // Create table for test
// table := db.Table(uuid.New().String(), config, log.NewNopLogger())
//
// // Spawn n workers that will insert values into the table
// wg := &sync.WaitGroup{}
// for id := range inserts {
// wg.Add(1)
// go func(id string, tbl *Table, w *sync.WaitGroup) {
// defer w.Done()
// for i := 0; i < iterations; i++ {
// if err := tbl.Insert(inserts[id][i*rows : i*rows+rows]); err != nil {
// fmt.Println("Received error on insert: ", err)
// }
// }
// }(id, table, wg)
// }
// wg.Wait()
//
// b.StopTimer()
//
// // Wait for all compaction routines to complete
// table.Sync()
//
// // Calculate the number of entries in database
// totalrows := int64(0)
// err := table.Iterator(memory.NewGoAllocator(), func(ar arrow.Record) error {
// totalrows += ar.NumRows()
// defer ar.Release()
//
// return nil
// })
// require.NoError(b, err)
// require.Equal(b, int64(rows*iterations*writers), totalrows)
//
// b.StartTimer()
// }
//}
func Benchmark_Table_Insert_10Rows_10Iter_10Writers(b *testing.B) {
benchmarkTableInserts(b, 10, 10, 10)
}

func Benchmark_Table_Insert_100Row_100Iter_100Writers(b *testing.B) {
benchmarkTableInserts(b, 100, 100, 100)
}

func benchmarkTableInserts(b *testing.B, rows, iterations, writers int) {
var (
ctx = context.Background()
config = NewTableConfig(
dynparquet.NewSampleSchema(),
)
c = New(prometheus.NewRegistry(), 512, 512*1024*1024)
)
db, err := c.DB("test")
require.NoError(b, err)
generateRows := func(id string, n int) *dynparquet.Buffer {
rows := make(dynparquet.Samples, 0, n)
for i := 0; i < n; i++ {
rows = append(rows, dynparquet.Sample{
Labels: []dynparquet.Label{ // TODO would be nice to not have all the same column
{Name: "label1", Value: id},
{Name: "label2", Value: "value2"},
},
Stacktrace: []uuid.UUID{
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2},
},
Timestamp: rand.Int63(),
Copy link
Member

Choose a reason for hiding this comment

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

Probably fine for a start, but this isn't really representative of our typical workloads, inserts don't happen with random, but roughly monotonically increasing timestamps. Probably good to optimize these cases as well, but we'll probably see CPU time spent in things that a real workload we use FrostDB for doesn't.

Value: int64(i),
})
}

buf, err := rows.ToBuffer(config.schema)
require.NoError(b, err)

buf.Sort()
return buf
}

// Pre-generate all rows we're inserting
inserts := make(map[string][]*dynparquet.Buffer, writers)
for i := 0; i < writers; i++ {
id := uuid.New().String()
inserts[id] = make([]*dynparquet.Buffer, iterations)
for j := 0; j < iterations; j++ {
inserts[id][j] = generateRows(id, rows)
}
}

b.ResetTimer()

for i := 0; i < b.N; i++ {
// Create table for test
table, err := db.Table(uuid.New().String(), config, log.NewNopLogger())
require.NoError(b, err)
// Spawn n workers that will insert values into the table
wg := &sync.WaitGroup{}
for id := range inserts {
wg.Add(1)
go func(id string, tbl *Table, w *sync.WaitGroup) {
defer w.Done()
var (
maxTx uint64
err error
)
for i := 0; i < iterations; i++ {
if maxTx, err = tbl.InsertBuffer(ctx, inserts[id][i]); err != nil {
fmt.Println("Received error on insert: ", err)
}
}
db.Wait(maxTx)
}(id, table, wg)
}
wg.Wait()

b.StopTimer()
pool := memory.NewGoAllocator()
// Wait for all compaction routines to complete
table.Sync()

// Calculate the number of entries in database
totalrows := int64(0)
err = table.View(func(tx uint64) error {
as, err := table.ArrowSchema(ctx, tx, pool, nil, nil, nil)
if err != nil {
return err
}
return table.Iterator(ctx, tx, pool, as, nil, nil, nil, func(ar arrow.Record) error {
defer ar.Release()
totalrows += ar.NumRows()

return nil
})
})
require.Equal(b, 0., testutil.ToFloat64(table.metrics.granulesCompactionAborted))
require.NoError(b, err)
require.Equal(b, int64(rows*iterations*writers), totalrows)

b.StartTimer()
}
}

func Test_Table_ReadIsolation(t *testing.T) {
table := basicTable(t, 2<<12)
Expand Down Expand Up @@ -1199,12 +1217,12 @@ func Test_Table_InsertCancellation(t *testing.T) {
err := table.View(func(tx uint64) error {
totalrows := int64(0)

as, err := table.ArrowSchema(ctx, tx, pool, nil, nil, nil)
as, err := table.ArrowSchema(context.Background(), tx, pool, nil, nil, nil)
if err != nil {
return err
}

err = table.Iterator(ctx, tx, pool, as, nil, nil, nil, func(ar arrow.Record) error {
err = table.Iterator(context.Background(), tx, pool, as, nil, nil, nil, func(ar arrow.Record) error {
totalrows += ar.NumRows()
defer ar.Release()

Expand Down