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

Commit 24b590d

Browse files
authored
Remove code which updates application_services_state.last_txn (#12680)
This column is unused as of #12209, so let's stop writing to it.
1 parent a34a41f commit 24b590d

File tree

5 files changed

+35
-55
lines changed

5 files changed

+35
-55
lines changed

changelog.d/12680.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove code which updates unused database column `application_services_state.last_txn`.

synapse/storage/databases/main/appservice.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,29 @@ async def get_appservice_state(
203203
"""Get the application service state.
204204
205205
Args:
206-
service: The service whose state to set.
206+
service: The service whose state to get.
207207
Returns:
208-
An ApplicationServiceState or none.
208+
An ApplicationServiceState, or None if we have yet to attempt any
209+
transactions to the AS.
209210
"""
210-
result = await self.db_pool.simple_select_one(
211+
# if we have created transactions for this AS but not yet attempted to send
212+
# them, we will have a row in the table with state=NULL (recording the stream
213+
# positions we have processed up to).
214+
#
215+
# On the other hand, if we have yet to create any transactions for this AS at
216+
# all, then there will be no row for the AS.
217+
#
218+
# In either case, we return None to indicate "we don't yet know the state of
219+
# this AS".
220+
result = await self.db_pool.simple_select_one_onecol(
211221
"application_services_state",
212222
{"as_id": service.id},
213-
["state"],
223+
retcol="state",
214224
allow_none=True,
215225
desc="get_appservice_state",
216226
)
217227
if result:
218-
return ApplicationServiceState(result.get("state"))
228+
return ApplicationServiceState(result)
219229
return None
220230

221231
async def set_appservice_state(
@@ -296,14 +306,6 @@ async def complete_appservice_txn(
296306
"""
297307

298308
def _complete_appservice_txn(txn: LoggingTransaction) -> None:
299-
# Set current txn_id for AS to 'txn_id'
300-
self.db_pool.simple_upsert_txn(
301-
txn,
302-
"application_services_state",
303-
{"as_id": service.id},
304-
{"last_txn": txn_id},
305-
)
306-
307309
# Delete txn
308310
self.db_pool.simple_delete_txn(
309311
txn,
@@ -452,16 +454,15 @@ async def set_appservice_stream_type_pos(
452454
% (stream_type,)
453455
)
454456

455-
def set_appservice_stream_type_pos_txn(txn: LoggingTransaction) -> None:
456-
stream_id_type = "%s_stream_id" % stream_type
457-
txn.execute(
458-
"UPDATE application_services_state SET %s = ? WHERE as_id=?"
459-
% stream_id_type,
460-
(pos, service.id),
461-
)
462-
463-
await self.db_pool.runInteraction(
464-
"set_appservice_stream_type_pos", set_appservice_stream_type_pos_txn
457+
# this may be the first time that we're recording any state for this AS, so
458+
# we don't yet know if a row for it exists; hence we have to upsert here.
459+
await self.db_pool.simple_upsert(
460+
table="application_services_state",
461+
keyvalues={"as_id": service.id},
462+
values={f"{stream_type}_stream_id": pos},
463+
# no need to lock when emulating upsert: as_id is a unique key
464+
lock=False,
465+
desc="set_appservice_stream_type_pos",
465466
)
466467

467468

synapse/storage/schema/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
6262
Changes in SCHEMA_VERSION = 69:
6363
- We now write to `device_lists_changes_in_room` table.
64-
- Use sequence to generate future `application_services_txns.txn_id`s
64+
- We now use a PostgreSQL sequence to generate future txn_ids for
65+
`application_services_txns`. `application_services_state.last_txn` is no longer
66+
updated.
6567
6668
Changes in SCHEMA_VERSION = 70:
6769
- event_reference_hashes is no longer written to.
@@ -71,6 +73,7 @@
7173
SCHEMA_COMPAT_VERSION = (
7274
# We now assume that `device_lists_changes_in_room` has been filled out for
7375
# recent device_list_updates.
76+
# ... and that `application_services_state.last_txn` is not used.
7477
69
7578
)
7679
"""Limit on how far the synapse codebase can be rolled back without breaking db compat

tests/handlers/test_appservice.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -434,16 +434,6 @@ def test_sending_read_receipt_batches_to_application_services(self):
434434
},
435435
)
436436

437-
# "Complete" a transaction.
438-
# All this really does for us is make an entry in the application_services_state
439-
# database table, which tracks the current stream_token per stream ID per AS.
440-
self.get_success(
441-
self.hs.get_datastores().main.complete_appservice_txn(
442-
0,
443-
interested_appservice,
444-
)
445-
)
446-
447437
# Now, pretend that we receive a large burst of read receipts (300 total) that
448438
# all come in at once.
449439
for i in range(300):

tests/storage/test_appservice.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import json
1515
import os
1616
import tempfile
17-
from typing import List, Optional, cast
17+
from typing import List, cast
1818
from unittest.mock import Mock
1919

2020
import yaml
@@ -149,15 +149,12 @@ def _add_service(self, url, as_token, id) -> None:
149149
outfile.write(yaml.dump(as_yaml))
150150
self.as_yaml_files.append(as_token)
151151

152-
def _set_state(
153-
self, id: str, state: ApplicationServiceState, txn: Optional[int] = None
154-
):
152+
def _set_state(self, id: str, state: ApplicationServiceState):
155153
return self.db_pool.runOperation(
156154
self.engine.convert_param_style(
157-
"INSERT INTO application_services_state(as_id, state, last_txn) "
158-
"VALUES(?,?,?)"
155+
"INSERT INTO application_services_state(as_id, state) VALUES(?,?)"
159156
),
160-
(id, state.value, txn),
157+
(id, state.value),
161158
)
162159

163160
def _insert_txn(self, as_id, txn_id, events):
@@ -280,17 +277,6 @@ def test_complete_appservice_txn_first_txn(
280277
self.store.complete_appservice_txn(txn_id=txn_id, service=service)
281278
)
282279

283-
res = self.get_success(
284-
self.db_pool.runQuery(
285-
self.engine.convert_param_style(
286-
"SELECT last_txn FROM application_services_state WHERE as_id=?"
287-
),
288-
(service.id,),
289-
)
290-
)
291-
self.assertEqual(1, len(res))
292-
self.assertEqual(txn_id, res[0][0])
293-
294280
res = self.get_success(
295281
self.db_pool.runQuery(
296282
self.engine.convert_param_style(
@@ -316,14 +302,13 @@ def test_complete_appservice_txn_updates_last_txn_state(
316302
res = self.get_success(
317303
self.db_pool.runQuery(
318304
self.engine.convert_param_style(
319-
"SELECT last_txn, state FROM application_services_state WHERE as_id=?"
305+
"SELECT state FROM application_services_state WHERE as_id=?"
320306
),
321307
(service.id,),
322308
)
323309
)
324310
self.assertEqual(1, len(res))
325-
self.assertEqual(txn_id, res[0][0])
326-
self.assertEqual(ApplicationServiceState.UP.value, res[0][1])
311+
self.assertEqual(ApplicationServiceState.UP.value, res[0][0])
327312

328313
res = self.get_success(
329314
self.db_pool.runQuery(

0 commit comments

Comments
 (0)