Skip to content

Commit ef90d9a

Browse files
committed
remove legacy execution parameters in cluster
1 parent f7b9b59 commit ef90d9a

34 files changed

Lines changed: 359 additions & 710 deletions

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Other
1616
* Cassandra 2.0 support removal (PYTHON-716)
1717
* PreparedStatement.column_metadata should be renamed to bind_metadata (PYTHON-884)
1818
* Remove Cluster.set_meta_refresh_enabled (PYTHON-890)
19+
* Remove legacy execution parameters (PYTHON-876)
1920
* cqlengine: disallow Counter create, save operations (PYTHON-497)
2021
* cqlengine: remove the negative indices slicing support in ModelQuerySet (PYTHON-875)
2122
* cqlengine: Remove Model.__default_ttl__ (PYTHON-889)

cassandra/cluster.py

Lines changed: 39 additions & 228 deletions
Large diffs are not rendered by default.

cassandra/cqlengine/connection.py

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import six
1818
import threading
1919

20-
from cassandra.cluster import Cluster, _NOT_SET, NoHostAvailable, UserTypeDoesNotExist, ConsistencyLevel
20+
from cassandra.cluster import Cluster, _NOT_SET, NoHostAvailable, UserTypeDoesNotExist
2121
from cassandra.query import SimpleStatement, dict_factory
2222

2323
from cassandra.cqlengine import CQLEngineException
@@ -62,7 +62,6 @@ class Connection(object):
6262
name = None
6363
hosts = None
6464

65-
consistency = None
6665
retry_connect = False
6766
lazy_connect = False
6867
lazy_connect_lock = None
@@ -71,11 +70,9 @@ class Connection(object):
7170
cluster = None
7271
session = None
7372

74-
def __init__(self, name, hosts, consistency=None,
75-
lazy_connect=False, retry_connect=False, cluster_options=None):
73+
def __init__(self, name, hosts, lazy_connect=False, retry_connect=False, cluster_options=None):
7674
self.hosts = hosts
7775
self.name = name
78-
self.consistency = consistency
7976
self.lazy_connect = lazy_connect
8077
self.retry_connect = retry_connect
8178
self.cluster_options = cluster_options if cluster_options else {}
@@ -108,17 +105,14 @@ def setup(self):
108105
self.lazy_connect = True
109106
raise
110107

111-
if self.consistency is not None:
112-
self.session.default_consistency_level = self.consistency
113-
114108
if DEFAULT_CONNECTION in _connections and _connections[DEFAULT_CONNECTION] == self:
115109
cluster = _connections[DEFAULT_CONNECTION].cluster
116110
session = _connections[DEFAULT_CONNECTION].session
117111

118112
self.setup_session()
119113

120114
def setup_session(self):
121-
self.session.row_factory = dict_factory
115+
self.session.cluster.profile_manager.default.row_factory = dict_factory
122116
enc = self.session.encoder
123117
enc.mapping[tuple] = enc.cql_encode_tuple
124118
_register_known_types(self.session.cluster)
@@ -139,21 +133,16 @@ def handle_lazy_connect(self):
139133
self.setup()
140134

141135

