Skip to content

Commit 77e0492

Browse files
sylwiaszunejkodkropachev
authored andcommitted
Perform LWT metadata protocol handshake
1 parent 5309326 commit 77e0492

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

cassandra/lwt_info.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2020 ScyllaDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
class _LwtInfo:
16+
"""
17+
Holds LWT-related information parsed from the server's supported features.
18+
"""
19+
20+
def __init__(self, lwt_meta_bit_mask):
21+
self.lwt_meta_bit_mask = lwt_meta_bit_mask

cassandra/protocol_features.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import logging
22

33
from cassandra.shard_info import _ShardingInfo
4+
from cassandra.lwt_info import _LwtInfo
45

56
log = logging.getLogger(__name__)
67

78

9+
LWT_ADD_METADATA_MARK = "SCYLLA_LWT_ADD_METADATA_MARK"
10+
LWT_OPTIMIZATION_META_BIT_MASK = "LWT_OPTIMIZATION_META_BIT_MASK"
811
RATE_LIMIT_ERROR_EXTENSION = "SCYLLA_RATE_LIMIT_ERROR"
912
TABLETS_ROUTING_V1 = "TABLETS_ROUTING_V1"
1013

@@ -13,19 +16,22 @@ class ProtocolFeatures(object):
1316
shard_id = 0
1417
sharding_info = None
1518
tablets_routing_v1 = False
19+
lwt_info = None
1620

17-
def __init__(self, rate_limit_error=None, shard_id=0, sharding_info=None, tablets_routing_v1=False):
21+
def __init__(self, rate_limit_error=None, shard_id=0, sharding_info=None, tablets_routing_v1=False, lwt_info=None):
1822
self.rate_limit_error = rate_limit_error
1923
self.shard_id = shard_id
2024
self.sharding_info = sharding_info
2125
self.tablets_routing_v1 = tablets_routing_v1
26+
self.lwt_info = lwt_info
2227

2328
@staticmethod
2429
def parse_from_supported(supported):
2530
rate_limit_error = ProtocolFeatures.maybe_parse_rate_limit_error(supported)
2631
shard_id, sharding_info = ProtocolFeatures.parse_sharding_info(supported)
2732
tablets_routing_v1 = ProtocolFeatures.parse_tablets_info(supported)
28-
return ProtocolFeatures(rate_limit_error, shard_id, sharding_info, tablets_routing_v1)
33+
lwt_info = ProtocolFeatures.parse_lwt_info(supported)
34+
return ProtocolFeatures(rate_limit_error, shard_id, sharding_info, tablets_routing_v1, lwt_info)
2935

3036
@staticmethod
3137
def maybe_parse_rate_limit_error(supported):
@@ -49,6 +55,8 @@ def add_startup_options(self, options):
4955
options[RATE_LIMIT_ERROR_EXTENSION] = ""
5056
if self.tablets_routing_v1:
5157
options[TABLETS_ROUTING_V1] = ""
58+
if self.lwt_info is not None:
59+
options[LWT_ADD_METADATA_MARK] = str(self.lwt_info.lwt_meta_bit_mask)
5260

5361
@staticmethod
5462
def parse_sharding_info(options):
@@ -72,3 +80,18 @@ def parse_sharding_info(options):
7280
@staticmethod
7381
def parse_tablets_info(options):
7482
return TABLETS_ROUTING_V1 in options
83+
84+
@staticmethod
85+
def parse_lwt_info(options):
86+
value_list = options.get(LWT_ADD_METADATA_MARK, [None])
87+
for value in value_list:
88+
if value is None or not value.startswith(LWT_OPTIMIZATION_META_BIT_MASK + "="):
89+
continue
90+
try:
91+
lwt_meta_bit_mask = int(value[len(LWT_OPTIMIZATION_META_BIT_MASK + "="):])
92+
return _LwtInfo(lwt_meta_bit_mask)
93+
except Exception as e:
94+
log.exception(f"Error while parsing {LWT_ADD_METADATA_MARK}: {e}")
95+
return None
96+
97+
return None

0 commit comments

Comments
 (0)