From c1c18da82c8e8d03c4e5b52910661632f8acfe46 Mon Sep 17 00:00:00 2001 From: cheng Date: Mon, 25 Jul 2016 13:23:04 +0000 Subject: [PATCH] check _last_part_moves when pretend_min_part_hours_passed pretend_min_part_hours_passed do things like this: self._last_part_moves[part] = 0xff this will throw exception if self._last_part_moves is None. this patch is to check self._last_part_moves to prevent exception. Closes-bug: #1578835 Change-Id: Ic83c7a338b45bfcf61f5ab6100e6db335c3fa81a --- swift/common/ring/builder.py | 4 +++- test/unit/cli/test_ringbuilder.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/swift/common/ring/builder.py b/swift/common/ring/builder.py index ee25ad7caa..028983966b 100644 --- a/swift/common/ring/builder.py +++ b/swift/common/ring/builder.py @@ -741,9 +741,11 @@ class RingBuilder(object): 255 hours ago and last move epoch to 'the beginning of time'. This can be used to force a full rebalance on the next call to rebalance. """ + self._last_part_moves_epoch = 0 + if not self._last_part_moves: + return for part in range(self.parts): self._last_part_moves[part] = 0xff - self._last_part_moves_epoch = 0 def get_part_devices(self, part): """ diff --git a/test/unit/cli/test_ringbuilder.py b/test/unit/cli/test_ringbuilder.py index 5e86b6fea1..49be6f26ec 100644 --- a/test/unit/cli/test_ringbuilder.py +++ b/test/unit/cli/test_ringbuilder.py @@ -1718,6 +1718,23 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin): ring_invalid_re = re.compile("Ring file .*\.ring\.gz is invalid") self.assertTrue(ring_invalid_re.findall(mock_stdout.getvalue())) + def test_pretend_min_part_hours_passed(self): + self.run_srb("create", 8, 3, 1) + argv_pretend = ["", self.tmpfile, "pretend_min_part_hours_passed"] + # pretend_min_part_hours_passed should success, even not rebalanced + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv_pretend) + self.run_srb("add", + "r1z1-10.1.1.1:2345/sda", 100.0, + "r1z1-10.1.1.1:2345/sdb", 100.0, + "r1z1-10.1.1.1:2345/sdc", 100.0) + argv_rebalance = ["", self.tmpfile, "rebalance"] + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv_rebalance) + self.run_srb("add", "r1z1-10.1.1.1:2345/sdd", 100.0) + # rebalance fail without pretend_min_part_hours_passed + self.assertSystemExit(EXIT_WARNING, ringbuilder.main, argv_rebalance) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv_pretend) + self.assertSystemExit(EXIT_SUCCESS, ringbuilder.main, argv_rebalance) + def test_rebalance(self): self.create_sample_ring() argv = ["", self.tmpfile, "rebalance", "3"]