142-
def register_connection(name, hosts=None, consistency=None, lazy_connect=False,
143-
retry_connect=False, cluster_options=None, default=False,
144-
session=None):
136+
def register_connection(name, hosts=None, lazy_connect=False, retry_connect=False,
137+
cluster_options=None, default=False, session=None):
145138
"""
146139
Add a connection to the connection registry. ``hosts`` and ``session`` are
147-
mutually exclusive, and ``consistency``, ``lazy_connect``,
148-
``retry_connect``, and ``cluster_options`` only work with ``hosts``. Using
140+
mutually exclusive, and ``lazy_connect``, ``retry_connect``,
141+
and ``cluster_options`` only work with ``hosts``. Using
149142
``hosts`` will create a new :class:`cassandra.cluster.Cluster` and
150143
:class:`cassandra.cluster.Session`.
151144
152145
:param list hosts: list of hosts, (``contact_points`` for :class:`cassandra.cluster.Cluster`).
153-
:param int consistency: The default :class:`~.ConsistencyLevel` for the
154-
registered connection's new session. Default is the same as
155-
:attr:`.Session.default_consistency_level`. For use with ``hosts`` only;
156-
will fail when used with ``session``.
157146
:param bool lazy_connect: True if should not connect until first use. For
158147
use with ``hosts`` only; will fail when used with ``session``.
159148
:param bool retry_connect: True if we should retry to connect even if there
@@ -173,7 +162,6 @@ def register_connection(name, hosts=None, consistency=None, lazy_connect=False,
173162

174163
if session is not None:
175164
invalid_config_args = (hosts is not None or
176-
consistency is not None or
177165
lazy_connect is not False or
178166
retry_connect is not False or
179167
cluster_options is not None)
@@ -184,12 +172,10 @@ def register_connection(name, hosts=None, consistency=None, lazy_connect=False,
184172
conn = Connection.from_session(name, session=session)
185173
conn.setup_session()
186174
else: # use hosts argument
187-
if consistency is None:
188-
consistency = ConsistencyLevel.LOCAL_ONE
189175
conn = Connection(
190176
name, hosts=hosts,
191-
consistency=consistency, lazy_connect=lazy_connect,
192-
retry_connect=retry_connect, cluster_options=cluster_options
177+
lazy_connect=lazy_connect, retry_connect=retry_connect,
178+
cluster_options=cluster_options
193179
)
194180
conn.setup()
195181

@@ -282,8 +268,8 @@ def set_session(s):
282268
if conn.session:
283269
log.warning("configuring new default connection for cqlengine when one was already set")
284270

285-
if s.row_factory is not dict_factory:
286-
raise CQLEngineException("Failed to initialize: 'Session.row_factory' must be 'dict_factory'.")
271+
if s.cluster.profile_manager.default.row_factory is not dict_factory:
272+
raise CQLEngineException("Failed to initialize: the default 'ExecutionProfile.row_factory' must be 'dict_factory'.")
287273
conn.session = s
288274
conn.cluster = s.cluster
289275

@@ -300,7 +286,6 @@ def set_session(s):
300286
def setup(
301287
hosts,
302288
default_keyspace,
303-
consistency=None,
304289
lazy_connect=False,
305290
retry_connect=False,
306291
**kwargs):
@@ -309,7 +294,6 @@ def setup(
309294
310295
:param list hosts: list of hosts, (``contact_points`` for :class:`cassandra.cluster.Cluster`)
311296
:param str default_keyspace: The default keyspace to use
312-
:param int consistency: The global default :class:`~.ConsistencyLevel` - default is the same as :attr:`.Session.default_consistency_level`
313297
:param bool lazy_connect: True if should not connect until first use
314298
:param bool retry_connect: True if we should retry to connect even if there was a connection failure initially
315299
:param \*\*kwargs: Pass-through keyword arguments for :class:`cassandra.cluster.Cluster`
@@ -318,7 +302,7 @@ def setup(
318302
from cassandra.cqlengine import models
319303
models.DEFAULT_KEYSPACE = default_keyspace
320304

321-
register_connection('default', hosts=hosts, consistency=consistency, lazy_connect=lazy_connect,
305+
register_connection('default', hosts=hosts, lazy_connect=lazy_connect,
322306
retry_connect=retry_connect, cluster_options=kwargs, default=True)
323307

324308

cassandra/query.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ def tuple_factory(colnames, rows):
7272
Example::
7373
7474
>>> from cassandra.query import tuple_factory
75+
>>> ep = ExecutionProfile(row_factory=tuple_factory)
76+
>>> cluster = Cluster(execution_profiles={EXEC_PROFILE_DEFAULT: ep})
7577
>>> session = cluster.connect('mykeyspace')
76-
>>> session.row_factory = tuple_factory
7778
>>> rows = session.execute("SELECT name, age FROM users LIMIT 1")
7879
>>> print rows[0]
7980
('Bob', 42)
@@ -92,8 +93,9 @@ def named_tuple_factory(colnames, rows):
9293
Example::
9394
9495
>>> from cassandra.query import named_tuple_factory
96+
>>> ep = ExecutionProfile(row_factory=named_tuple_factory)
97+
>>> cluster = Cluster(execution_profiles={EXEC_PROFILE_DEFAULT: ep})
9598
>>> session = cluster.connect('mykeyspace')
96-
>>> session.row_factory = named_tuple_factory
9799
>>> rows = session.execute("SELECT name, age FROM users LIMIT 1")
98100
>>> user = rows[0]
99101
@@ -122,7 +124,7 @@ def named_tuple_factory(colnames, rows):
122124
"(see Python 'namedtuple' documentation for details on name rules). "
123125
"Results will be returned with positional names. "
124126
"Avoid this by choosing different names, using SELECT \"<col name>\" AS aliases, "
125-
"or specifying a different row_factory on your Session" %
127+
"or specifying a different row_factory on your ExecutionProfile" %
126128
(colnames, clean_column_names))
127129
Row = namedtuple('Row', _sanitize_identifiers(clean_column_names))
128130

@@ -136,8 +138,9 @@ def dict_factory(colnames, rows):
136138
Example::
137139
138140
>>> from cassandra.query import dict_factory
141+
>>> ep = ExecutionProfile(row_factory=dict_factory)
142+
>>> cluster = Cluster(execution_profiles={EXEC_PROFILE_DEFAULT: ep})
139143
>>> session = cluster.connect('mykeyspace')
140-
>>> session.row_factory = dict_factory
141144
>>> rows = session.execute("SELECT name, age FROM users LIMIT 1")
142145
>>> print rows[0]
143146
{u'age': 42, u'name': u'Bob'}
@@ -194,8 +197,7 @@ class Statement(object):
194197
keyspace = None
195198
"""
196199
The string name of the keyspace this query acts on. This is used when
197-
:class:`~.TokenAwarePolicy` is configured for
198-
:attr:`.Cluster.load_balancing_policy`
200+
:class:`~.TokenAwarePolicy` is configured in the profile load balancing policy.
199201
200202
It is set implicitly on :class:`.BoundStatement`, and :class:`.BatchStatement`,
201203
but must be set explicitly on :class:`.SimpleStatement`.

