def validate(self, stats=False):

        # "len" showed up in profiling, so it's just computed once.
        dev_len = len(self.devs)

        parts_on_devs = sum(d['parts'] for d in self._iter_devs())

        if not self._replica2part2dev:
            raise exceptions.RingValidationError(
                '_replica2part2dev empty; did you forget to rebalance?')

        parts_in_map = sum(len(p2d) for p2d in self._replica2part2dev)
        if parts_on_devs != parts_in_map:
            raise exceptions.RingValidationError(
                'All partitions are not double accounted for: %d != %d' %
                (parts_on_devs, parts_in_map))
        if stats:
            # dev_usage[dev_id] will equal the number of partitions assigned to
            # that device.
            dev_usage = array('I', (0 for _junk in range(dev_len)))
            for part2dev in self._replica2part2dev:
                for dev_id in part2dev:
                    dev_usage[dev_id] += 1

        for dev in self._iter_devs():
            if not isinstance(dev['port'], int):
                raise exceptions.RingValidationError(
                    "Device %d has port %r, which is not an integer." %
                    (dev['id'], dev['port']))

        int_replicas = int(math.ceil(self.replicas))
        rep2part_len = map(len, self._replica2part2dev)
        # check the assignments of each part's replicas
        for part in range(self.parts):
            devs_for_part = []
            for replica, part_len in enumerate(rep2part_len):
                if part_len <= part:
                    # last replica may be short on parts because of floating
                    # replica count
                    if replica + 1 < int_replicas:
                        raise exceptions.RingValidationError(
                            "The partition assignments of replica %r were "
                            "shorter than expected (%s < %s) - this should "
                            "only happen for the last replica" % (
                                replica,
                                len(self._replica2part2dev[replica]),
                                self.parts,
                            ))
                    break
                dev_id = self._replica2part2dev[replica][part]
                if dev_id >= dev_len or not self.devs[dev_id]:
                    raise exceptions.RingValidationError(
                        "Partition %d, replica %d was not allocated "
                        "to a device." %
                        (part, replica))
                devs_for_part.append(dev_id)
            if len(devs_for_part) != len(set(devs_for_part)):
                raise exceptions.RingValidationError(
                    "The partition %s has been assigned to "
                    "duplicate devices %r" % (
                        part, devs_for_part))

        if stats:
            weight_of_one_part = self.weight_of_one_part()
            worst = 0
            for dev in self._iter_devs():
                if not dev['weight']:
                    if dev_usage[dev['id']]:
                        # If a device has no weight, but has partitions, then
                        # its overage is considered "infinity" and therefore
                        # always the worst possible. We show MAX_BALANCE for
                        # convenience.
                        worst = MAX_BALANCE
                        break
                    continue
                skew = abs(100.0 * dev_usage[dev['id']] /
                           (dev['weight'] * weight_of_one_part) - 100.0)
                if skew > worst:
                    worst = skew
            return dev_usage, worst
        return None, None
Validate the ring.
dataset/ETHPy150Open openstack/swift/swift/common/ring/builder.py/RingBuilder.validate