forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathprotocol_features.py
More file actions
84 lines (68 loc) · 3.43 KB
/
protocol_features.py
File metadata and controls
84 lines (68 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import logging
from cassandra.shard_info import _ShardingInfo
log = logging.getLogger(__name__)
RATE_LIMIT_ERROR_EXTENSION = "SCYLLA_RATE_LIMIT_ERROR"
TABLETS_ROUTING_V1 = "TABLETS_ROUTING_V1"
USE_METADATA_ID = "SCYLLA_USE_METADATA_ID"
class ProtocolFeatures(object):
rate_limit_error = None
shard_id = 0
sharding_info = None
tablets_routing_v1 = False
use_metadata_id = False
def __init__(self, rate_limit_error=None, shard_id=0, sharding_info=None, tablets_routing_v1=False, use_metadata_id=False):
self.rate_limit_error = rate_limit_error
self.shard_id = shard_id
self.sharding_info = sharding_info
self.tablets_routing_v1 = tablets_routing_v1
self.use_metadata_id = use_metadata_id
@staticmethod
def parse_from_supported(supported):
rate_limit_error = ProtocolFeatures.maybe_parse_rate_limit_error(supported)
shard_id, sharding_info = ProtocolFeatures.parse_sharding_info(supported)
tablets_routing_v1 = ProtocolFeatures.parse_tablets_info(supported)
use_metadata_id = ProtocolFeatures.parse_metadata_id_info(supported)
return ProtocolFeatures(rate_limit_error, shard_id, sharding_info, tablets_routing_v1, use_metadata_id)
@staticmethod
def maybe_parse_rate_limit_error(supported):
vals = supported.get(RATE_LIMIT_ERROR_EXTENSION)
if vals is not None:
code_str = ProtocolFeatures.get_cql_extension_field(vals, "ERROR_CODE")
return int(code_str)
# Looks up a field which starts with `key=` and returns the rest
@staticmethod
def get_cql_extension_field(vals, key):
for v in vals:
stripped_v = v.strip()
if stripped_v.startswith(key) and stripped_v[len(key)] == '=':
result = stripped_v[len(key) + 1:]
return result
return None
def add_startup_options(self, options):
if self.rate_limit_error is not None:
options[RATE_LIMIT_ERROR_EXTENSION] = ""
if self.tablets_routing_v1:
options[TABLETS_ROUTING_V1] = ""
if self.use_metadata_id:
options[USE_METADATA_ID] = ""
@staticmethod
def parse_sharding_info(options):
shard_id = options.get('SCYLLA_SHARD', [''])[0] or None
shards_count = options.get('SCYLLA_NR_SHARDS', [''])[0] or None
partitioner = options.get('SCYLLA_PARTITIONER', [''])[0] or None
sharding_algorithm = options.get('SCYLLA_SHARDING_ALGORITHM', [''])[0] or None
sharding_ignore_msb = options.get('SCYLLA_SHARDING_IGNORE_MSB', [''])[0] or None
shard_aware_port = options.get('SCYLLA_SHARD_AWARE_PORT', [''])[0] or None
shard_aware_port_ssl = options.get('SCYLLA_SHARD_AWARE_PORT_SSL', [''])[0] or None
log.debug("Parsing sharding info from message options %s", options)
if not (shard_id or shards_count or partitioner == "org.apache.cassandra.dht.Murmur3Partitioner" or
sharding_algorithm == "biased-token-round-robin" or sharding_ignore_msb):
return 0, None
return int(shard_id), _ShardingInfo(shard_id, shards_count, partitioner, sharding_algorithm, sharding_ignore_msb,
shard_aware_port, shard_aware_port_ssl)
@staticmethod
def parse_tablets_info(options):
return TABLETS_ROUTING_V1 in options
@staticmethod
def parse_metadata_id_info(options):
return USE_METADATA_ID in options