Ejemplo n.º 1
0
    def test_truncate_desired(self):
        cluster = mock.Mock()
        cluster.min_size = 10
        cluster.max_size = 50

        # No constraints
        for desired in [10, 11, 12, 49, 50]:
            actual = su.truncate_desired(cluster, desired, None, None)
            self.assertEqual(desired, actual)

        # min_size specified
        actual = su.truncate_desired(cluster, 10, 20, None)
        self.assertEqual(20, actual)

        # min_size None
        actual = su.truncate_desired(cluster, 5, None, None)
        self.assertEqual(10, actual)

        # max_size specified
        actual = su.truncate_desired(cluster, 20, None, -1)
        self.assertEqual(20, actual)

        actual = su.truncate_desired(cluster, 15, None, 30)
        self.assertEqual(15, actual)

        actual = su.truncate_desired(cluster, 40, None, 30)
        self.assertEqual(30, actual)

        # max_size not specified
        actual = su.truncate_desired(cluster, 40, None, None)
        self.assertEqual(40, actual)

        actual = su.truncate_desired(cluster, 60, None, None)
        self.assertEqual(50, actual)
Ejemplo n.º 2
0
    def test_truncate_desired(self):
        cluster = mock.Mock()
        cluster.min_size = 10
        cluster.max_size = 50

        # No constraints
        for desired in [10, 11, 12, 49, 50]:
            actual = su.truncate_desired(cluster, desired, None, None)
            self.assertEqual(desired, actual)

        # min_size specified
        actual = su.truncate_desired(cluster, 10, 20, None)
        self.assertEqual(20, actual)

        # min_size None
        actual = su.truncate_desired(cluster, 5, None, None)
        self.assertEqual(10, actual)

        # max_size specified
        actual = su.truncate_desired(cluster, 20, None, -1)
        self.assertEqual(20, actual)

        actual = su.truncate_desired(cluster, 15, None, 30)
        self.assertEqual(15, actual)

        actual = su.truncate_desired(cluster, 40, None, 30)
        self.assertEqual(30, actual)

        # max_size not specified
        actual = su.truncate_desired(cluster, 40, None, None)
        self.assertEqual(40, actual)

        actual = su.truncate_desired(cluster, 60, None, None)
        self.assertEqual(50, actual)
Ejemplo n.º 3
0
    def do_resize(self):
        adj_type = self.inputs.get(consts.ADJUSTMENT_TYPE, None)
        number = self.inputs.get(consts.ADJUSTMENT_NUMBER, None)
        min_size = self.inputs.get(consts.ADJUSTMENT_MIN_SIZE, None)
        max_size = self.inputs.get(consts.ADJUSTMENT_MAX_SIZE, None)
        min_step = self.inputs.get(consts.ADJUSTMENT_MIN_STEP, None)
        strict = self.inputs.get(consts.ADJUSTMENT_STRICT, False)

        desired = self.cluster.desired_capacity
        if adj_type is not None:
            # number must be not None according to previous tests
            desired = scaleutils.calculate_desired(
                desired, adj_type, number, min_step)

        # truncate adjustment if permitted (strict==False)
        if strict is False:
            desired = scaleutils.truncate_desired(
                self.cluster, desired, min_size, max_size)

        # check provided params against current properties
        # desired is checked when strict is True
        result = scaleutils.check_size_params(self.cluster, desired, min_size,
                                              max_size, strict)
        if result != '':
            return self.RES_ERROR, result

        # save sanitized properties
        self._update_cluster_properties(desired, min_size, max_size)
        node_list = self.cluster.nodes
        current_size = len(node_list)

        # delete nodes if necessary
        if desired < current_size:
            adjustment = current_size - desired
            if 'deletion' not in self.data:
                self.data['deletion'] = {'count': adjustment}
            candidates = []
            # Choose victims randomly
            i = adjustment
            while i > 0:
                r = random.randrange(len(node_list))
                candidates.append(node_list[r].id)
                node_list.remove(node_list[r])
                i = i - 1

            result, reason = self._delete_nodes(candidates)
            if result != self.RES_OK:
                return result, reason

        # Create new nodes if desired_capacity increased
        if desired > current_size:
            delta = desired - current_size
            self.data['creation'] = {'count': delta}
            result, reason = self._create_nodes(delta)
            if result != self.RES_OK:
                return result, reason

        reason = _('Cluster resize succeeded.')
        self.cluster.set_status(self.context, self.cluster.ACTIVE, reason)
        return self.RES_OK, reason