diff options
author | Clay Gerrard <clay.gerrard@gmail.com> | 2018-03-16 18:44:14 -0700 |
---|---|---|
committer | Clay Gerrard <clay.gerrard@gmail.com> | 2018-03-16 18:45:16 -0700 |
commit | 2c4c23defc615e0f672244457c4d15cd718971dd (patch) | |
tree | 8d018d11380c89704734ad12ff3f355a516c48c0 | |
parent | b08c70d38e1d49b73c241b227e4fcc62fe164a04 (diff) | |
download | swift-2c4c23defc615e0f672244457c4d15cd718971dd.tar.gz |
Encapsulate some general utility in utils
... but either way works.
Change-Id: I56b7689270e528a1d2099ff96030ce660e588c94
Related-Change-Id: Ic108f5c38f700ac4c7bcf8315bf4c55306951361
-rw-r--r-- | swift/common/db_replicator.py | 28 | ||||
-rw-r--r-- | swift/common/utils.py | 30 | ||||
-rw-r--r-- | test/unit/common/test_utils.py | 16 |
3 files changed, 49 insertions, 25 deletions
diff --git a/swift/common/db_replicator.py b/swift/common/db_replicator.py index 0ef17094f..689db0e99 100644 --- a/swift/common/db_replicator.py +++ b/swift/common/db_replicator.py @@ -33,7 +33,7 @@ from swift.common.direct_client import quote from swift.common.utils import get_logger, whataremyips, storage_directory, \ renamer, mkdirs, lock_parent_directory, config_true_value, \ unlink_older_than, dump_recon_cache, rsync_module_interpolation, \ - json, Timestamp, list_from_csv + json, Timestamp, parse_overrides from swift.common import ring from swift.common.ring.utils import is_local_device from swift.common.http import HTTP_NOT_FOUND, HTTP_INSUFFICIENT_STORAGE @@ -47,20 +47,6 @@ from swift.common.swob import Response, HTTPNotFound, HTTPNoContent, \ DEBUG_TIMINGS_THRESHOLD = 10 -def parse_overrides(daemon_kwargs): - devices = list_from_csv(daemon_kwargs.get('devices', '')) - if not devices: - devices = Everything() - - partitions = [ - int(part) for part in - list_from_csv(daemon_kwargs.get('partitions', ''))] - if not partitions: - partitions = Everything() - - return devices, partitions - - def quarantine_db(object_file, server_type): """ In the case that a corrupt file is found, move it to a quarantined area to @@ -149,15 +135,6 @@ def roundrobin_datadirs(datadirs): its.remove(it) -class Everything(object): - """ - A container that contains everything. If "e" is an instance of - Everything, then "x in e" is true for all x. - """ - def __contains__(self, element): - return True - - class ReplConnection(BufferedHTTPConnection): """ Helper to simplify REPLICATEing to a remote server. @@ -679,7 +656,8 @@ class Replicator(Daemon): def run_once(self, *args, **kwargs): """Run a replication pass once.""" - devices_to_replicate, partitions_to_replicate = parse_overrides(kwargs) + devices_to_replicate, partitions_to_replicate = parse_overrides( + **kwargs) self._zero_stats() dirs = [] diff --git a/swift/common/utils.py b/swift/common/utils.py index 8209e7189..c0a837c4f 100644 --- a/swift/common/utils.py +++ b/swift/common/utils.py @@ -3416,6 +3416,15 @@ def get_valid_utf8_str(str_or_unicode): return valid_unicode_str.encode('utf-8') +class Everything(object): + """ + A container that contains everything. If "e" is an instance of + Everything, then "x in e" is true for all x. + """ + def __contains__(self, element): + return True + + def list_from_csv(comma_separated_str): """ Splits the str given and returns a properly stripped list of the comma @@ -3426,6 +3435,27 @@ def list_from_csv(comma_separated_str): return [] +def parse_overrides(devices='', partitions='', **kwargs): + """ + Given daemon kwargs parse out device and partition overrides or Everything. + + :returns: a tuple of (devices, partitions) which an used like containers to + check if a given partition (integer) or device (string) is "in" + the collection on which we should act. + """ + devices = list_from_csv(devices) + if not devices: + devices = Everything() + + partitions = [ + int(part) for part in + list_from_csv(partitions)] + if not partitions: + partitions = Everything() + + return devices, partitions + + def csv_append(csv_string, item): """ Appends an item to a comma-separated string. diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index 9fb543f51..c6359a69a 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -3518,6 +3518,22 @@ cluster_dfw1 = http://dfw1.host/v1/ utils.get_hmac('GET', '/path', 1, 'abc'), 'b17f6ff8da0e251737aa9e3ee69a881e3e092e2f') + def test_parse_overrides(self): + devices, partitions = utils.parse_overrides(devices='sdb1,sdb2') + self.assertIn('sdb1', devices) + self.assertIn('sdb2', devices) + self.assertNotIn('sdb3', devices) + self.assertIn(1, partitions) + self.assertIn('1', partitions) # matches because of Everything + self.assertIn(None, partitions) # matches because of Everything + devices, partitions = utils.parse_overrides(partitions='1,2,3') + self.assertIn('sdb1', devices) + self.assertIn('1', devices) # matches because of Everything + self.assertIn(None, devices) # matches because of Everything + self.assertIn(1, partitions) + self.assertNotIn('1', partitions) + self.assertNotIn(None, partitions) + def test_get_policy_index(self): # Account has no information about a policy req = Request.blank( |