Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Timo Ley
synapse
Commits
9267741a
Commit
9267741a
authored
5 years ago
by
Erik Johnston
Browse files
Options
Downloads
Patches
Plain Diff
Fix `devices_last_seen` background update.
Fixes #6134.
parent
6d0f559f
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
synapse/storage/client_ips.py
+39
-7
39 additions, 7 deletions
synapse/storage/client_ips.py
synapse/storage/engines/postgres.py
+7
-0
7 additions, 0 deletions
synapse/storage/engines/postgres.py
synapse/storage/engines/sqlite.py
+8
-0
8 additions, 0 deletions
synapse/storage/engines/sqlite.py
with
54 additions
and
7 deletions
synapse/storage/client_ips.py
+
39
−
7
View file @
9267741a
...
...
@@ -463,14 +463,46 @@ class ClientIpStore(background_updates.BackgroundUpdateStore):
last_device_id
=
progress
.
get
(
"
last_device_id
"
,
""
)
def
_devices_last_seen_update_txn
(
txn
):
# This consists of two queries:
#
# 1. The sub-query searches for the next N devices and joins
# against user_ips to find the max last_seen associated with
# that device.
# 2. The outer query then joins again against user_ips on
# user/device/last_seen. This *should* hopefully only
# return one row, but if it does return more than one then
# we'll just end up updating the same device row multiple
# times, which is fine.
if
self
.
database_engine
.
supports_tuple_comparison
:
where_clause
=
"
(user_id, device_id) > (?, ?)
"
where_args
=
[
last_user_id
,
last_device_id
]
else
:
# We explicitly do a `user_id >= ? AND (...)` here to ensure
# that an index is used, as doing `user_id > ? OR (user_id = ? AND ...)`
# makes it hard for query optimiser to tell that it can use the
# index on user_id
where_clause
=
"
user_id >= ? AND (user_id > ? OR device_id > ?)
"
where_args
=
[
last_user_id
,
last_user_id
,
last_device_id
]
sql
=
"""
SELECT u.last_seen, u.ip, u.user_agent, user_id, device_id FROM devices
INNER JOIN user_ips AS u USING (user_id, device_id)
WHERE user_id > ? OR (user_id = ? AND device_id > ?)
ORDER BY user_id ASC, device_id ASC
LIMIT ?
"""
txn
.
execute
(
sql
,
(
last_user_id
,
last_user_id
,
last_device_id
,
batch_size
))
SELECT
last_seen, ip, user_agent, user_id, device_id
FROM (
SELECT
user_id, device_id, MAX(u.last_seen) AS last_seen
FROM devices
INNER JOIN user_ips AS u USING (user_id, device_id)
WHERE %(where_clause)s
GROUP BY user_id, device_id
ORDER BY user_id ASC, device_id ASC
LIMIT ?
) c
INNER JOIN user_ips AS u USING (user_id, device_id, last_seen)
"""
%
{
"
where_clause
"
:
where_clause
}
txn
.
execute
(
sql
,
where_args
+
[
batch_size
])
rows
=
txn
.
fetchall
()
if
not
rows
:
...
...
This diff is collapsed.
Click to expand it.
synapse/storage/engines/postgres.py
+
7
−
0
View file @
9267741a
...
...
@@ -72,6 +72,13 @@ class PostgresEngine(object):
"""
return
True
@property
def
supports_tuple_comparison
(
self
):
"""
Do we support comparing tuples, i.e. `(a, b) > (c, d)`?
"""
return
True
def
is_deadlock
(
self
,
error
):
if
isinstance
(
error
,
self
.
module
.
DatabaseError
):
# https://www.postgresql.org/docs/current/static/errcodes-appendix.html
...
...
This diff is collapsed.
Click to expand it.
synapse/storage/engines/sqlite.py
+
8
−
0
View file @
9267741a
...
...
@@ -38,6 +38,14 @@ class Sqlite3Engine(object):
"""
return
self
.
module
.
sqlite_version_info
>=
(
3
,
24
,
0
)
@property
def
supports_tuple_comparison
(
self
):
"""
Do we support comparing tuples, i.e. `(a, b) > (c, d)`? This requires
SQLite 3.15+.
"""
return
self
.
module
.
sqlite_version_info
>=
(
3
,
15
,
0
)
def
check_database
(
self
,
txn
):
pass
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment