:py:mod:`subset` ================ .. py:module:: conda._private.shards.subset .. autoapi-nested-parse:: Sharded repodata subsets. Traverse dependencies of installed and to-be-installed packages to generate a useful subset for the solver. The algorithm developed here is a direct result of the following CEP: - https://conda.org/learn/ceps/cep-0016 (Sharded Repodata) In this algorithm we treat a (channel, package name) as a node, its dependencies as edges. We then traverse all edges to discover all reachable (channel, package name) tuples. The solver should be able to find a solution with only this subset. This subset is overgenerous since the user is unlikely to want to install very old packages and their dependencies. If this is too slow, we could deploy heuristics that automatically ignore older package versions. We could also allow the user to configure minimum versions of common packages and ignore older versions and their dependencies, falling back to a full solve if unsatisfiable. We treat both sharded and monolithic repodata as if they were made up of per-package shards, computing a subset of both. This is because it is possible for the monolithic repodata to mention packages that exist in the true sharded repodata but would not be found by only traversing the shards. We treat all repodata as sharded, even if no actual sharded repodata has been found. ## Example usage The following constructs several repodata (`noarch` and `linux-64`) from a single channel name and a list of root packages: ``` from conda.models.channel import Channel from _conda.shards_subset import build_repodata_subset channel = Channel("conda-forge-sharded/linux-64") channel_data = build_repodata_subset(["python", "pandas"], [channel.url()]) repodata = {} for url in channel_data: repodata[url] = channel_data.build_repodata() # ... this is what's fed to the solver ``` Classes ------- .. autoapisummary:: conda._private.shards.subset.Node conda._private.shards.subset.NodeId conda._private.shards.subset.RepodataSubset conda._private.shards.subset.QueueCache Functions --------- .. autoapisummary:: conda._private.shards.subset._nodes_from_packages conda._private.shards.subset.build_repodata_subset conda._private.shards.subset.cache_fetch_thread conda._private.shards.subset.network_fetch_thread conda._private.shards.subset.offline_nofetch_thread Attributes ---------- .. autoapisummary:: conda._private.shards.subset.THREAD_WAIT_TIMEOUT conda._private.shards.subset.REACHABLE_PIPELINED_MAX_TIMEOUTS conda._private.shards.subset.QUEUE_TIMEOUT conda._private.shards.subset._T .. py:data:: THREAD_WAIT_TIMEOUT :value: 5 .. py:data:: REACHABLE_PIPELINED_MAX_TIMEOUTS :value: 10 .. py:data:: QUEUE_TIMEOUT :value: 1 .. py:class:: Node .. py:attribute:: distance :type: int :value: 9223372036854775807 .. py:attribute:: package :type: str :value: '' .. py:attribute:: channel :type: str :value: '' .. py:attribute:: visited :type: bool :value: False .. py:attribute:: shard_url :type: str :value: '' .. py:attribute:: requested_extras :type: frozenset[str] .. py:attribute:: expanded_extras :type: frozenset[str] .. py:attribute:: expanded :type: bool :value: False .. py:method:: to_id() -> NodeId .. py:class:: NodeId .. py:attribute:: package :type: str .. py:attribute:: channel :type: str .. py:attribute:: shard_url :type: str :value: '' .. py:method:: __hash__() .. py:function:: _nodes_from_packages(root_packages: list[str], shardlikes: collections.abc.Iterable[conda._private.shards.shards.ShardBase], root_extras: collections.abc.Mapping[str, collections.abc.Iterable[str]] | None = None) -> collections.abc.Iterator[tuple[NodeId, Node]] Yield (NodeId, Node) for all root packages found in shardlikes. .. py:class:: RepodataSubset(shardlikes: collections.abc.Iterable[conda._private.shards.shards.ShardBase], spec_to_package_name: collections.abc.Callable[[str], str] = spec_to_package_name, repodata_version: int = 1, depth: int = sys.maxsize) Build a subset of repodata by traversing all packages that are dependencies and transitive dependencies of a root set of packages. .. py:attribute:: shardlikes :type: collections.abc.Sequence[conda._private.shards.shards.ShardBase] .. py:attribute:: DEFAULT_STRATEGY :value: 'pipelined' .. py:attribute:: _nodes :type: dict[NodeId, Node] .. py:attribute:: _use_only_tar_bz2 :type: bool .. py:attribute:: _add_pip_as_python_dependency :type: bool .. py:attribute:: _spec_to_package_name :type: collections.abc.Callable[[str], str] .. py:attribute:: _repodata_version :value: 1 .. py:attribute:: depth :value: 9223372036854775807 .. py:property:: node_count :type: int Number of (channel, package) nodes discovered during traversal. .. py:method:: has_strategy(strategy: str) -> bool :classmethod: Return True if this class provides the named shard traversal strategy. .. py:method:: _expansion_specs(node: Node, shard: conda._private.shards.typing.ShardDict) -> collections.abc.Iterator[tuple[str, frozenset[str]]] Yield ``(package name, extras requested by the spec)`` for everything reachable from ``shard`` that has not been traversed for ``node`` yet. On the first expansion that is the shard's ``depends`` plus every group in ``node.requested_extras``. When the node is revisited because another dependency activated additional groups, only those new groups are traversed. Records the groups as expanded on the node. .. py:method:: _neighbors(node: Node) -> collections.abc.Iterator[Node] Retrieve all unvisited neighbors of a node, plus already visited neighbors that need to be revisited because this node's dependency specs activate additional extras groups on them. Neighbors in the context are dependencies of a package. NOTE: This method assumes that the required shards have already been retrieved from the network via batch_retrieve_from_network() before neighbors() is called. It uses visit_package() to access already-loaded shards. .. py:method:: _outgoing(node: Node) All nodes that can be reached by this node, plus cost. .. py:method:: reachable(root_packages, *, strategy=DEFAULT_STRATEGY, root_extras: collections.abc.Mapping[str, collections.abc.Iterable[str]] | None = None) -> None Run named reachability strategy or the default. Update `self.shardlikes` with reachable package records. Later, [shardlike.build_repodata() for shardlike in shardlikes] can be used to generate repodata.json-format subsets of each channel. :param root_packages: installed and requested package names. :param strategy: traversal algorithm, "bfs" or "pipelined". :param root_extras: optional mapping of root package name to an iterable of CEP 44 extras requested for it (e.g. `{"package": ["extras_group"]}`). Extras explicitly set on dependency specs of fetched packages (e.g. `pkg-u[extras=more]`) are followed transitively; groups that nobody requested stay inactive. .. py:method:: reachable_bfs(root_packages, root_extras: collections.abc.Mapping[str, collections.abc.Iterable[str]] | None = None) Fetch all packages reachable from `root_packages`' by following dependencies using the "breadth-first search" algorithm. Update associated `self.shardlikes` to contain enough data to build a repodata subset. .. py:method:: _reachable_bfs(root_packages, shard_cache: conda._private.shards.cache.ShardCache, root_extras: collections.abc.Mapping[str, collections.abc.Iterable[str]] | None = None) Inner reachable_bfs() implementation. .. py:method:: reachable_pipelined(root_packages, root_extras: collections.abc.Mapping[str, collections.abc.Iterable[str]] | None = None) Fetch all packages reachable from `root_packages`' by following dependencies. Build repodata subset using concurrent threads to follow dependencies, fetch from cache, and fetch from network. .. py:method:: _reachable_pipelined(root_packages, network_worker: collections.abc.Callable[[queue.SimpleQueue[collections.abc.Sequence[NodeId] | None], queue.SimpleQueue[list[tuple[NodeId, conda._private.shards.typing.ShardDict] | Exception]], RepodataSubset._reachable_pipelined.cache, collections.abc.Sequence[conda._private.shards.shards.ShardBase]], None], cache: RepodataSubset._reachable_pipelined.cache, root_extras: collections.abc.Mapping[str, collections.abc.Iterable[str]] | None = None) Set up queues and threads for shard traversal with a configurable network_worker. Called by reachable_pipelined() .. py:method:: _pipelined_traversal(root_packages, cache_in_queue: queue.SimpleQueue[list[NodeId] | None], shard_out_queue: queue.SimpleQueue[list[tuple[NodeId, conda._private.shards.typing.ShardDict]] | Exception], cache_thread: threading.Thread, network_thread: threading.Thread, root_extras: collections.abc.Mapping[str, collections.abc.Iterable[str]] | None = None) Run reachability algorithm given queues to submit and receive shards. .. py:method:: _visit_node(parent_node: Node, mentioned_packages: collections.abc.Iterable[tuple[str, frozenset[str]]], extras_by_package: collections.abc.Mapping[str, collections.abc.Iterable[str]] | None = None) -> collections.abc.Iterable[NodeId] Broadcast mentioned packages across channels. yield pending NodeId's. ``mentioned_packages`` are ``(name, extras)`` pairs; extras are the CEP 44 groups requested for that package by the spec that mentions it. A package that already has a node only yields again if the extras add groups to a node whose shard was already processed. .. py:method:: _drain_pending(pending: set[NodeId], shardlikes_by_url: dict[str, conda._private.shards.shards.ShardBase]) -> tuple[list[tuple[NodeId, conda._private.shards.typing.ShardDict]], list[NodeId]] Check pending for in-memory shards. Clear pending. Return a list of shards we have and shards we need to fetch. .. py:function:: build_repodata_subset(root_packages: collections.abc.Iterable[str], channels: dict[str, conda.models.channel.Channel], algorithm: Literal['bfs', 'pipelined'] = RepodataSubset.DEFAULT_STRATEGY, spec_to_package_name_func: collections.abc.Callable[[str], str] = spec_to_package_name, repodata_version: int = 1, depth: int = sys.maxsize, root_extras: collections.abc.Mapping[str, collections.abc.Iterable[str]] | None = None) -> dict[str, conda._private.shards.shards.ShardBase] | None Retrieve all necessary information to build a repodata subset. This function implements the conda.gateways.shards.BuildRepodataSubset protocol, allowing it to be passed to solvers that support sharded repodata optimization. Params: root_packages: iterable of installed and requested package names channels: Channel objects; dict form preferred. algorithm: desired traversal algorithm ("bfs" or "pipelined") spec_to_package_name_func: callable to convert package specs to names. Defaults to the standard spec_to_package_name. repodata_version: repodata format version (1 = classic, 3 = v3). depth: the maximum depth of dependant packages to include in the repodata subset. root_extras: optional mapping of root package name to an iterable of CEP 44 extras requested for it (e.g. `{"package": ["extras_group"]}`). Extras on dependency specs are followed transitively; see `RepodataSubset.reachable`. :returns: None if there are no shards available, or a mapping of channel URL's to ShardBase objects where build_repodata() returns the computed subset. .. py:data:: _T .. py:class:: QueueCache(queue) Implement insert() interface of .cache.ShardCache() as a queue, instead of giving network thread direct access to the database. .. py:attribute:: queue :type: queue.SimpleQueue .. py:method:: insert(shard: conda._private.shards.cache.AnnotatedRawShard) .. py:method:: copy() .. py:method:: __enter__() .. py:method:: __exit__(*_) .. py:function:: cache_fetch_thread(in_queue: queue.SimpleQueue[collections.abc.Sequence[NodeId] | None], shard_out_queue: queue.SimpleQueue[collections.abc.Sequence[tuple[NodeId, conda._private.shards.typing.ShardDict] | Exception]], network_out_queue: queue.SimpleQueue[collections.abc.Sequence[NodeId] | None], cache: conda._private.shards.cache.ShardCache) Fetch batches of shards from cache until in_queue sees None. Enqueue found shards to shard_out_queue, and not found shards to network_out_queue. When we see None on in_queue, send None to both out queues and exit. :param in_queue: NodeId (URLs) to fetch. :param shard_out_queue: fetched shards sent to queue. :param network_out_queue: cache misses forwarded to queue. Same queue is network_fetch_thread's in_queue. :param cache: used to retrieve shards. .. py:function:: network_fetch_thread(in_queue: queue.SimpleQueue[collections.abc.Sequence[NodeId] | None], shard_out_queue: queue.SimpleQueue[list[tuple[NodeId, conda._private.shards.typing.ShardDict] | Exception]], cache: conda._private.shards.cache.ShardCache | QueueCache, shardlikes: collections.abc.Sequence[conda._private.shards.shards.ShardBase]) Fetch shards from the network that are received on in_queue, until we see None. Unhandled exceptions also go to shard_out_queue, and exit this thread. :param in_queue: NodeId (URLs) to fetch. :param shard_out_queue: fetched shards sent to queue. :param cache: once shards are decoded they are stored in cache. :param shardlikes: list of (network-only) shard index objects. .. py:function:: offline_nofetch_thread(in_queue: queue.SimpleQueue[collections.abc.Sequence[NodeId] | None], shard_out_queue: queue.SimpleQueue[list[tuple[NodeId, conda._private.shards.typing.ShardDict] | Exception]], cache: conda._private.shards.cache.ShardCache, shardlikes: collections.abc.Sequence[conda._private.shards.shards.ShardBase]) For offline mode, where network requests are not allowed. Pretend that every network request is an empty shard. Don't save those to the cache. Depending on how many shards are in sqlite3 and which packages were requested, the user may or may not get enough repodata for a solution. :param in_queue: NodeId (URLs) to fetch. :param shard_out_queue: fetched shards sent to queue. :param cache: once shards are decoded they are stored in cache. :param shardlikes: list of (network-only) shard index objects.