2015-04-09 10:41:36 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-07 04:26:29 +00:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2015-04-09 10:41:36 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
import contextlib
|
2020-08-25 16:32:30 +00:00
|
|
|
import heapq
|
2015-04-09 10:41:36 +00:00
|
|
|
import threading
|
2018-07-09 06:09:20 +00:00
|
|
|
from collections import deque
|
2020-08-25 16:32:30 +00:00
|
|
|
from typing import Dict, List, Set
|
2020-05-04 16:17:45 +00:00
|
|
|
|
|
|
|
from typing_extensions import Deque
|
|
|
|
|
2020-08-05 20:38:57 +00:00
|
|
|
from synapse.storage.database import DatabasePool, LoggingTransaction
|
2020-07-16 09:56:49 +00:00
|
|
|
from synapse.storage.util.sequence import PostgresSequenceGenerator
|
2015-04-09 10:41:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class IdGenerator(object):
|
2016-03-01 14:32:56 +00:00
|
|
|
def __init__(self, db_conn, table, column):
|
2015-04-09 10:41:36 +00:00
|
|
|
self._lock = threading.Lock()
|
2016-04-01 12:29:05 +00:00
|
|
|
self._next_id = _load_current_id(db_conn, table, column)
|
2015-04-09 10:41:36 +00:00
|
|
|
|
2016-03-01 14:32:56 +00:00
|
|
|
def get_next(self):
|
2015-04-29 18:17:00 +00:00
|
|
|
with self._lock:
|
2015-04-09 10:41:36 +00:00
|
|
|
self._next_id += 1
|
2016-03-01 13:35:37 +00:00
|
|
|
return self._next_id
|
|
|
|
|
|
|
|
|
2016-04-01 12:50:54 +00:00
|
|
|
def _load_current_id(db_conn, table, column, step=1):
|
2017-03-15 12:16:55 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
|
|
|
db_conn (object):
|
|
|
|
table (str):
|
|
|
|
column (str):
|
|
|
|
step (int):
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int
|
|
|
|
"""
|
2016-03-01 13:35:37 +00:00
|
|
|
cur = db_conn.cursor()
|
2016-04-01 12:50:54 +00:00
|
|
|
if step == 1:
|
2019-04-03 09:07:29 +00:00
|
|
|
cur.execute("SELECT MAX(%s) FROM %s" % (column, table))
|
2016-04-01 12:29:05 +00:00
|
|
|
else:
|
2019-04-03 09:07:29 +00:00
|
|
|
cur.execute("SELECT MIN(%s) FROM %s" % (column, table))
|
2019-10-31 15:43:24 +00:00
|
|
|
(val,) = cur.fetchone()
|
2016-03-01 13:35:37 +00:00
|
|
|
cur.close()
|
2016-04-01 12:50:54 +00:00
|
|
|
current_id = int(val) if val else step
|
|
|
|
return (max if step > 0 else min)(current_id, step)
|
2015-04-09 10:41:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StreamIdGenerator(object):
|
|
|
|
"""Used to generate new stream ids when persisting events while keeping
|
|
|
|
track of which transactions have been completed.
|
|
|
|
|
|
|
|
This allows us to get the "current" stream id, i.e. the stream id such that
|
|
|
|
all ids less than or equal to it have completed. This handles the fact that
|
|
|
|
persistence of events can complete out of order.
|
|
|
|
|
2016-04-01 14:07:01 +00:00
|
|
|
Args:
|
|
|
|
db_conn(connection): A database connection to use to fetch the
|
|
|
|
initial value of the generator from.
|
|
|
|
table(str): A database table to read the initial value of the id
|
|
|
|
generator from.
|
|
|
|
column(str): The column of the database table to read the initial
|
|
|
|
value from the id generator from.
|
|
|
|
extra_tables(list): List of pairs of database tables and columns to
|
|
|
|
use to source the initial value of the generator from. The value
|
|
|
|
with the largest magnitude is used.
|
|
|
|
step(int): which direction the stream ids grow in. +1 to grow
|
|
|
|
upwards, -1 to grow downwards.
|
2016-04-01 12:29:05 +00:00
|
|
|
|
2015-04-09 10:41:36 +00:00
|
|
|
Usage:
|
2020-08-25 14:10:08 +00:00
|
|
|
with await stream_id_gen.get_next() as stream_id:
|
2015-04-09 10:41:36 +00:00
|
|
|
# ... persist event ...
|
|
|
|
"""
|
2019-04-03 09:07:29 +00:00
|
|
|
|
2016-04-01 12:50:54 +00:00
|
|
|
def __init__(self, db_conn, table, column, extra_tables=[], step=1):
|
2016-04-01 14:08:20 +00:00
|
|
|
assert step != 0
|
2015-04-09 10:41:36 +00:00
|
|
|
self._lock = threading.Lock()
|
2016-04-01 12:50:54 +00:00
|
|
|
self._step = step
|
|
|
|
self._current = _load_current_id(db_conn, table, column, step)
|
2016-03-15 17:01:43 +00:00
|
|
|
for table, column in extra_tables:
|
2016-04-01 12:50:54 +00:00
|
|
|
self._current = (max if step > 0 else min)(
|
2019-04-03 09:07:29 +00:00
|
|
|
self._current, _load_current_id(db_conn, table, column, step)
|
2016-03-15 17:01:43 +00:00
|
|
|
)
|
2020-05-04 16:17:45 +00:00
|
|
|
self._unfinished_ids = deque() # type: Deque[int]
|
2015-04-09 10:41:36 +00:00
|
|
|
|
2020-08-25 14:10:08 +00:00
|
|
|
async def get_next(self):
|
2015-04-09 10:41:36 +00:00
|
|
|
"""
|
|
|
|
Usage:
|
2020-08-25 14:10:08 +00:00
|
|
|
with await stream_id_gen.get_next() as stream_id:
|
2015-04-09 10:41:36 +00:00
|
|
|
# ... persist event ...
|
|
|
|
"""
|
2015-04-29 18:15:23 +00:00
|
|
|
with self._lock:
|
2016-04-01 12:50:54 +00:00
|
|
|
self._current += self._step
|
2016-04-01 12:29:05 +00:00
|
|
|
next_id = self._current
|
2015-04-09 10:41:36 +00:00
|
|
|
|
|
|
|
self._unfinished_ids.append(next_id)
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def manager():
|
2015-04-15 09:25:43 +00:00
|
|
|
try:
|
|
|
|
yield next_id
|
|
|
|
finally:
|
|
|
|
with self._lock:
|
|
|
|
self._unfinished_ids.remove(next_id)
|
2015-04-09 10:41:36 +00:00
|
|
|
|
2016-01-26 15:51:06 +00:00
|
|
|
return manager()
|
2015-04-09 10:41:36 +00:00
|
|
|
|
2020-08-25 14:10:08 +00:00
|
|
|
async def get_next_mult(self, n):
|
2015-06-25 16:18:19 +00:00
|
|
|
"""
|
|
|
|
Usage:
|
2020-08-25 14:10:08 +00:00
|
|
|
with await stream_id_gen.get_next(n) as stream_ids:
|
2015-06-25 16:18:19 +00:00
|
|
|
# ... persist events ...
|
|
|
|
"""
|
|
|
|
with self._lock:
|
2016-04-01 12:29:05 +00:00
|
|
|
next_ids = range(
|
2016-04-01 12:50:54 +00:00
|
|
|
self._current + self._step,
|
|
|
|
self._current + self._step * (n + 1),
|
2019-04-03 09:07:29 +00:00
|
|
|
self._step,
|
2016-04-01 12:29:05 +00:00
|
|
|
)
|
2016-04-07 15:26:52 +00:00
|
|
|
self._current += n * self._step
|
2015-06-25 16:18:19 +00:00
|
|
|
|
|
|
|
for next_id in next_ids:
|
|
|
|
self._unfinished_ids.append(next_id)
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def manager():
|
|
|
|
try:
|
|
|
|
yield next_ids
|
|
|
|
finally:
|
|
|
|
with self._lock:
|
|
|
|
for next_id in next_ids:
|
|
|
|
self._unfinished_ids.remove(next_id)
|
|
|
|
|
2016-01-26 15:51:06 +00:00
|
|
|
return manager()
|
2015-06-25 16:18:19 +00:00
|
|
|
|
2016-04-01 12:29:05 +00:00
|
|
|
def get_current_token(self):
|
2015-04-09 10:41:36 +00:00
|
|
|
"""Returns the maximum stream id such that all stream ids less than or
|
|
|
|
equal to it have been successfully persisted.
|
2017-03-15 12:16:55 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
int
|
2015-04-09 10:41:36 +00:00
|
|
|
"""
|
|
|
|
with self._lock:
|
|
|
|
if self._unfinished_ids:
|
2016-04-01 12:50:54 +00:00
|
|
|
return self._unfinished_ids[0] - self._step
|
2015-04-09 10:41:36 +00:00
|
|
|
|
2016-04-01 12:29:05 +00:00
|
|
|
return self._current
|
2016-03-01 13:35:37 +00:00
|
|
|
|
2020-08-19 09:39:31 +00:00
|
|
|
def get_current_token_for_writer(self, instance_name: str) -> int:
|
|
|
|
"""Returns the position of the given writer.
|
|
|
|
|
|
|
|
For streams with single writers this is equivalent to
|
|
|
|
`get_current_token`.
|
|
|
|
"""
|
|
|
|
return self.get_current_token()
|
|
|
|
|
2016-03-01 13:35:37 +00:00
|
|
|
|
2020-05-04 16:17:45 +00:00
|
|
|
class MultiWriterIdGenerator:
|
|
|
|
"""An ID generator that tracks a stream that can have multiple writers.
|
|
|
|
|
|
|
|
Uses a Postgres sequence to coordinate ID assignment, but positions of other
|
|
|
|
writers will only get updated when `advance` is called (by replication).
|
|
|
|
|
|
|
|
Note: Only works with Postgres.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
db_conn
|
|
|
|
db
|
|
|
|
instance_name: The name of this instance.
|
|
|
|
table: Database table associated with stream.
|
|
|
|
instance_column: Column that stores the row's writer's instance name
|
|
|
|
id_column: Column that stores the stream ID.
|
|
|
|
sequence_name: The name of the postgres sequence used to generate new
|
|
|
|
IDs.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
db_conn,
|
2020-08-05 20:38:57 +00:00
|
|
|
db: DatabasePool,
|
2020-05-04 16:17:45 +00:00
|
|
|
instance_name: str,
|
|
|
|
table: str,
|
|
|
|
instance_column: str,
|
|
|
|
id_column: str,
|
|
|
|
sequence_name: str,
|
|
|
|
):
|
|
|
|
self._db = db
|
|
|
|
self._instance_name = instance_name
|
|
|
|
|
|
|
|
# We lock as some functions may be called from DB threads.
|
|
|
|
self._lock = threading.Lock()
|
|
|
|
|
|
|
|
self._current_positions = self._load_current_ids(
|
|
|
|
db_conn, table, instance_column, id_column
|
|
|
|
)
|
|
|
|
|
|
|
|
# Set of local IDs that we're still processing. The current position
|
|
|
|
# should be less than the minimum of this set (if not empty).
|
|
|
|
self._unfinished_ids = set() # type: Set[int]
|
|
|
|
|
2020-08-25 16:32:30 +00:00
|
|
|
# We track the max position where we know everything before has been
|
|
|
|
# persisted. This is done by a) looking at the min across all instances
|
|
|
|
# and b) noting that if we have seen a run of persisted positions
|
|
|
|
# without gaps (e.g. 5, 6, 7) then we can skip forward (e.g. to 7).
|
|
|
|
#
|
|
|
|
# Note: There is no guarentee that the IDs generated by the sequence
|
|
|
|
# will be gapless; gaps can form when e.g. a transaction was rolled
|
|
|
|
# back. This means that sometimes we won't be able to skip forward the
|
|
|
|
# position even though everything has been persisted. However, since
|
|
|
|
# gaps should be relatively rare it's still worth doing the book keeping
|
|
|
|
# that allows us to skip forwards when there are gapless runs of
|
|
|
|
# positions.
|
|
|
|
self._persisted_upto_position = (
|
|
|
|
min(self._current_positions.values()) if self._current_positions else 0
|
|
|
|
)
|
|
|
|
self._known_persisted_positions = [] # type: List[int]
|
|
|
|
|
2020-07-16 09:56:49 +00:00
|
|
|
self._sequence_gen = PostgresSequenceGenerator(sequence_name)
|
|
|
|
|
2020-05-04 16:17:45 +00:00
|
|
|
def _load_current_ids(
|
|
|
|
self, db_conn, table: str, instance_column: str, id_column: str
|
|
|
|
) -> Dict[str, int]:
|
|
|
|
sql = """
|
|
|
|
SELECT %(instance)s, MAX(%(id)s) FROM %(table)s
|
|
|
|
GROUP BY %(instance)s
|
|
|
|
""" % {
|
|
|
|
"instance": instance_column,
|
|
|
|
"id": id_column,
|
|
|
|
"table": table,
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = db_conn.cursor()
|
|
|
|
cur.execute(sql)
|
|
|
|
|
|
|
|
# `cur` is an iterable over returned rows, which are 2-tuples.
|
|
|
|
current_positions = dict(cur)
|
|
|
|
|
|
|
|
cur.close()
|
|
|
|
|
|
|
|
return current_positions
|
|
|
|
|
2020-08-25 16:32:30 +00:00
|
|
|
def _load_next_id_txn(self, txn) -> int:
|
2020-07-16 09:56:49 +00:00
|
|
|
return self._sequence_gen.get_next_id_txn(txn)
|
2020-05-04 16:17:45 +00:00
|
|
|
|
2020-08-25 16:32:30 +00:00
|
|
|
def _load_next_mult_id_txn(self, txn, n: int) -> List[int]:
|
|
|
|
return self._sequence_gen.get_next_mult_txn(txn, n)
|
|
|
|
|
2020-05-04 16:17:45 +00:00
|
|
|
async def get_next(self):
|
|
|
|
"""
|
|
|
|
Usage:
|
|
|
|
with await stream_id_gen.get_next() as stream_id:
|
|
|
|
# ... persist event ...
|
|
|
|
"""
|
|
|
|
next_id = await self._db.runInteraction("_load_next_id", self._load_next_id_txn)
|
|
|
|
|
|
|
|
# Assert the fetched ID is actually greater than what we currently
|
|
|
|
# believe the ID to be. If not, then the sequence and table have got
|
|
|
|
# out of sync somehow.
|
2020-08-19 09:39:31 +00:00
|
|
|
assert self.get_current_token_for_writer(self._instance_name) < next_id
|
2020-05-04 16:17:45 +00:00
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
self._unfinished_ids.add(next_id)
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def manager():
|
|
|
|
try:
|
|
|
|
yield next_id
|
|
|
|
finally:
|
|
|
|
self._mark_id_as_finished(next_id)
|
|
|
|
|
|
|
|
return manager()
|
|
|
|
|
2020-08-25 16:32:30 +00:00
|
|
|
async def get_next_mult(self, n: int):
|
|
|
|
"""
|
|
|
|
Usage:
|
|
|
|
with await stream_id_gen.get_next_mult(5) as stream_ids:
|
|
|
|
# ... persist events ...
|
|
|
|
"""
|
|
|
|
next_ids = await self._db.runInteraction(
|
|
|
|
"_load_next_mult_id", self._load_next_mult_id_txn, n
|
|
|
|
)
|
|
|
|
|
|
|
|
# Assert the fetched ID is actually greater than any ID we've already
|
|
|
|
# seen. If not, then the sequence and table have got out of sync
|
|
|
|
# somehow.
|
|
|
|
assert max(self.get_positions().values(), default=0) < min(next_ids)
|
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
self._unfinished_ids.update(next_ids)
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def manager():
|
|
|
|
try:
|
|
|
|
yield next_ids
|
|
|
|
finally:
|
|
|
|
for i in next_ids:
|
|
|
|
self._mark_id_as_finished(i)
|
|
|
|
|
|
|
|
return manager()
|
|
|
|
|
2020-05-04 16:17:45 +00:00
|
|
|
def get_next_txn(self, txn: LoggingTransaction):
|
|
|
|
"""
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
stream_id = stream_id_gen.get_next(txn)
|
|
|
|
# ... persist event ...
|
|
|
|
"""
|
|
|
|
|
|
|
|
next_id = self._load_next_id_txn(txn)
|
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
self._unfinished_ids.add(next_id)
|
|
|
|
|
|
|
|
txn.call_after(self._mark_id_as_finished, next_id)
|
|
|
|
txn.call_on_exception(self._mark_id_as_finished, next_id)
|
|
|
|
|
|
|
|
return next_id
|
|
|
|
|
|
|
|
def _mark_id_as_finished(self, next_id: int):
|
|
|
|
"""The ID has finished being processed so we should advance the
|
|
|
|
current poistion if possible.
|
|
|
|
"""
|
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
self._unfinished_ids.discard(next_id)
|
|
|
|
|
|
|
|
# Figure out if its safe to advance the position by checking there
|
|
|
|
# aren't any lower allocated IDs that are yet to finish.
|
|
|
|
if all(c > next_id for c in self._unfinished_ids):
|
|
|
|
curr = self._current_positions.get(self._instance_name, 0)
|
|
|
|
self._current_positions[self._instance_name] = max(curr, next_id)
|
|
|
|
|
2020-08-27 12:20:34 +00:00
|
|
|
self._add_persisted_position(next_id)
|
|
|
|
|
2020-08-19 09:39:31 +00:00
|
|
|
def get_current_token(self) -> int:
|
|
|
|
"""Returns the maximum stream id such that all stream ids less than or
|
|
|
|
equal to it have been successfully persisted.
|
2020-05-04 16:17:45 +00:00
|
|
|
"""
|
|
|
|
|
2020-08-19 09:39:31 +00:00
|
|
|
# Currently we don't support this operation, as it's not obvious how to
|
|
|
|
# condense the stream positions of multiple writers into a single int.
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def get_current_token_for_writer(self, instance_name: str) -> int:
|
|
|
|
"""Returns the position of the given writer.
|
|
|
|
"""
|
2020-05-04 16:17:45 +00:00
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
return self._current_positions.get(instance_name, 0)
|
|
|
|
|
|
|
|
def get_positions(self) -> Dict[str, int]:
|
|
|
|
"""Get a copy of the current positon map.
|
|
|
|
"""
|
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
return dict(self._current_positions)
|
|
|
|
|
|
|
|
def advance(self, instance_name: str, new_id: int):
|
|
|
|
"""Advance the postion of the named writer to the given ID, if greater
|
|
|
|
than existing entry.
|
|
|
|
"""
|
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
self._current_positions[instance_name] = max(
|
|
|
|
new_id, self._current_positions.get(instance_name, 0)
|
|
|
|
)
|
2020-08-25 16:32:30 +00:00
|
|
|
|
|
|
|
self._add_persisted_position(new_id)
|
|
|
|
|
|
|
|
def get_persisted_upto_position(self) -> int:
|
|
|
|
"""Get the max position where all previous positions have been
|
|
|
|
persisted.
|
|
|
|
|
|
|
|
Note: In the worst case scenario this will be equal to the minimum
|
|
|
|
position across writers. This means that the returned position here can
|
|
|
|
lag if one writer doesn't write very often.
|
|
|
|
"""
|
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
return self._persisted_upto_position
|
|
|
|
|
|
|
|
def _add_persisted_position(self, new_id: int):
|
|
|
|
"""Record that we have persisted a position.
|
|
|
|
|
|
|
|
This is used to keep the `_current_positions` up to date.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# We require that the lock is locked by caller
|
|
|
|
assert self._lock.locked()
|
|
|
|
|
|
|
|
heapq.heappush(self._known_persisted_positions, new_id)
|
|
|
|
|
|
|
|
# We move the current min position up if the minimum current positions
|
|
|
|
# of all instances is higher (since by definition all positions less
|
|
|
|
# that that have been persisted).
|
|
|
|
min_curr = min(self._current_positions.values())
|
|
|
|
self._persisted_upto_position = max(min_curr, self._persisted_upto_position)
|
|
|
|
|
|
|
|
# We now iterate through the seen positions, discarding those that are
|
|
|
|
# less than the current min positions, and incrementing the min position
|
|
|
|
# if its exactly one greater.
|
|
|
|
#
|
|
|
|
# This is also where we discard items from `_known_persisted_positions`
|
|
|
|
# (to ensure the list doesn't infinitely grow).
|
|
|
|
while self._known_persisted_positions:
|
|
|
|
if self._known_persisted_positions[0] <= self._persisted_upto_position:
|
|
|
|
heapq.heappop(self._known_persisted_positions)
|
|
|
|
elif (
|
|
|
|
self._known_persisted_positions[0] == self._persisted_upto_position + 1
|
|
|
|
):
|
|
|
|
heapq.heappop(self._known_persisted_positions)
|
|
|
|
self._persisted_upto_position += 1
|
|
|
|
else:
|
|
|
|
# There was a gap in seen positions, so there is nothing more to
|
|
|
|
# do.
|
|
|
|
break
|