Advertisement
Guest User

synapse state explanation

a guest
Jul 28th, 2021
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. ## Introduction to the state tables
  2. ### What is state?
  3. State is things like who is in a room, what the room topic/name is, who has
  4. what priviledge levels etc. Synapse keeps track of it so that it can spot invalid
  5. events (e.g. ones sent by banned users, or by people with insufficient priviledge).
  6.  
  7. ### What is a state group?
  8.  
  9. Synapse needs to keep track of the state at the moment of each event. A state group
  10. corresponds to a unique state. The database table `event_to_state_groups` keeps track
  11. of the mapping from event ids to state group ids.
  12.  
  13. Consider the following simplified example:
  14. ```
  15. State group id | State
  16. _____________________________________________
  17. 1 | Alice in room
  18. 2 | Alice in room, Bob in room
  19. 3 | Bob in room
  20.  
  21.  
  22. Event id | What the event was
  23. ______________________________________
  24. 1 | Alice sends a message
  25. 3 | Bob joins the room
  26. 4 | Bob sends a message
  27. 5 | Alice leaves the room
  28. 6 | Bob sends a message
  29.  
  30.  
  31. Event id | State group id
  32. _________________________
  33. 1 | 1
  34. 2 | 1
  35. 3 | 2
  36. 4 | 2
  37. 5 | 3
  38. 6 | 3
  39. ```
  40. ### What are deltas and predecessors?
  41. When a new state event happens (e.g. Bob joins the room) a new state group is created.
  42. BUT instead of copying all of the state from the previous state group, we just store
  43. the change from the previous group (saving on lots of storage space!). The difference
  44. from the previous state group is called the "delta"
  45.  
  46. So for the previous example we would have the following (Note only rows 1 and two will
  47. make sense at this point):
  48.  
  49. ```
  50. State group id | Previous state group id | Delta
  51. ____________________________________________________________
  52. 1 | NONE | Alice in room
  53. 2 | 1 | Bob in room
  54. 3 | NONE | Bob in room
  55. ```
  56. So what happened with row 3? Well the way that deltas work in synapse is that they
  57. can only add new state or overwrite old state, but they cannot remove it. (So if the
  58. room topic is changed then that is just overwriting state, but removing alice from
  59. the room is neither an addition or an overwriting). If it is impossible to find
  60. a delta, then you just start from scratch again with a "snapshot" of the entire state.
  61.  
  62. (NOTE this might not be how synapse handles leaving rooms but it works for illustrative
  63. purposes)
  64.  
  65. The state of a state group is worked out by following the previous state group's and adding
  66. together all of the deltas (with the most recent taking precedence)
  67.  
  68. The mapping from state group to previous state group takes place in `state_group_edges`
  69. and the deltas are stored in `state_groups_state`
  70.  
  71. ### What are we compressing then?
  72. In order to speed up the converstion from state group id to state, there is a limit of 100
  73. hops set by synapse (that is we will only ever have to lookup the deltas for a maximum of
  74. 100 state groups). It does this by taking another "snapshot" every 100 state groups.
  75.  
  76. However, it is these snapshots that take up the bulk of the storage in a synapse database,
  77. so we want to find a way to reduce the number of them without dramatically increasing the
  78. maximum number of hops needed to do lookups.
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement