Skip to content

Commit

Permalink
Add semgrep rule to catch non-determinism in FSM
Browse files Browse the repository at this point in the history
See `message:` in rule for details.
  • Loading branch information
schmichael committed Jul 12, 2022
1 parent 7b55f7a commit 2a2bd0d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .semgrep/fsm_time.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
rules:
- id: "no-time-in-fsm"
patterns:
- pattern: time.Now()

# Metric state is local to the server and therefore must use time.
- pattern-not-inside: |
defer metrics.MeasureSince(..., time.Now())
# The timetable's whole point is to roughly track timestamps for Raft log
# indexes, so it must use time.
- pattern-not-inside: |
$N.timetable.Witness(log.Index, time.Now().UTC())
message: |
time.Now() should not be called from within the Server's FSM. Apply Raft
log messages to the State Store must be deterministic so that each server
contains exactly the same state. Since time drifts between nodes, it must
be set before the Raft log message is applied so that all Raft members
see the same timestamp.
languages:
- "go"
severity: "WARNING"
paths:
include:
- "nomad/fsm.*"
- "nomad/state/state_store.*"

0 comments on commit 2a2bd0d

Please sign in to comment.