-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add checks for postgres sequence consistency #8402
Changes from all commits
f81a693
6ac22c5
aa10531
06d460a
5291106
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add checks on startup that PostgreSQL sequences are consistent with their associated tables. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,9 +12,8 @@ | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| from synapse.storage.database import DatabasePool | ||
| from synapse.storage.engines import IncorrectDatabaseSetup | ||
| from synapse.storage.util.id_generators import MultiWriterIdGenerator | ||
|
|
||
| from tests.unittest import HomeserverTestCase | ||
|
|
@@ -59,7 +58,7 @@ def _create(conn): | |
| writers=writers, | ||
| ) | ||
|
|
||
| return self.get_success(self.db_pool.runWithConnection(_create)) | ||
| return self.get_success_or_raise(self.db_pool.runWithConnection(_create)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahhh, right. I wonder if there are places we do this wrong. 😢
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually when waiting on a deferred you know whether you're expecting this to succeed ( |
||
|
|
||
| def _insert_rows(self, instance_name: str, number: int): | ||
| """Insert N rows as the given instance, inserting with stream IDs pulled | ||
|
|
@@ -411,6 +410,23 @@ async def _get_next_async(): | |
| self.get_success(_get_next_async()) | ||
| self.assertEqual(id_gen_3.get_persisted_upto_position(), 6) | ||
|
|
||
| def test_sequence_consistency(self): | ||
| """Test that we error out if the table and sequence diverges. | ||
| """ | ||
|
|
||
| # Prefill with some rows | ||
| self._insert_row_with_id("master", 3) | ||
|
|
||
| # Now we add a row *without* updating the stream ID | ||
| def _insert(txn): | ||
| txn.execute("INSERT INTO foobar VALUES (26, 'master')") | ||
|
|
||
| self.get_success(self.db_pool.runInteraction("_insert", _insert)) | ||
|
|
||
| # Creating the ID gen should error | ||
| with self.assertRaises(IncorrectDatabaseSetup): | ||
| self._create_id_generator("first") | ||
|
|
||
|
|
||
| class BackwardsMultiWriterIdGeneratorTestCase(HomeserverTestCase): | ||
| """Tests MultiWriterIdGenerator that produce *negative* stream IDs. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This uses
PostgresSequenceGeneratordirectly (instead ofbuild_sequence_generator) since you have to use postgresql forMultiWriterIdGeneratorto make sense?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup