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

Change statement randomization logic to be able to control statements ratio across all statement types #340

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
151 changes: 151 additions & 0 deletions pkg/jobs/functions_cases_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright 2019 ScyllaDB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//nolint:thelper
package jobs_test

import (
"testing"

"golang.org/x/exp/rand"

"github.com/scylladb/gemini/pkg/generators"
"github.com/scylladb/gemini/pkg/jobs"
"github.com/scylladb/gemini/pkg/typedef"
)

var genCheckfullCases = []int{
jobs.GenSinglePartitionID,
jobs.GenSinglePartitionMvID,
jobs.GenMultiplePartitionID,
jobs.GenMultiplePartitionMvID,
jobs.GenClusteringRangeID,
jobs.GenClusteringRangeMvID,
jobs.GenMultiplePartitionClusteringRangeID,
jobs.GenMultiplePartitionClusteringRangeMvID,
jobs.GenSingleIndexQueryID,
}

var genCheckNoIndex = []int{
jobs.GenSinglePartitionID,
jobs.GenSinglePartitionMvID,
jobs.GenMultiplePartitionID,
jobs.GenMultiplePartitionMvID,
jobs.GenClusteringRangeID,
jobs.GenClusteringRangeMvID,
jobs.GenMultiplePartitionClusteringRangeID,
jobs.GenMultiplePartitionClusteringRangeMvID,
}

var genCheckNoMV = []int{
jobs.GenSinglePartitionID,
jobs.GenMultiplePartitionID,
jobs.GenClusteringRangeID,
jobs.GenMultiplePartitionClusteringRangeID,
jobs.GenSingleIndexQueryID,
}

var genCheckNoClustering = []int{
jobs.GenSinglePartitionID,
jobs.GenSinglePartitionMvID,
jobs.GenMultiplePartitionID,
jobs.GenMultiplePartitionMvID,
jobs.GenSingleIndexQueryID,
}

var genCheckMin = []int{
jobs.GenSinglePartitionID,
jobs.GenMultiplePartitionID,
}

type comparer struct {
expected []int
received typedef.CasesInfo
}

func TestGetFuncCases(t *testing.T) {
tableFull := getTestTable()

tableNoIndexes := getTestTable()
tableNoIndexes.Indexes = nil

tableNoMV := getTestTable()
tableNoMV.MaterializedViews = nil

tableNoClustering := getTestTable()
tableNoClustering.ClusteringKeys = nil

tableMin := getTestTable()
tableMin.Indexes = nil
tableMin.MaterializedViews = nil
tableMin.ClusteringKeys = nil

genCheckList := map[string]comparer{
"genCheck_fullCases": {received: typedef.UpdateFuncCases(&tableFull, jobs.GenCheckStmtConditions, jobs.GenCheckStmtRatios), expected: genCheckfullCases},
"genCheck_NoIndex": {received: typedef.UpdateFuncCases(&tableNoIndexes, jobs.GenCheckStmtConditions, jobs.GenCheckStmtRatios), expected: genCheckNoIndex},
"genCheck_NoMV": {received: typedef.UpdateFuncCases(&tableNoMV, jobs.GenCheckStmtConditions, jobs.GenCheckStmtRatios), expected: genCheckNoMV},
"genCheck_NoClustering": {received: typedef.UpdateFuncCases(&tableNoClustering, jobs.GenCheckStmtConditions, jobs.GenCheckStmtRatios), expected: genCheckNoClustering},
"genCheck_Min": {received: typedef.UpdateFuncCases(&tableMin, jobs.GenCheckStmtConditions, jobs.GenCheckStmtRatios), expected: genCheckMin},
}
compareResults(t, genCheckList)

funcsList := typedef.UpdateFuncCases(&tableFull, jobs.GenCheckStmtConditions, jobs.GenCheckStmtRatios)
idx := funcsList.RandomCase(rand.New(rand.NewSource(123)))
_ = idx
}

func compareResults(t *testing.T, results map[string]comparer) {
for caseName := range results {
checkPresenceCases(t, caseName, results[caseName].received, results[caseName].expected...)
}
}