docs/api/cassandra/cluster.rst

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@
1717

1818
.. autoattribute:: auth_provider
1919

20-
.. autoattribute:: load_balancing_policy
21-
2220
.. autoattribute:: reconnection_policy
2321

24-
.. autoattribute:: default_retry_policy
25-
:annotation: = <cassandra.policies.RetryPolicy object>
26-
2722
.. autoattribute:: conviction_policy_factory
2823

2924
.. autoattribute:: address_translator
@@ -120,18 +115,6 @@
120115

121116
.. autoclass:: Session ()
122117

123-
.. autoattribute:: default_timeout
124-
:annotation: = 10.0
125-
126-
.. autoattribute:: default_consistency_level
127-
:annotation: = LOCAL_ONE
128-
129-
.. autoattribute:: default_serial_consistency_level
130-
:annotation: = None
131-
132-
.. autoattribute:: row_factory
133-
:annotation: = <function named_tuple_factory>
134-
135118
.. autoattribute:: default_fetch_size
136119

137120
.. autoattribute:: use_client_timestamp

docs/cqlengine/upgrade_guide.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ Upgrade Guide 3.x to 4.x
4646
# The creation will fail properly due to our pre-check in Pet.save_async()
4747
Pet.create(owner_id=ownder_uuid, pet_id=pet_uuid, pet_type='cat', name='rambo')
4848

49+
* Due to the legacy execution parameters removal in the core driver, you now have to use
50+
execution profiles as well for cqlengine setup or connection registration.
51+
52+
Before::
53+
54+
connection.setup(...,
55+
consistency=ConsistencyLevel.ONE,
56+
load_balancing_policy=RoundRobinPolicy()
57+
)
58+
59+
After::
60+
61+
ep = ExecutionProfile(load_balancing_policy=RoundRobinPolicy(),
62+
consistency_level=ConsistencyLevel.ONE)
63+
64+
connection.setup(..., execution_profiles={EXEC_PROFILE_DEFAULT: ep})
65+
66+
4967
* Model.__table_name__ is now case sensitive
5068
* Model.__table_name_case_sensitive__ has been removed
5169
* Model with counters cannot use `create` or `save` anymore. You have to use the `update` mechanism::

docs/execution_profiles.rst

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,22 @@ a single connected ``Session``. Execution profiles are being introduced to deal
66
configuration options, especially as the database platform evolves more complex workloads.
77

88
The Execution Profile API is being introduced now, in an experimental capacity, in order to take advantage of it in
9-
existing projects, and to gauge interest and feedback in the community. For now, the legacy configuration remains
10-
intact, but legacy and Execution Profile APIs cannot be used simultaneously on the same client ``Cluster``.
9+
existing projects, and to gauge interest and feedback in the community.
1110

1211
This document explains how Execution Profiles relate to existing settings, and shows how to use the new profiles for
1312
request execution.
1413

15-
Mapping Legacy Parameters to Profiles
16-
-------------------------------------
14+
Execution Profile Options
15+
-------------------------
1716

18-
Execution profiles can inherit from :class:`.cluster.ExecutionProfile`, and currently provide the following options,
19-
previously input from the noted attributes:
17+
Execution profiles can inherit from :class:`.cluster.ExecutionProfile` and currently provide the following options:
2018

