-
Notifications
You must be signed in to change notification settings - Fork 713
/
core.clj
490 lines (443 loc) · 18.2 KB
/
core.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
(ns elasticsearch.core
(:require [cheshire.core :as json]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.tools.logging :refer [info]]
[jepsen [core :as jepsen]
[db :as db]
[util :as util :refer [meh timeout]]
[control :as c :refer [|]]
[client :as client]
[checker :as checker]
[model :as model]
[generator :as gen]
[nemesis :as nemesis]
[store :as store]
[report :as report]
[tests :as tests]]
[jepsen.checker.timeline :as timeline]
[jepsen.control.net :as net]
[jepsen.os.debian :as debian]
[clj-http.client :as http]
[clojurewerkz.elastisch.rest :as es]
[clojurewerkz.elastisch.rest.admin :as esa]
[clojurewerkz.elastisch.rest.document :as esd]
[clojurewerkz.elastisch.rest.index :as esi]
[clojurewerkz.elastisch.rest.response :as esr]))
(defn http-error
"Takes an elastisch ExInfo exception and extracts the HTTP error response as
a string."
[ex]
; AFIACT this is the shortest path to actual information about what went
; wrong
(-> ex .getData :body json/parse-string (get "error")))
(defn wait
"Waits for elasticsearch to be healthy on the current node. Color is red,
yellow, or green; timeout is in seconds."
[timeout-secs color]
(timeout (* 1000 timeout-secs)
(throw (RuntimeException.
(str "Timed out after "
timeout-secs
" s waiting for elasticsearch cluster recovery")))
(loop []
(when
(try
(c/exec :curl :-XGET
(str "http://localhost:9200/_cluster/health?"
"wait_for_status=" (name color)
"&timeout=" timeout-secs "s"))
false
(catch RuntimeException e true))
(recur)))))
(defn primaries
"Returns a map of nodes to the node that node thinks is the current primary,
as a map of keywords to keywords. Assumes elasticsearch node names are the
same as the provided node names."
[nodes]
(->> nodes
(pmap (fn [node]
(try
(let [res (-> (str "http://" (name node)
":9200/_cluster/state")
(http/get {:as :json-string-keys})
:body)
primary (get res "master_node")]
[node
(keyword (get-in res ["nodes" primary "name"]))])
(catch java.net.ConnectException e [node nil])
(catch clojure.lang.ExceptionInfo e
(when-not (re-find #"MasterNotDiscoveredException"
(http-error e))
(throw e))
[node nil]))))
(into {})))
(defn self-primaries
"A sequence of nodes which think they are primaries."
[nodes]
(->> nodes
primaries
(filter (partial apply =))
(map key)))
(defn install!
"Install elasticsearch!"
[node version]
(c/su
(c/cd "/tmp"
(let [debfile (str "elasticsearch-" version ".deb")
uri (str "https://download.elasticsearch.org/"
"elasticsearch/elasticsearch/"
debfile)]
(when-not (= version (debian/installed-version "elasticsearch"))
(debian/uninstall! ["elasticsearch"])
(loop []
(info node "downloading elasticsearch")
(c/exec :wget :-c uri)
(c/exec :wget :-c (str uri ".sha1.txt"))
(when (try
(c/exec :sha1sum :-c (str debfile ".sha1.txt"))
false
(catch RuntimeException e
(info "SHA failed" (.getMessage e))
true))
(recur)))
(info node "installing elasticsearch")
(debian/install ["openjdk-7-jre-headless"])
(c/exec :dpkg :-i :--force-confnew debfile))))))
(defn configure!
"Configures elasticsearch."
[node test]
(c/su
(info node "configuring elasticsearch")
(c/exec :echo
(-> "default"
io/resource
slurp)
:> "/etc/default/elasticsearch")
(c/exec :echo
(-> "elasticsearch.yml"
io/resource
slurp
(str/replace "$NAME" (name node))
(str/replace "$HOSTS" (json/generate-string
(vals (c/on-many (:nodes test)
(net/local-ip))))))
:> "/etc/elasticsearch/elasticsearch.yml")))
(defn start!
[node]
"Starts elasticsearch."
(info node "starting elasticsearch")
(c/su (c/exec :service :elasticsearch :restart))
(wait 60 :green)
(info node "elasticsearch ready"))
(defn stop!
"Shuts down elasticsearch."
[node]
(c/su
(meh (c/exec :service :elasticsearch :stop))
(meh (c/exec :killall :-9 :elasticsearch)))
(info node "elasticsearch stopped"))
(defn nuke!
"Shuts down server and destroys all data."
[node]
(stop! node)
(c/su
(c/exec :rm :-rf "/var/lib/elasticsearch/elasticsearch")
(meh
(c/exec :truncate :--size 0 "/var/log/elasticsearch/elasticsearch.log")))
(info node "elasticsearch nuked"))
(defn db
"Elasticsearch for a particular version."
[version]
(reify db/DB
(setup! [_ test node]
(doto node
(nuke!)
(install! version)
(configure! test)
(start!)))
(teardown! [_ test node]
;; Leave system up, to collect logs, analyze post mortem, etc
)))
(def index-name "jepsen-index")
(defn index-already-exists-error?
"Return true if the error is due to the index already existing, false
otherwise."
[error]
(and
(-> error .getData :status (= 400))
(re-find #"IndexAlreadyExistsException"
(http-error error))))
(defrecord CreateSetClient [client]
client/Client
(setup! [_ test node]
(let [; client (es/connect [[(name node) 9300]])]
client (es/connect (str "http://" (name node) ":9200"))]
;; Create index
(try
(esi/create client index-name
:mappings {"number" {:properties
{:num {:type "integer"
:store "yes"}}}}
:settings {"index" {"refresh_interval" "1s"}})
(catch clojure.lang.ExceptionInfo e
(when-not (index-already-exists-error? e)
(throw e))))
(esa/cluster-health client
{:index [index-name] :level "indices"
:wait_for_status "green"
:wait_for_nodes (count (:nodes test))})
(CreateSetClient. client)))
(invoke! [this test op]
(case (:f op)
:add (timeout 5000
(assoc op :type :info :value :timed-out)
(try
(let [r (esd/create client index-name "number" {:num (:value op)})]
(cond (esr/ok? r)
(assoc op :type :ok)
(esr/timed-out? r)
(assoc op :type :info :value :timed-out)
:else
(assoc op :type :info :value r)))
(catch java.net.SocketException e
(assoc op :type :info :value (.getMessage e)))
(catch java.net.ConnectException e
(assoc op :type :fail :value (.getMessage e)))))
:read (try
(info "Waiting for recovery before read")
; Elasticsearch lies about cluster state during split brain
; woooooooo
(c/on-many (:nodes test) (wait 1000 :green))
(info "Elasticsearch reports green, but it's probably lying, so waiting another 10s")
(Thread/sleep (* 10 1000))
(info "Recovered; flushing index before read")
(esi/flush client index-name)
(assoc op :type :ok
:value (->> (esd/search client index-name "number"
:scroll "10s"
:size 20)
(esd/scroll-seq client)
(map (comp :num :_source))
(into (sorted-set))))
(catch RuntimeException e
(assoc op :type :fail :value (.getMessage e))))))
(teardown! [_ test]
(.close client)))
(defn create-set-client
"A set implemented by creating independent documents"
[]
(CreateSetClient. nil))
; Use ElasticSearch MVCC to do CAS read/write cycles, implementing a set.
(defrecord CASSetClient [mapping-type doc-id client]
client/Client
(setup! [_ test node]
(let [; client (es/connect [[(name node) 9300]])]
client (es/connect (str "http://" (name node) ":9200"))]
;; Create index
(try
(esi/create client index-name
:mappings {mapping-type {:properties {}}})
(catch clojure.lang.ExceptionInfo e
(when-not (index-already-exists-error? e)
(throw e))))
(esa/cluster-health client
{:index [index-name] :level "indices"
:wait_for_status "green"
:wait_for_nodes (count (:nodes test))})
; Initial empty set
(esd/create client index-name mapping-type {:values []} :id doc-id)
(CASSetClient. mapping-type doc-id client)))
(invoke! [this test op]
(case (:f op)
:add (timeout 5000 (assoc op :type :info :value :timed-out)
(let [current (esd/get client index-name mapping-type doc-id
:preference "_primary")]
(if-not (esr/found? current)
; Can't write without a read
(assoc op :type :fail)
(let [version (:_version current)
values (-> current :_source :values)
values' (vec (conj values (:value op)))
r (esd/put client index-name mapping-type doc-id
{:values values'}
:version version)]
(cond ; lol esr/ok? actually means :found, and
; esr/found? means :ok or :found, and
; *neither* of those appear in a successful
; conditional put, so I hope this is how
; you're supposed to interpret responses.
(esr/conflict? r)
(assoc op :type :fail)
(esr/timed-out? r)
(assoc op :type :info :value :timed-out)
:else (assoc op :type :ok))))))
:read (try
(info "Waiting for recovery before read")
(c/on-many (:nodes test) (wait 200 :green))
(info "Recovered; flushing index before read")
(esi/flush client index-name)
(assoc op :type :ok
:value (->> (esd/get client index-name mapping-type doc-id
:preference "_primary")
:_source
:values
(into (sorted-set)))))))
(teardown! [_ test]
(.close client)))
(defn cas-set-client []
(CASSetClient. "cas-sets" "0" nil))
; Nemeses
(defn mostly-small-nonempty-subset
"Returns a subset of the given collection, with a logarithmically decreasing
probability of selecting more elements. Always selects at least one element.
(->> #(mostly-small-nonempty-subset [1 2 3 4 5])
repeatedly
(map count)
(take 10000)
frequencies
sort)
; => ([1 3824] [2 2340] [3 1595] [4 1266] [5 975])"
[xs]
(-> xs
count
inc
Math/log
rand
Math/exp
long
(take (shuffle xs))))
(def isolate-self-primaries-nemesis
"A nemesis which completely isolates any node that thinks it is the primary."
(nemesis/partitioner
(fn [nodes]
(let [ps (self-primaries nodes)]
(nemesis/complete-grudge
; All nodes that aren't self-primaries in one partition
(cons (remove (set ps) nodes)
; Each self-primary in a different partition
(map list ps)))))))
(def crash-nemesis
"A nemesis that crashes a random subset of nodes."
(nemesis/node-start-stopper
mostly-small-nonempty-subset
(fn start [test node] (c/su (c/exec :killall :-9 :java)) [:killed node])
(fn stop [test node] (start! node) [:restarted node])))
(def crash-primary-nemesis
"A nemesis that crashes a random primary node."
(nemesis/node-start-stopper
(comp rand-nth self-primaries)
(fn start [test node] (c/su (c/exec :killall :-9 :java)) [:killed node])
(fn stop [test node] (start! node) [:restarted node])))
(defn adds
"Generator that emits :add operations for sequential integers."
[]
(->> (range)
(map (fn [x] {:type :invoke, :f :add, :value x}))
gen/seq))
(defn recover
"A generator which stops the nemesis and allows some time for recovery."
[]
(gen/nemesis
(gen/phases
(gen/once {:type :info, :f :stop})
(gen/sleep 20))))
(defn read-once
"A generator which reads exactly once."
[]
(gen/clients
(gen/once {:type :invoke, :f :read})))
(defn es-test
"Defaults for testing elasticsearch."
[name opts]
(merge tests/noop-test
{:name (str "elasticsearch " name)
:os debian/os
:db (db "1.5.0")}
opts))
(defn create-test
"A generic create test."
[name opts]
(es-test (str "create " name)
(merge {:client (create-set-client)
:model (model/set)
:checker (checker/compose
{:html timeline/html
:set checker/set
:latency (checker/latency-graph
"report")})}
opts)))
(defn create-isolate-primaries-test
"Inserts docs into a set while isolating all primaries with a partition."
[]
(create-test "isolate primaries"
{:nemesis isolate-self-primaries-nemesis
:generator (gen/phases
(->> (adds)
(gen/stagger 1/10)
(gen/delay 1)
(gen/nemesis
(gen/seq (cycle
[(gen/sleep 30)
{:type :info :f :start}
(gen/sleep 200)
{:type :info :f :stop}])))
(gen/time-limit 800))
(recover)
(read-once))}))
(defn create-pause-test
"Inserts docs into a set while pausing random primaries with SIGSTOP/SIGCONT."
[]
(create-test "pause"
{:nemesis (nemesis/hammer-time
(comp rand-nth self-primaries) "java")
:generator (gen/phases
(->> (adds)
(gen/stagger 1/10)
(gen/delay 1)
(gen/nemesis
(gen/seq (cycle
[(gen/sleep 10)
{:type :info :f :start}
(gen/sleep 120)
{:type :info :f :stop}])))
(gen/time-limit 600))
(recover)
(read-once))}))
(defn create-crash-test
"Inserts docs into a set while killing random nodes and restarting them."
[]
(create-test "crash"
{:nemesis crash-nemesis
:generator (gen/phases
(->> (adds)
(gen/stagger 1/10)
(gen/delay 1/10)
(gen/nemesis
(gen/seq (cycle
[(gen/sleep 1)
{:type :info :f :start}
(gen/sleep 1)
{:type :info :f :stop}])))
(gen/time-limit 600))
(recover)
(read-once))}))
(defn create-bridge-test
"Inserts docs into a set while weaving the network into happy little
intersecting majority rings"
[]
(create-test "bridge"
{:nemesis (nemesis/partitioner (comp nemesis/bridge shuffle))
:generator (gen/phases
(->> (adds)
(gen/stagger 1/10)
(gen/delay 1)
(gen/nemesis
(gen/seq (cycle
[(gen/sleep 10)
{:type :info, :f :start}
(gen/sleep 120)
{:type :info, :f :stop}])))
(gen/time-limit 600))
(recover)
(read-once))}))