From 860b1b4841618ec401c3652dcf19ac8b5d167f50 Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Thu, 12 Feb 2015 19:11:52 +0000 Subject: [PATCH 1/7] Only attempt to fetch presence state of JOINed members in room initialSync (SYN-202) --- synapse/handlers/message.py | 1 + 1 file changed, 1 insertion(+) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index 3355adefc..c6d6188d9 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -372,6 +372,7 @@ class MessageHandler(BaseHandler): room_members = [ m for m in current_state.values() if m.type == EventTypes.Member + and m.content["membership"] == Membership.JOIN ] presence_handler = self.hs.get_handlers().presence_handler From 29805213d1c20c89c20a4b3075c30597b4402338 Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Thu, 12 Feb 2015 19:12:57 +0000 Subject: [PATCH 2/7] Can now remove the FIXME too --- synapse/handlers/message.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index c6d6188d9..5f4cc1a84 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -386,16 +386,9 @@ class MessageHandler(BaseHandler): ) presence.append(member_presence) except SynapseError as e: - if e.code == 404: - # FIXME: We are doing this as a warn since this gets hit a - # lot and spams the logs. Why is this happening? - logger.warn( - "Failed to get member presence of %r", m.user_id - ) - else: - logger.exception( - "Failed to get member presence of %r", m.user_id - ) + logger.exception( + "Failed to get member presence of %r", m.user_id + ) time_now = self.clock.time_msec() From 5f6e6530d0f43c637e527cfe1797801879dbc389 Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Thu, 12 Feb 2015 19:15:23 +0000 Subject: [PATCH 3/7] Appease pyflakes --- synapse/handlers/message.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index 5f4cc1a84..e028cfc2e 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -385,7 +385,7 @@ class MessageHandler(BaseHandler): as_event=True, ) presence.append(member_presence) - except SynapseError as e: + except SynapseError: logger.exception( "Failed to get member presence of %r", m.user_id ) From 93d07c87dc9b837492fa0f691a77e716a2fe512b Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Thu, 12 Feb 2015 19:19:37 +0000 Subject: [PATCH 4/7] Reindent code to be less human-readable to keep pep8 from complaining --- synapse/handlers/message.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index e028cfc2e..7b9685be7 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -372,7 +372,7 @@ class MessageHandler(BaseHandler): room_members = [ m for m in current_state.values() if m.type == EventTypes.Member - and m.content["membership"] == Membership.JOIN + and m.content["membership"] == Membership.JOIN ] presence_handler = self.hs.get_handlers().presence_handler From 8c652a2b5f4ed07abd682543ad6a365c6d50b8d3 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Fri, 13 Feb 2015 14:20:05 +0000 Subject: [PATCH 5/7] When we see a difference in current state, actually use state conflict resolution algorithm --- synapse/handlers/federation.py | 34 +++++++++++++++++++++++++++ synapse/state.py | 43 +++++++++++++++++++++++----------- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/synapse/handlers/federation.py b/synapse/handlers/federation.py index 77c81fe2d..5b225ffb5 100644 --- a/synapse/handlers/federation.py +++ b/synapse/handlers/federation.py @@ -858,6 +858,40 @@ class FederationHandler(BaseHandler): # Do auth conflict res. logger.debug("Different auth: %s", different_auth) + different_events = yield defer.gatherResults( + [ + self.store.get_event( + d, + allow_none=True, + allow_rejected=False, + ) + for d in different_auth + if d in have_events and not have_events[d] + ], + consumeErrors=True + ) + + if different_events: + local_view = dict(auth_events) + remote_view = dict(auth_events) + remote_view.update({ + (d.type, d.state_key) for d in different_events + }) + + new_state, _ = self.state.resolve_events( + [local_view, remote_view], + event + ) + + auth_events.update(new_state) + + current_state = set(e.event_id for e in auth_events.values()) + different_auth = event_auth_events - current_state + + context.current_state.update(auth_events) + context.state_group = None + + if different_auth and not event.internal_metadata.is_outlier(): # Only do auth resolution if we have something new to say. # We can't rove an auth failure. do_resolution = False diff --git a/synapse/state.py b/synapse/state.py index 98aaa2be5..fe5f3dc84 100644 --- a/synapse/state.py +++ b/synapse/state.py @@ -259,13 +259,37 @@ class StateHandler(object): defer.returnValue((name, state, prev_states)) + new_state, prev_states = self._resolve_events( + state_groups.values(), event_type, state_key + ) + + if self._state_cache is not None: + cache = _StateCacheEntry( + state=new_state, + state_group=None, + ts=self.clock.time_msec() + ) + + self._state_cache[frozenset(event_ids)] = cache + + defer.returnValue((None, new_state, prev_states)) + + def resolve_events(self, state_sets, event): + if event.is_state(): + return self._resolve_events( + state_sets, event.type, event.state_key + ) + else: + return self._resolve_events(state_sets) + + def _resolve_events(self, state_sets, event_type=None, state_key=""): state = {} - for group, g_state in state_groups.items(): - for s in g_state: + for st in state_sets: + for e in st: state.setdefault( - (s.type, s.state_key), + (e.type, e.state_key), {} - )[s.event_id] = s + )[e.event_id] = e unconflicted_state = { k: v.values()[0] for k, v in state.items() @@ -302,16 +326,7 @@ class StateHandler(object): new_state = unconflicted_state new_state.update(resolved_state) - if self._state_cache is not None: - cache = _StateCacheEntry( - state=new_state, - state_group=None, - ts=self.clock.time_msec() - ) - - self._state_cache[frozenset(event_ids)] = cache - - defer.returnValue((None, new_state, prev_states)) + return new_state, prev_states @log_function def _resolve_state_events(self, conflicted_state, auth_events): From e441c10a7302a3b0ddfa3caa7823fa6f62372f82 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Fri, 13 Feb 2015 14:23:39 +0000 Subject: [PATCH 6/7] pyflakes --- synapse/handlers/federation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/handlers/federation.py b/synapse/handlers/federation.py index 5b225ffb5..d667d358a 100644 --- a/synapse/handlers/federation.py +++ b/synapse/handlers/federation.py @@ -878,7 +878,7 @@ class FederationHandler(BaseHandler): (d.type, d.state_key) for d in different_events }) - new_state, _ = self.state.resolve_events( + new_state, prev_state = self.state.resolve_events( [local_view, remote_view], event ) From 0546126cc5fb1a138c96481061084e9e5fc86e34 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Fri, 13 Feb 2015 14:36:40 +0000 Subject: [PATCH 7/7] Bump version --- synapse/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/__init__.py b/synapse/__init__.py index c0aa2a757..1060fcc86 100644 --- a/synapse/__init__.py +++ b/synapse/__init__.py @@ -16,4 +16,4 @@ """ This is a reference implementation of a Matrix home server. """ -__version__ = "0.7.0b" +__version__ = "0.7.0c"