state.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from twisted.internet import defer
  16. from synapse.util.logutils import log_function
  17. from synapse.util.caches.expiringcache import ExpiringCache
  18. from synapse.util.metrics import Measure
  19. from synapse.api.constants import EventTypes
  20. from synapse.api.errors import AuthError
  21. from synapse.api.auth import AuthEventTypes
  22. from synapse.events.snapshot import EventContext
  23. from synapse.util.async import Linearizer
  24. from collections import namedtuple
  25. from frozendict import frozendict
  26. import logging
  27. import hashlib
  28. import os
  29. logger = logging.getLogger(__name__)
  30. KeyStateTuple = namedtuple("KeyStateTuple", ("context", "type", "state_key"))
  31. CACHE_SIZE_FACTOR = float(os.environ.get("SYNAPSE_CACHE_FACTOR", 0.1))
  32. SIZE_OF_CACHE = int(1000 * CACHE_SIZE_FACTOR)
  33. EVICTION_TIMEOUT_SECONDS = 60 * 60
  34. _NEXT_STATE_ID = 1
  35. def _gen_state_id():
  36. global _NEXT_STATE_ID
  37. s = "X%d" % (_NEXT_STATE_ID,)
  38. _NEXT_STATE_ID += 1
  39. return s
  40. class _StateCacheEntry(object):
  41. __slots__ = ["state", "state_group", "state_id", "prev_group", "delta_ids"]
  42. def __init__(self, state, state_group, prev_group=None, delta_ids=None):
  43. self.state = frozendict(state)
  44. self.state_group = state_group
  45. self.prev_group = prev_group
  46. self.delta_ids = frozendict(delta_ids) if delta_ids is not None else None
  47. # The `state_id` is a unique ID we generate that can be used as ID for
  48. # this collection of state. Usually this would be the same as the
  49. # state group, but on worker instances we can't generate a new state
  50. # group each time we resolve state, so we generate a separate one that
  51. # isn't persisted and is used solely for caches.
  52. # `state_id` is either a state_group (and so an int) or a string. This
  53. # ensures we don't accidentally persist a state_id as a stateg_group
  54. if state_group:
  55. self.state_id = state_group
  56. else:
  57. self.state_id = _gen_state_id()
  58. class StateHandler(object):
  59. """ Responsible for doing state conflict resolution.
  60. """
  61. def __init__(self, hs):
  62. self.clock = hs.get_clock()
  63. self.store = hs.get_datastore()
  64. self.hs = hs
  65. # dict of set of event_ids -> _StateCacheEntry.
  66. self._state_cache = None
  67. self.resolve_linearizer = Linearizer()
  68. def start_caching(self):
  69. logger.debug("start_caching")
  70. self._state_cache = ExpiringCache(
  71. cache_name="state_cache",
  72. clock=self.clock,
  73. max_len=SIZE_OF_CACHE,
  74. expiry_ms=EVICTION_TIMEOUT_SECONDS * 1000,
  75. reset_expiry_on_get=True,
  76. )
  77. self._state_cache.start()
  78. @defer.inlineCallbacks
  79. def get_current_state(self, room_id, event_type=None, state_key="",
  80. latest_event_ids=None):
  81. """ Retrieves the current state for the room. This is done by
  82. calling `get_latest_events_in_room` to get the leading edges of the
  83. event graph and then resolving any of the state conflicts.
  84. This is equivalent to getting the state of an event that were to send
  85. next before receiving any new events.
  86. If `event_type` is specified, then the method returns only the one
  87. event (or None) with that `event_type` and `state_key`.
  88. Returns:
  89. map from (type, state_key) to event
  90. """
  91. if not latest_event_ids:
  92. latest_event_ids = yield self.store.get_latest_event_ids_in_room(room_id)
  93. ret = yield self.resolve_state_groups(room_id, latest_event_ids)
  94. state = ret.state
  95. if event_type:
  96. event_id = state.get((event_type, state_key))
  97. event = None
  98. if event_id:
  99. event = yield self.store.get_event(event_id, allow_none=True)
  100. defer.returnValue(event)
  101. return
  102. state_map = yield self.store.get_events(state.values(), get_prev_content=False)
  103. state = {
  104. key: state_map[e_id] for key, e_id in state.items() if e_id in state_map
  105. }
  106. defer.returnValue(state)
  107. @defer.inlineCallbacks
  108. def get_current_state_ids(self, room_id, event_type=None, state_key="",
  109. latest_event_ids=None):
  110. if not latest_event_ids:
  111. latest_event_ids = yield self.store.get_latest_event_ids_in_room(room_id)
  112. ret = yield self.resolve_state_groups(room_id, latest_event_ids)
  113. state = ret.state
  114. if event_type:
  115. defer.returnValue(state.get((event_type, state_key)))
  116. return
  117. defer.returnValue(state)
  118. @defer.inlineCallbacks
  119. def get_current_user_in_room(self, room_id, latest_event_ids=None):
  120. if not latest_event_ids:
  121. latest_event_ids = yield self.store.get_latest_event_ids_in_room(room_id)
  122. entry = yield self.resolve_state_groups(room_id, latest_event_ids)
  123. joined_users = yield self.store.get_joined_users_from_state(
  124. room_id, entry.state_id, entry.state
  125. )
  126. defer.returnValue(joined_users)
  127. @defer.inlineCallbacks
  128. def compute_event_context(self, event, old_state=None):
  129. """ Fills out the context with the `current state` of the graph. The
  130. `current state` here is defined to be the state of the event graph
  131. just before the event - i.e. it never includes `event`
  132. If `event` has `auth_events` then this will also fill out the
  133. `auth_events` field on `context` from the `current_state`.
  134. Args:
  135. event (EventBase)
  136. Returns:
  137. an EventContext
  138. """
  139. context = EventContext()
  140. if event.internal_metadata.is_outlier():
  141. # If this is an outlier, then we know it shouldn't have any current
  142. # state. Certainly store.get_current_state won't return any, and
  143. # persisting the event won't store the state group.
  144. if old_state:
  145. context.prev_state_ids = {
  146. (s.type, s.state_key): s.event_id for s in old_state
  147. }
  148. if event.is_state():
  149. context.current_state_events = dict(context.prev_state_ids)
  150. key = (event.type, event.state_key)
  151. context.current_state_events[key] = event.event_id
  152. else:
  153. context.current_state_events = context.prev_state_ids
  154. else:
  155. context.current_state_ids = {}
  156. context.prev_state_ids = {}
  157. context.prev_state_events = []
  158. context.state_group = self.store.get_next_state_group()
  159. defer.returnValue(context)
  160. if old_state:
  161. context.prev_state_ids = {
  162. (s.type, s.state_key): s.event_id for s in old_state
  163. }
  164. context.state_group = self.store.get_next_state_group()
  165. if event.is_state():
  166. key = (event.type, event.state_key)
  167. if key in context.prev_state_ids:
  168. replaces = context.prev_state_ids[key]
  169. if replaces != event.event_id: # Paranoia check
  170. event.unsigned["replaces_state"] = replaces
  171. context.current_state_ids = dict(context.prev_state_ids)
  172. context.current_state_ids[key] = event.event_id
  173. else:
  174. context.current_state_ids = context.prev_state_ids
  175. context.prev_state_events = []
  176. defer.returnValue(context)
  177. if event.is_state():
  178. entry = yield self.resolve_state_groups(
  179. event.room_id, [e for e, _ in event.prev_events],
  180. event_type=event.type,
  181. state_key=event.state_key,
  182. )
  183. else:
  184. entry = yield self.resolve_state_groups(
  185. event.room_id, [e for e, _ in event.prev_events],
  186. )
  187. curr_state = entry.state
  188. context.prev_state_ids = curr_state
  189. if event.is_state():
  190. context.state_group = self.store.get_next_state_group()
  191. key = (event.type, event.state_key)
  192. if key in context.prev_state_ids:
  193. replaces = context.prev_state_ids[key]
  194. event.unsigned["replaces_state"] = replaces
  195. context.current_state_ids = dict(context.prev_state_ids)
  196. context.current_state_ids[key] = event.event_id
  197. context.prev_group = entry.prev_group
  198. context.delta_ids = entry.delta_ids
  199. if context.delta_ids is not None:
  200. context.delta_ids = dict(context.delta_ids)
  201. context.delta_ids[key] = event.event_id
  202. else:
  203. if entry.state_group is None:
  204. entry.state_group = self.store.get_next_state_group()
  205. entry.state_id = entry.state_group
  206. context.state_group = entry.state_group
  207. context.current_state_ids = context.prev_state_ids
  208. context.prev_group = entry.prev_group
  209. context.delta_ids = entry.delta_ids
  210. context.prev_state_events = []
  211. defer.returnValue(context)
  212. @defer.inlineCallbacks
  213. @log_function
  214. def resolve_state_groups(self, room_id, event_ids, event_type=None, state_key=""):
  215. """ Given a list of event_ids this method fetches the state at each
  216. event, resolves conflicts between them and returns them.
  217. Returns:
  218. a Deferred tuple of (`state_group`, `state`, `prev_state`).
  219. `state_group` is the name of a state group if one and only one is
  220. involved. `state` is a map from (type, state_key) to event, and
  221. `prev_state` is a list of event ids.
  222. """
  223. logger.debug("resolve_state_groups event_ids %s", event_ids)
  224. state_groups_ids = yield self.store.get_state_groups_ids(
  225. room_id, event_ids
  226. )
  227. logger.debug(
  228. "resolve_state_groups state_groups %s",
  229. state_groups_ids.keys()
  230. )
  231. group_names = frozenset(state_groups_ids.keys())
  232. if len(group_names) == 1:
  233. name, state_list = state_groups_ids.items().pop()
  234. defer.returnValue(_StateCacheEntry(
  235. state=state_list,
  236. state_group=name,
  237. prev_group=name,
  238. delta_ids={},
  239. ))
  240. with (yield self.resolve_linearizer.queue(group_names)):
  241. if self._state_cache is not None:
  242. cache = self._state_cache.get(group_names, None)
  243. if cache:
  244. defer.returnValue(cache)
  245. logger.info(
  246. "Resolving state for %s with %d groups", room_id, len(state_groups_ids)
  247. )
  248. state = {}
  249. for st in state_groups_ids.values():
  250. for key, e_id in st.items():
  251. state.setdefault(key, set()).add(e_id)
  252. conflicted_state = {
  253. k: list(v)
  254. for k, v in state.items()
  255. if len(v) > 1
  256. }
  257. if conflicted_state:
  258. logger.info("Resolving conflicted state for %r", room_id)
  259. state_map = yield self.store.get_events(
  260. [e_id for st in state_groups_ids.values() for e_id in st.values()],
  261. get_prev_content=False
  262. )
  263. state_sets = [
  264. [state_map[e_id] for key, e_id in st.items() if e_id in state_map]
  265. for st in state_groups_ids.values()
  266. ]
  267. new_state, _ = self._resolve_events(
  268. state_sets, event_type, state_key
  269. )
  270. new_state = {
  271. key: e.event_id for key, e in new_state.items()
  272. }
  273. else:
  274. new_state = {
  275. key: e_ids.pop() for key, e_ids in state.items()
  276. }
  277. state_group = None
  278. new_state_event_ids = frozenset(new_state.values())
  279. for sg, events in state_groups_ids.items():
  280. if new_state_event_ids == frozenset(e_id for e_id in events):
  281. state_group = sg
  282. break
  283. if state_group is None:
  284. # Worker instances don't have access to this method, but we want
  285. # to set the state_group on the main instance to increase cache
  286. # hits.
  287. if hasattr(self.store, "get_next_state_group"):
  288. state_group = self.store.get_next_state_group()
  289. prev_group = None
  290. delta_ids = None
  291. for old_group, old_ids in state_groups_ids.items():
  292. if not set(new_state.iterkeys()) - set(old_ids.iterkeys()):
  293. n_delta_ids = {
  294. k: v
  295. for k, v in new_state.items()
  296. if old_ids.get(k) != v
  297. }
  298. if not delta_ids or len(n_delta_ids) < len(delta_ids):
  299. prev_group = old_group
  300. delta_ids = n_delta_ids
  301. cache = _StateCacheEntry(
  302. state=new_state,
  303. state_group=state_group,
  304. prev_group=prev_group,
  305. delta_ids=delta_ids,
  306. )
  307. if self._state_cache is not None:
  308. self._state_cache[group_names] = cache
  309. defer.returnValue(cache)
  310. def resolve_events(self, state_sets, event):
  311. logger.info(
  312. "Resolving state for %s with %d groups", event.room_id, len(state_sets)
  313. )
  314. if event.is_state():
  315. return self._resolve_events(
  316. state_sets, event.type, event.state_key
  317. )
  318. else:
  319. return self._resolve_events(state_sets)
  320. def _resolve_events(self, state_sets, event_type=None, state_key=""):
  321. """
  322. Returns
  323. (dict[(str, str), synapse.events.FrozenEvent], list[str]): a tuple
  324. (new_state, prev_states). new_state is a map from (type, state_key)
  325. to event. prev_states is a list of event_ids.
  326. """
  327. with Measure(self.clock, "state._resolve_events"):
  328. state = {}
  329. for st in state_sets:
  330. for e in st:
  331. state.setdefault(
  332. (e.type, e.state_key),
  333. {}
  334. )[e.event_id] = e
  335. unconflicted_state = {
  336. k: v.values()[0] for k, v in state.items()
  337. if len(v.values()) == 1
  338. }
  339. conflicted_state = {
  340. k: v.values()
  341. for k, v in state.items()
  342. if len(v.values()) > 1
  343. }
  344. if event_type:
  345. prev_states_events = conflicted_state.get(
  346. (event_type, state_key), []
  347. )
  348. prev_states = [s.event_id for s in prev_states_events]
  349. else:
  350. prev_states = []
  351. auth_events = {
  352. k: e for k, e in unconflicted_state.items()
  353. if k[0] in AuthEventTypes
  354. }
  355. try:
  356. resolved_state = self._resolve_state_events(
  357. conflicted_state, auth_events
  358. )
  359. except:
  360. logger.exception("Failed to resolve state")
  361. raise
  362. new_state = unconflicted_state
  363. new_state.update(resolved_state)
  364. return new_state, prev_states
  365. @log_function
  366. def _resolve_state_events(self, conflicted_state, auth_events):
  367. """ This is where we actually decide which of the conflicted state to
  368. use.
  369. We resolve conflicts in the following order:
  370. 1. power levels
  371. 2. join rules
  372. 3. memberships
  373. 4. other events.
  374. """
  375. resolved_state = {}
  376. power_key = (EventTypes.PowerLevels, "")
  377. if power_key in conflicted_state:
  378. events = conflicted_state[power_key]
  379. logger.debug("Resolving conflicted power levels %r", events)
  380. resolved_state[power_key] = self._resolve_auth_events(
  381. events, auth_events)
  382. auth_events.update(resolved_state)
  383. for key, events in conflicted_state.items():
  384. if key[0] == EventTypes.JoinRules:
  385. logger.debug("Resolving conflicted join rules %r", events)
  386. resolved_state[key] = self._resolve_auth_events(
  387. events,
  388. auth_events
  389. )
  390. auth_events.update(resolved_state)
  391. for key, events in conflicted_state.items():
  392. if key[0] == EventTypes.Member:
  393. logger.debug("Resolving conflicted member lists %r", events)
  394. resolved_state[key] = self._resolve_auth_events(
  395. events,
  396. auth_events
  397. )
  398. auth_events.update(resolved_state)
  399. for key, events in conflicted_state.items():
  400. if key not in resolved_state:
  401. logger.debug("Resolving conflicted state %r:%r", key, events)
  402. resolved_state[key] = self._resolve_normal_events(
  403. events, auth_events
  404. )
  405. return resolved_state
  406. def _resolve_auth_events(self, events, auth_events):
  407. reverse = [i for i in reversed(self._ordered_events(events))]
  408. auth_events = dict(auth_events)
  409. prev_event = reverse[0]
  410. for event in reverse[1:]:
  411. auth_events[(prev_event.type, prev_event.state_key)] = prev_event
  412. try:
  413. # FIXME: hs.get_auth() is bad style, but we need to do it to
  414. # get around circular deps.
  415. # The signatures have already been checked at this point
  416. self.hs.get_auth().check(event, auth_events, do_sig_check=False)
  417. prev_event = event
  418. except AuthError:
  419. return prev_event
  420. return event
  421. def _resolve_normal_events(self, events, auth_events):
  422. for event in self._ordered_events(events):
  423. try:
  424. # FIXME: hs.get_auth() is bad style, but we need to do it to
  425. # get around circular deps.
  426. # The signatures have already been checked at this point
  427. self.hs.get_auth().check(event, auth_events, do_sig_check=False)
  428. return event
  429. except AuthError:
  430. pass
  431. # Use the last event (the one with the least depth) if they all fail
  432. # the auth check.
  433. return event
  434. def _ordered_events(self, events):
  435. def key_func(e):
  436. return -int(e.depth), hashlib.sha1(e.event_id).hexdigest()
  437. return sorted(events, key=key_func)