remove CLUSTER_DELETE from batch-policy

Batch policy will not support CLUSTER_DELETE action anymore.

Change-Id: I28be68277b21625373d421703e157a87e1bdad93
This commit is contained in:
RUIJIE YUAN 2017-06-04 11:56:31 +08:00
parent 8dd682aa2c
commit b6cd2f5bce
2 changed files with 17 additions and 76 deletions

View File

@ -58,7 +58,6 @@ class BatchPolicy(base.Policy):
TARGET = [
('BEFORE', consts.CLUSTER_UPDATE),
('BEFORE', consts.CLUSTER_DELETE),
]
PROFILE_TYPE = [
@ -93,21 +92,20 @@ class BatchPolicy(base.Policy):
self.max_batch_size = self.properties[self.MAX_BATCH_SIZE]
self.pause_time = self.properties[self.PAUSE_TIME]
def _get_batch_size(self, total, action):
"""Get batch size for update/delete operation.
def _get_batch_size(self, total):
"""Get batch size for update operation.
:param total: Total number of nodes.
:param action: Name of action being executed.
:returns: Size of each batch and number of batches.
"""
batch_num = 0
batch_size = 0
diff = 0
# if the action is CLUSTER_DELETE or number of nodes less than
# min_in_service, we divided it to 2 batches
# if the number of nodes less than min_in_service,
# we divided it to 2 batches
diff = int(math.ceil(float(total) / 2))
if (action == consts.CLUSTER_UPDATE and total > self.min_in_service):
if total > self.min_in_service:
diff = total - self.min_in_service
# max_batch_size is -1 if not specified
@ -145,21 +143,13 @@ class BatchPolicy(base.Policy):
def _create_plan(self, action):
nodes = action.entity.nodes
action_name = action.action
plan = {'pause_time': self.pause_time}
if len(nodes) == 0:
if action_name == consts.CLUSTER_UPDATE:
plan['plan'] = []
return True, plan
else:
plan['batch_size'] = 0
return True, plan
batch_size, batch_num = self._get_batch_size(len(nodes), action_name)
if action_name == consts.CLUSTER_DELETE:
plan['batch_size'] = batch_size
plan['plan'] = []
return True, plan
batch_size, batch_num = self._get_batch_size(len(nodes))
plan['plan'] = self._pick_nodes(nodes, batch_size, batch_num)
return True, plan
@ -170,7 +160,7 @@ class BatchPolicy(base.Policy):
'status': base.CHECK_OK,
'reason': _('Batching request validated.'),
}
# for updating and deleting
# for updating
result, value = self._create_plan(action)
if result is False:
@ -179,10 +169,7 @@ class BatchPolicy(base.Policy):
'reason': value,
}
else:
if action.action == consts.CLUSTER_UPDATE:
pd['update'] = value
else:
pd['delete'] = value
pd['update'] = value
action.data.update(pd)
action.store(action.context)

View File

@ -42,28 +42,20 @@ class TestBatchPolicy(base.SenlinTestCase):
self.assertEqual(2, policy.max_batch_size)
self.assertEqual(60, policy.pause_time)
def test__get_batch_size_for_update(self):
def test__get_batch_size(self):
policy = bp.BatchPolicy('test-batch', self.spec)
size, number = policy._get_batch_size(5, 'CLUSTER_UPDATE')
size, number = policy._get_batch_size(5)
self.assertEqual(2, size)
self.assertEqual(3, number)
def test__get_batch_size_for_delete_greater_than_max(self):
policy = bp.BatchPolicy('test-batch', self.spec)
size, number = policy._get_batch_size(5, 'CLUSTER_DELETE')
self.assertEqual(2, size)
self.assertEqual(3, number)
def test__get_batch_size_for_delete_less_than_max(self):
def test__get_batch_size_less_than_max(self):
spec = copy.deepcopy(self.spec)
spec['properties']['max_batch_size'] = 3
policy = bp.BatchPolicy('test-batch', spec)
size, number = policy._get_batch_size(4, 'CLUSTER_DELETE')
size, number = policy._get_batch_size(3)
self.assertEqual(2, size)
self.assertEqual(2, number)
@ -73,7 +65,7 @@ class TestBatchPolicy(base.SenlinTestCase):
spec['properties']['min_in_service'] = 2
policy = bp.BatchPolicy('test-batch', spec)
size, number = policy._get_batch_size(1, 'CLUSTER_UPDATE')
size, number = policy._get_batch_size(1)
self.assertEqual(1, size)
self.assertEqual(1, number)
@ -83,7 +75,7 @@ class TestBatchPolicy(base.SenlinTestCase):
spec['properties']['max_batch_size'] = -1
policy = bp.BatchPolicy('test-batch', spec)
size, number = policy._get_batch_size(5, 'CLUSTER_UPDATE')
size, number = policy._get_batch_size(5)
self.assertEqual(4, size)
self.assertEqual(2, number)
@ -137,30 +129,9 @@ class TestBatchPolicy(base.SenlinTestCase):
'plan': [{'1', '2'}, {'3'}]
}
self.assertEqual(excepted_plan, plan)
mock_cal.assert_called_once_with(3, 'CLUSTER_UPDATE')
mock_cal.assert_called_once_with(3)
mock_pick.assert_called_once_with([node1, node2, node3], 2, 2)
@mock.patch.object(bp.BatchPolicy, '_get_batch_size')
def test__create_plan_for_delete(self, mock_cal):
action = mock.Mock(context=self.context, action='CLUSTER_DELETE')
cluster = mock.Mock(id='cid')
node1, node2, node3 = mock.Mock(), mock.Mock(), mock.Mock()
cluster.nodes = [node1, node2, node3]
action.entity = cluster
mock_cal.return_value = (2, 2)
policy = bp.BatchPolicy('test-batch', self.spec)
res, plan = policy._create_plan(action)
self.assertTrue(res)
excepted_plan = {
'pause_time': self.spec['properties']['pause_time'],
'batch_size': 2
}
self.assertEqual(excepted_plan, plan)
mock_cal.assert_called_once_with(3, 'CLUSTER_DELETE')
def test__create_plan_for_update_no_node(self):
action = mock.Mock(context=self.context, action='CLUSTER_UPDATE')
cluster = mock.Mock(id='cid')
@ -177,23 +148,6 @@ class TestBatchPolicy(base.SenlinTestCase):
}
self.assertEqual(excepted_plan, value)
def test__create_plan_for_delete_no_node(self):
action = mock.Mock(context=self.context, action='CLUSTER_DELETE')
cluster = mock.Mock(id='cid')
cluster.nodes = []
action.entity = cluster
policy = bp.BatchPolicy('test-batch', self.spec)
res, value = policy._create_plan(action)
self.assertTrue(res)
excepted_plan = {
'pause_time': self.spec['properties']['pause_time'],
'batch_size': 0,
}
self.assertEqual(excepted_plan, value)
@mock.patch.object(bp.BatchPolicy, '_create_plan')
def test_pre_op_for_update(self, mock_plan):
action = mock.Mock()