/
rules.clj
513 lines (491 loc) · 18.2 KB
/
rules.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
(ns com.manigfeald.raft.rules
"rules are a combination of a head which is a predicate and a body
that updates and returns the passed in state. when a rule is applied
to a value, if the head returns true for a given value that value is
give to the body, and the updated value is returned, if the head
returns false the passed in value is returned unmodified.
syntactically rules have a third component, which is the name/binding
form that the value will be bound to in both the head and the body."
(:require [com.manigfeald.raft.core :refer :all]))
(defrecord Rule [head body]
clojure.lang.IFn
(invoke [this arg]
(if (head arg)
(let [r (binding [*log-context* (:name this)]
(body arg))]
#_(assert (or (zero? (:last-applied (:raft-state r)))
(contains? (get (:log (:raft-state r))
(:last-applied (:raft-state r)))
:return))
(with-out-str
(prn "rule" this)
(println "before:")
(prn arg)
(println "after:")
(prn r)
(println)))
(assert (>= (:commit-index (:raft-state r))
(:last-applied (:raft-state r)))
(with-out-str
(prn "rule" this)
(println "before:")
(prn arg)
(println "after:")
(prn r)
(println)))
[true r #_(update-in r [:applied-rules] conj this)])
[false arg])))
(defrecord CompoundRule [head subrules]
clojure.lang.IFn
(invoke [_ arg]
(reduce
(fn [[applied? arg] rule]
(if (head arg)
(let [[new-applied? new-arg] (rule arg)]
[(or applied? new-applied?) new-arg])
(reduced [applied? arg])))
[false arg]
subrules)))
(alter-meta! #'->CompoundRule assoc :no-doc true)
(alter-meta! #'->Rule assoc :no-doc true)
(alter-meta! #'map->CompoundRule assoc :no-doc true)
(alter-meta! #'map->Rule assoc :no-doc true)
(defmacro rule
"a rule is a combination of some way to decide if the rule
applies (the head) and some way to apply the rule (the body)"
([head body bindings]
(if-let [line (:line (meta &form))]
`(rule ~(str "line-" line)
~head ~body ~bindings)
`(->Rule (fn [~bindings]
~head)
(fn [~bindings]
~body))))
([name head body bindings]
`(assoc (->Rule (fn ~(symbol (str (clojure.core/name name) "-head"))
[~bindings]
~head)
(fn ~(symbol (str (clojure.core/name name) "-body"))
[~bindings]
~body))
:name ~name)))
(defmacro guard
"a guard is a rule with a normal rule head, but the body is a
composition of other rules"
[head & things]
(let [rules (butlast things)
bindings (last things)]
`(->CompoundRule (fn [~bindings]
~head)
~(vec rules))))
;;
(def keep-up-apply
(rule
(and (> commit-index last-applied)
;; only apply when the latest thing to apply is from the
;; current term, this deals with split writes
(log-entries-this-term-and-committed? raft-state))
(-> state
(update-in [:raft-state] advance-applied-to-commit))
{:as state
{:keys [commit-index last-applied current-term]
:as raft-state} :raft-state}))
(def jump-to-newer-term
(rule
(and message-term
(> message-term current-term))
(-> state
(log-trace "jump-to-newer-term" message-term)
(update-in [:raft-state] merge {:node-type :follower
:current-term message-term
:leader-id nil
:votes 0N
:voted-for nil}))
{:as state
:keys [id]
{{message-term :term} :message} :io
{:keys [current-term]} :raft-state}))
;;
(def follower-respond-to-append-entries-with-wrong-term
(rule
:fail-old-term
(> current-term message-term)
(-> state
(reject-append-entries leader-id current-term id)
(log-trace "rejecting append entries from" leader-id))
{:as state
:keys [id]
{{leader-id :leader-id
message-term :term} :message} :io
{:keys [current-term node-type]} :raft-state}))
(def follower-respond-to-append-entries-with-unknown-prev
(rule
:fail-missing-prevs
(and (= current-term message-term)
(not (log-contains? raft-state prev-log-term prev-log-index)))
(-> state
(log-trace "rejecting append entries from" leader-id)
(reject-append-entries leader-id current-term id))
{:as state
:keys [id]
{{prev-log-index :prev-log-index
prev-log-term :prev-log-term
leader-id :leader-id
message-term :term} :message} :io
{:keys [current-term node-type] :as raft-state} :raft-state}))
(def follower-respond-to-append-entries
(guard
(and (= :follower node-type)
(= message-type :append-entries))
#'follower-respond-to-append-entries-with-wrong-term
#'follower-respond-to-append-entries-with-unknown-prev
(rule
:good
(and (= message-term current-term)
(log-contains? raft-state prev-log-term prev-log-index))
(-> state
(update-in [:raft-state] insert-entries entries)
(accept-append-entries leader-id current-term id)
(as-> n
(if (> leader-commit commit-index)
(assoc-in n [:raft-state :commit-index]
(min leader-commit
(last-log-index (:raft-state n))))
n)))
{:as state
:keys [id]
{{prev-log-index :prev-log-index
prev-log-term :prev-log-term
message-term :term
entries :entries
leader-id :leader-id
leader-commit :leader-commit} :message} :io
{:keys [current-term node-type commit-index] :as raft-state} :raft-state})
{{{message-type :type
:as message} :message} :io
{:keys [node-type]} :raft-state}))
(def follower-respond-to-request-vote
(guard
(and (= :follower node-type)
(= message-type :request-vote))
(rule
:success
(and (or (= voted-for candidate-id)
(nil? voted-for))
(>= last-log-index (com.manigfeald.raft.core/last-log-index
raft-state))
(>= last-log-term (com.manigfeald.raft.core/last-log-term raft-state))
(or (log-contains? raft-state last-log-term last-log-index)
(nil? (log-entry-of raft-state last-log-index))))
(-> state
(log-trace "votes for" candidate-id "in" current-term)
(consume-message)
(assoc-in [:raft-state :voted-for] candidate-id)
(publish [{:type :request-vote-response
:target candidate-id
:term current-term
:success? true
:from id}]))
{:as state
:keys [id]
{{message-type :type
candidate-id :candidate-id
:keys [last-log-index last-log-term] :as message} :message} :io
{:keys [current-term node-type voted-for] :as raft-state} :raft-state})
(rule
(not (and (or (= voted-for candidate-id)
(nil? voted-for))
(or (log-contains? raft-state last-log-term last-log-index)
(and (:term (log-entry-of raft-state last-log-index))
(> last-log-term
(:term (log-entry-of raft-state last-log-index)))))))
(-> state
(log-trace "doesn't vote for" candidate-id "in" current-term
;; {:last-log-index last-log-index
;; :last-log-term last-log-term
;; :voted-for voted-for}
;; (com.manigfeald.raft.log/indices-and-terms
;; (:log (:raft-state state)))
)
(consume-message)
(publish [{:type :request-vote-response
:target candidate-id
:term current-term
:success? false
:from id}]))
{:as state
:keys [id]
{{message-type :type
candidate-id :candidate-id
:keys [last-log-index last-log-term]
:as message} :message} :io
{:keys [current-term node-type voted-for] :as raft-state} :raft-state})
{{{message-type :type} :message} :io
{:keys [node-type]} :raft-state}))
(def become-candidate-and-call-for-election
(rule
(and (= node-type :follower)
(>= now next-timeout))
(-> state
(log-trace "call for election")
(consume-message)
(update-in [:raft-state] merge
{:node-type :candidate
:votes 1
:voted-for id
:current-term (inc current-term)
:leader-id nil})
(assoc-in [:timer :next-timeout]
(+ now period))
(update-in [:io :out-queue] empty)
(publish (broadcast
node-set
{:type :request-vote
:candidate-id id
:last-log-index (last-log-index raft-state)
:last-log-term (last-log-term raft-state)
:term (inc current-term)
:from id})))
{:as state
:keys [id]
{:keys [now period next-timeout]} :timer
{:keys [current-term node-set node-type] :as raft-state} :raft-state}))
;;
(def candidate-receive-votes
(guard
(and (= node-type :candidate)
(= message-type :request-vote-response)
success?)
(rule
(enough-votes? (count node-set) (inc votes))
(-> state
(consume-message)
(log-trace "received vote from" from "in" current-term)
(log-trace "becoming leader with" (inc votes) "in" current-term)
(update-in [:raft-state] merge {:node-type :leader
:votes 0N
:voted-for nil
:leader-id id})
(update-in [:raft-leader-state] merge
{:match-index
(into {} (for [node node-set]
[node 0N]))
:next-index
(into {} (for [node node-set]
[node (inc (last-log-index raft-state))]))})
(assoc-in [:timer :next-timeout] (+ now period))
(update-in [:raft-state] add-to-log
{:term current-term
:payload nil
:operation-type :noop})
(publish (broadcast node-set
{:type :append-entries
:term current-term
:leader-id id
:prev-log-index (last-log-index raft-state)
:prev-log-term (last-log-term raft-state)
:entries []
:leader-commit commit-index
:from id})))
{:as state
:keys [id]
{{:keys [success? from]} :message} :io
{:keys [now period]} :timer
{:keys [votes node-set current-term commit-index]
:as raft-state} :raft-state})
(rule
(not (enough-votes? (count node-set) (inc votes)))
(-> state
(consume-message)
(log-trace "received vote from" from "in" current-term)
(update-in [:raft-state :votes] inc))
{:as state
:keys [id]
{{:keys [from]} :message} :io
{:keys [votes node-set current-term]} :raft-state})
{{{:keys [success?]
message-type :type} :message} :io
{:keys [node-type]} :raft-state}))
(def candidate-respond-to-append-entries
(rule
(and (= node-type :candidate)
(= message-type :append-entries))
(-> state
(log-trace "candidate-respond-to-append-entries")
(consume-message)
(update-in [:raft-state] merge {:node-type :follower
:voted-for leader-id
:votes 0N})
(assoc-in [:timer :next-timeout] (+ period now)))
{:as state
{{message-type :type
leader-id :leader-id} :message} :io
{:keys [node-type]} :raft-state
{:keys [period now]} :timer}))
(def candidate-election-timeout
(rule
(and (= node-type :candidate)
(>= now next-timeout))
(-> state
(log-trace "candidate-election-timeout")
(consume-message)
(update-in [:raft-state] merge
{:node-type :candidate
:votes 1
:voted-for id
:current-term (inc current-term)})
(assoc-in [:timer :next-timeout]
(+ now period))
(publish (broadcast
node-set
{:type :request-vote
:candidate-id id
:last-log-index (last-log-index raft-state)
:last-log-term (last-log-term raft-state)
:term (inc current-term)
:from id})))
{:as state
:keys [id]
{:keys [now next-timeout period]} :timer
{:keys [node-type current-term node-set] :as raft-state} :raft-state}))
;;
(def heart-beat
(rule
(and (= node-type :leader)
(>= now next-timeout))
(-> state
(publish (broadcast node-set
{:type :append-entries
:term current-term
:leader-id id
:prev-log-index (last-log-index raft-state)
:prev-log-term (last-log-term raft-state)
:entries []
:leader-commit commit-index
:from id})))
{:as state
:keys [id]
{:keys [now next-timeout]} :timer
{:keys [node-type node-set current-term commit-index] :as raft-state}
:raft-state}))
;; TODO: need a test to ensure that the leaders last-log-index is
;; considered when deciding to advance the commit index
(def leader-receive-command
(rule
(and (= node-type :leader)
(= message-type :operation))
(-> state
(log-trace "received command serial" (:serial message))
(consume-message)
(update-in [:raft-state] add-to-log (assoc (dissoc message :type) :term current-term))
(as-> state
(assoc-in state [:raft-leader-state :match-index id]
(last-log-index (:raft-state state)))))
{:as state
:keys [id]
{{:as message message-type :type} :message} :io
{:keys [match-index]} :raft-leader-state
{:keys [current-term node-type commit-index node-set]
:as raft-state} :raft-state}))
;; TODO: rate limit this rule
(def update-followers
(rule
(and (= :leader node-type)
(seq (for [[node next-index] next-index
:when (>= (last-log-index raft-state) next-index)]
node)))
(-> state
;; (log-trace "update followers")
(publish (for [[node next-index] next-index
:when (not= node id)
:when (>= (last-log-index raft-state) next-index)]
{:type :append-entries
:target node
:term current-term
:leader-id id
:prev-log-index (max 0N (dec next-index))
:prev-log-term (or (:term (log-entry-of raft-state
(dec next-index)))
0N)
:entries (for [index (range (max 0N (dec next-index))
(inc next-index))
:when (not (zero? index))]
(dissoc (log-entry-of raft-state index) :return))
:leader-commit commit-index
:from id})))
{:as state
:keys [id]
{:keys [next-index]} :raft-leader-state
{:keys [now next-timeout period]} :timer
{:as raft-state
:keys [commit-index current-term node-type]} :raft-state}))
(def update-commit
(rule
:update-commit
(and (= :leader node-type)
(possible-new-commit commit-index raft-state match-index node-set
current-term))
(-> state
(log-trace "update-commit"
(possible-new-commit
commit-index raft-state match-index node-set current-term)
"match-index value frequencies"
(frequencies
(vals (:match-index (:raft-leader-state state)))))
(assoc-in [:raft-state :commit-index]
(possible-new-commit
commit-index raft-state match-index node-set current-term)))
{:as state
{:keys [match-index]} :raft-leader-state
{:keys [commit-index node-set current-term node-type]
:as raft-state} :raft-state}))
(def leader-handle-append-entries-response
(guard
(and (= node-type :leader)
(= message-type :append-entries-response))
(rule
(not success?)
(-> state
(consume-message)
(update-in [:raft-leader-state :next-index from] dec)
(update-in [:raft-leader-state :next-index from] max 0N))
{:as state
{{:keys [success? from]} :message} :io})
(rule
success?
(-> state
(consume-message)
(assoc-in [:raft-leader-state :next-index from] (inc last-log-index))
(assoc-in [:raft-leader-state :match-index from] last-log-index))
{:as state
{{:keys [success? from last-log-index]} :message} :io})
{:as state
{{message-type :type} :message} :io
{:keys [node-type]} :raft-state}))
(def ignore-messages-from-old-terms
(rule
(and message-term
(> current-term message-term))
(-> state
(consume-message))
{:as state
{{message-term :term} :message} :io
{:keys [current-term]} :raft-state}))
(def rules-of-raft
(guard
true
#'ignore-messages-from-old-terms
#'keep-up-apply
#'jump-to-newer-term
#'follower-respond-to-append-entries
#'follower-respond-to-request-vote
#'become-candidate-and-call-for-election
#'candidate-receive-votes
#'candidate-respond-to-append-entries
#'candidate-election-timeout
#'heart-beat
#'leader-receive-command
#'update-followers
#'update-commit
#'leader-handle-append-entries-response
_))