|
42 | 42 |
|
43 | 43 | from cassandra import (ConsistencyLevel, AuthenticationFailed, |
44 | 44 | OperationTimedOut, UnsupportedOperation, |
45 | | - SchemaTargetType, DriverException) |
| 45 | + SchemaTargetType, DriverException, ProtocolVersion) |
46 | 46 | from cassandra.connection import (ConnectionException, ConnectionShutdown, |
47 | 47 | ConnectionHeartbeat, ProtocolVersionUnsupported) |
48 | 48 | from cassandra.cqltypes import UserType |
|
57 | 57 | IsBootstrappingErrorMessage, |
58 | 58 | BatchMessage, RESULT_KIND_PREPARED, |
59 | 59 | RESULT_KIND_SET_KEYSPACE, RESULT_KIND_ROWS, |
60 | | - RESULT_KIND_SCHEMA_CHANGE, MIN_SUPPORTED_VERSION, |
61 | | - ProtocolHandler) |
| 60 | + RESULT_KIND_SCHEMA_CHANGE, ProtocolHandler) |
62 | 61 | from cassandra.metadata import Metadata, protect_name, murmur3 |
63 | 62 | from cassandra.policies import (TokenAwarePolicy, DCAwareRoundRobinPolicy, SimpleConvictionPolicy, |
64 | 63 | ExponentialReconnectionPolicy, HostDistance, |
@@ -355,45 +354,18 @@ class Cluster(object): |
355 | 354 | server will be automatically used. |
356 | 355 | """ |
357 | 356 |
|
358 | | - protocol_version = 4 |
| 357 | + protocol_version = ProtocolVersion.V4 |
359 | 358 | """ |
360 | 359 | The maximum version of the native protocol to use. |
361 | 360 |
|
| 361 | + See :class:`.ProtocolVersion` for more information about versions. |
| 362 | +
|
362 | 363 | If not set in the constructor, the driver will automatically downgrade |
363 | 364 | version based on a negotiation with the server, but it is most efficient |
364 | 365 | to set this to the maximum supported by your version of Cassandra. |
365 | 366 | Setting this will also prevent conflicting versions negotiated if your |
366 | 367 | cluster is upgraded. |
367 | 368 |
|
368 | | - Version 2 of the native protocol adds support for lightweight transactions, |
369 | | - batch operations, and automatic query paging. The v2 protocol is |
370 | | - supported by Cassandra 2.0+. |
371 | | -
|
372 | | - Version 3 of the native protocol adds support for protocol-level |
373 | | - client-side timestamps (see :attr:`.Session.use_client_timestamp`), |
374 | | - serial consistency levels for :class:`~.BatchStatement`, and an |
375 | | - improved connection pool. |
376 | | -
|
377 | | - Version 4 of the native protocol adds a number of new types, server warnings, |
378 | | - new failure messages, and custom payloads. Details in the |
379 | | - `project docs <https://github.com/apache/cassandra/blob/trunk/doc/native_protocol_v4.spec>`_ |
380 | | -
|
381 | | - The following table describes the native protocol versions that |
382 | | - are supported by each version of Cassandra: |
383 | | -
|
384 | | - +-------------------+-------------------+ |
385 | | - | Cassandra Version | Protocol Versions | |
386 | | - +===================+===================+ |
387 | | - | 1.2 | 1 | |
388 | | - +-------------------+-------------------+ |
389 | | - | 2.0 | 1, 2 | |
390 | | - +-------------------+-------------------+ |
391 | | - | 2.1 | 1, 2, 3 | |
392 | | - +-------------------+-------------------+ |
393 | | - | 2.2 | 1, 2, 3, 4 | |
394 | | - +-------------------+-------------------+ |
395 | | - | 3.x | 3, 4 | |
396 | | - +-------------------+-------------------+ |
397 | 369 | """ |
398 | 370 |
|
399 | 371 | allow_beta_protocol_version = False |
@@ -1141,15 +1113,14 @@ def protocol_downgrade(self, host_addr, previous_version): |
1141 | 1113 | if self._protocol_version_explicit: |
1142 | 1114 | raise DriverException("ProtocolError returned from server while using explicitly set client protocol_version %d" % (previous_version,)) |
1143 | 1115 |
|
1144 | | - new_version = previous_version - 1 |
1145 | | - if new_version < self.protocol_version: |
1146 | | - if new_version >= MIN_SUPPORTED_VERSION: |
1147 | | - log.warning("Downgrading core protocol version from %d to %d for %s. " |
1148 | | - "To avoid this, it is best practice to explicitly set Cluster(protocol_version) to the version supported by your cluster. " |
1149 | | - "http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version", self.protocol_version, new_version, host_addr) |
1150 | | - self.protocol_version = new_version |
1151 | | - else: |
1152 | | - raise DriverException("Cannot downgrade protocol version (%d) below minimum supported version: %d" % (new_version, MIN_SUPPORTED_VERSION)) |
| 1116 | + try: |
| 1117 | + new_version = next(v for v in sorted(ProtocolVersion.SUPPORTED_VERSIONS, reversed=True) if v < previous_version) |
| 1118 | + log.warning("Downgrading core protocol version from %d to %d for %s. " |
| 1119 | + "To avoid this, it is best practice to explicitly set Cluster(protocol_version) to the version supported by your cluster. " |
| 1120 | + "http://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version", self.protocol_version, new_version, host_addr) |
| 1121 | + self.protocol_version = new_version |
| 1122 | + except StopIteration: |
| 1123 | + raise DriverException("Cannot downgrade protocol version below minimum supported version: %d" % (ProtocolVersion.MIN_SUPPORTED,)) |
1153 | 1124 |
|
1154 | 1125 | def connect(self, keyspace=None, wait_for_all_pools=False): |
1155 | 1126 | """ |
|
0 commit comments