def _is_balanced(self):
        """Determines if the current assignment is a balanced one"""
        if (
            len(self.current_assignment[self._get_consumer_with_least_subscriptions()])
            >= len(self.current_assignment[self._get_consumer_with_most_subscriptions()]) - 1
        ):
            # if minimum and maximum numbers of partitions assigned to consumers differ by at most one return true
            return True

        # create a mapping from partitions to the consumer assigned to them
        all_assigned_partitions = {}
        for consumer_id, consumer_partitions in six.iteritems(self.current_assignment):
            for partition in consumer_partitions:
                if partition in all_assigned_partitions:
                    log.error("{} is assigned to more than one consumer.".format(partition))
                all_assigned_partitions[partition] = consumer_id

        # for each consumer that does not have all the topic partitions it can get
        # make sure none of the topic partitions it could but did not get cannot be moved to it
        # (because that would break the balance)
        for consumer, _ in self.sorted_current_subscriptions:
            consumer_partition_count = len(self.current_assignment[consumer])
            # skip if this consumer already has all the topic partitions it can get
            if consumer_partition_count == len(self.consumer_to_all_potential_partitions[consumer]):
                continue

            # otherwise make sure it cannot get any more
            for partition in self.consumer_to_all_potential_partitions[consumer]:
                if partition not in self.current_assignment[consumer]:
                    other_consumer = all_assigned_partitions[partition]
                    other_consumer_partition_count = len(self.current_assignment[other_consumer])
                    if consumer_partition_count < other_consumer_partition_count:
                        return False
        return True
Determines if the current assignment is a balanced one
kafka/coordinator/assignors/sticky/sticky_assignor.py