|
| 1 | +import logging |
| 2 | +import uuid |
| 3 | + |
| 4 | +import typing |
| 5 | + |
| 6 | +from hazelcast.errors import ClientOfflineError |
| 7 | +from hazelcast.hash import hash_to_index |
| 8 | +from hazelcast.serialization.compact import SchemaNotReplicatedError |
| 9 | + |
| 10 | +_logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class _PartitionTable: |
| 14 | + __slots__ = ("connection", "version", "partitions") |
| 15 | + |
| 16 | + def __init__(self, connection, version, partitions): |
| 17 | + self.connection = connection |
| 18 | + self.version = version |
| 19 | + self.partitions = partitions |
| 20 | + |
| 21 | + def __repr__(self): |
| 22 | + return "PartitionTable(connection=%s, version=%s)" % (self.connection, self.version) |
| 23 | + |
| 24 | + |
| 25 | +class PartitionService: |
| 26 | + """ |
| 27 | + Allows retrieving information about the partition count, the partition |
| 28 | + owner or the partition id of a key. |
| 29 | + """ |
| 30 | + |
| 31 | + __slots__ = ("_service", "_serialization_service", "_send_schema_and_retry_fn") |
| 32 | + |
| 33 | + def __init__(self, internal_partition_service, serialization_service, send_schema_and_retry_fn): |
| 34 | + self._service = internal_partition_service |
| 35 | + self._serialization_service = serialization_service |
| 36 | + self._send_schema_and_retry_fn = send_schema_and_retry_fn |
| 37 | + |
| 38 | + def get_partition_owner(self, partition_id: int) -> typing.Optional[uuid.UUID]: |
| 39 | + """ |
| 40 | + Returns the owner of the partition if it's set, ``None`` otherwise. |
| 41 | +
|
| 42 | + Args: |
| 43 | + partition_id: The partition id. |
| 44 | +
|
| 45 | + Returns: |
| 46 | + Owner of the partition |
| 47 | + """ |
| 48 | + return self._service.get_partition_owner(partition_id) |
| 49 | + |
| 50 | + async def get_partition_id(self, key: typing.Any) -> int: |
| 51 | + """ |
| 52 | + Returns the partition id for a key data. |
| 53 | +
|
| 54 | + Args: |
| 55 | + key: The given key. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + The partition id. |
| 59 | + """ |
| 60 | + try: |
| 61 | + key_data = self._serialization_service.to_data(key) |
| 62 | + except SchemaNotReplicatedError as e: |
| 63 | + await self._send_schema_and_retry_fn(e, lambda: None) |
| 64 | + return await self.get_partition_id(key) |
| 65 | + |
| 66 | + return self._service.get_partition_id(key_data) |
| 67 | + |
| 68 | + def get_partition_count(self) -> int: |
| 69 | + """ |
| 70 | + Returns partition count of the connected cluster. |
| 71 | +
|
| 72 | + If partition table is not fetched yet, this method returns ``0``. |
| 73 | +
|
| 74 | + Returns: |
| 75 | + The partition count |
| 76 | + """ |
| 77 | + return self._service.partition_count |
| 78 | + |
| 79 | + |
| 80 | +class InternalPartitionService: |
| 81 | + __slots__ = ("partition_count", "_client", "_partition_table") |
| 82 | + |
| 83 | + def __init__(self, client): |
| 84 | + self.partition_count = 0 |
| 85 | + self._client = client |
| 86 | + self._partition_table = _PartitionTable(None, -1, {}) |
| 87 | + |
| 88 | + def handle_partitions_view_event(self, connection, partitions, version): |
| 89 | + _logger.debug("Handling new partition table with version: %s", version) |
| 90 | + |
| 91 | + table = self._partition_table |
| 92 | + if not self._should_be_applied(connection, partitions, version, table): |
| 93 | + return |
| 94 | + |
| 95 | + new_partitions = self._prepare_partitions(partitions) |
| 96 | + new_table = _PartitionTable(connection, version, new_partitions) |
| 97 | + self._partition_table = new_table |
| 98 | + |
| 99 | + def get_partition_owner(self, partition_id): |
| 100 | + return self._partition_table.partitions.get(partition_id, None) |
| 101 | + |
| 102 | + def get_partition_id(self, key): |
| 103 | + if self.partition_count == 0: |
| 104 | + # Partition count can not be zero for the SYNC mode. |
| 105 | + # On the SYNC mode, we are waiting for the first connection to be established. |
| 106 | + # We are initializing the partition count with the value coming from the server with authentication. |
| 107 | + # This error is used only for ASYNC mode client. |
| 108 | + raise ClientOfflineError() |
| 109 | + return hash_to_index(key.get_partition_hash(), self.partition_count) |
| 110 | + |
| 111 | + def check_and_set_partition_count(self, partition_count): |
| 112 | + if self.partition_count == 0: |
| 113 | + self.partition_count = partition_count |
| 114 | + return True |
| 115 | + return self.partition_count == partition_count |
| 116 | + |
| 117 | + @classmethod |
| 118 | + def _should_be_applied(cls, connection, partitions, version, current): |
| 119 | + if not partitions: |
| 120 | + _logger.debug( |
| 121 | + "Partition view will not be applied since response is empty. " |
| 122 | + "Sending connection: %s, version: %s, current table: %s", |
| 123 | + connection, |
| 124 | + version, |
| 125 | + current, |
| 126 | + ) |
| 127 | + return False |
| 128 | + |
| 129 | + if connection != current.connection: |
| 130 | + _logger.debug( |
| 131 | + "Partition view event coming from a new connection. Old: %s, new: %s", |
| 132 | + current.connection, |
| 133 | + connection, |
| 134 | + ) |
| 135 | + return True |
| 136 | + |
| 137 | + if version <= current.version: |
| 138 | + _logger.debug( |
| 139 | + "Partition view will not be applied since response state version is older. " |
| 140 | + "Sending connection: %s, version: %s, current table: %s", |
| 141 | + connection, |
| 142 | + version, |
| 143 | + current, |
| 144 | + ) |
| 145 | + return False |
| 146 | + |
| 147 | + return True |
| 148 | + |
| 149 | + @classmethod |
| 150 | + def _prepare_partitions(cls, partitions): |
| 151 | + new_partitions = {} |
| 152 | + for uuid, partition_list in partitions: |
| 153 | + for partition in partition_list: |
| 154 | + new_partitions[partition] = uuid |
| 155 | + return new_partitions |
| 156 | + |
| 157 | + |
| 158 | +def string_partition_strategy(key): |
| 159 | + if key is None: |
| 160 | + return None |
| 161 | + try: |
| 162 | + index_of = key.index("@") |
| 163 | + return key[index_of + 1 :] |
| 164 | + except ValueError: |
| 165 | + return key |
0 commit comments