func checkPresenceCases(t *testing.T, caseName string, funcs typedef.CasesInfo, expected ...int) {
received := make([]int, 0, len(expected))
for i := range expected {
for j := range funcs.List {
if expected[i] == funcs.List[j].ID {
received = append(received, expected[i])
break
}
}
}
if len(received) != len(expected) {
t.Errorf("wrong function cases for case:%s \nexpected:%v \nreceived:%v ", caseName, expected, received)
}
}

func getTestTable() typedef.Table {
col := typedef.ColumnDef{
Name: "col_0",
Type: typedef.TYPE_INT,
}
cols := typedef.Columns{&col, &col}
index := typedef.IndexDef{
Name: "id_1",
Column: cols[0],
}
return typedef.Table{
Name: "tb1",
PartitionKeys: cols,
ClusteringKeys: cols,
Columns: cols,
Indexes: typedef.Indexes{index, index},
MaterializedViews: generators.CreateMaterializedViews(cols, "mv1", cols, cols),
KnownIssues: map[string]bool{
typedef.KnownIssuesJSONWithTuples: true,
},
TableOptions: nil,
}
}
148 changes: 70 additions & 78 deletions pkg/jobs/gen_check_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package jobs

import (
"math"

"github.com/scylladb/gocqlx/v2/qb"
"golang.org/x/exp/rand"

Expand All @@ -25,89 +23,83 @@ import (
"github.com/scylladb/gemini/pkg/utils"
)

const (
GenSinglePartitionID = iota
GenSinglePartitionMvID
GenMultiplePartitionID
GenMultiplePartitionMvID
GenClusteringRangeID
GenClusteringRangeMvID
GenMultiplePartitionClusteringRangeID
GenMultiplePartitionClusteringRangeMvID
GenSingleIndexQueryID
)

var GenCheckStmtConditions = typedef.CasesConditions{
GenSinglePartitionID: func(table *typedef.Table) bool { return true },
GenSinglePartitionMvID: func(table *typedef.Table) bool { return table.HasMV() },
GenMultiplePartitionID: func(table *typedef.Table) bool { return true },
GenMultiplePartitionMvID: func(table *typedef.Table) bool { return table.HasMV() },
GenClusteringRangeID: func(table *typedef.Table) bool { return table.HasClusteringKeys() },
GenClusteringRangeMvID: func(table *typedef.Table) bool { return table.HasClusteringKeys() && table.HasMV() },
GenMultiplePartitionClusteringRangeID: func(table *typedef.Table) bool { return table.HasClusteringKeys() },
GenMultiplePartitionClusteringRangeMvID: func(table *typedef.Table) bool { return table.HasClusteringKeys() && table.HasMV() },
GenSingleIndexQueryID: func(table *typedef.Table) bool { return table.HasIndexes() },
}

var GenCheckStmtRatios = typedef.CasesRatios{
GenSinglePartitionID: 20,
GenSinglePartitionMvID: 20,
GenMultiplePartitionID: 20,
GenMultiplePartitionMvID: 20,
GenClusteringRangeID: 20,
GenClusteringRangeMvID: 20,
GenMultiplePartitionClusteringRangeID: 20,
GenMultiplePartitionClusteringRangeMvID: 20,
GenSingleIndexQueryID: 1,
}

func GenCheckStmt(
s *typedef.Schema,
table *typedef.Table,
g generators.GeneratorInterface,
rnd *rand.Rand,
p *typedef.PartitionRangeConfig,
) *typedef.Stmt {
n := 0
mvNum := -1
maxClusteringRels := 0
numQueryPKs := 0
if len(table.MaterializedViews) > 0 && rnd.Int()%2 == 0 {
mvNum = utils.RandInt2(rnd, 0, len(table.MaterializedViews))
}

switch mvNum {
case -1:
if len(table.Indexes) > 0 {
n = rnd.Intn(5)
} else {
n = rnd.Intn(4)
}
switch n {
case 0:
return genSinglePartitionQuery(s, table, g)
case 1:
numQueryPKs = utils.RandInt2(rnd, 1, table.PartitionKeys.Len())
multiplier := int(math.Pow(float64(numQueryPKs), float64(table.PartitionKeys.Len())))
if multiplier > 100 {
numQueryPKs = 1
}
return genMultiplePartitionQuery(s, table, g, numQueryPKs)
case 2:
maxClusteringRels = utils.RandInt2(rnd, 0, table.ClusteringKeys.Len())
return genClusteringRangeQuery(s, table, g, rnd, p, maxClusteringRels)
case 3:
numQueryPKs = utils.RandInt2(rnd, 1, table.PartitionKeys.Len())
multiplier := int(math.Pow(float64(numQueryPKs), float64(table.PartitionKeys.Len())))
if multiplier > 100 {
numQueryPKs = 1
}
maxClusteringRels = utils.RandInt2(rnd, 0, table.ClusteringKeys.Len())
return genMultiplePartitionClusteringRangeQuery(s, table, g, rnd, p, numQueryPKs, maxClusteringRels)
case 4:
// Reducing the probability to hit these since they often take a long time to run
switch rnd.Intn(5) {
case 0:
idxCount := utils.RandInt2(rnd, 1, len(table.Indexes))
return genSingleIndexQuery(s, table, g, rnd, p, idxCount)
default:
return genSinglePartitionQuery(s, table, g)
}
}
default:
n = rnd.Intn(4)
switch n {
case 0:
return genSinglePartitionQueryMv(s, table, g, rnd, p, mvNum)
case 1:
lenPartitionKeys := table.MaterializedViews[mvNum].PartitionKeys.Len()
numQueryPKs = utils.RandInt2(rnd, 1, lenPartitionKeys)
multiplier := int(math.Pow(float64(numQueryPKs), float64(lenPartitionKeys)))
if multiplier > 100 {
numQueryPKs = 1
}
return genMultiplePartitionQueryMv(s, table, g, rnd, p, mvNum, numQueryPKs)
case 2:
lenClusteringKeys := table.MaterializedViews[mvNum].ClusteringKeys.Len()
maxClusteringRels = utils.RandInt2(rnd, 0, lenClusteringKeys)
return genClusteringRangeQueryMv(s, table, g, rnd, p, mvNum, maxClusteringRels)
case 3:
lenPartitionKeys := table.MaterializedViews[mvNum].PartitionKeys.Len()
numQueryPKs = utils.RandInt2(rnd, 1, lenPartitionKeys)
multiplier := int(math.Pow(float64(numQueryPKs), float64(lenPartitionKeys)))
if multiplier > 100 {
numQueryPKs = 1
}
lenClusteringKeys := table.MaterializedViews[mvNum].ClusteringKeys.Len()
maxClusteringRels = utils.RandInt2(rnd, 0, lenClusteringKeys)
return genMultiplePartitionClusteringRangeQueryMv(s, table, g, rnd, p, mvNum, numQueryPKs, maxClusteringRels)
}
}
switch table.AvailableFuncs.Check.RandomCase(rnd) {
case GenSinglePartitionID:
return genSinglePartitionQuery(s, table, g)
case GenMultiplePartitionID:
numQueryPKs := utils.RandIntLimited(rnd, 1, table.PartitionKeys.Len())
return genMultiplePartitionQuery(s, table, g, numQueryPKs)
case GenClusteringRangeID:
maxClusteringRels := utils.RandInt2(rnd, 1, table.ClusteringKeys.Len())
return genClusteringRangeQuery(s, table, g, rnd, p, maxClusteringRels)
case GenMultiplePartitionClusteringRangeID:
numQueryPKs := utils.RandIntLimited(rnd, 1, table.PartitionKeys.Len())
maxClusteringRels := utils.RandInt2(rnd, 1, table.ClusteringKeys.Len())
return genMultiplePartitionClusteringRangeQuery(s, table, g, rnd, p, numQueryPKs, maxClusteringRels)
case GenSinglePartitionMvID:
mvNum := utils.RandInt2(rnd, 0, table.MaterializedViews.Len())
return genSinglePartitionQueryMv(s, table, g, rnd, p, mvNum)
case GenMultiplePartitionMvID:
mvNum := utils.RandInt2(rnd, 0, table.MaterializedViews.Len())
numQueryPKs := utils.RandIntLimited(rnd, 1, table.PartitionKeys.Len())
return genMultiplePartitionQueryMv(s, table, g, rnd, p, mvNum, numQueryPKs)
case GenClusteringRangeMvID:
mvNum := utils.RandInt2(rnd, 0, table.MaterializedViews.Len())
maxClusteringRels := utils.RandInt2(rnd, 1, table.ClusteringKeys.Len())
return genClusteringRangeQueryMv(s, table, g, rnd, p, mvNum, maxClusteringRels)
case GenMultiplePartitionClusteringRangeMvID:
mvNum := utils.RandInt2(rnd, 0, table.MaterializedViews.Len())
numQueryPKs := utils.RandIntLimited(rnd, 1, table.PartitionKeys.Len())
maxClusteringRels := utils.RandInt2(rnd, 1, table.ClusteringKeys.Len())
return genMultiplePartitionClusteringRangeQueryMv(s, table, g, rnd, p, mvNum, numQueryPKs, maxClusteringRels)
case GenSingleIndexQueryID:
idxCount := utils.RandInt2(rnd, 0, table.Indexes.Len())
return genSingleIndexQuery(s, table, g, rnd, p, idxCount)

}
return nil
}

Expand Down Expand Up @@ -240,7 +232,7 @@ func genMultiplePartitionQueryMv(
return nil
}
if i == 0 {
values = appendValue(pk.Type, r, p, values)
values = append(values, pk.Type.GenValue(r, p)...)
typs = append(typs, pk.Type)
} else {
values = append(values, vs.Value[i-1])
Expand Down
34 changes: 21 additions & 13 deletions pkg/jobs/gen_ddl_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,31 @@ import (
"github.com/scylladb/gemini/pkg/typedef"
)

const (
GenAddColumnStmtID = iota
GenDropColumnStmtID
)

var GenDdlStmtConditions = typedef.CasesConditions{
GenAddColumnStmtID: func(table *typedef.Table) bool { return true },
GenDropColumnStmtID: func(table *typedef.Table) bool { return len(table.Columns) > 1 },
}

var GenDdlStmtRatios = typedef.CasesRatios{
GenAddColumnStmtID: 3,
GenDropColumnStmtID: 1,
}

func GenDDLStmt(s *typedef.Schema, t *typedef.Table, r *rand.Rand, _ *typedef.PartitionRangeConfig, sc *typedef.SchemaConfig) (*typedef.Stmts, error) {
maxVariant := 1
if len(t.Columns) > 0 {
maxVariant = 2
}
switch n := r.Intn(maxVariant + 2); n {
// case 0: // Alter column not supported in Cassandra from 3.0.11
// return t.alterColumn(s.Keyspace.Name)
case 2:
switch t.AvailableFuncs.DDL.RandomCase(r) {
case GenDropColumnStmtID:
colNum := r.Intn(len(t.Columns))
return genDropColumnStmt(t, s.Keyspace.Name, colNum)
default:
case GenAddColumnStmtID:
column := typedef.ColumnDef{Name: generators.GenColumnName("col", len(t.Columns)+1), Type: generators.GenColumnType(len(t.Columns)+1, sc)}
return genAddColumnStmt(t, s.Keyspace.Name, &column)
}
}

func appendValue(columnType typedef.Type, r *rand.Rand, p *typedef.PartitionRangeConfig, values []interface{}) []interface{} {
return append(values, columnType.GenValue(r, p)...)
return nil, nil
}

func genAddColumnStmt(t *typedef.Table, keyspace string, column *typedef.ColumnDef) (*typedef.Stmts, error) {
Expand Down Expand Up @@ -76,6 +82,7 @@ func genAddColumnStmt(t *typedef.Table, keyspace string, column *typedef.ColumnD
List: stmts,
PostStmtHook: func() {
t.Columns = append(t.Columns, column)
t.AvailableFuncs = UpdateAllCases(t)
t.ResetQueryCache()
},
}, nil
Expand Down Expand Up @@ -128,6 +135,7 @@ func genDropColumnStmt(t *typedef.Table, keyspace string, colNum int) (*typedef.
List: stmts,
PostStmtHook: func() {
t.Columns = t.Columns.Remove(colNum)
t.AvailableFuncs = UpdateAllCases(t)
t.ResetQueryCache()
},
}, nil
Expand Down
Loading