Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 0e09919

Browse files
committed
Extract CQL templates from code to config files. Closes #888
1 parent 5a8688e commit 0e09919

File tree

24 files changed

+235
-72
lines changed

24 files changed

+235
-72
lines changed

cmd/mt-index-migrate/main.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ import (
99

1010
"github.com/gocql/gocql"
1111
"github.com/grafana/metrictank/cluster/partitioner"
12-
"github.com/grafana/metrictank/idx/cassandra"
12+
"github.com/grafana/metrictank/util"
1313
"github.com/raintank/worldping-api/pkg/log"
1414
"gopkg.in/raintank/schema.v1"
1515
)
1616

1717
var (
18-
dryRun = flag.Bool("dry-run", true, "run in dry-run mode. No changes will be made.")
19-
logLevel = flag.Int("log-level", 2, "log level. 0=TRACE|1=DEBUG|2=INFO|3=WARN|4=ERROR|5=CRITICAL|6=FATAL")
20-
srcCassAddr = flag.String("src-cass-addr", "localhost", "Address of cassandra host to migrate from.")
21-
dstCassAddr = flag.String("dst-cass-addr", "localhost", "Address of cassandra host to migrate to.")
22-
srcKeyspace = flag.String("src-keyspace", "raintank", "Cassandra keyspace in use on source.")
23-
dstKeyspace = flag.String("dst-keyspace", "raintank", "Cassandra keyspace in use on destination.")
24-
partitionScheme = flag.String("partition-scheme", "byOrg", "method used for partitioning metrics. (byOrg|bySeries)")
25-
numPartitions = flag.Int("num-partitions", 1, "number of partitions in cluster")
26-
27-
wg sync.WaitGroup
18+
dryRun = flag.Bool("dry-run", true, "run in dry-run mode. No changes will be made.")
19+
logLevel = flag.Int("log-level", 2, "log level. 0=TRACE|1=DEBUG|2=INFO|3=WARN|4=ERROR|5=CRITICAL|6=FATAL")
20+
srcCassAddr = flag.String("src-cass-addr", "localhost", "Address of cassandra host to migrate from.")
21+
dstCassAddr = flag.String("dst-cass-addr", "localhost", "Address of cassandra host to migrate to.")
22+
srcKeyspace = flag.String("src-keyspace", "raintank", "Cassandra keyspace in use on source.")
23+
dstKeyspace = flag.String("dst-keyspace", "raintank", "Cassandra keyspace in use on destination.")
24+
partitionScheme = flag.String("partition-scheme", "byOrg", "method used for partitioning metrics. (byOrg|bySeries)")
25+
numPartitions = flag.Int("num-partitions", 1, "number of partitions in cluster")
26+
cassandraTemplate = flag.String("cassandra-idx-cql-template", "/etc/metrictank/idx-cassandra.toml", "Cassandra CQL toml template for keyspace/keytable IDX creation.")
27+
wg sync.WaitGroup
2828
)
2929

