Skip to content

Commit

Permalink
schema: index queries used with low frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik Johansson committed Jul 8, 2019
1 parent 1ef6a42 commit 4473ceb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Unreleased

- Index queries reapplied with low frequency for certain types.
- Fix for invalid materialized view ddl statement.

# 1.4.0
Expand Down
59 changes: 34 additions & 25 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ type Type interface {
}

type IndexDef struct {
Name string `json:"name"`
Column string `json:"column"`
Name string `json:"name"`
Column string `json:"column"`
ColumnIdx int `json:"column_idx"`
}

type Columns []ColumnDef
Expand Down Expand Up @@ -367,13 +368,23 @@ func GenSchema(sc SchemaConfig) *Schema {
}

func createIndexes(numColumns int, columns []ColumnDef) []IndexDef {
var indexes []IndexDef
if numColumns > 0 {
numIndexes := rand.Intn(numColumns)
for i := 0; i < numIndexes; i++ {
if columns[i].Type.Indexable() {
indexes = append(indexes, IndexDef{Name: genIndexName("col", i), Column: columns[i].Name})
}
if numColumns <= 0 {
return nil
}
numIndexes := rand.Intn(numColumns)
if numIndexes == 0 {
// Always try to create at least 1 index
numIndexes = 1
}
createdCount := 0
indexes := make([]IndexDef, 0, numIndexes)
for i, col := range columns {
if col.Type.Indexable() && typeIn(col, typesForIndex) {
indexes = append(indexes, IndexDef{Name: genIndexName("col", i), Column: col.Name, ColumnIdx: i})
createdCount++
}
if createdCount == numIndexes {
break
}
}
return indexes
Expand Down Expand Up @@ -658,7 +669,14 @@ func (s *Schema) GenCheckStmt(t *Table, source *Source, r *rand.Rand, p Partitio
case 3:
return s.genMultiplePartitionClusteringRangeQuery(t, source, r, p)
case 4:
return s.genSingleIndexQuery(t, source, r, p)
// Reducing the probability to hit these since they often take a long time to run
n := r.Intn(5)
switch n {
case 0:
return s.genSingleIndexQuery(t, source, r, p)
default:
return s.genSinglePartitionQuery(t, source, r, p)
}
}
return nil
}
Expand Down Expand Up @@ -848,7 +866,8 @@ func (s *Schema) genSingleIndexQuery(t *Table, source *Source, r *rand.Rand, p P
defer t.mu.RUnlock()

var (
typs []Type
values []interface{}
typs []Type
)

if len(t.Indexes) == 0 {
Expand All @@ -861,22 +880,12 @@ func (s *Schema) genSingleIndexQuery(t *Table, source *Source, r *rand.Rand, p P
pkNum = 1
}
*/
values, ok := source.GetOld()
if !ok {
return nil
}
pkNum := len(t.PartitionKeys)
builder := qb.Select(s.Keyspace.Name + "." + t.Name)
partitionKeys := t.PartitionKeys
for i := 0; i < pkNum; i++ {
pk := partitionKeys[i]
builder = builder.Where(qb.Eq(pk.Name))
typs = append(typs, pk.Type)
for _, idx := range t.Indexes {
builder = builder.Where(qb.Eq(idx.Column))
values = appendValue(t.Columns[idx.ColumnIdx].Type, r, p, values)
typs = append(typs, t.Columns[idx.ColumnIdx].Type)
}
idx := r.Intn(len(t.Indexes))
builder = builder.Where(qb.Eq(t.Indexes[idx].Column))
values = appendValue(t.Columns[idx].Type, r, p, values)
typs = append(typs, t.Columns[idx].Type)
return &Stmt{
Query: builder,
Values: func() []interface{} {
Expand Down
12 changes: 12 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (

// TODO: Add support for time when gocql bug is fixed.
var (
typesForIndex = []SimpleType{TYPE_DECIMAL, TYPE_DOUBLE, TYPE_FLOAT, TYPE_INT, TYPE_SMALLINT, TYPE_TINYINT, TYPE_VARINT}
partitionKeyTypes = []SimpleType{TYPE_INT, TYPE_SMALLINT, TYPE_TINYINT, TYPE_VARINT}
pkTypes = []SimpleType{TYPE_ASCII, TYPE_BIGINT, TYPE_BLOB, TYPE_DATE, TYPE_DECIMAL, TYPE_DOUBLE, TYPE_FLOAT, TYPE_INET, TYPE_INT, TYPE_SMALLINT, TYPE_TEXT /*TYPE_TIME,*/, TYPE_TIMESTAMP, TYPE_TIMEUUID, TYPE_TINYINT, TYPE_UUID, TYPE_VARCHAR, TYPE_VARINT}
types = append(append([]SimpleType{}, pkTypes...), TYPE_BOOLEAN, TYPE_DURATION)
Expand Down Expand Up @@ -808,3 +809,14 @@ func getSimpleTypeColumn(data map[string]interface{}) (ColumnDef, error) {
Type: st.Type,
}, err
}

func typeIn(columnDef ColumnDef, indexTypes []SimpleType) bool {
if t, ok := columnDef.Type.(SimpleType); ok {
for _, typ := range indexTypes {
if t == typ {
return true
}
}
}
return false
}

0 comments on commit 4473ceb

Please sign in to comment.