|
@@ -12,10 +12,9 @@
|
|
|
# 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 logging
|
|
|
-from typing import Optional
|
|
|
|
|
|
-from mock import Mock
|
|
|
+import logging
|
|
|
+from typing import Any, Dict, List, Optional, Tuple
|
|
|
|
|
|
import attr
|
|
|
|
|
@@ -25,6 +24,7 @@ from twisted.web.http import HTTPChannel
|
|
|
|
|
|
from synapse.app.generic_worker import GenericWorkerServer
|
|
|
from synapse.http.site import SynapseRequest
|
|
|
+from synapse.replication.slave.storage._base import BaseSlavedStore
|
|
|
from synapse.replication.tcp.client import ReplicationDataHandler
|
|
|
from synapse.replication.tcp.handler import ReplicationCommandHandler
|
|
|
from synapse.replication.tcp.protocol import ClientReplicationStreamProtocol
|
|
@@ -65,9 +65,7 @@ class BaseStreamTestCase(unittest.HomeserverTestCase):
|
|
|
# databases objects are the same.
|
|
|
self.worker_hs.get_datastore().db = hs.get_datastore().db
|
|
|
|
|
|
- self.test_handler = Mock(
|
|
|
- wraps=TestReplicationDataHandler(self.worker_hs.get_datastore())
|
|
|
- )
|
|
|
+ self.test_handler = self._build_replication_data_handler()
|
|
|
self.worker_hs.replication_data_handler = self.test_handler
|
|
|
|
|
|
repl_handler = ReplicationCommandHandler(self.worker_hs)
|
|
@@ -78,6 +76,9 @@ class BaseStreamTestCase(unittest.HomeserverTestCase):
|
|
|
self._client_transport = None
|
|
|
self._server_transport = None
|
|
|
|
|
|
+ def _build_replication_data_handler(self):
|
|
|
+ return TestReplicationDataHandler(self.worker_hs.get_datastore())
|
|
|
+
|
|
|
def reconnect(self):
|
|
|
if self._client_transport:
|
|
|
self.client.close()
|
|
@@ -174,22 +175,28 @@ class BaseStreamTestCase(unittest.HomeserverTestCase):
|
|
|
class TestReplicationDataHandler(ReplicationDataHandler):
|
|
|
"""Drop-in for ReplicationDataHandler which just collects RDATA rows"""
|
|
|
|
|
|
- def __init__(self, hs):
|
|
|
- super().__init__(hs)
|
|
|
- self.streams = set()
|
|
|
- self._received_rdata_rows = []
|
|
|
+ def __init__(self, store: BaseSlavedStore):
|
|
|
+ super().__init__(store)
|
|
|
+
|
|
|
+ # streams to subscribe to: map from stream id to position
|
|
|
+ self.stream_positions = {} # type: Dict[str, int]
|
|
|
+
|
|
|
+ # list of received (stream_name, token, row) tuples
|
|
|
+ self.received_rdata_rows = [] # type: List[Tuple[str, int, Any]]
|
|
|
|
|
|
def get_streams_to_replicate(self):
|
|
|
- positions = {s: 0 for s in self.streams}
|
|
|
- for stream, token, _ in self._received_rdata_rows:
|
|
|
- if stream in self.streams:
|
|
|
- positions[stream] = max(token, positions.get(stream, 0))
|
|
|
- return positions
|
|
|
+ return self.stream_positions
|
|
|
|
|
|
async def on_rdata(self, stream_name, token, rows):
|
|
|
await super().on_rdata(stream_name, token, rows)
|
|
|
for r in rows:
|
|
|
- self._received_rdata_rows.append((stream_name, token, r))
|
|
|
+ self.received_rdata_rows.append((stream_name, token, r))
|
|
|
+
|
|
|
+ if (
|
|
|
+ stream_name in self.stream_positions
|
|
|
+ and token > self.stream_positions[stream_name]
|
|
|
+ ):
|
|
|
+ self.stream_positions[stream_name] = token
|
|
|
|
|
|
|
|
|
@attr.s()
|
|
@@ -221,7 +228,7 @@ class _PushHTTPChannel(HTTPChannel):
|
|
|
super().__init__()
|
|
|
self.reactor = reactor
|
|
|
|
|
|
- self._pull_to_push_producer = None
|
|
|
+ self._pull_to_push_producer = None # type: Optional[_PullToPushProducer]
|
|
|
|
|
|
def registerProducer(self, producer, streaming):
|
|
|
# Convert pull producers to push producer.
|