2015-04-09 04:41:36 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2015-04-09 04:41:36 -06: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.
|
|
|
|
|
|
|
|
from collections import deque
|
|
|
|
import contextlib
|
|
|
|
import threading
|
|
|
|
|
|
|
|
|
|
|
|
class IdGenerator(object):
|
2016-03-01 07:32:56 -07:00
|
|
|
def __init__(self, db_conn, table, column):
|
2015-04-09 04:41:36 -06:00
|
|
|
self._lock = threading.Lock()
|
2016-04-01 06:29:05 -06:00
|
|
|
self._next_id = _load_current_id(db_conn, table, column)
|
2015-04-09 04:41:36 -06:00
|
|
|
|
2016-03-01 07:32:56 -07:00
|
|
|
def get_next(self):
|
2015-04-29 12:17:00 -06:00
|
|
|
with self._lock:
|
2015-04-09 04:41:36 -06:00
|
|
|
self._next_id += 1
|
2016-03-01 06:35:37 -07:00
|
|
|
return self._next_id
|
|
|
|
|
|
|
|
|
2016-04-01 06:50:54 -06:00
|
|
|
def _load_current_id(db_conn, table, column, step=1):
|
2017-03-15 06:16:55 -06:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
|
|
|
db_conn (object):
|
|
|
|
table (str):
|
|
|
|
column (str):
|
|
|
|
step (int):
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int
|
|
|
|
"""
|
2016-03-01 06:35:37 -07:00
|
|
|
cur = db_conn.cursor()
|
2016-04-01 06:50:54 -06:00
|
|
|
if step == 1:
|
2016-04-01 06:29:05 -06:00
|
|
|
cur.execute("SELECT MAX(%s) FROM %s" % (column, table,))
|
|
|
|
else:
|
|
|
|
cur.execute("SELECT MIN(%s) FROM %s" % (column, table,))
|
2016-03-01 06:35:37 -07:00
|
|
|
val, = cur.fetchone()
|
|
|
|
cur.close()
|
2016-04-01 06:50:54 -06:00
|
|
|
current_id = int(val) if val else step
|
|
|
|
return (max if step > 0 else min)(current_id, step)
|
2015-04-09 04:41:36 -06: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 08:07:01 -06: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 06:29:05 -06:00
|
|
|
|
2015-04-09 04:41:36 -06:00
|
|
|
Usage:
|
2016-03-01 07:32:56 -07:00
|
|
|
with stream_id_gen.get_next() as stream_id:
|
2015-04-09 04:41:36 -06:00
|
|
|
# ... persist event ...
|
|
|
|
"""
|
2016-04-01 06:50:54 -06:00
|
|
|
def __init__(self, db_conn, table, column, extra_tables=[], step=1):
|
2016-04-01 08:08:20 -06:00
|
|
|
assert step != 0
|
2015-04-09 04:41:36 -06:00
|
|
|
self._lock = threading.Lock()
|
2016-04-01 06:50:54 -06:00
|
|
|
self._step = step
|
|
|
|
self._current = _load_current_id(db_conn, table, column, step)
|
2016-03-15 11:01:43 -06:00
|
|
|
for table, column in extra_tables:
|
2016-04-01 06:50:54 -06:00
|
|
|
self._current = (max if step > 0 else min)(
|
2016-04-01 06:29:05 -06:00
|
|
|
self._current,
|
2016-04-01 06:50:54 -06:00
|
|
|
_load_current_id(db_conn, table, column, step)
|
2016-03-15 11:01:43 -06:00
|
|
|
)
|
2015-04-09 04:41:36 -06:00
|
|
|
self._unfinished_ids = deque()
|
|
|
|
|
2016-03-01 07:32:56 -07:00
|
|
|
def get_next(self):
|
2015-04-09 04:41:36 -06:00
|
|
|
"""
|
|
|
|
Usage:
|
2016-03-01 07:32:56 -07:00
|
|
|
with stream_id_gen.get_next() as stream_id:
|
2015-04-09 04:41:36 -06:00
|
|
|
# ... persist event ...
|
|
|
|
"""
|
2015-04-29 12:15:23 -06:00
|
|
|
with self._lock:
|
2016-04-01 06:50:54 -06:00
|
|
|
self._current += self._step
|
2016-04-01 06:29:05 -06:00
|
|
|
next_id = self._current
|
2015-04-09 04:41:36 -06:00
|
|
|
|
|
|
|
self._unfinished_ids.append(next_id)
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def manager():
|
2015-04-15 03:25:43 -06:00
|
|
|
try:
|
|
|
|
yield next_id
|
|
|
|
finally:
|
|
|
|
with self._lock:
|
|
|
|
self._unfinished_ids.remove(next_id)
|
2015-04-09 04:41:36 -06:00
|
|
|
|
2016-01-26 08:51:06 -07:00
|
|
|
return manager()
|
2015-04-09 04:41:36 -06:00
|
|
|
|
2016-03-01 07:32:56 -07:00
|
|
|
def get_next_mult(self, n):
|
2015-06-25 10:18:19 -06:00
|
|
|
"""
|
|
|
|
Usage:
|
2016-03-01 07:32:56 -07:00
|
|
|
with stream_id_gen.get_next(n) as stream_ids:
|
2015-06-25 10:18:19 -06:00
|
|
|
# ... persist events ...
|
|
|
|
"""
|
|
|
|
with self._lock:
|
2016-04-01 06:29:05 -06:00
|
|
|
next_ids = range(
|
2016-04-01 06:50:54 -06:00
|
|
|
self._current + self._step,
|
|
|
|
self._current + self._step * (n + 1),
|
|
|
|
self._step
|
2016-04-01 06:29:05 -06:00
|
|
|
)
|
2016-04-07 09:26:52 -06:00
|
|
|
self._current += n * self._step
|
2015-06-25 10:18:19 -06: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 08:51:06 -07:00
|
|
|
return manager()
|
2015-06-25 10:18:19 -06:00
|
|
|
|
2016-04-01 06:29:05 -06:00
|
|
|
def get_current_token(self):
|
2015-04-09 04:41:36 -06:00
|
|
|
"""Returns the maximum stream id such that all stream ids less than or
|
|
|
|
equal to it have been successfully persisted.
|
2017-03-15 06:16:55 -06:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
int
|
2015-04-09 04:41:36 -06:00
|
|
|
"""
|
|
|
|
with self._lock:
|
|
|
|
if self._unfinished_ids:
|
2016-04-01 06:50:54 -06:00
|
|
|
return self._unfinished_ids[0] - self._step
|
2015-04-09 04:41:36 -06:00
|
|
|
|
2016-04-01 06:29:05 -06:00
|
|
|
return self._current
|
2016-03-01 06:35:37 -07:00
|
|
|
|
|
|
|
|
|
|
|
class ChainedIdGenerator(object):
|
|
|
|
"""Used to generate new stream ids where the stream must be kept in sync
|
|
|
|
with another stream. It generates pairs of IDs, the first element is an
|
|
|
|
integer ID for this stream, the second element is the ID for the stream
|
|
|
|
that this stream needs to be kept in sync with."""
|
|
|
|
|
|
|
|
def __init__(self, chained_generator, db_conn, table, column):
|
|
|
|
self.chained_generator = chained_generator
|
|
|
|
self._lock = threading.Lock()
|
2016-04-01 06:29:05 -06:00
|
|
|
self._current_max = _load_current_id(db_conn, table, column)
|
2016-03-01 06:35:37 -07:00
|
|
|
self._unfinished_ids = deque()
|
|
|
|
|
|
|
|
def get_next(self):
|
|
|
|
"""
|
|
|
|
Usage:
|
|
|
|
with stream_id_gen.get_next() as (stream_id, chained_id):
|
|
|
|
# ... persist event ...
|
|
|
|
"""
|
|
|
|
with self._lock:
|
|
|
|
self._current_max += 1
|
|
|
|
next_id = self._current_max
|
2016-04-01 06:29:05 -06:00
|
|
|
chained_id = self.chained_generator.get_current_token()
|
2016-03-01 06:35:37 -07:00
|
|
|
|
|
|
|
self._unfinished_ids.append((next_id, chained_id))
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def manager():
|
|
|
|
try:
|
|
|
|
yield (next_id, chained_id)
|
|
|
|
finally:
|
|
|
|
with self._lock:
|
|
|
|
self._unfinished_ids.remove((next_id, chained_id))
|
|
|
|
|
|
|
|
return manager()
|
|
|
|
|
2016-04-01 06:29:05 -06:00
|
|
|
def get_current_token(self):
|
2016-03-01 06:35:37 -07:00
|
|
|
"""Returns the maximum stream id such that all stream ids less than or
|
|
|
|
equal to it have been successfully persisted.
|
|
|
|
"""
|
|
|
|
with self._lock:
|
|
|
|
if self._unfinished_ids:
|
|
|
|
stream_id, chained_id = self._unfinished_ids[0]
|
|
|
|
return (stream_id - 1, chained_id)
|
|
|
|
|
2016-04-01 06:29:05 -06:00
|
|
|
return (self._current_max, self.chained_generator.get_current_token())
|