Skip to content

api: extend connect with fetch_schema param #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ test/data/*.key
!test/data/localhost.enc.key
test/data/*.pem
test/data/*.srl

.rocks
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
- Support `fetch_schema` parameter for a connection (#219).

### Added

Expand Down
2 changes: 1 addition & 1 deletion docs/source/quick-start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ Through the :class:`~tarantool.Connection` object, you can access

>>> import tarantool
>>> from tarantool.error import CrudModuleError, CrudModuleManyError, DatabaseError
>>> conn = tarantool.Connection(host='localhost',port=3301)
>>> conn = tarantool.Connection(host='localhost',port=3301,fetch_schema=False)

>>> conn.crud_
conn.crud_count( conn.crud_insert( conn.crud_insert_object_many(
Expand Down
79 changes: 67 additions & 12 deletions tarantool/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,8 @@ def __init__(self, host, port,
ssl_password_file=DEFAULT_SSL_PASSWORD_FILE,
packer_factory=default_packer_factory,
unpacker_factory=default_unpacker_factory,
auth_type=None):
auth_type=None,
fetch_schema=True):
"""
:param host: Server hostname or IP address. Use ``None`` for
Unix sockets.
Expand Down Expand Up @@ -736,6 +737,18 @@ def __init__(self, host, port,
``"chap-sha1"``.
:type auth_type: :obj:`None` or :obj:`str`, optional

:param bool fetch_schema: If ``False``, schema is not loaded on connect
and schema updates are not automatically loaded.
As a result, these methods become unavailable:
:meth:`~tarantool.Connection.replace`,
:meth:`~tarantool.Connection.insert`,
:meth:`~tarantool.Connection.delete`,
:meth:`~tarantool.Connection.upsert`,
:meth:`~tarantool.Connection.update`,
:meth:`~tarantool.Connection.select`,
:meth:`~tarantool.Connection.space`.
:type fetch_schema: :obj:`bool`, optional

:raise: :exc:`~tarantool.error.ConfigurationError`,
:meth:`~tarantool.Connection.connect` exceptions

Expand Down Expand Up @@ -766,8 +779,9 @@ def __init__(self, host, port,
self.socket_timeout = socket_timeout
self.reconnect_delay = reconnect_delay
self.reconnect_max_attempts = reconnect_max_attempts
self.schema = Schema(self)
self.schema_version = 1
self.fetch_schema = fetch_schema
self.schema = None
self.schema_version = 0
self._socket = None
self.connected = False
self.error = True
Expand Down Expand Up @@ -1023,7 +1037,11 @@ def connect(self):
if self.transport == SSL_TRANSPORT:
self.wrap_socket_ssl()
self.handshake()
self.load_schema()
if self.fetch_schema:
self.schema = Schema(self)
self.load_schema()
else:
self.schema = None
except SslError as e:
raise e
except Exception as e:
Expand Down Expand Up @@ -1118,7 +1136,8 @@ def _send_request_wo_reconnect(self, request, on_push=None, on_push_ctx=None):
response = request.response_class(self, self._read_response())
break
except SchemaReloadException as e:
self.update_schema(e.schema_version)
if self.schema is not None:
self.update_schema(e.schema_version)
continue

while response._code == IPROTO_CHUNK:
Expand Down Expand Up @@ -1255,6 +1274,9 @@ def update_schema(self, schema_version):
:meta private:
"""

if self.schema is None:
self.schema = Schema(self)

self.schema_version = schema_version
self.flush_schema()

Expand All @@ -1269,6 +1291,19 @@ def flush_schema(self):
self.schema.flush()
self.load_schema()

def _schemaful_connection_check(self):
"""
Checks whether the connection is schemaful.
If the connection is schemaless, an exception will be thrown
about unsupporting the method in connection opened
with fetch_schema=False.

:raise: :exc:`~tarantool.error.NotSupportedError`
"""
if self.schema is None:
raise NotSupportedError('This method is not available in ' +
'connection opened with fetch_schema=False')

def call(self, func_name, *args, on_push=None, on_push_ctx=None):
"""
Execute a CALL request: call a stored Lua function.
Expand Down Expand Up @@ -1366,11 +1401,14 @@ def replace(self, space_name, values, on_push=None, on_push_ctx=None):
:exc:`~tarantool.error.DatabaseError`,
:exc:`~tarantool.error.SchemaError`,
:exc:`~tarantool.error.NetworkError`,
:exc:`~tarantool.error.SslError`
:exc:`~tarantool.error.SslError`,
:exc:`~tarantool.error.NotSupportedError`

.. _replace: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/replace/
"""

self._schemaful_connection_check()

if isinstance(space_name, str):
space_name = self.schema.get_space(space_name).sid
if on_push is not None and not callable(on_push):
Expand Down Expand Up @@ -1411,7 +1449,7 @@ def authenticate(self, user, password):
password=self.password,
auth_type=self._get_auth_type())
auth_response = self._send_request_wo_reconnect(request)
if auth_response.return_code == 0:
if auth_response.return_code == 0 and self.schema is not None:
self.flush_schema()
return auth_response

Expand Down Expand Up @@ -1584,11 +1622,14 @@ def insert(self, space_name, values, on_push=None, on_push_ctx=None):
:exc:`~tarantool.error.DatabaseError`,
:exc:`~tarantool.error.SchemaError`,
:exc:`~tarantool.error.NetworkError`,
:exc:`~tarantool.error.SslError`
:exc:`~tarantool.error.SslError`,
:exc:`~tarantool.error.NotSupportedError`

.. _insert: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/insert/
"""

self._schemaful_connection_check()

if isinstance(space_name, str):
space_name = self.schema.get_space(space_name).sid
if on_push is not None and not callable(on_push):
Expand Down Expand Up @@ -1623,11 +1664,14 @@ def delete(self, space_name, key, *, index=0, on_push=None, on_push_ctx=None):
:exc:`~tarantool.error.DatabaseError`,
:exc:`~tarantool.error.SchemaError`,
:exc:`~tarantool.error.NetworkError`,
:exc:`~tarantool.error.SslError`
:exc:`~tarantool.error.SslError`,
:exc:`~tarantool.error.NotSupportedError`

.. _delete: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/delete/
"""

self._schemaful_connection_check()

key = wrap_key(key)
if isinstance(space_name, str):
space_name = self.schema.get_space(space_name).sid
Expand Down Expand Up @@ -1682,11 +1726,14 @@ def upsert(self, space_name, tuple_value, op_list, *, index=0, on_push=None, on_
:exc:`~tarantool.error.DatabaseError`,
:exc:`~tarantool.error.SchemaError`,
:exc:`~tarantool.error.NetworkError`,
:exc:`~tarantool.error.SslError`
:exc:`~tarantool.error.SslError`,
:exc:`~tarantool.error.NotSupportedError`

.. _upsert: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/upsert/
"""

self._schemaful_connection_check()

if isinstance(space_name, str):
space_name = self.schema.get_space(space_name).sid
if isinstance(index, str):
Expand Down Expand Up @@ -1770,11 +1817,14 @@ def update(self, space_name, key, op_list, *, index=0, on_push=None, on_push_ctx
:exc:`~tarantool.error.DatabaseError`,
:exc:`~tarantool.error.SchemaError`,
:exc:`~tarantool.error.NetworkError`,
:exc:`~tarantool.error.SslError`
:exc:`~tarantool.error.SslError`,
:exc:`~tarantool.error.NotSupportedError`

.. _update: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/update/
"""

self._schemaful_connection_check()

key = wrap_key(key)
if isinstance(space_name, str):
space_name = self.schema.get_space(space_name).sid
Expand Down Expand Up @@ -1956,11 +2006,14 @@ def select(self, space_name, key=None, *, offset=0, limit=0xffffffff, index=0, i
:exc:`~tarantool.error.DatabaseError`,
:exc:`~tarantool.error.SchemaError`,
:exc:`~tarantool.error.NetworkError`,
:exc:`~tarantool.error.SslError`
:exc:`~tarantool.error.SslError`,
:exc:`~tarantool.error.NotSupportedError`

.. _select: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/select/
"""

self._schemaful_connection_check()

if iterator is None:
iterator = ITERATOR_EQ
if key is None or (isinstance(key, (list, tuple)) and
Expand Down Expand Up @@ -1996,6 +2049,8 @@ def space(self, space_name):
:raise: :exc:`~tarantool.error.SchemaError`
"""

self._schemaful_connection_check()

return Space(self, space_name)

def generate_sync(self):
Expand Down
9 changes: 7 additions & 2 deletions tarantool/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ def __init__(self,
call_16=False,
connection_timeout=CONNECTION_TIMEOUT,
strategy_class=RoundRobinStrategy,
refresh_delay=POOL_REFRESH_DELAY):
refresh_delay=POOL_REFRESH_DELAY,
fetch_schema=True):
"""
:param addrs: List of dictionaries describing server addresses:

Expand Down Expand Up @@ -452,6 +453,9 @@ def __init__(self,
`box.info.ro`_ status background refreshes, in seconds.
:type connection_timeout: :obj:`float`, optional

:param fetch_schema: Refer to
:paramref:`~tarantool.Connection.params.fetch_schema`.

:raise: :exc:`~tarantool.error.ConfigurationError`,
:class:`~tarantool.Connection` exceptions

Expand Down Expand Up @@ -500,7 +504,8 @@ def __init__(self,
ssl_ciphers=addr['ssl_ciphers'],
ssl_password=addr['ssl_password'],
ssl_password_file=addr['ssl_password_file'],
auth_type=addr['auth_type'])
auth_type=addr['auth_type'],
fetch_schema=fetch_schema)
)

if connect_now:
Expand Down
9 changes: 7 additions & 2 deletions tarantool/mesh_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ def __init__(self, host=None, port=None,
addrs=None,
strategy_class=RoundRobinStrategy,
cluster_discovery_function=None,
cluster_discovery_delay=CLUSTER_DISCOVERY_DELAY):
cluster_discovery_delay=CLUSTER_DISCOVERY_DELAY,
fetch_schema=True):
"""
:param host: Refer to
:paramref:`~tarantool.Connection.params.host`.
Expand Down Expand Up @@ -425,6 +426,9 @@ def __init__(self, host=None, port=None,
list refresh.
:type cluster_discovery_delay: :obj:`float`, optional

:param fetch_schema: Refer to
:paramref:`~tarantool.Connection.params.fetch_schema`.

:raises: :exc:`~tarantool.error.ConfigurationError`,
:class:`~tarantool.Connection` exceptions,
:class:`~tarantool.MeshConnection.connect` exceptions
Expand Down Expand Up @@ -489,7 +493,8 @@ def __init__(self, host=None, port=None,
ssl_ciphers=addr['ssl_ciphers'],
ssl_password=addr['ssl_password'],
ssl_password_file=addr['ssl_password_file'],
auth_type=addr['auth_type'])
auth_type=addr['auth_type'],
fetch_schema=fetch_schema)

def connect(self):
"""
Expand Down
10 changes: 7 additions & 3 deletions tarantool/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,13 @@ def header(self, length):
"""

self._sync = self.conn.generate_sync()
header = self._dumps({IPROTO_REQUEST_TYPE: self.request_type,
IPROTO_SYNC: self._sync,
IPROTO_SCHEMA_ID: self.conn.schema_version})
header_fields = {
IPROTO_REQUEST_TYPE: self.request_type,
IPROTO_SYNC: self._sync,
}
if self.conn.schema is not None:
header_fields[IPROTO_SCHEMA_ID] = self.conn.schema_version
header = self._dumps(header_fields)

return self._dumps(length + len(header)) + header

Expand Down
Loading