def pick_name(self, db, service, service_type, instance_id):
        hostname = None
        # Check for duplicates. But only if instances have names.
        if self.check_dup(db, instance_id) and aerostat.get_hostname(
                db, instance_id):
            logging.warn('Duplicate instance found')
            return None

        results = list(db.servers.find({'service': service}))
        # We only want to count instances in our service with hostnames.
        named_in_service = [item for item in results if item['hostname']]
        num = len(named_in_service)
        logging.info('%s number of hosts with same service found' % num)

        if service_type == 'masterful':
            master_hostname = '%s-master' % (service,)
            if not named_in_service:
                hostname = master_hostname  # first instance will be master.
            elif aerostat.hostname_exists(db,
                        master_hostname) and not self.hostname_instance_exists(
                                db, master_hostname):
                hostname = master_hostname  # replace fallen master.
            else:
                smallest_slave_gap = self.get_smallest_gap(db, service)
                if smallest_slave_gap:
                    hostname = smallest_slave_gap
                else:
                    hostname = '%s-slave-%s' % (service, num)
        else:  # We're iterative.
            smallest_gap = self.get_smallest_gap(db, service)
            # find out if there are gaps in the hostnames, use smallest.
            if smallest_gap:
                hostname = smallest_gap
            else:
                hostname =  '%s-%s' % (service, num) # New instance, no gaps.

        return hostname
Check against the names in the aerostat database.
dataset/ETHPy150Open urbanairship/aerostat/aerostat/registrar.py/Registrar.pick_name