Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3d58ab6

Browse files
committedApr 17, 2018
Extract CQL templates from code to config files. Closes grafana#888
1 parent 52d5e51 commit 3d58ab6

19 files changed

+145
-35
lines changed
 

‎cmd/mt-index-migrate/main.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"io/ioutil"
45
"flag"
56
"fmt"
67
"os"
@@ -9,7 +10,6 @@ import (
910

1011
"github.com/gocql/gocql"
1112
"github.com/grafana/metrictank/cluster/partitioner"
12-
"github.com/grafana/metrictank/idx/cassandra"
1313
"github.com/raintank/worldping-api/pkg/log"
1414
"gopkg.in/raintank/schema.v1"
1515
)
@@ -23,10 +23,21 @@ var (
2323
dstKeyspace = flag.String("dst-keyspace", "raintank", "Cassandra keyspace in use on destination.")
2424
partitionScheme = flag.String("partition-scheme", "byOrg", "method used for partitioning metrics. (byOrg|bySeries)")
2525
numPartitions = flag.Int("num-partitions", 1, "number of partitions in cluster")
26-
26+
idxKeytableSchemaTemplate = flag.String("cassandra-idx-keytable-schema-template", "idx-keytable-schema-cassandra.template", "Cassandra CQL template for keytable IDX creation.")
2727
wg sync.WaitGroup
2828
)
2929

30+
func FileRead(Filename string) string {
31+
file, err := os.Open(Filename) // For read access.
32+
if err != nil {
33+
log.Fatal(5, "Error with configuration file: %s", Filename)
34+
}
35+
defer file.Close()
36+
content, err := ioutil.ReadAll(file)
37+
str := string(content)
38+
return str
39+
}
40+
3041
func main() {
3142
flag.Usage = func() {
3243
fmt.Fprintln(os.Stderr, "mt-index-migrate")
@@ -63,8 +74,11 @@ func main() {
6374
log.Fatal(4, "failed to create cql session for destination cassandra. %s", err)
6475
}
6576

77+
// read idx table definition from template
78+
var tableSchema = FileRead(*idxKeytableSchemaTemplate)
79+
6680
// ensure the dest table exists.
67-
err = dstSession.Query(fmt.Sprintf(cassandra.TableSchema, *dstKeyspace)).Exec()
81+
err = dstSession.Query(fmt.Sprintf(tableSchema, *dstKeyspace)).Exec()
6882
if err != nil {
6983
log.Fatal(4, "cassandra-idx failed to initialize cassandra table. %s", err)
7084
}

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ 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+
cassandraKeyspaceSchemaTemplate = flag.String("storage-keyspace-schema-template", "/etc/metrictank/storage-keyspace-schema-cassandra.template", "Cassandra CQL template for keyspace creation")
27+
cassandraKeytableSchemaTemplate = flag.String("storage-keytable-schema-template", "/etc/metrictank/storage-keytable-schema-cassandra.template", "Cassandra CQL template for keytable creation")
28+
2629
cassandraSSL = flag.Bool("cassandra-ssl", false, "enable SSL connection to cassandra")
2730
cassandraCaPath = flag.String("cassandra-ca-path", "/etc/metrictank/ca.pem", "cassandra CA certificate path when using SSL")
2831
cassandraHostVerification = flag.Bool("cassandra-host-verification", true, "host (hostname and server cert) verification when using SSL")
@@ -69,7 +72,7 @@ func main() {
6972
panic(fmt.Sprintf("Error creating directory: %s", err))
7073
}
7174

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)
75+
store, err := cassandra.NewCassandraStore(*cassandraAddrs, *cassandraKeyspace, *cassandraConsistency, *cassandraCaPath, *cassandraUsername, *cassandraPassword, *cassandraHostSelectionPolicy, *cassandraTimeout, cassandraReadConcurrency, cassandraReadConcurrency, cassandraReadQueueSize, 0, *cassandraRetries, *cqlProtocolVersion, windowFactor, cassandraOmitReadTimeout, *cassandraSSL, *cassandraAuth, *cassandraHostVerification, *cassandraCreateKeyspace, *cassandraKeyspaceSchemaTemplate, *cassandraKeytableSchemaTemplate, ttls)
7376
if err != nil {
7477
panic(fmt.Sprintf("Failed to instantiate cassandra: %s", err))
7578
}

‎cmd/mt-store-cat/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ 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+
cassandraKeyspaceSchemaTemplate = flag.String("storage-keyspace-schema-template", "/etc/metrictank/storage-keyspace-schema-cassandra.template", "Cassandra CQL template for keyspace creation")
47+
cassandraKeytableSchemaTemplate = flag.String("storage-keytable-schema-template", "/etc/metrictank/storage-keytable-schema-cassandra.template", "Cassandra CQL template for keytable creation")
48+
4649
cassandraSSL = flag.Bool("cassandra-ssl", false, "enable SSL connection to cassandra")
4750
cassandraCaPath = flag.String("cassandra-ca-path", "/etc/metrictank/ca.pem", "cassandra CA certificate path when using SSL")
4851
cassandraHostVerification = flag.Bool("cassandra-host-verification", true, "host (hostname and server cert) verification when using SSL")
@@ -162,7 +165,7 @@ func main() {
162165
}
163166
}
164167

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)
168+
store, err := cassandra.NewCassandraStore(*cassandraAddrs, *cassandraKeyspace, *cassandraConsistency, *cassandraCaPath, *cassandraUsername, *cassandraPassword, *cassandraHostSelectionPolicy, *cassandraTimeout, *cassandraReadConcurrency, *cassandraReadConcurrency, *cassandraReadQueueSize, 0, *cassandraRetries, *cqlProtocolVersion, *windowFactor, *cassandraOmitReadTimeout, *cassandraSSL, *cassandraAuth, *cassandraHostVerification, *cassandraCreateKeyspace, *cassandraKeyspaceSchemaTemplate, *cassandraKeytableSchemaTemplate ,nil)
166169
if err != nil {
167170
log.Fatal(4, "failed to initialize cassandra. %s", err)
168171
}

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

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

