Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Fix client_reader worker being able to handle /context requests #3597

Merged
merged 4 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/3597.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for client_reader to handle more APIs
2 changes: 2 additions & 0 deletions synapse/app/client_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from synapse.metrics import RegistryProxy
from synapse.metrics.resource import METRICS_PREFIX, MetricsResource
from synapse.replication.slave.storage._base import BaseSlavedStore
from synapse.replication.slave.storage.account_data import SlavedAccountDataStore
from synapse.replication.slave.storage.appservice import SlavedApplicationServiceStore
from synapse.replication.slave.storage.client_ips import SlavedClientIpStore
from synapse.replication.slave.storage.directory import DirectoryStore
Expand Down Expand Up @@ -58,6 +59,7 @@


class ClientReaderSlavedStore(
SlavedAccountDataStore,
SlavedEventStore,
SlavedKeyStore,
RoomStore,
Expand Down
5 changes: 1 addition & 4 deletions synapse/app/synchrotron.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
from synapse.server import HomeServer
from synapse.storage.engines import create_engine
from synapse.storage.presence import UserPresenceState
from synapse.storage.roommember import RoomMemberStore
from synapse.util.httpresourcetree import create_resource_tree
from synapse.util.logcontext import LoggingContext, run_in_background
from synapse.util.manhole import manhole
Expand All @@ -81,9 +80,7 @@ class SynchrotronSlavedStore(
RoomStore,
BaseSlavedStore,
):
did_forget = (
RoomMemberStore.__dict__["did_forget"]
)
pass


UPDATE_SYNCING_USERS_MS = 10 * 1000
Expand Down
12 changes: 7 additions & 5 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from synapse.api.constants import EventTypes, JoinRules, RoomCreationPreset
from synapse.api.errors import AuthError, Codes, StoreError, SynapseError
from synapse.types import RoomAlias, RoomID, RoomStreamToken, UserID
from synapse.types import RoomAlias, RoomID, RoomStreamToken, StreamToken, UserID
from synapse.util import stringutils
from synapse.visibility import filter_events_for_client

Expand Down Expand Up @@ -418,8 +418,6 @@ def get_event_context(self, user, room_id, event_id, limit):
before_limit = math.floor(limit / 2.)
after_limit = limit - before_limit

now_token = yield self.hs.get_event_sources().get_current_token()

users = yield self.store.get_users_in_room(room_id)
is_peeking = user.to_string() not in users

Expand Down Expand Up @@ -462,11 +460,15 @@ def filter_evts(events):
)
results["state"] = list(state[last_event_id].values())

results["start"] = now_token.copy_and_replace(
# We use a dummy token here as we only care about the room portion of
# the token, which we replace.
token = StreamToken.START

results["start"] = token.copy_and_replace(
"room_key", results["start"]
).to_string()

results["end"] = now_token.copy_and_replace(
results["end"] = token.copy_and_replace(
"room_key", results["end"]
).to_string()

Expand Down
52 changes: 27 additions & 25 deletions synapse/storage/roommember.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,30 @@ def _get_joined_hosts(self, room_id, state_group, current_state_ids, state_entry
def _get_joined_hosts_cache(self, room_id):
return _JoinedHostsCache(self, room_id)

@cachedInlineCallbacks(num_args=2)
def did_forget(self, user_id, room_id):
"""Returns whether user_id has elected to discard history for room_id.

Returns False if they have since re-joined."""
def f(txn):
sql = (
"SELECT"
" COUNT(*)"
" FROM"
" room_memberships"
" WHERE"
" user_id = ?"
" AND"
" room_id = ?"
" AND"
" forgotten = 0"
)
txn.execute(sql, (user_id, room_id))
rows = txn.fetchall()
return rows[0][0]
count = yield self.runInteraction("did_forget_membership", f)
defer.returnValue(count == 0)


class RoomMemberStore(RoomMemberWorkerStore):
def __init__(self, db_conn, hs):
Expand Down Expand Up @@ -568,32 +592,10 @@ def f(txn):
)
txn.execute(sql, (user_id, room_id))

txn.call_after(self.did_forget.invalidate, (user_id, room_id))
return self.runInteraction("forget_membership", f)

@cachedInlineCallbacks(num_args=2)
def did_forget(self, user_id, room_id):
"""Returns whether user_id has elected to discard history for room_id.

Returns False if they have since re-joined."""
def f(txn):
sql = (
"SELECT"
" COUNT(*)"
" FROM"
" room_memberships"
" WHERE"
" user_id = ?"
" AND"
" room_id = ?"
" AND"
" forgotten = 0"
self._invalidate_cache_and_stream(
txn, self.did_forget, (user_id, room_id,),
)
txn.execute(sql, (user_id, room_id))
rows = txn.fetchall()
return rows[0][0]
count = yield self.runInteraction("did_forget_membership", f)
defer.returnValue(count == 0)
return self.runInteraction("forget_membership", f)

@defer.inlineCallbacks
def _background_add_membership_profile(self, progress, batch_size):
Expand Down