Skip to content

Commit

Permalink
gemini: random compaction strategy picked
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik Johansson committed Jun 10, 2019
1 parent 4eb1c2e commit 9922a30
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 16 deletions.
64 changes: 64 additions & 0 deletions compaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package gemini

import (
"encoding/json"
"strings"
)

type CompactionStrategy struct {
Class string `json:"class"`
Enabled bool `json:"enabled,omitempty"`
TombstoneThreshold float32 `json:"tombstone_threshold,omitempty"`
TombstoneCompactionInterval int `json:"tombstone_compaction_interval,omitempty"`
BucketHigh float32 `json:"bucket_high,omitempty"`
BucketLow float32 `json:"bucket_low,omitempty"`
MinSSTableSize int `json:"min_sstable_size,omitempty"`
MinThreshold int `json:"min_threshold,omitempty"`
MaxThreshold int `json:"max_threshold,omitempty"`
SSTableSizeInMB int `json:"sstable_size_in_mb,omitempty"`
CompactionWindowUnit string `json:"compaction_window_unit,omitempty"`
CompactionWindowSize int `json:"compaction_window_size,omitempty"`
SplitDuringFlush bool `json:"split_during_flush,omitempty"`
}

func (cs *CompactionStrategy) ToCQL() string {
b, _ := json.Marshal(cs)
return strings.ReplaceAll(string(b), "\"", "'")
}

func NewSizeTieredCompactionStrategy() *CompactionStrategy {
return &CompactionStrategy{
Class: "SizeTieredCompactionStrategy",
Enabled: true,
TombstoneThreshold: 0.2,
TombstoneCompactionInterval: 86400,
BucketHigh: 1.5,
BucketLow: 0.5,
MinSSTableSize: 50,
MinThreshold: 4,
MaxThreshold: 32,
}
}

func NewLeveledCompactionStrategy() *CompactionStrategy {
return &CompactionStrategy{
Class: "LeveledCompactionStrategy",
Enabled: true,
TombstoneThreshold: 0.2,
TombstoneCompactionInterval: 86400,
SSTableSizeInMB: 160,
}
}

func NewTimeWindowCompationStrategy() *CompactionStrategy {
return &CompactionStrategy{
Class: "TimeWindowCompationStrategy",
Enabled: true,
TombstoneThreshold: 0.2,
TombstoneCompactionInterval: 86400,
CompactionWindowUnit: "DAYS",
CompactionWindowSize: 1,
MinThreshold: 4,
MaxThreshold: 32,
}
}
51 changes: 35 additions & 16 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ func (cs Columns) ToJSONMap(values map[string]interface{}, p *PartitionRange) ma
}

type Table struct {
Name string `json:"name"`
PartitionKeys Columns `json:"partition_keys"`
ClusteringKeys Columns `json:"clustering_keys"`
Columns Columns `json:"columns"`
Indexes []IndexDef `json:"indexes"`
MaterializedViews []MaterializedView `json:"materialized_views"`
KnownIssues map[string]bool `json:"known_issues"`
Name string `json:"name"`
PartitionKeys Columns `json:"partition_keys"`
ClusteringKeys Columns `json:"clustering_keys"`
Columns Columns `json:"columns"`
CompactionStrategy *CompactionStrategy `json:"compaction_strategy"`
Indexes []IndexDef `json:"indexes"`
MaterializedViews []MaterializedView `json:"materialized_views"`
KnownIssues map[string]bool `json:"known_issues"`
}