95+
cassandraKeyspaceSchemaTemplate = flag.String("storage-keyspace-schema-template", "/etc/metrictank/storage-keyspace-schema-cassandra.template", "Cassandra CQL template for keyspace creation")
96+
cassandraKeytableSchemaTemplate = flag.String("storage-keytable-schema-template", "/etc/metrictank/storage-keytable-schema-cassandra.template", "Cassandra CQL template for keytable creation")
97+
9598
gitHash = "(none)"
9699
)
97100

@@ -151,7 +154,7 @@ func main() {
151154
log.SetLevel(log.InfoLevel)
152155
}
153156

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)
157+
store, err := cassandraStore.NewCassandraStore(*cassandraAddrs, *cassandraKeyspace, *cassandraConsistency, *cassandraCaPath, *cassandraUsername, *cassandraPassword, *cassandraHostSelectionPolicy, *cassandraTimeout, *cassandraReadConcurrency, *cassandraReadConcurrency, *cassandraReadQueueSize, 0, *cassandraRetries, *cqlProtocolVersion, *windowFactor, 60, *cassandraSSL, *cassandraAuth, *cassandraHostVerification, *cassandraCreateKeyspace, *cassandraKeyspaceSchemaTemplate, *cassandraKeytableSchemaTemplate, nil)
155158
if err != nil {
156159
panic(fmt.Sprintf("Failed to initialize cassandra: %q", err))
157160
}

‎docker/docker-dev/docker-compose.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ 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//etc/metrictank/storage-keyspace-schema-cassandra.template:/etc/metrictank//etc/metrictank/storage-keyspace-schema-cassandra.template
16+
- ../../scripts/config//etc/metrictank/storage-keytable-schema-cassandra.template:/etc/metrictank//etc/metrictank/storage-keytable-schema-cassandra.template
17+
1518
environment:
1619
WAIT_HOSTS: cassandra:9042
1720
WAIT_TIMEOUT: 60
@@ -52,4 +55,4 @@ services:
5255
ports:
5356
- "8125:8125/udp"
5457
volumes:
55-
- "../statsdaemon.ini:/etc/statsdaemon.ini"
58+
- "../statsdaemon.ini:/etc/statsdaemon.ini"

‎docs/config.md

+8
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ cassandra-auth = false
100100
cassandra-username = cassandra
101101
# password for authentication
102102
cassandra-password = cassandra
103+
# select cassandra keyspace schema template file
104+
storage-keyspace-schema-template = /etc/metrictank/storage-keyspace-schema-cassandra.template
105+
# select cassandra keytable schema template file
106+
storage-keytable-schema-template = /etc/metrictank/storage-keytable-schema-cassandra.template
103107
```
104108

105109
## Profiling and logging ##
@@ -410,6 +414,10 @@ username = cassandra
410414
password = cassandra
411415
# enable the creation of the index keyspace and tables, only one node needs this
412416
create-keyspace = true
417+
# keyspace schema template for metric_idx
418+
cassandra-idx-keyspace-schema-template = "idx-keyspace-schema-cassandra.template"
419+
# keyspace schema for metric_idx
420+
cassandra-idx-keytable-schema-template = "idx-keyspace-schema-cassandra.template"
413421
```
414422