21-
- load_balancing_policy - :attr:`.Cluster.load_balancing_policy`
22-
- request_timeout - :attr:`.Session.default_timeout`, optional :meth:`.Session.execute` parameter
23-
- retry_policy - :attr:`.Cluster.default_retry_policy`, optional :attr:`.Statement.retry_policy` attribute
24-
- consistency_level - :attr:`.Session.default_consistency_level`, optional :attr:`.Statement.consistency_level` attribute
25-
- serial_consistency_level - :attr:`.Session.default_serial_consistency_level`, optional :attr:`.Statement.serial_consistency_level` attribute
26-
- row_factory - :attr:`.Session.row_factory` attribute
27-
28-
When using the new API, these parameters can be defined by instances of :class:`.cluster.ExecutionProfile`.
19+
- :attr:`.ExecutionProfile.load_balancing_policy`
20+
- :attr:`.ExecutionProfile.request_timeout` (optional :meth:`.Session.execute` parameter)
21+
- :attr:`.ExecutionProfile.retry_policy` (optional :attr:`.Statement.retry_policy` attribute)
22+
- :attr:`.ExecutionProfile.consistency_level` (optional :attr:`.Statement.consistency_level` attribute)
23+
- :attr:`.ExecutionProfile.serial_consistency_level` (optional :attr:`.Statement.serial_consistency_level` attribute)
24+
- :attr:`.ExecutionProfile.row_factory`
2925

3026
Using Execution Profiles
3127
------------------------

docs/faq.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ With prepared statements, the replicas are obtained by ``routing_key``, based on
7575
7676
How does the driver manage request retries?
7777
-------------------------------------------
78-
By default, retries are managed by the :attr:`.Cluster.default_retry_policy` set on the session Cluster. It can also
79-
be specialized per statement by setting :attr:`.Statement.retry_policy`.
78+
By default, retries are managed by the :attr:`.ExecutionProfile.retry_policy` of the selected profile of the
79+
Cluster. It can also be specialized per statement by setting :attr:`.Statement.retry_policy`.
8080
8181
Retries are presently attempted on the same coordinator, but this may change in the future.
8282

docs/getting_started.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ behavior in some other way, this is the place to do it:
4040
.. code-block:: python
4141
4242
from cassandra.cluster import Cluster
43-
from cassandra.policies import DCAwareRoundRobinPolicy
43+
from cassandra.auth import PlainTextAuthProvider
4444
45+
auth_provider = PlainTextAuthProvider(username='cassandra', password='cassandra')
4546
cluster = Cluster(
4647
['10.1.1.3', '10.1.1.4', '10.1.1.5'],
47-
load_balancing_policy=DCAwareRoundRobinPolicy(local_dc='US_EAST'),
48+
auth_provider=auth_provider,
4849
port=9042)
4950
5051
@@ -118,7 +119,8 @@ examples are equivalent:
118119
print row[0], row[1], row[2]
119120
120121
If you prefer another result format, such as a ``dict`` per row, you
121-
can change the :attr:`~.Session.row_factory` attribute.
122+
can change the :attr:`~.ExecutionProfile.row_factory` attribute of the
123+
selected execution profile of the cluster.
122124

123125
For queries that will be run repeatedly, you should use
124126
`Prepared statements <#prepared-statements>`_.
@@ -328,7 +330,7 @@ replicas of the data you are interacting with need to respond for
328330
the query to be considered a success.
329331
330332
By default, :attr:`.ConsistencyLevel.LOCAL_ONE` will be used for all queries.
331-
You can specify a different default for the session on :attr:`.Session.default_consistency_level`.
333+
You can specify a different default using execution profiles (See: :attr:`.ExecutionProfile.consistency_level`).
332334
To specify a different consistency level per request, wrap queries
333335
in a :class:`~.SimpleStatement`:
334336

docs/upgrading.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ Upgrading to 4.0
1212
* :attr:`Cluster.set_meta_refresh_enabled` has been removed. Set :attr:`Cluster.schema_metadata_enabled`
1313
and :attr:`Cluster.token_metadata_enabled` instead.
1414

15+
* Cassandra 2.0 and protocol version 2 support have been removed.
16+
* No more legacy execution parameters. You need to use execution profiles. The
17+
following attributes have been removed:
18+
19+
- Cluster.load_balancing_policy
20+
- Cluster.default_retry_policy
21+
- Session.default_timeout
22+
- Session.default_consistency_level
23+
- Session.default_serial_consistency_level
24+
- Session.row_factory
1525

1626
Upgrading to 3.0
1727
----------------

0 commit comments

Comments
 (0)