-
Notifications
You must be signed in to change notification settings - Fork 15
/
db.go
138 lines (115 loc) · 3.44 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"context"
"fmt"
"log"
"net"
"os"
"github.com/gocql/gocql"
)
type DB interface {
IncrementAndGet(counterName string) (Counter, error)
}
type cassandraDB struct {
session *gocql.Session
hostname string
}
func NewCassandra() (DB, error) {
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
addr := os.Getenv("CASSANDRA_ADDRESS")
if addr == "" {
return nil, fmt.Errorf("CASSANDRA_ADDRESS must be set")
}
// We need to resolve all IPs of cassandra server and connect to them
ips, err := net.LookupHost(addr)
if err != nil {
return nil, fmt.Errorf("cannot resolve %s: %s", addr, err)
}
log.Printf("Resolved cassandra address %s to %+v", addr, ips)
if err := createKeyspace(ips); err != nil {
return nil, err
}
cluster := gocql.NewCluster(ips...)
cluster.Keyspace = "caas"
session, err := cluster.CreateSession()
if err != nil {
return nil, err
}
if err := createTables(session); err != nil {
return nil, err
}
return &cassandraDB{session, hostname}, nil
}
func createKeyspace(ips []string) error {
cluster := gocql.NewCluster(ips...)
session, err := cluster.CreateSession()
if err != nil {
return err
}
defer session.Close()
cql := "CREATE KEYSPACE IF NOT EXISTS caas WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};"
query := session.Query(cql)
if err := query.Exec(); err != nil {
log.Printf("error creating keyspace: %s", err)
return err
}
log.Printf("keyspace created")
return nil
}
func createTables(session *gocql.Session) error {
cql := "CREATE TABLE IF NOT EXISTS counter (name text, value counter, PRIMARY KEY (name))"
query := session.Query(cql)
if err := query.Exec(); err != nil {
log.Printf("error creating table: %s", err)
return err
}
log.Printf("table created")
return nil
}
func (c *cassandraDB) IncrementAndGet(counterName string) (Counter, error) {
observer := &queryLogger{}
err := c.increment(counterName, observer)
if err != nil {
return Counter{}, fmt.Errorf("error incrementing %q: %s", counterName, err)
}
count, err := c.get(counterName, observer)
if err != nil {
return Counter{}, fmt.Errorf("error getting %q: %s", counterName, err)
}
return Counter{Value: count, Name: counterName, Host: c.hostname, DBStats: observer.stats}, nil
}
func (c *cassandraDB) increment(name string, observer gocql.QueryObserver) error {
query := c.session.Query(`UPDATE counter SET value=value+1 WHERE name = ?`, name)
query.Observer(observer).RetryPolicy(&gocql.SimpleRetryPolicy{NumRetries: 5})
return query.Exec()
}
func (c *cassandraDB) get(name string, observer gocql.QueryObserver) (count int64, err error) {
m := map[string]interface{}{}
cql := "SELECT name, value FROM counter WHERE name=? LIMIT 1"
query := c.session.Query(cql, name).Consistency(gocql.One)
query.Observer(observer).RetryPolicy(&gocql.SimpleRetryPolicy{NumRetries: 5})
if err := query.MapScan(m); err != nil {
return 0, err
}
return m["value"].(int64), nil
}
type queryLogger struct {
stats []QueryStat
}
var _ gocql.QueryObserver = &queryLogger{}
func (q *queryLogger) ObserveQuery(_ context.Context, query gocql.ObservedQuery) {
if q.stats == nil {
q.stats = []QueryStat{}
}
stat := QueryStat{
Statement: query.Statement,
Attempts: query.Metrics.Attempts,
Time: fmt.Sprintf("%f miliseconds", query.End.Sub(query.Start).Seconds()*1000),
Host: query.Host.ConnectAddress().String(),
Rows: query.Rows,
}
q.stats = append(q.stats, stat)
}