def _refresh_node_list_and_token_map(self, connection, preloaded_results=None,
                                         force_token_rebuild=False):

        if preloaded_results:
            log.debug("[control connection] Refreshing node list and token map using preloaded results")
            peers_result = preloaded_results[0]
            local_result = preloaded_results[1]
        else:
            cl = ConsistencyLevel.ONE
            if not self._token_meta_enabled:
                log.debug("[control connection] Refreshing node list without token map")
                sel_peers = self._SELECT_PEERS_NO_TOKENS
                sel_local = self._SELECT_LOCAL_NO_TOKENS
            else:
                log.debug("[control connection] Refreshing node list and token map")
                sel_peers = self._SELECT_PEERS
                sel_local = self._SELECT_LOCAL
            peers_query = QueryMessage(query=sel_peers, consistency_level=cl)
            local_query = QueryMessage(query=sel_local, consistency_level=cl)
            peers_result, local_result = connection.wait_for_responses(
                peers_query, local_query, timeout=self._timeout)

        peers_result = dict_factory(*peers_result.results)

        partitioner = None
        token_map = {}

        if local_result.results:
            local_rows = dict_factory(*(local_result.results))
            local_row = local_rows[0]
            cluster_name = local_row["cluster_name"]
            self._cluster.metadata.cluster_name = cluster_name

            host = self._cluster.metadata.get_host(connection.host)
            if host:
                datacenter = local_row.get("data_center")
                rack = local_row.get("rack")
                self._update_location_info(host, datacenter, rack)
                host.listen_address = local_row.get("listen_address")
                host.broadcast_address = local_row.get("broadcast_address")
                host.release_version = local_row.get("release_version")

            partitioner = local_row.get("partitioner")
            tokens = local_row.get("tokens")
            if partitioner and tokens:
                token_map[host] = tokens

        # Check metadata.partitioner to see if we haven't built anything yet. If
        # every node in the cluster was in the contact points, we won't discover
        # any new nodes, so we need this additional check.  (See PYTHON-90)
        should_rebuild_token_map = force_token_rebuild or self._cluster.metadata.partitioner is None
        found_hosts = set()
        for row in peers_result:
            addr = self._rpc_from_peer_row(row)

            tokens = row.get("tokens", None)
            if 'tokens' in row and not tokens:  # it was selected, but empty
                log.warning("Excluding host (%s) with no tokens in system.peers table of %s." % (addr, connection.host))
                continue

            found_hosts.add(addr)

            host = self._cluster.metadata.get_host(addr)
            datacenter = row.get("data_center")
            rack = row.get("rack")
            if host is None:
                log.debug("[control connection] Found new host to connect to: %s", addr)
                host, _ = self._cluster.add_host(addr, datacenter, rack, signal=True, refresh_nodes=False)
                should_rebuild_token_map = True
            else:
                should_rebuild_token_map |= self._update_location_info(host, datacenter, rack)

            host.broadcast_address = row.get("peer")
            host.release_version = row.get("release_version")

            if partitioner and tokens:
                token_map[host] = tokens

        for old_host in self._cluster.metadata.all_hosts():
            if old_host.address != connection.host and old_host.address not in found_hosts:
                should_rebuild_token_map = True
                if old_host.address not in self._cluster.contact_points:
                    log.debug("[control connection] Removing host not found in peers metadata: %r", old_host)
                    self._cluster.remove_host(old_host)

        log.debug("[control connection] Finished fetching ring info")
        if partitioner and should_rebuild_token_map:
            log.debug("[control connection] Rebuilding token map due to topology changes")
            self._cluster.metadata.rebuild_token_map(partitioner, token_map)
dataset/ETHPy150Open datastax/python-driver/cassandra/cluster.py/ControlConnection._refresh_node_list_and_token_map/original