-
Notifications
You must be signed in to change notification settings - Fork 726
/
Copy pathdb.clj
318 lines (282 loc) · 10.2 KB
/
db.clj
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
(ns jepsen.stolon.db
"Database setup and automation."
(:require [cheshire.core :as json]
[clojure [pprint :refer [pprint]]
[string :as str]]
[clojure.java.io :as io]
[clojure.tools.logging :refer [info warn]]
[dom-top.core :refer [disorderly
real-pmap]]
[jepsen [control :as c]
[core :as jepsen]
[db :as db]
[util :as util :refer [meh random-nonempty-subset]]]
[jepsen.control.util :as cu]
[jepsen.control.net :as cn]
[jepsen.etcd.db :as etcd]
[jepsen.os.debian :as debian]
[jepsen.stolon [client :as sc]]
[slingshot.slingshot :refer [try+ throw+]]))
(def dir "/opt/stolon")
(def data-dir (str dir "/data"))
(def user
"The OS user which will run Stolon, postgres, etc."
"postgres")
(def sentinel-logfile (str dir "/sentinel.log"))
(def sentinel-pidfile (str dir "/sentinel.pid"))
(def sentinel-bin "stolon-sentinel")
(def keeper-logfile (str dir "/keeper.log"))
(def keeper-pidfile (str dir "/keeper.pid"))
(def keeper-bin "stolon-keeper")
(def proxy-logfile (str dir "/proxy.log"))
(def proxy-pidfile (str dir "/proxy.pid"))
(def proxy-bin "stolon-proxy")
(def cluster-name "jepsen-cluster")
(def just-postgres-log-file
"/var/log/postgresql/postgresql-12-main.log")
(defn install-pg!
"Installs postgresql"
[test node]
(c/su
; Install apt key
(c/exec :wget :--quiet :-O :- "https://www.postgresql.org/media/keys/ACCC4CF8.asc" c/| :apt-key :add :-)
; Add repo
(debian/install [:lsb-release])
(let [release (c/exec :lsb_release :-cs)]
(debian/add-repo! "postgresql"
(str "deb http://apt.postgresql.org/pub/repos/apt/ "
release "-pgdg main")))
; Install
(debian/install [:postgresql-12 :postgresql-client-12])
; Deactivate default install
(c/exec :service :postgresql :stop)
(c/exec "update-rc.d" :postgresql :disable)))
(defn install-stolon!
"Downloads and installs Stolon."
[test node]
(c/su
(let [version (:version test)]
(cu/install-archive! (str "https://github.com/sorintlab/stolon/releases/download/v"
version "/stolon-v" version "-linux-amd64.tar.gz")
dir))
(c/exec :chown :-R (str user ":" user) dir)))
(defn store-endpoints
"Computes the etcd address for stolon commands"
[]
(str "http://" (cn/local-ip) ":2379"))
(defn stolonctl!
"Calls stolonctl with args. Provides cluster name and store connection args
automatically."
[& args]
(c/sudo user
(c/cd dir
(apply c/exec "bin/stolonctl"
:--cluster-name "jepsen-cluster"
:--store-backend "etcdv3"
:--store-endpoints (store-endpoints)
args))))
(defn initial-cluster-spec
"The data structure we use for our stolon initial cluster specification. See
https://github.com/sorintlab/stolon/blob/master/doc/cluster_spec.md for
options."
[test]
{:synchronousReplication true
:initMode :new
:sleepInterval "1s"
:requestTimeout "2s"
:failInterval "4s"
:proxyCheckInterval "1s"
:proxyTimeout "3s"
; TODO: this is set to 48 hours, and it feels DANGEROUS, let's make it small
; later.
:deadKeeperRemovalInterval "48h"
:maxStandbysPerSender (dec (count (:nodes test)))
; Default is 1, but I'm pretty sure that allows bad things to happen.
:minSynchronousStandbys 1
:maxSynchronousStandbys 1
})
(defn start-sentinel!
"Starts a Stolon sentinel process."
[test node]
(c/cd dir
(c/sudo user
(let [init-spec "init-spec.json"]
(c/exec :echo (json/generate-string
(initial-cluster-spec test))
:> init-spec)
(cu/start-daemon!
{:chdir dir
:logfile sentinel-logfile
:pidfile sentinel-pidfile}
(str dir "/bin/" sentinel-bin)
:--cluster-name cluster-name
:--store-backend "etcdv3"
:--store-endpoints (store-endpoints)
:--initial-cluster-spec init-spec)))))
(defn node->pg-id
"Turns a node into a postgres ID, e.g. pg1"
[test node]
(str "pg" (inc (.indexOf (:nodes test) node))))
(defn pg-id->node
"Turns a postgres ID into a node."
[test pg-id]
(nth (:nodes test)
(Long/parseLong ((re-find #"pg(.+)" pg-id) 1))))
(defn start-keeper!
"Starts a Stolon keeper process."
[test node]
(let [pg-id (node->pg-id test node)]
(c/sudo user
(cu/start-daemon!
{:chdir dir
:logfile keeper-logfile
:pidfile keeper-pidfile}
(str dir "/bin/" keeper-bin)
:--cluster-name cluster-name
:--store-backend "etcdv3"
:--store-endpoints (store-endpoints)
:--uid pg-id
:--data-dir (str data-dir "/" pg-id)
:--pg-su-password (:postgres-password test)
:--pg-repl-username "repluser"
:--pg-repl-password (:postgres-password test)
:--pg-listen-address (cn/local-ip)
:--pg-port 5433
:--pg-bin-path "/usr/lib/postgresql/12/bin"))))
(defn start-proxy!
"Starts the Stolon proxy process."
[test node]
(c/sudo user
(cu/start-daemon!
{:chdir dir
:logfile proxy-logfile
:pidfile proxy-pidfile}
(str dir "/bin/" proxy-bin)
:--cluster-name cluster-name
:--store-backend "etcdv3"
:--store-endpoints (store-endpoints)
:--listen-address "0.0.0.0")))
(defn stop-sentinel!
"Kills the sentinel process."
[test node]
(c/sudo user
(cu/stop-daemon! sentinel-bin sentinel-pidfile)))
(defn stop-keeper!
"Kills the keeper process."
[test node]
(c/sudo user
(cu/stop-daemon! keeper-bin keeper-pidfile)))
(defn stop-proxy!
"Kills the proxy process."
[test node]
(c/sudo user
(cu/stop-daemon! proxy-bin proxy-pidfile)))
(defn status
"Returns the status of the stolon cluster, as a string."
[]
(c/sudo user (stolonctl! :status)))
(defn primary-keeper
"Returns the keeper the current node thinks is primary."
[test]
(let [status (status)]
(if-let [match (re-find #"Master Keeper:\s+(.+)\n" status)]
(pg-id->node test (nth match 1))
(info :couldn't-parse status))))
(defrecord Stolon [etcd etcd-test]
db/DB
(setup! [db test node]
(db/setup! etcd (etcd-test test) node)
(install-pg! test node)
(install-stolon! test node)
(start-sentinel! test node)
(start-keeper! test node)
(start-proxy! test node)
(sc/close! (sc/await-open test node)))
(teardown! [db test node]
(stop-proxy! test node)
(stop-keeper! test node)
(stop-sentinel! test node)
(c/su (c/exec :rm :-rf dir))
(db/teardown! etcd (etcd-test test) node))
db/LogFiles
(log-files [db test node]
(concat [sentinel-logfile
keeper-logfile
proxy-logfile]
(db/log-files etcd (etcd-test test) node)))
db/Primary
(setup-primary! [db test node])
(primaries [db test]
(->> (c/on-nodes test (fn [test node]
(try+
(primary-keeper test)
(catch [:exit 1] e
; Ah well
nil))))
vals
(remove nil?)
distinct)))
(defn db
"Sets up stolon"
[opts]
(Stolon.
(etcd/db)
; A function which transforms our test into a test for an etcd DB.
(let [initialized? (atom false)
members (atom (into (sorted-set) (:nodes opts)))]
(fn [test]
(assoc test
:version (:etcd-version test)
:initialized? initialized?
:members members)))))
(defn just-postgres
"A database which just runs a regular old Postgres instance, to help rule out
bugs in Stolon specifically."
[opts]
(let [tcpdump (db/tcpdump {:ports [5432]
; Haaack, hardcoded for my particular cluster
; control node
:filter "host 192.168.122.1"})]
(reify db/DB
(setup! [_ test node]
(db/setup! tcpdump test node)
(install-pg! test node)
(c/su (c/exec :echo (slurp (io/resource "pg_hba.conf"))
:> "/etc/postgresql/12/main/pg_hba.conf")
(c/exec :echo (slurp (io/resource "postgresql.conf"))
:> "/etc/postgresql/12/main/postgresql.conf"))
(c/sudo user
(c/exec "/usr/lib/postgresql/12/bin/initdb"
:-D "/var/lib/postgresql/12/main"))
(c/su (c/exec :service :postgresql :start)))
(teardown! [_ test node]
(c/su (c/exec :service :postgresql :stop)
; This might not actually work, so we have to kill the processes
; too
(cu/grepkill! "postgres")
(c/exec :rm :-rf (c/lit "/var/lib/postgresql/12/main/*")))
(c/sudo user
(c/exec :truncate :-s 0 just-postgres-log-file))
(db/teardown! tcpdump test node))
db/LogFiles
(log-files [_ test node]
(concat (db/log-files tcpdump test node)
[just-postgres-log-file]))
db/Primary
(setup-primary! [db test node])
(primaries [db test]
; Everyone's a winner! Really, there should only be one node here,
; so... it's kinda trivial.
(:nodes test))
db/Process
(start! [db test node]
(c/su (c/exec :service :postgresql :restart)))
(kill! [db test node]
(doseq [pattern (shuffle
["postgres -D" ; Main process
"main: checkpointer"
"main: background writer"
"main: walwriter"
"main: autovacuum launcher"])]
(Thread/sleep (rand-int 100))
(info "Killing" pattern "-" (cu/grepkill! pattern)))))))