3030
func main() {
@@ -63,8 +63,11 @@ func main() {
6363
log.Fatal(4, "failed to create cql session for destination cassandra. %s", err)
6464
}
6565

66+
// read key(space|table) information from templates
67+
keytable_cql_template := util.ReadEntry(*cassandraTemplate, "cassandra-idx.create_keytable_cql_template").(string)
68+
6669
// ensure the dest table exists.
67-
err = dstSession.Query(fmt.Sprintf(cassandra.TableSchema, *dstKeyspace)).Exec()
70+
err = dstSession.Query(fmt.Sprintf(keytable_cql_template, *dstKeyspace)).Exec()
6871
if err != nil {
6972
log.Fatal(4, "cassandra-idx failed to initialize cassandra table. %s", err)
7073
}

cmd/mt-split-metrics-by-ttl/main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var (
2323
cqlProtocolVersion = flag.Int("cql-protocol-version", 4, "cql protocol version to use")
2424
cassandraCreateKeyspace = flag.Bool("cassandra-create-keyspace", true, "enable the creation of the metrictank keyspace")
2525

26+
cassandraTemplate = flag.String("cassandra-cql-template", "/etc/metrictank/storage-cassandra.toml", "Cassandra CQL templates for keyspace/table creation")
27+
2628
cassandraSSL = flag.Bool("cassandra-ssl", false, "enable SSL connection to cassandra")
2729
cassandraCaPath = flag.String("cassandra-ca-path", "/etc/metrictank/ca.pem", "cassandra CA certificate path when using SSL")
2830
cassandraHostVerification = flag.Bool("cassandra-host-verification", true, "host (hostname and server cert) verification when using SSL")
@@ -69,7 +71,7 @@ func main() {
6971
panic(fmt.Sprintf("Error creating directory: %s", err))
7072
}
7173

72-
store, err := cassandra.NewCassandraStore(*cassandraAddrs, *cassandraKeyspace, *cassandraConsistency, *cassandraCaPath, *cassandraUsername, *cassandraPassword, *cassandraHostSelectionPolicy, *cassandraTimeout, cassandraReadConcurrency, cassandraReadConcurrency, cassandraReadQueueSize, 0, *cassandraRetries, *cqlProtocolVersion, windowFactor, cassandraOmitReadTimeout, *cassandraSSL, *cassandraAuth, *cassandraHostVerification, *cassandraCreateKeyspace, ttls)
74+
store, err := cassandra.NewCassandraStore(*cassandraAddrs, *cassandraKeyspace, *cassandraConsistency, *cassandraCaPath, *cassandraUsername, *cassandraPassword, *cassandraHostSelectionPolicy, *cassandraTimeout, cassandraReadConcurrency, cassandraReadConcurrency, cassandraReadQueueSize, 0, *cassandraRetries, *cqlProtocolVersion, windowFactor, cassandraOmitReadTimeout, *cassandraSSL, *cassandraAuth, *cassandraHostVerification, *cassandraCreateKeyspace, *cassandraTemplate, ttls)
7375
if err != nil {
7476
panic(fmt.Sprintf("Failed to instantiate cassandra: %s", err))
7577
}

cmd/mt-store-cat/main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ var (
4343
cqlProtocolVersion = flag.Int("cql-protocol-version", 4, "cql protocol version to use")
4444
cassandraCreateKeyspace = flag.Bool("cassandra-create-keyspace", true, "enable the creation of the metrictank keyspace")
4545

46+
cassandraTemplate = flag.String("cassandra-cql-template", "/etc/metrictank/storage-cassandra.toml", "Cassandra CQL templates for keyspace/table creation")
47+
4648
cassandraSSL = flag.Bool("cassandra-ssl", false, "enable SSL connection to cassandra")
4749
cassandraCaPath = flag.String("cassandra-ca-path", "/etc/metrictank/ca.pem", "cassandra CA certificate path when using SSL")
4850
cassandraHostVerification = flag.Bool("cassandra-host-verification", true, "host (hostname and server cert) verification when using SSL")
@@ -162,7 +164,7 @@ func main() {
162164
}
163165
}
164166

165-
store, err := cassandra.NewCassandraStore(*cassandraAddrs, *cassandraKeyspace, *cassandraConsistency, *cassandraCaPath, *cassandraUsername, *cassandraPassword, *cassandraHostSelectionPolicy, *cassandraTimeout, *cassandraReadConcurrency, *cassandraReadConcurrency, *cassandraReadQueueSize, 0, *cassandraRetries, *cqlProtocolVersion, *windowFactor, *cassandraOmitReadTimeout, *cassandraSSL, *cassandraAuth, *cassandraHostVerification, *cassandraCreateKeyspace, nil)
167+
store, err := cassandra.NewCassandraStore(*cassandraAddrs, *cassandraKeyspace, *cassandraConsistency, *cassandraCaPath, *cassandraUsername, *cassandraPassword, *cassandraHostSelectionPolicy, *cassandraTimeout, *cassandraReadConcurrency, *cassandraReadConcurrency, *cassandraReadQueueSize, 0, *cassandraRetries, *cqlProtocolVersion, *windowFactor, *cassandraOmitReadTimeout, *cassandraSSL, *cassandraAuth, *cassandraHostVerification, *cassandraCreateKeyspace, *cassandraTemplate, nil)
166168
if err != nil {
167169
log.Fatal(4, "failed to initialize cassandra. %s", err)
168170
}

cmd/mt-whisper-importer-writer/main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ var (
9292
cassandraUsername = globalFlags.String("cassandra-username", "cassandra", "username for authentication")
9393
cassandraPassword = globalFlags.String("cassandra-password", "cassandra", "password for authentication")
9494

95+
cassandraTemplate = flag.String("cassandra-cql-template", "/etc/metrictank/storage-cassandra.toml", "Cassandra CQL templates for keyspace/table creation")
96+
9597
gitHash = "(none)"
9698
)
9799

@@ -151,7 +153,7 @@ func main() {
151153
log.SetLevel(log.InfoLevel)
152154
}
153155

154-
store, err := cassandraStore.NewCassandraStore(*cassandraAddrs, *cassandraKeyspace, *cassandraConsistency, *cassandraCaPath, *cassandraUsername, *cassandraPassword, *cassandraHostSelectionPolicy, *cassandraTimeout, *cassandraReadConcurrency, *cassandraReadConcurrency, *cassandraReadQueueSize, 0, *cassandraRetries, *cqlProtocolVersion, *windowFactor, 60, *cassandraSSL, *cassandraAuth, *cassandraHostVerification, *cassandraCreateKeyspace, nil)
156+
store, err := cassandraStore.NewCassandraStore(*cassandraAddrs, *cassandraKeyspace, *cassandraConsistency, *cassandraCaPath, *cassandraUsername, *cassandraPassword, *cassandraHostSelectionPolicy, *cassandraTimeout, *cassandraReadConcurrency, *cassandraReadConcurrency, *cassandraReadQueueSize, 0, *cassandraRetries, *cqlProtocolVersion, *windowFactor, 60, *cassandraSSL, *cassandraAuth, *cassandraHostVerification, *cassandraCreateKeyspace, *cassandraTemplate, nil)
155157
if err != nil {
156158
panic(fmt.Sprintf("Failed to initialize cassandra: %q", err))
157159
}

docker/docker-chaos/metrictank.ini

+5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ cassandra-retries = 0
5858
cql-protocol-version = 4
5959
# enable the creation of the mdata keyspace and tables, only one node needs this
6060
cassandra-create-keyspace = false
61+
# cassandra CQL toml template
62+
cassandra-cql-template = /etc/metrictank/storage-cassandra.toml
6163

6264
# enable SSL connection to cassandra
6365
cassandra-ssl = false
@@ -351,6 +353,9 @@ username = cassandra
351353
password = cassandra
352354
# enable the creation of the index keyspace and tables, only one node needs this
353355
create-keyspace = false
356+
# cassandra CQL toml template
357+
cassandra-idx-cql-template = /etc/metrictank/idx-cassandra.toml
358+
354359

355360
### in-memory only
356361
[memory-idx]

docker/docker-cluster/metrictank.ini

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ cassandra-retries = 0
5858
cql-protocol-version = 4
5959
# enable the creation of the mdata keyspace and tables, only one node needs this
6060
cassandra-create-keyspace = false
61-
61+
# cassandra CQL toml template
62+
cassandra-cql-template = /etc/metrictank/storage-cassandra.toml
6263
# enable SSL connection to cassandra
6364
cassandra-ssl = false
6465
# cassandra CA certficate path when using SSL
@@ -351,6 +352,8 @@ username = cassandra
351352
password = cassandra
352353
# enable the creation of the index keyspace and tables, only one node needs this
353354
create-keyspace = false
355+
# cassandra CQL toml template
356+
cassandra-idx-cql-template = /etc/metrictank/idx-cassandra.toml
354357

355358
### in-memory only
356359
[memory-idx]

docker/docker-dev-custom-cfg-kafka/metrictank.ini

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ cassandra-retries = 0
5858
cql-protocol-version = 4
5959
# enable the creation of the mdata keyspace and tables, only one node needs this
6060
cassandra-create-keyspace = true
61+
# cassandra CQL toml template
62+
cassandra-cql-template = /etc/metrictank/storage-cassandra.toml
6163

6264
# enable SSL connection to cassandra
6365
cassandra-ssl = false
@@ -351,6 +353,8 @@ username = cassandra
351353
password = cassandra
352354
# enable the creation of the index keyspace and tables, only one node needs this
353355
create-keyspace = true
356+
# cassandra CQL toml template
357+
cassandra-idx-cql-template = /etc/metrictank/idx-cassandra.toml
354358

355359
### in-memory only
356360
[memory-idx]

docker/docker-dev/docker-compose.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ services:
1212
- ../../scripts/config/metrictank-docker.ini:/etc/metrictank/metrictank.ini
1313
- ../../scripts/config/storage-schemas.conf:/etc/metrictank/storage-schemas.conf
1414
- ../../scripts/config/storage-aggregation.conf:/etc/metrictank/storage-aggregation.conf
15+
- ../../scripts/config/storage-cassandra.toml:/etc/metrictank/storage-cassandra.toml
16+
- ../../scripts/config/storage-scylladb.toml:/etc/metrictank/storage-scylladb.toml
17+
- ../../scripts/config/idx-cassandra.toml:/etc/metrictank/idx-cassandra.toml
18+
- ../../scripts/config/idx-scylladb.toml:/etc/metrictank/idx-scylladb.toml
19+
1520
environment:
1621
WAIT_HOSTS: cassandra:9042
1722
WAIT_TIMEOUT: 60
@@ -52,4 +57,4 @@ services:
5257
ports:
5358
- "8125:8125/udp"
5459
volumes:
55-
- "../statsdaemon.ini:/etc/statsdaemon.ini"
60+
- "../statsdaemon.ini:/etc/statsdaemon.ini"

docs/config.md

+4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ cassandra-retries = 0
8888
cql-protocol-version = 4
8989
# enable the creation of the mdata keyspace and tables, only one node needs this
9090
cassandra-create-keyspace = true
91+
# cassandra CQL toml template
92+
cassandra-cql-template = /etc/metrictank/storage-cassandra.toml
9193
# enable SSL connection to cassandra
9294
cassandra-ssl = false
9395
# cassandra CA certficate path when using SSL
@@ -412,6 +414,8 @@ username = cassandra
412414
password = cassandra
413415
# enable the creation of the index keyspace and tables, only one node needs this
414416
create-keyspace = true
417+
# cassandra CQL toml template
418+
cassandra-idx-cql-template = /etc/metrictank/idx-cassandra.toml
415419
```
416420

417421
### in-memory only

docs/tools.md

+10
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ cass config flags:
9090
enable cassandra user authentication
9191
-ca-path string
9292
cassandra CA certficate path when using SSL (default "/etc/metrictank/ca.pem")
93+
-cassandra-idx-cql-template string
94+
Cassandra CQL template for IDX keyspace creation (default "/etc/metrictank/idx-cassandra.toml")
9395
-consistency string
9496
write consistency (any|one|two|three|quorum|all|local_quorum|each_quorum|local_one (default "one")
9597
-create-keyspace
@@ -152,6 +154,8 @@ or for resetting partition information when the number of partitions being used
152154
153155
Flags:
154156
157+
-cassandra-idx-cql-template string
158+
Cassandra CQL toml template for keyspace/keytable IDX creation. (default "/etc/metrictank/idx-cassandra.toml")
155159
-dry-run
156160
run in dry-run mode. No changes will be made. (default true)
157161
-dst-cass-addr string
@@ -271,6 +275,8 @@ Flags:
271275
cassandra CA certificate path when using SSL (default "/etc/metrictank/ca.pem")
272276
-cassandra-consistency string
273277
write consistency (any|one|two|three|quorum|all|local_quorum|each_quorum|local_one (default "one")
278+
-cassandra-cql-template string
279+
Cassandra CQL templates for keyspace/table creation (default "/etc/metrictank/storage-cassandra.toml")
274280
-cassandra-create-keyspace
275281
enable the creation of the metrictank keyspace (default true)
276282
-cassandra-host-selection-policy string
@@ -327,6 +333,8 @@ Flags:
327333
cassandra CA certificate path when using SSL (default "/etc/metrictank/ca.pem")
328334
-cassandra-consistency string
329335
write consistency (any|one|two|three|quorum|all|local_quorum|each_quorum|local_one (default "one")
336+
-cassandra-cql-template string
337+
Cassandra CQL templates for keyspace/table creation (default "/etc/metrictank/storage-cassandra.toml")
330338
-cassandra-create-keyspace
331339
enable the creation of the metrictank keyspace (default true)
332340
-cassandra-host-selection-policy string
@@ -598,6 +606,8 @@ cass config flags:
598606
enable cassandra user authentication
599607
-ca-path string
600608
cassandra CA certficate path when using SSL (default "/etc/metrictank/ca.pem")
609+
-cassandra-idx-cql-template string
610+
Cassandra CQL template for IDX keyspace creation (default "/etc/metrictank/idx-cassandra.toml")
601611
-consistency string
602612
write consistency (any|one|two|three|quorum|all|local_quorum|each_quorum|local_one (default "one")
603613
-create-keyspace

idx/cassandra/cassandra.go

+29-38
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,12 @@ import (
1313
"github.com/grafana/metrictank/idx"
1414
"github.com/grafana/metrictank/idx/memory"
1515
"github.com/grafana/metrictank/stats"
16+
"github.com/grafana/metrictank/util"
1617
"github.com/raintank/worldping-api/pkg/log"
1718
"github.com/rakyll/globalconf"
1819
"gopkg.in/raintank/schema.v1"
1920
)
2021

21-
const KeyspaceSchema = `CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1} AND durable_writes = true`
22-
const TableSchema = `CREATE TABLE IF NOT EXISTS %s.metric_idx (
23-
id text,
24-
orgid int,
25-
partition int,
26-
name text,
27-
metric text,
28-
interval int,
29-
unit text,
30-
mtype text,
31-
tags set<text>,
32-
lastupdate int,
33-
PRIMARY KEY (partition, id)
34-
) WITH compaction = {'class': 'SizeTieredCompactionStrategy'}
35-
AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}`
36-
3722
var (
3823
// metric idx.cassadra.query-insert.ok is how many insert queries for a metric completed successfully (triggered by an add or an update)
3924
statQueryInsertOk = stats.NewCounter32("idx.cassandra.query-insert.ok")
@@ -63,26 +48,27 @@ var (
6348
statSaveSkipped = stats.NewCounter32("idx.cassandra.save.skipped")
6449
errmetrics = cassandra.NewErrMetrics("idx.cassandra")
6550

66-
Enabled bool
67-
ssl bool
68-
auth bool
69-
hostverification bool
70-
createKeyspace bool
71-
keyspace string
72-
hosts string
73-
capath string
74-
username string
75-
password string
76-
consistency string
77-
timeout time.Duration
78-
numConns int
79-
writeQueueSize int
80-
protoVer int
81-
maxStale time.Duration
82-
pruneInterval time.Duration
83-
updateCassIdx bool
84-
updateInterval time.Duration
85-
updateInterval32 uint32
51+
Enabled bool
52+
ssl bool
53+
auth bool
54+
hostverification bool
55+
createKeyspace bool
56+
cassandraIdxTemplate string
57+
keyspace string
58+
hosts string
59+
capath string
60+
username string
61+
password string
62+
consistency string
63+
timeout time.Duration
64+
numConns int
65+
writeQueueSize int
66+
protoVer int
67+
maxStale time.Duration
68+
pruneInterval time.Duration
69+
updateCassIdx bool
70+
updateInterval time.Duration
71+
updateInterval32 uint32
8672
)
8773

8874
func ConfigSetup() *flag.FlagSet {
@@ -101,6 +87,7 @@ func ConfigSetup() *flag.FlagSet {
10187
casIdx.DurationVar(&pruneInterval, "prune-interval", time.Hour*3, "Interval at which the index should be checked for stale series.")
10288
casIdx.IntVar(&protoVer, "protocol-version", 4, "cql protocol version to use")
10389
casIdx.BoolVar(&createKeyspace, "create-keyspace", true, "enable the creation of the index keyspace and tables, only one node needs this")
90+
casIdx.StringVar(&cassandraIdxTemplate, "cassandra-idx-cql-template", "/etc/metrictank/idx-cassandra.toml", "Cassandra CQL template for IDX keyspace creation")
10491

10592
casIdx.BoolVar(&ssl, "ssl", false, "enable SSL connection to cassandra")
10693
casIdx.StringVar(&capath, "ca-path", "/etc/metrictank/ca.pem", "cassandra CA certficate path when using SSL")
@@ -173,13 +160,17 @@ func (c *CasIdx) InitBare() error {
173160
return fmt.Errorf("failed to create cassandra session: %s", err)
174161
}
175162

163+
// read templates
164+
keyspace_cql_template := util.ReadEntry(cassandraIdxTemplate, "cassandra-idx.create_keyspace_cql_template").(string)
165+
keytable_cql_template := util.ReadEntry(cassandraIdxTemplate, "cassandra-idx.create_keytable_cql_template").(string)
166+
176167
// create the keyspace or ensure it exists
177168
if createKeyspace {
178-
err = tmpSession.Query(fmt.Sprintf(KeyspaceSchema, keyspace)).Exec()
169+
err = tmpSession.Query(fmt.Sprintf(keyspace_cql_template, keyspace)).Exec()
179170
if err != nil {
180171
return fmt.Errorf("failed to initialize cassandra keyspace: %s", err)
181172
}
182-
err = tmpSession.Query(fmt.Sprintf(TableSchema, keyspace)).Exec()
173+
err = tmpSession.Query(fmt.Sprintf(keytable_cql_template, keyspace)).Exec()
183174
if err != nil {
184175
return fmt.Errorf("failed to initialize cassandra table: %s", err)
185176
}

metrictank-sample.ini

+4-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ cassandra-retries = 0
6161
cql-protocol-version = 4
6262
# enable the creation of the mdata keyspace and tables, only one node needs this
6363
cassandra-create-keyspace = true
64-
64+
# cassandra CQL toml template
65+
cassandra-cql-template = /etc/metrictank/storage-cassandra.toml
6566
# enable SSL connection to cassandra
6667
cassandra-ssl = false
6768
# cassandra CA certficate path when using SSL
@@ -354,6 +355,8 @@ username = cassandra
354355
password = cassandra
355356
# enable the creation of the index keyspace and tables, only one node needs this
356357
create-keyspace = true
358+
# cassandra CQL toml template
359+
cassandra-idx-cql-template = /etc/metrictank/idx-cassandra.toml
357360

358361
### in-memory only
359362
[memory-idx]

0 commit comments

Comments
 (0)