-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[API-510] [API-519] Implement compact serialization (#526)
* Implement compact serialization This PR introduces new compact serialization to the Python client. Users interact with this API through, `compact_serializers` configuration element, which will be documented along with other aspects of the implementation in the future PRs. `compact_serializers` is a dictionary from the type of user class to `CompactSerializer` associated with it. Schema registration and fetching mechanisms are implemented with a minimum overhead to other serialization options. They follow the structures described as `Controlled Serialization` and `Eager Deserialization` in the metadata distribution design. In this PR, only `map#put` and `map#get` APIs support compact serialization. Other APIs will be added in further PRs to not complicate this already huge PR. * address review comments * address review comments * pre-allocate lists * rename read_xxx_or methods to read_xxx_or_default * address review comments * remove unnecessary quotes and unused import * add a note about which thread executes the local schema registrations * simplify _initialize_on_cluster * address review comments * update black version to latest to deal with breaking changes in one of its dependencies
- Loading branch information
Showing
46 changed files
with
7,159 additions
and
712 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import logging | ||
import typing | ||
|
||
from hazelcast.errors import HazelcastSerializationError | ||
from hazelcast.future import Future, ImmediateFuture | ||
from hazelcast.invocation import InvocationService, Invocation | ||
from hazelcast.protocol.codec import ( | ||
client_fetch_schema_codec, | ||
client_send_schema_codec, | ||
client_send_all_schemas_codec, | ||
) | ||
from hazelcast.serialization.compact import CompactStreamSerializer, Schema | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
class CompactSchemaService: | ||
def __init__( | ||
self, | ||
compact_serializer: CompactStreamSerializer, | ||
invocation_service: InvocationService, | ||
): | ||
self._compact_serializer = compact_serializer | ||
self._invocation_service = invocation_service | ||
|
||
def fetch_schema(self, schema_id: int) -> Future: | ||
_logger.debug( | ||
"Could not find schema with the id %s locally. It will be fetched from the cluster.", | ||
schema_id, | ||
) | ||
|
||
request = client_fetch_schema_codec.encode_request(schema_id) | ||
fetch_schema_invocation = Invocation( | ||
request, | ||
response_handler=client_fetch_schema_codec.decode_response, | ||
) | ||
self._invocation_service.invoke(fetch_schema_invocation) | ||
return fetch_schema_invocation.future | ||
|
||
def send_schema(self, schema: Schema, clazz: typing.Type) -> Future: | ||
request = client_send_schema_codec.encode_request(schema) | ||
invocation = Invocation(request) | ||
|
||
def continuation(future): | ||
future.result() | ||
self._compact_serializer.register_sent_schema(schema, clazz) | ||
|
||
self._invocation_service.invoke(invocation) | ||
return invocation.future.continue_with(continuation) | ||
|
||
def send_all_schemas(self) -> Future: | ||
schemas = self._compact_serializer.get_sent_schemas() | ||
if not schemas: | ||
_logger.debug("There is no schema to send to the cluster.") | ||
return ImmediateFuture(None) | ||
|
||
_logger.debug("Sending the following schemas to the cluster: %s", schemas) | ||
|
||
request = client_send_all_schemas_codec.encode_request(schemas) | ||
invocation = Invocation(request, urgent=True) | ||
self._invocation_service.invoke(invocation) | ||
return invocation.future | ||
|
||
def register_fetched_schema(self, schema_id: int, schema: typing.Optional[Schema]) -> None: | ||
if not schema: | ||
raise HazelcastSerializationError( | ||
f"The schema with the id {schema_id} can not be found in the cluster." | ||
) | ||
|
||
self._compact_serializer.register_fetched_schema(schema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.