func (t *Table) GetCreateTable(ks Keyspace) string {
Expand All @@ -107,11 +108,17 @@ func (t *Table) GetCreateTable(ks Keyspace) string {
columns = append(columns, fmt.Sprintf("%s %s", cdef.Name, cdef.Type.CQLDef()))
}

var stmt string
if len(clusteringKeys) == 0 {
return fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s (%s, PRIMARY KEY ((%s)))", ks.Name, t.Name, strings.Join(columns, ","), strings.Join(partitionKeys, ","))
stmt = fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s (%s, PRIMARY KEY ((%s)))", ks.Name, t.Name, strings.Join(columns, ","), strings.Join(partitionKeys, ","))
} else {
stmt = fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s (%s, PRIMARY KEY ((%s), %s))", ks.Name, t.Name, strings.Join(columns, ","),
strings.Join(partitionKeys, ","), strings.Join(clusteringKeys, ","))
}
if t.CompactionStrategy != nil {
stmt = stmt + " WITH compaction = " + t.CompactionStrategy.ToCQL() + ";"
}
return fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s (%s, PRIMARY KEY ((%s), %s))", ks.Name, t.Name, strings.Join(columns, ","),
strings.Join(partitionKeys, ","), strings.Join(clusteringKeys, ","))
return stmt
}

func (t *Table) GetCreateTypes(keyspace Keyspace) []string {
Expand Down Expand Up @@ -241,12 +248,13 @@ func GenSchema() *Schema {
}

table := Table{
Name: "table1",
PartitionKeys: partitionKeys,
ClusteringKeys: clusteringKeys,
Columns: columns,
MaterializedViews: mvs,
Indexes: indexes,
Name: "table1",
PartitionKeys: partitionKeys,
ClusteringKeys: clusteringKeys,
Columns: columns,
CompactionStrategy: randomCompactionStrategy(),
MaterializedViews: mvs,
Indexes: indexes,
KnownIssues: map[string]bool{
KnownIssuesJsonWithTuples: true,
},
Expand All @@ -255,6 +263,17 @@ func GenSchema() *Schema {
return builder.Build()
}

func randomCompactionStrategy() *CompactionStrategy {
switch rand.Intn(3) {
case 0:
return NewLeveledCompactionStrategy()
case 1:
return NewTimeWindowCompationStrategy()
default:
return NewSizeTieredCompactionStrategy()
}
}

func (s *Schema) GetCreateSchema() []string {
createKeyspace := fmt.Sprintf("CREATE KEYSPACE IF NOT EXISTS %s WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1}", s.Keyspace.Name)

Expand Down
17 changes: 17 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ func TestGetCreateSchema(t *testing.T) {
},
want: "CREATE TABLE IF NOT EXISTS ks1.tbl0 (pk0 text, PRIMARY KEY ((pk0)))",
},
"single_partition_key_compact": {
table: &Table{
Name: "tbl0",
PartitionKeys: createColumns(1, "pk"),
CompactionStrategy: NewLeveledCompactionStrategy(),
},
want: "CREATE TABLE IF NOT EXISTS ks1.tbl0 (pk0 text, PRIMARY KEY ((pk0))) WITH compaction = {'class':'LeveledCompactionStrategy','enabled':true,'tombstone_threshold':0.2,'tombstone_compaction_interval':86400,'sstable_size_in_mb':160};",
},
"single_partition_key_single_column": {
table: &Table{
Name: "tbl0",
Expand Down Expand Up @@ -51,6 +59,15 @@ func TestGetCreateSchema(t *testing.T) {
},
want: "CREATE TABLE IF NOT EXISTS ks1.tbl0 (pk0 text,ck0 text, PRIMARY KEY ((pk0), ck0))",
},
"single_partition_key_single_clustering_key_compact": {
table: &Table{
Name: "tbl0",
PartitionKeys: createColumns(1, "pk"),
ClusteringKeys: createColumns(1, "ck"),
CompactionStrategy: NewLeveledCompactionStrategy(),
},
want: "CREATE TABLE IF NOT EXISTS ks1.tbl0 (pk0 text,ck0 text, PRIMARY KEY ((pk0), ck0)) WITH compaction = {'class':'LeveledCompactionStrategy','enabled':true,'tombstone_threshold':0.2,'tombstone_compaction_interval':86400,'sstable_size_in_mb':160};",
},
"single_partition_key_single_clustering_key_single_column": {
table: &Table{
Name: "tbl0",
Expand Down

0 comments on commit 9922a30

Please sign in to comment.