summaryrefslogtreecommitdiff
path: root/swift/common
diff options
context:
space:
mode:
authorindianwhocodes <nairashwin952013@gmail.com>2022-10-10 10:27:00 -0700
committerAlistair Coles <alistairncoles@gmail.com>2023-02-10 09:34:59 +0000
commit9ec90d4d56c598ab5567128fd59dca8c54fe8dc4 (patch)
tree15d6566dcf9b9eb17420077fb5fd373f485808f8 /swift/common
parentd9bf70ae2b28222f62470bba5f6877bbbe057532 (diff)
downloadswift-9ec90d4d56c598ab5567128fd59dca8c54fe8dc4.tar.gz
proxy-server exception logging shows replication_ip/port
Adding a "use_replication" field to the node dict, a helper function to set use_replication dict value for a node copy by looking up the header value for x-backend-use-replication-network Change-Id: Ie05af464765dc10cf585be851f462033fc6bdec7
Diffstat (limited to 'swift/common')
-rw-r--r--swift/common/request_helpers.py46
-rw-r--r--swift/common/utils.py50
2 files changed, 77 insertions, 19 deletions
diff --git a/swift/common/request_helpers.py b/swift/common/request_helpers.py
index 0d4d538eb..7fa97c731 100644
--- a/swift/common/request_helpers.py
+++ b/swift/common/request_helpers.py
@@ -40,7 +40,7 @@ from swift.common.utils import split_path, validate_device_partition, \
close_if_possible, maybe_multipart_byteranges_to_document_iters, \
multipart_byteranges_to_document_iters, parse_content_type, \
parse_content_range, csv_append, list_from_csv, Spliterator, quote, \
- RESERVED, config_true_value, md5, CloseableChain
+ RESERVED, config_true_value, md5, CloseableChain, select_ip_port
from swift.common.wsgi import make_subrequest
@@ -901,13 +901,39 @@ def update_ignore_range_header(req, name):
req.headers[hdr] = csv_append(req.headers.get(hdr), name)
+def is_use_replication_network(headers=None):
+ """
+ Determine if replication network should be used.
+
+ :param headers: a dict of headers
+ :return: the value of the ``x-backend-use-replication-network`` item from
+ ``headers``. If no ``headers`` are given or the item is not found then
+ False is returned.
+ """
+ if headers:
+ for h, v in headers.items():
+ if h.lower() == USE_REPLICATION_NETWORK_HEADER:
+ return config_true_value(v)
+ return False
+
+
def get_ip_port(node, headers):
- use_replication_network = False
- for h, v in headers.items():
- if h.lower() == USE_REPLICATION_NETWORK_HEADER:
- use_replication_network = config_true_value(v)
- break
- if use_replication_network:
- return node['replication_ip'], node['replication_port']
- else:
- return node['ip'], node['port']
+ """
+ Get the ip address and port that should be used for the given ``node``.
+ The normal ip address and port are returned unless the ``node`` or
+ ``headers`` indicate that the replication ip address and port should be
+ used.
+
+ If the ``headers`` dict has an item with key
+ ``x-backend-use-replication-network`` and a truthy value then the
+ replication ip address and port are returned. Otherwise if the ``node``
+ dict has an item with key ``use_replication`` and truthy value then the
+ replication ip address and port are returned. Otherwise the normal ip
+ address and port are returned.
+
+ :param node: a dict describing a node
+ :param headers: a dict of headers
+ :return: a tuple of (ip address, port)
+ """
+ return select_ip_port(
+ node, use_replication=is_use_replication_network(headers))
diff --git a/swift/common/utils.py b/swift/common/utils.py
index d6e9a515f..f6139b0f4 100644
--- a/swift/common/utils.py
+++ b/swift/common/utils.py
@@ -2813,17 +2813,49 @@ def parse_socket_string(socket_string, default_port):
return (host, port)
-def node_to_string(node_dict, replication=False):
- if replication:
- ip = node_dict['replication_ip']
- port = node_dict['replication_port']
+def select_ip_port(node_dict, use_replication=False):
+ """
+ Get the ip address and port that should be used for the given
+ ``node_dict``.
+
+ If ``use_replication`` is True then the replication ip address and port are
+ returned.
+
+ If ``use_replication`` is False (the default) and the ``node`` dict has an
+ item with key ``use_replication`` then that item's value will determine if
+ the replication ip address and port are returned.
+
+ If neither ``use_replication`` nor ``node_dict['use_replication']``
+ indicate otherwise then the normal ip address and port are returned.
+
+ :param node_dict: a dict describing a node
+ :param use_replication: if True then the replication ip address and port
+ are returned.
+ :return: a tuple of (ip address, port)
+ """
+ if use_replication or node_dict.get('use_replication', False):
+ node_ip = node_dict['replication_ip']
+ node_port = node_dict['replication_port']
else:
- ip = node_dict['ip']
- port = node_dict['port']
- if ':' in ip:
+ node_ip = node_dict['ip']
+ node_port = node_dict['port']
+ return node_ip, node_port
+
+
+def node_to_string(node_dict, replication=False):
+ """
+ Get a string representation of a node's location.
+
+ :param node_dict: a dict describing a node
+ :param replication: if True then the replication ip address and port are
+ used, otherwise the normal ip address and port are used.
+ :return: a string of the form <ip address>:<port>/<device>
+ """
+ node_ip, node_port = select_ip_port(node_dict, use_replication=replication)
+ if ':' in node_ip:
# IPv6
- ip = '[%s]' % ip
- return '{}:{}/{}'.format(ip, port, node_dict['device'])
+ node_ip = '[%s]' % node_ip
+ return '{}:{}/{}'.format(node_ip, node_port, node_dict['device'])
def storage_directory(datadir, partition, name_hash):