|
| 1 | +from typing import cast |
| 2 | + |
| 3 | +from opentelemetry.trace import get_tracer |
| 4 | + |
| 5 | +from cbltest.api.database import Database |
| 6 | +from cbltest.api.replicator import ( |
| 7 | + ReplicatorCollectionEntry, |
| 8 | +) |
| 9 | +from cbltest.logging import cbl_error, cbl_trace |
| 10 | +from cbltest.requests import TestServerRequestType |
| 11 | +from cbltest.v1.requests import ( |
| 12 | + PostGetMultipeerReplicatorStatusRequestBody, |
| 13 | + PostStartMultipeerReplicatorRequestBody, |
| 14 | + PostStopMultipeerReplicatorRequestBody, |
| 15 | +) |
| 16 | +from cbltest.v1.responses import ( |
| 17 | + MultipeerReplicatorStatusEntry, |
| 18 | + PostGetMultipeerReplicatorStatusResponse, |
| 19 | + PostStartMultipeerReplicatorResponse, |
| 20 | +) |
| 21 | +from cbltest.version import VERSION |
| 22 | + |
| 23 | + |
| 24 | +class MultipeerReplicatorStatus: |
| 25 | + """ |
| 26 | + A class representing the status of a Couchbase Lite multipeer replicator |
| 27 | + """ |
| 28 | + |
| 29 | + @property |
| 30 | + def replicators(self) -> list[MultipeerReplicatorStatusEntry]: |
| 31 | + """Gets the list of replicators and their statuses""" |
| 32 | + return self.__replicators |
| 33 | + |
| 34 | + def __init__(self, replicators: list[MultipeerReplicatorStatusEntry]): |
| 35 | + self.__replicators = replicators |
| 36 | + |
| 37 | + |
| 38 | +class MultipeerReplicator: |
| 39 | + """ |
| 40 | + A class representing a Couchbase Lite multipeer replicator inside a test server |
| 41 | + """ |
| 42 | + |
| 43 | + @property |
| 44 | + def peerGroupID(self) -> str: |
| 45 | + """Gets the peer group ID for the replicator""" |
| 46 | + return self.__peerGroupID |
| 47 | + |
| 48 | + @property |
| 49 | + def database(self) -> Database: |
| 50 | + """Gets the database for the replicator""" |
| 51 | + return self.__database |
| 52 | + |
| 53 | + @property |
| 54 | + def collections(self) -> list[ReplicatorCollectionEntry]: |
| 55 | + """Gets the collections for the replicator""" |
| 56 | + return self.__collections |
| 57 | + |
| 58 | + def __init__( |
| 59 | + self, |
| 60 | + peerGroupID: str, |
| 61 | + database: Database, |
| 62 | + collections: list[ReplicatorCollectionEntry], |
| 63 | + ): |
| 64 | + assert database._request_factory.version == 1, ( |
| 65 | + "This version of the cbl test API requires request API v1" |
| 66 | + ) |
| 67 | + self.__index = database._index |
| 68 | + self.__request_factory = database._request_factory |
| 69 | + self.__peerGroupID = peerGroupID |
| 70 | + self.__database = database |
| 71 | + assert len(collections) > 0, "At least one collection is required" |
| 72 | + self.__collections = collections |
| 73 | + self.__tracer = get_tracer(__name__, VERSION) |
| 74 | + self.__id: str = "" |
| 75 | + |
| 76 | + async def start(self) -> None: |
| 77 | + """ |
| 78 | + Starts the multipeer replicator |
| 79 | + """ |
| 80 | + with self.__tracer.start_as_current_span("start_multipeer_replicator"): |
| 81 | + payload = PostStartMultipeerReplicatorRequestBody( |
| 82 | + self.__peerGroupID, self.__database.name, self.__collections |
| 83 | + ) |
| 84 | + |
| 85 | + req = self.__request_factory.create_request( |
| 86 | + TestServerRequestType.START_MULTIPEER_REPLICATOR, payload |
| 87 | + ) |
| 88 | + resp = await self.__request_factory.send_request(self.__index, req) |
| 89 | + if resp.error is not None: |
| 90 | + cbl_error( |
| 91 | + "Failed to start multipeer replicator (see trace log for details)" |
| 92 | + ) |
| 93 | + cbl_trace(resp.error.message) |
| 94 | + return None |
| 95 | + |
| 96 | + cast_resp = cast(PostStartMultipeerReplicatorResponse, resp) |
| 97 | + self.__id = cast_resp.replicator_id |
| 98 | + |
| 99 | + async def stop(self) -> None: |
| 100 | + """ |
| 101 | + Stops the multipeer replicator |
| 102 | + """ |
| 103 | + with self.__tracer.start_as_current_span("stop_multipeer_replicator"): |
| 104 | + if not self.__id: |
| 105 | + cbl_error("Cannot stop multipeer replicator, it has not been started") |
| 106 | + return None |
| 107 | + |
| 108 | + req = self.__request_factory.create_request( |
| 109 | + TestServerRequestType.STOP_MULTIPEER_REPLICATOR, |
| 110 | + PostStopMultipeerReplicatorRequestBody(self.__id), |
| 111 | + ) |
| 112 | + resp = await self.__request_factory.send_request(self.__index, req) |
| 113 | + if resp.error is not None: |
| 114 | + cbl_error( |
| 115 | + "Failed to stop multipeer replicator (see trace log for details)" |
| 116 | + ) |
| 117 | + cbl_trace(resp.error.message) |
| 118 | + return |
| 119 | + |
| 120 | + self.__id = "" |
| 121 | + |
| 122 | + async def get_status(self) -> MultipeerReplicatorStatus | None: |
| 123 | + """ |
| 124 | + Gets the status of the multipeer replicator |
| 125 | + """ |
| 126 | + with self.__tracer.start_as_current_span("get_multipeer_replicator_status"): |
| 127 | + if not self.__id: |
| 128 | + cbl_error( |
| 129 | + "Cannot get status of multipeer replicator, it has not been started" |
| 130 | + ) |
| 131 | + return None |
| 132 | + |
| 133 | + req = self.__request_factory.create_request( |
| 134 | + TestServerRequestType.MULTIPEER_REPLICATOR_STATUS, |
| 135 | + PostGetMultipeerReplicatorStatusRequestBody(self.__id), |
| 136 | + ) |
| 137 | + resp = await self.__request_factory.send_request(self.__index, req) |
| 138 | + if resp.error is not None: |
| 139 | + cbl_error( |
| 140 | + "Failed to get multipeer replicator status (see trace log for details)" |
| 141 | + ) |
| 142 | + cbl_trace(resp.error.message) |
| 143 | + return None |
| 144 | + |
| 145 | + cast_resp = cast(PostGetMultipeerReplicatorStatusResponse, resp) |
| 146 | + return MultipeerReplicatorStatus(cast_resp.replicators) |
0 commit comments