Update architecture-state-store.md (#15049)

This commit is contained in:
Charlie Voiselle 2022-10-27 14:03:43 -04:00 committed by GitHub
parent 8ac41c167f
commit d57e333534
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 23 deletions

View File

@ -13,37 +13,37 @@ This architecture has a few implications:
the state store. These values must be provided as parameters from the RPC the state store. These values must be provided as parameters from the RPC
handler. handler.
```go ```go
# Incorrect: generating a timestamp in the state store is not deterministic. # Incorrect: generating a timestamp in the state store is not deterministic.
func (s *StateStore) UpsertObject(...) { func (s *StateStore) UpsertObject(...) {
# ... # ...
obj.CreateTime = time.Now() obj.CreateTime = time.Now()
# ... # ...
} }
# Correct: non-deterministic values should be passed as inputs: # Correct: non-deterministic values should be passed as inputs:
func (s *StateStore) UpsertObject(..., timestamp time.Time) { func (s *StateStore) UpsertObject(..., timestamp time.Time) {
# ... # ...
obj.CreateTime = timestamp obj.CreateTime = timestamp
# ... # ...
} }
``` ```
* Every object you read from the state store must be copied before it can be * Every object you read from the state store must be copied before it can be
mutated, because mutating the object modifies it outside the raft mutated, because mutating the object modifies it outside the raft
workflow. The result can be servers having inconsistent state, transactions workflow. The result can be servers having inconsistent state, transactions
breaking, or even server panics. breaking, or even server panics.
```go ```go
# Incorrect: job is mutated without copying. # Incorrect: job is mutated without copying.
job, err := state.JobByID(ws, namespace, id) job, err := state.JobByID(ws, namespace, id)
job.Status = structs.JobStatusRunning job.Status = structs.JobStatusRunning
# Correct: only the job copy is mutated. # Correct: only the job copy is mutated.
job, err := state.JobByID(ws, namespace, id) job, err := state.JobByID(ws, namespace, id)
updateJob := job.Copy() updateJob := job.Copy()
updateJob.Status = structs.JobStatusRunning updateJob.Status = structs.JobStatusRunning
``` ```
Adding new objects to the state store should be done as part of adding new RPC Adding new objects to the state store should be done as part of adding new RPC
endpoints. See the [RPC Endpoint Checklist][]. endpoints. See the [RPC Endpoint Checklist][].