415423
### in-memory only

‎idx/cassandra/cassandra.go

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cassandra
22

33
import (
4+
"os"
5+
"io/ioutil"
46
"flag"
57
"fmt"
68
"strings"
@@ -18,22 +20,6 @@ import (
1820
"gopkg.in/raintank/schema.v1"
1921
)
2022

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-
3723
var (
3824
// metric idx.cassadra.query-insert.ok is how many insert queries for a metric completed successfully (triggered by an add or an update)
3925
statQueryInsertOk = stats.NewCounter32("idx.cassandra.query-insert.ok")
@@ -68,6 +54,8 @@ var (
6854
auth bool
6955
hostverification bool
7056
createKeyspace bool
57+
keyspaceSchemaTemplate string
58+
keytableSchemaTemplate string
7159
keyspace string
7260
hosts string
7361
capath string
@@ -101,6 +89,8 @@ func ConfigSetup() *flag.FlagSet {
10189
casIdx.DurationVar(&pruneInterval, "prune-interval", time.Hour*3, "Interval at which the index should be checked for stale series.")
10290
casIdx.IntVar(&protoVer, "protocol-version", 4, "cql protocol version to use")
10391
casIdx.BoolVar(&createKeyspace, "create-keyspace", true, "enable the creation of the index keyspace and tables, only one node needs this")
92+
casIdx.StringVar(&keyspaceSchemaTemplate, "cassandra-idx-keyspace-schema-template", "/etc/metrictank/idx-keyspace-schema-cassandra.template", "Cassandra CQL template for IDX keyspace creation")
93+
casIdx.StringVar(&keytableSchemaTemplate, "cassandra-idx-keytable-schema-template", "/etc/metrictank/idx-keytable-schema-cassandra.template", "Cassandra CQL template for IDX keytable creation")
10494

10595
casIdx.BoolVar(&ssl, "ssl", false, "enable SSL connection to cassandra")
10696
casIdx.StringVar(&capath, "ca-path", "/etc/metrictank/ca.pem", "cassandra CA certficate path when using SSL")
@@ -165,6 +155,17 @@ func New() *CasIdx {
165155
return idx
166156
}
167157

158+
func FileRead(Filename string) string {
159+
file, err := os.Open(Filename) // For read access.
160+
if err != nil {
161+
log.Fatal(5, "Error with configuration file: %s", Filename)
162+
}
163+
defer file.Close()
164+
content, err := ioutil.ReadAll(file)
165+
str := string(content)
166+
return str
167+
}
168+
168169
// InitBare makes sure the keyspace, tables, and index exists in cassandra and creates a session
169170
func (c *CasIdx) InitBare() error {
170171
var err error
@@ -173,6 +174,10 @@ func (c *CasIdx) InitBare() error {
173174
return fmt.Errorf("failed to create cassandra session: %s", err)
174175
}
175176

177+
// read templates
178+
var KeyspaceSchema = FileRead(keyspaceSchemaTemplate)
179+
var TableSchema = FileRead(keytableSchemaTemplate)
180+
176181
// create the keyspace or ensure it exists
177182
if createKeyspace {
178183
err = tmpSession.Query(fmt.Sprintf(KeyspaceSchema, keyspace)).Exec()

‎metrictank-sample.ini

+8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ 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+
# select cassandra keyspace schema template file
65+
storage-keyspace-schema-template = /etc/metrictank/storage-keyspace-schema-cassandra.template
66+
# select cassandra keytable schema template file
67+
storage-keytable-schema-template = /etc/metrictank/storage-keytable-schema-cassandra.template
6468

6569
# enable SSL connection to cassandra
6670
cassandra-ssl = false
@@ -352,6 +356,10 @@ username = cassandra
352356
password = cassandra
353357
# enable the creation of the index keyspace and tables, only one node needs this
354358
create-keyspace = true
359+
# select cassandra keyspace schema template file
360+
cassandra-idx-keyspace-schema-template = idx-keyspace-schema-cassandra.template
361+
# select cassandra keytable schema template file
362+
cassandra-idx-keytable-schema-template = idx-keytable-schema-cassandra.template
355363

356364
### in-memory only
357365
[memory-idx]

‎metrictank.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ var (
8888
cassandraUsername = flag.String("cassandra-username", "cassandra", "username for authentication")
8989
cassandraPassword = flag.String("cassandra-password", "cassandra", "password for authentication")
9090

91+
cassandraKeyspaceSchemaTemplate = flag.String("storage-keyspace-schema-template", "/etc/metrictank/storage-keyspace-schema-cassandra.template", "Cassandra CQL template for keyspace creation")
92+
cassandraKeytableSchemaTemplate = flag.String("storage-keytable-schema-template", "/etc/metrictank/storage-keytable-schema-cassandra.template", "Cassandra CQL template for keytable creation")
93+
9194
// Profiling, instrumentation and logging:
9295
blockProfileRate = flag.Int("block-profile-rate", 0, "see https://golang.org/pkg/runtime/#SetBlockProfileRate")
9396
memProfileRate = flag.Int("mem-profile-rate", 512*1024, "0 to disable. 1 for max precision (expensive!) see https://golang.org/pkg/runtime/#pkg-variables")
@@ -277,7 +280,7 @@ func main() {
277280
/***********************************
278281
Initialize our backendStore
279282
***********************************/
280-
store, err = cassandraStore.NewCassandraStore(*cassandraAddrs, *cassandraKeyspace, *cassandraConsistency, *cassandraCaPath, *cassandraUsername, *cassandraPassword, *cassandraHostSelectionPolicy, *cassandraTimeout, *cassandraReadConcurrency, *cassandraWriteConcurrency, *cassandraReadQueueSize, *cassandraWriteQueueSize, *cassandraRetries, *cqlProtocolVersion, *cassandraWindowFactor, *cassandraOmitReadTimeout, *cassandraSSL, *cassandraAuth, *cassandraHostVerification, *cassandraCreateKeyspace, mdata.TTLs())
283+
store, err = cassandraStore.NewCassandraStore(*cassandraAddrs, *cassandraKeyspace, *cassandraConsistency, *cassandraCaPath, *cassandraUsername, *cassandraPassword, *cassandraHostSelectionPolicy, *cassandraTimeout, *cassandraReadConcurrency, *cassandraWriteConcurrency, *cassandraReadQueueSize, *cassandraWriteQueueSize, *cassandraRetries, *cqlProtocolVersion, *cassandraWindowFactor, *cassandraOmitReadTimeout, *cassandraSSL, *cassandraAuth, *cassandraHostVerification, *cassandraCreateKeyspace, *cassandraKeyspaceSchemaTemplate, *cassandraKeytableSchemaTemplate, mdata.TTLs())
281284
if err != nil {
282285
log.Fatal(4, "failed to initialize cassandra. %s", err)
283286
}

‎scripts/Dockerfile

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ RUN mkdir -p /etc/metrictank
77
COPY config/metrictank-docker.ini /etc/metrictank/metrictank.ini
88
COPY config/storage-schemas.conf /etc/metrictank/storage-schemas.conf
99
COPY config/storage-aggregation.conf /etc/metrictank/storage-aggregation.conf
10+
COPY config//etc/metrictank/storage-keyspace-schema-cassandra.template /etc/metrictank/storage-keyspace-schema-cassandra.template
11+
COPY config//etc/metrictank/storage-keytable-schema-cassandra.template /etc/metrictank/storage-keytable-schema-cassandra.template
12+
COPY config/idx-keyspace-schema-cassandra.template /etc/metrictank/idx-keyspace-schema-cassandra.template
13+
COPY config/idx-keytable-schema-cassandra.template /etc/metrictank/idx-keytable-schema-cassandra.template
1014

1115
COPY build/* /usr/bin/
1216

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1} AND durable_writes = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1} AND durable_writes = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CREATE TABLE IF NOT EXISTS %s.metric_idx (
2+
id text,
3+
orgid int,
4+
partition int,
5+
name text,
6+
metric text,
7+
interval int,
8+
unit text,
9+
mtype text,
10+
tags set<text>,
11+
lastupdate int,
12+
PRIMARY KEY (partition, id)
13+
) WITH compaction = {'class': 'SizeTieredCompactionStrategy'}
14+
AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CREATE TABLE IF NOT EXISTS %s.metric_idx (
2+
id text,
3+
orgid int,
4+
partition int,
5+
name text,
6+
metric text,
7+
interval int,
8+
unit text,
9+
mtype text,
10+
tags set<text>,
11+
lastupdate int,
12+
PRIMARY KEY (partition, id)
13+
) WITH compaction = {'class': 'SizeTieredCompactionStrategy'}
14+
AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1} AND durable_writes = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1} AND durable_writes = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE IF NOT EXISTS %s.%s (
2+
key ascii,
3+
ts int,
4+
data blob,
5+
PRIMARY KEY (key, ts)
6+
) WITH CLUSTERING ORDER BY (ts DESC)
7+
AND compaction = { 'class': 'TimeWindowCompactionStrategy', 'compaction_window_unit': 'HOURS', 'compaction_window_size': '%d', 'tombstone_threshold': '0.2', 'tombstone_compaction_interval': '86400'}
8+
AND compression = { 'class': 'LZ4Compressor' }
9+
AND gc_grace_seconds = %d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE IF NOT EXISTS %s.%s (
2+
key ascii,
3+
ts int,
4+
data blob,
5+
PRIMARY KEY (key, ts)
6+
) WITH CLUSTERING ORDER BY (ts DESC)
7+
AND compaction = { 'class': 'TimeWindowCompactionStrategy', 'compaction_window_unit': 'HOURS', 'compaction_window_size': '%d', 'tombstone_threshold': '0.2', 'tombstone_compaction_interval': '86400'}
8+
AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
9+
AND gc_grace_seconds = %d

‎store/cassandra/store_cassandra.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cassandra
22

33
import (
4+
"os"
5+
"io/ioutil"
46
"bytes"
57
"context"
68
"encoding/binary"
@@ -29,16 +31,6 @@ import (
2931

3032
const Month_sec = 60 * 60 * 24 * 28
3133

32-
const keyspace_schema = `CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1} AND durable_writes = true`
33-
const table_schema = `CREATE TABLE IF NOT EXISTS %s.%s (
34-
key ascii,
35-
ts int,
36-
data blob,
37-
PRIMARY KEY (key, ts)
38-
) WITH CLUSTERING ORDER BY (ts DESC)
39-
AND compaction = { 'class': 'TimeWindowCompactionStrategy', 'compaction_window_unit': 'HOURS', 'compaction_window_size': '%d', 'tombstone_threshold': '0.2', 'tombstone_compaction_interval': '86400'}
40-
AND compression = { 'class': 'LZ4Compressor' }
41-
AND gc_grace_seconds = %d`
4234
const Table_name_format = `metric_%d`
4335

4436
var (
@@ -179,7 +171,18 @@ func GetTTLTable(ttl uint32, windowFactor int, nameFormat string) ttlTable {
179171
}
180172
}
181173

182-
func NewCassandraStore(addrs, keyspace, consistency, CaPath, Username, Password, hostSelectionPolicy string, timeout, readers, writers, readqsize, writeqsize, retries, protoVer, windowFactor, omitReadTimeout int, ssl, auth, hostVerification bool, createKeyspace bool, ttls []uint32) (*CassandraStore, error) {
174+
func ReadFile(Filename string) string {
175+
file, err := os.Open(Filename) // For read access.
176+
if err != nil {
177+
log.Fatal(5, "Error with configuration file: %s", Filename)
178+
}
179+
defer file.Close()
180+
content, err := ioutil.ReadAll(file)
181+
str := string(content)
182+
return str
183+
}
184+
185+
func NewCassandraStore(addrs, keyspace, consistency, CaPath, Username, Password, hostSelectionPolicy string, timeout, readers, writers, readqsize, writeqsize, retries, protoVer, windowFactor, omitReadTimeout int, ssl, auth, hostVerification bool, createKeyspace bool, cassandraKeyspaceSchemaTemplate string, cassandraKeytableSchemaTemplate string, ttls []uint32) (*CassandraStore, error) {
183186

184187
stats.NewGauge32("store.cassandra.write_queue.size").Set(writeqsize)
185188
stats.NewGauge32("store.cassandra.num_writers").Set(writers)
@@ -206,6 +209,9 @@ func NewCassandraStore(addrs, keyspace, consistency, CaPath, Username, Password,
206209
if err != nil {
207210
return nil, err
208211
}
212+
// read key(space|table) information from templates
213+
var keyspace_schema = ReadFile(cassandraKeyspaceSchemaTemplate)
214+
var table_schema = ReadFile(cassandraKeytableSchemaTemplate)
209215

210216
ttlTables := GetTTLTables(ttls, windowFactor, Table_name_format)
211217

0 commit comments

Comments
 (0)
Please sign in to comment.