-
Notifications
You must be signed in to change notification settings - Fork 1
/
cql_server.go
372 lines (312 loc) · 9.23 KB
/
cql_server.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path"
"regexp"
"strings"
"sync"
"syscall"
"text/template"
"time"
"github.com/ansel1/merry"
"github.com/gocql/gocql"
"github.com/google/uuid"
)
const CREATE_KEYSPACE_TEMPLATE = `CREATE KEYSPACE IF NOT EXISTS yacht
WITH REPLICATION = { 'class': '%s', 'replication_factor' : %d }
AND DURABLE_WRITES=true`
// A pre-installed CQL server to which we connect via a URI
type CQLServerURI struct {
uri string
replicationFactor int
replicationStrategy string
cluster *gocql.ClusterConfig
}
func (server *CQLServerURI) ModeName() string {
return "uri"
}
// Destroy yacht keyspace when done
type CQLServerURI_artefact struct {
session *gocql.Session
}
func (a *CQLServerURI_artefact) Remove() {
a.session.Query("DROP KEYSPACE IF EXISTS yacht").Exec()
}
func (server *CQLServerURI) Start(lane *Lane) error {
if server.replicationFactor == 0 {
server.replicationFactor = 1
}
if server.replicationStrategy == "" {
server.replicationStrategy = "SimpleStrategy"
}
server.cluster = gocql.NewCluster(server.uri)
server.cluster.Timeout, _ = time.ParseDuration("30s")
// Create an administrative session to prepare
// administrative server for testing
session, err := server.cluster.CreateSession()
if err != nil {
return merry.Wrap(err)
}
artefact := CQLServerURI_artefact{session: session}
// Cleanup before running the suit
artefact.Remove()
// Create a keyspace for testing
var create_keyspace = fmt.Sprintf(CREATE_KEYSPACE_TEMPLATE,
server.replicationStrategy, server.replicationFactor)
err = session.Query(create_keyspace).Exec()
if err != nil {
return merry.Wrap(err)
}
server.cluster.Keyspace = "yacht"
lane.AddSuiteArtefact(&artefact)
return nil
}
func (server *CQLServerURI) Connect() (Connection, error) {
session, err := server.cluster.CreateSession()
if err != nil {
return nil, merry.Prepend(err, "when connecting to '"+server.uri+"'")
}
return &CQLConnection{session: session}, nil
}
// A single Scylla server
type CQLServerConfig struct {
Dir string
URI string
Seed string
SMP int
ClusterName string
SkipWaitForGossipToSettle int
}
var SCYLLA_CONF_TEMPLATE string = `
cluster_name: {{.ClusterName}}
developer_mode: true
experimental: true
data_file_directories:
- {{.Dir}}/data
commitlog_directory: {{.Dir}}/commitlog
hints_directory: {{.Dir}}/hints
view_hints_directory: {{.Dir}}/view_hints
listen_address: {{.URI}}
rpc_address: {{.URI}}
api_address: {{.URI}}
prometheus_address: {{.URI}}
seed_provider:
- class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
- seeds: {{.Seed}}
skip_wait_for_gossip_to_settle: {{.SkipWaitForGossipToSettle}}
ring_delay_ms: 3000
`
type CQLServer struct {
CQLServerURI
builddir string
cfg CQLServerConfig
exe string
logFileName string
configFileName string
cmd *exec.Cmd
logFile *os.File
}
func (server *CQLServer) ModeName() string {
return "single"
}
func (server *CQLServer) Start(lane *Lane) error {
if err := server.FindScyllaExecutable(); err != nil {
return err
}
if err := server.Install(lane); err != nil {
return err
}
ylog.Printf("Starting server %s...", server.cfg.URI)
if err := server.DoStart(lane); err != nil {
return err
}
ylog.Printf("Started server %s", server.cfg.URI)
server.CQLServerURI.uri = server.cfg.URI
return server.CQLServerURI.Start(lane)
}
func (server *CQLServer) FindScyllaExecutable() error {
server.exe = path.Join(server.builddir, "scylla")
if st, err := os.Stat(server.exe); err != nil {
return err
} else if st.Mode()|0111 == 0 {
return merry.Errorf("%s is not executable", server.exe)
}
return nil
}
type ReleaseURI_artefact struct {
uri string
lane *Lane
}
func (a *ReleaseURI_artefact) Remove() {
if a.uri != "" {
a.lane.ReleaseURI(a.uri)
}
}
type CQLServer_uninstall_artefact struct {
server *CQLServer
}
func (a *CQLServer_uninstall_artefact) Remove() {
os.RemoveAll(a.server.cfg.Dir)
os.Remove(a.server.logFileName)
}
func (server *CQLServer) Install(lane *Lane) error {
var err error
// Scylla assumes all instances of a cluster use the same port,
// so each instance needs an own IP address. The IP address
// can be set by the cluster. Otherwise set it here.
if server.cfg.URI == "" {
if server.cfg.URI, err = lane.LeaseURI(); err != nil {
return err
}
lane.AddSuiteArtefact(&ReleaseURI_artefact{uri: server.cfg.URI, lane: lane})
}
// Set the seed if it has not been pre-set.
if server.cfg.Seed == "" {
server.cfg.Seed = server.cfg.URI
}
// Instance subdirectory is a directory inside the lane,
// so that each lane can run a cluster of instances
// Derive subdirectory name from URI
server.cfg.Dir = path.Join(lane.Dir(), server.cfg.URI)
server.cfg.SMP = 1
// Only reset ClusterName if it was not provided
if server.cfg.ClusterName == "" {
server.cfg.ClusterName = uuid.New().String()
}
server.logFileName = path.Join(lane.Dir(), server.cfg.URI+".log")
// SCYLLA_CONF env variable is actually SCYLLA_CONF_DIR environment
// variable, and the configuration file name is assumed to be scylla.yaml
server.configFileName = path.Join(server.cfg.Dir, "scylla.yaml")
lane.AddSuiteArtefact(&CQLServer_uninstall_artefact{server: server})
if err := os.MkdirAll(server.cfg.Dir, 0750); err != nil {
return err
}
// Redirect command output to a log file, derive log file name
// from URI
var logFile *os.File
if logFile, err = os.OpenFile(server.logFileName,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
return err
}
// Open another file descriptor to ensure its position is not advanced
// by writes
if server.logFile, err = os.Open(server.logFileName); err != nil {
return err
}
// Create a configuration file. Unfortunately, Scylla can't start without
// one. Since we have to create a configuration file, let's avoid
// command line options.
configFile, err := os.OpenFile(server.configFileName,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
statement := template.Must(template.New("SCYLLA_CONF").Parse(SCYLLA_CONF_TEMPLATE))
statement.Execute(configFile, &server.cfg)
configFile.Close()
// Do not confuse Scylla binary if we derived this from the parent process
os.Unsetenv("SCYLLA_HOME")
cmd := exec.Command(server.exe, fmt.Sprintf("--smp=%d", server.cfg.SMP))
cmd.Dir = server.cfg.Dir
cmd.Env = append(cmd.Env, fmt.Sprintf("SCYLLA_CONF=%s", server.cfg.Dir))
cmd.Stdout = logFile
cmd.Stderr = logFile
server.cmd = cmd
return nil
}
type CQLServer_stop_artefact struct {
cmd *exec.Cmd
}
func (a *CQLServer_stop_artefact) Remove() {
ylog.Printf("Stopping server %d", a.cmd.Process.Pid)
a.cmd.Process.Kill()
// 3 seconds is enough for a good database to die gracefully:
// send SIGKILL if SIGTERM doesn't reach its target
timer := time.AfterFunc(3*time.Second, func() {
syscall.Kill(a.cmd.Process.Pid, syscall.SIGKILL)
})
timer.Stop()
a.cmd.Process.Wait()
ylog.Printf("Stopped server %d", a.cmd.Process.Pid)
}
func FindLogFilePattern(file *os.File, pattern string) bool {
var patternRE = regexp.MustCompile(pattern)
var scanner = bufio.NewScanner(file)
for scanner.Scan() {
if patternRE.Match(scanner.Bytes()) {
return true
}
}
return false
}
func (server *CQLServer) DoStart(lane *Lane) error {
const START_TIMEOUT = 300 * time.Second
lane.AddExitArtefact(&CQLServer_stop_artefact{cmd: server.cmd})
if err := server.cmd.Start(); err != nil {
return err
}
start := time.Now()
for _ = range time.Tick(time.Millisecond * 10) {
if FindLogFilePattern(server.logFile, "Scylla.*initialization completed") {
break
}
if time.Now().Sub(start) > START_TIMEOUT {
return merry.Errorf("failed to start server %s on lane %s, check server log at %s",
server.cfg.URI, lane.id, palette.Path(server.logFileName))
}
}
return nil
}
// CQLCluster testing mode
type CQLCluster struct {
servers [3]*CQLServer
builddir string
clusterName string
}
func (cluster *CQLCluster) ModeName() string {
return "cluster"
}
func (cluster *CQLCluster) Start(lane *Lane) error {
var seeds = make([]string, len(cluster.servers))
var err error
for i, _ := range seeds {
if seeds[i], err = lane.LeaseURI(); err != nil {
return err
}
lane.AddSuiteArtefact(&ReleaseURI_artefact{uri: seeds[i], lane: lane})
}
var seedsStr = strings.Join(seeds, ", ")
var wg sync.WaitGroup
wg.Add(len(cluster.servers))
status := make(chan error, len(cluster.servers))
startOne := func(server *CQLServer) {
defer wg.Done()
if err := server.Start(lane); err != nil {
status <- err
}
}
cluster.clusterName = uuid.New().String()
for i, _ := range cluster.servers {
server := CQLServer{builddir: cluster.builddir}
// Set a shared cluster name
server.cfg.ClusterName = cluster.clusterName
server.cfg.URI = seeds[i]
server.cfg.Seed = seedsStr
server.CQLServerURI.replicationFactor = len(cluster.servers)
// We need gossip for clustered start
server.cfg.SkipWaitForGossipToSettle = 5
go startOne(&server)
cluster.servers[i] = &server
}
wg.Wait()
select {
case err := <-status:
return err
default:
return nil
}
}
func (cluster *CQLCluster) Connect() (Connection, error) {
return cluster.servers[0].Connect()
}