diff --git a/swh/dataset/exporters/edges.py b/swh/dataset/exporters/edges.py index b911ebf..1d82d32 100644 --- a/swh/dataset/exporters/edges.py +++ b/swh/dataset/exporters/edges.py @@ -1,229 +1,230 @@ # Copyright (C) 2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import base64 import os import os.path import shlex import subprocess import tempfile from typing import Tuple import uuid from swh.dataset.exporter import ExporterDispatch from swh.dataset.utils import ZSTFile, remove_pull_requests -from swh.model.hashutil import hash_to_bytes, hash_to_hex -from swh.model.identifiers import ExtendedObjectType, origin_identifier +from swh.model.hashutil import hash_to_hex +from swh.model.model import Origin +from swh.model.swhids import ExtendedObjectType def swhid(object_type, object_id): # We use string interpolation here instead of using ExtendedSWHID to format, # as building temporary ExtendedSWHID objects has a non-negligeable impact # on performance. return f"swh:1:{object_type.value}:{hash_to_hex(object_id)}" class GraphEdgesExporter(ExporterDispatch): """ Implementation of an exporter which writes all the graph edges of a specific type to a Zstandard-compressed CSV file. Each row of the CSV is in the format: `` ``. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.writers = {} def get_writers_for(self, obj_type: ExtendedObjectType): if obj_type not in self.writers: dataset_path = self.export_path / obj_type.name.lower() dataset_path.mkdir(exist_ok=True) unique_id = str(uuid.uuid4()) nodes_file = dataset_path / ("graph-{}.nodes.csv.zst".format(unique_id)) edges_file = dataset_path / ("graph-{}.edges.csv.zst".format(unique_id)) node_writer = self.exit_stack.enter_context(ZSTFile(str(nodes_file), "w")) edge_writer = self.exit_stack.enter_context(ZSTFile(str(edges_file), "w")) self.writers[obj_type] = (node_writer, edge_writer) return self.writers[obj_type] def get_node_writer_for(self, obj_type: ExtendedObjectType): return self.get_writers_for(obj_type)[0] def get_edge_writer_for(self, obj_type: ExtendedObjectType): return self.get_writers_for(obj_type)[1] def write_node(self, node: Tuple[ExtendedObjectType, bytes]): node_type, node_id = node if node_id is None: return node_swhid = swhid(object_type=node_type, object_id=node_id) node_writer = self.get_node_writer_for(node_type) node_writer.write("{}\n".format(node_swhid)) def write_edge( self, src: Tuple[ExtendedObjectType, bytes], dst: Tuple[ExtendedObjectType, bytes], *, labels=None, ): src_type, src_id = src dst_type, dst_id = dst if src_id is None or dst_id is None: return src_swhid = swhid(object_type=src_type, object_id=src_id) dst_swhid = swhid(object_type=dst_type, object_id=dst_id) edge_line = " ".join([src_swhid, dst_swhid] + (labels if labels else [])) edge_writer = self.get_edge_writer_for(src_type) edge_writer.write("{}\n".format(edge_line)) def process_origin(self, origin): - origin_id = hash_to_bytes(origin_identifier({"url": origin["url"]})) + origin_id = Origin(url=origin["url"]).id self.write_node((ExtendedObjectType.ORIGIN, origin_id)) def process_origin_visit_status(self, visit_status): - origin_id = hash_to_bytes(origin_identifier({"url": visit_status["origin"]})) + origin_id = Origin(url=visit_status["origin"]).id self.write_edge( (ExtendedObjectType.ORIGIN, origin_id), (ExtendedObjectType.SNAPSHOT, visit_status["snapshot"]), ) def process_snapshot(self, snapshot): if self.config.get("remove_pull_requests"): remove_pull_requests(snapshot) self.write_node((ExtendedObjectType.SNAPSHOT, snapshot["id"])) for branch_name, branch in snapshot["branches"].items(): original_branch_name = branch_name while branch and branch.get("target_type") == "alias": branch_name = branch["target"] branch = snapshot["branches"].get(branch_name) if branch is None or not branch_name: continue self.write_edge( (ExtendedObjectType.SNAPSHOT, snapshot["id"]), (ExtendedObjectType[branch["target_type"].upper()], branch["target"]), labels=[base64.b64encode(original_branch_name).decode(),], ) def process_release(self, release): self.write_node((ExtendedObjectType.RELEASE, release["id"])) self.write_edge( (ExtendedObjectType.RELEASE, release["id"]), (ExtendedObjectType[release["target_type"].upper()], release["target"]), ) def process_revision(self, revision): self.write_node((ExtendedObjectType.REVISION, revision["id"])) self.write_edge( (ExtendedObjectType.REVISION, revision["id"]), (ExtendedObjectType.DIRECTORY, revision["directory"]), ) for parent in revision["parents"]: self.write_edge( (ExtendedObjectType.REVISION, revision["id"]), (ExtendedObjectType.REVISION, parent), ) def process_directory(self, directory): self.write_node((ExtendedObjectType.DIRECTORY, directory["id"])) for entry in directory["entries"]: entry_type_mapping = { "file": ExtendedObjectType.CONTENT, "dir": ExtendedObjectType.DIRECTORY, "rev": ExtendedObjectType.REVISION, } self.write_edge( (ExtendedObjectType.DIRECTORY, directory["id"]), (entry_type_mapping[entry["type"]], entry["target"]), labels=[base64.b64encode(entry["name"]).decode(), str(entry["perms"])], ) def process_content(self, content): self.write_node((ExtendedObjectType.CONTENT, content["sha1_git"])) def sort_graph_nodes(export_path, config): """ Generate the node list from the edges files. We cannot solely rely on the object IDs that are read in the journal, as some nodes that are referred to as destinations in the edge file might not be present in the archive (e.g a rev_entry referring to a revision that we do not have crawled yet). The most efficient way of getting all the nodes that are mentioned in the edges file is therefore to use sort(1) on the gigantic edge files to get all the unique node IDs, while using the disk as a temporary buffer. This pipeline does, in order: - concatenate and write all the compressed edges files in graph.edges.csv.zst (using the fact that ZST compression is an additive function) ; - deflate the edges ; - count the number of edges and write it in graph.edges.count.txt ; - count the number of occurrences of each edge type and write them in graph.edges.stats.txt ; - concatenate all the (deflated) nodes from the export with the destination edges, and sort the output to get the list of unique graph nodes ; - count the number of unique graph nodes and write it in graph.nodes.count.txt ; - count the number of occurrences of each node type and write them in graph.nodes.stats.txt ; - compress and write the resulting nodes in graph.nodes.csv.zst. """ # Use awk as a replacement of `sort | uniq -c` to avoid buffering everything # in memory counter_command = "awk '{ t[$0]++ } END { for (i in t) print i,t[i] }'" sort_script = """ pv {export_path}/*/*.edges.csv.zst | tee {export_path}/graph.edges.csv.zst | zstdcat | tee >( wc -l > {export_path}/graph.edges.count.txt ) | tee >( cut -d: -f3,6 | {counter_command} | sort \ > {export_path}/graph.edges.stats.txt ) | tee >( cut -d' ' -f3 | grep . | \ sort -u -S{sort_buffer_size} -T{buffer_path} | \ zstdmt > {export_path}/graph.labels.csv.zst ) | cut -d' ' -f2 | cat - <( zstdcat {export_path}/*/*.nodes.csv.zst ) | sort -u -S{sort_buffer_size} -T{buffer_path} | tee >( wc -l > {export_path}/graph.nodes.count.txt ) | tee >( cut -d: -f3 | {counter_command} | sort \ > {export_path}/graph.nodes.stats.txt ) | zstdmt > {export_path}/graph.nodes.csv.zst """ # Use bytes for the sorting algorithm (faster than being locale-specific) env = { **os.environ.copy(), "LC_ALL": "C", "LC_COLLATE": "C", "LANG": "C", } sort_buffer_size = config.get("sort_buffer_size", "4G") disk_buffer_dir = config.get("disk_buffer_dir", export_path) with tempfile.TemporaryDirectory( prefix=".graph_node_sort_", dir=disk_buffer_dir ) as buffer_path: subprocess.run( [ "bash", "-c", sort_script.format( export_path=shlex.quote(str(export_path)), buffer_path=shlex.quote(str(buffer_path)), sort_buffer_size=shlex.quote(sort_buffer_size), counter_command=counter_command, ), ], env=env, ) diff --git a/swh/dataset/journalprocessor.py b/swh/dataset/journalprocessor.py index 684b7c5..60ad40b 100644 --- a/swh/dataset/journalprocessor.py +++ b/swh/dataset/journalprocessor.py @@ -1,440 +1,441 @@ # Copyright (C) 2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import collections import concurrent.futures from concurrent.futures import FIRST_EXCEPTION, ProcessPoolExecutor import contextlib from hashlib import sha1 import logging import multiprocessing from pathlib import Path import time from typing import Any, Container, Dict, List, Mapping, Optional, Sequence, Tuple, Type from confluent_kafka import Message, TopicPartition import tqdm from swh.dataset.exporter import Exporter from swh.dataset.utils import LevelDBSet from swh.journal.client import JournalClient from swh.journal.serializers import kafka_to_value -from swh.model.identifiers import origin_identifier +from swh.model.hashutil import hash_to_hex +from swh.model.model import Origin from swh.storage.fixer import fix_objects class JournalClientOffsetRanges(JournalClient): """ A subclass of JournalClient reading only inside some specific offset range. Partition assignments have to be manually given to the class. This client can only read a single topic at a time. """ def __init__( self, *args, offset_ranges: Mapping[int, Tuple[int, int]] = None, assignment: Sequence[int] = None, progress_queue: multiprocessing.Queue = None, refresh_every: int = 200, **kwargs, ): """ Args: offset_ranges: A mapping of partition_id -> (low, high) offsets that define the boundaries of the messages to consume. assignment: The list of partitions to assign to this client. progress_queue: a multiprocessing.Queue where the current progress will be reported. refresh_every: the refreshing rate of the progress reporting. """ self.offset_ranges = offset_ranges self.progress_queue = progress_queue self.refresh_every = refresh_every self.assignment = assignment self._messages_to_commit: List[Message] = [] self.count = None self.topic_name: Optional[str] = None kwargs["stop_on_eof"] = True # Stop when the assignment is empty super().__init__(*args, **kwargs) def subscribe(self): self.topic_name = self.subscription[0] time.sleep(0.1) # https://github.com/edenhill/librdkafka/issues/1983 logging.debug("Changing assignment to %s", str(self.assignment)) self.consumer.assign( [TopicPartition(self.topic_name, pid) for pid in self.assignment] ) def unsubscribe(self, partitions: Container[int]): assert self.assignment is not None self.assignment = [pid for pid in self.assignment if pid not in partitions] self.consumer.assign( [TopicPartition(self.topic_name, pid) for pid in self.assignment] ) def process(self, worker_fn): self.count = 0 try: if self.assignment: super().process(worker_fn) finally: self.progress_queue.put(None) def handle_offset(self, message): """ Check whether the client has reached the end of the current partition, and trigger a reassignment if that is the case. """ offset = message.offset() partition_id = message.partition() if offset < 0: # Uninitialized partition offset return if self.count % self.refresh_every == 0: self.progress_queue.put({partition_id: offset}) if offset >= self.offset_ranges[partition_id][1] - 1: if partition_id in self.assignment: self.progress_queue.put({partition_id: offset}) # unsubscribe from partition but make sure current message's # offset will be committed after executing the worker_fn in # process(); see handle_messages() below self._messages_to_commit.append(message) self.unsubscribe([partition_id]) def deserialize_message(self, message): """ Override of the message deserialization to hook the handling of the message offset. We also return the raw objects instead of deserializing them because we will need the partition ID later. """ self.handle_offset(message) self.count += 1 return message def handle_messages(self, messages, worker_fn): """Override of the handle_messages() method to get a chance to commit messages. Make sure messages properly handled by `worker_fn` (executed in super()) do get committed in kafka even if their originating partition has been desubscribed from. This helps having a consistent view of the consumption of each partition at the end of the export process (EOF). """ nb_processed, at_eof = super().handle_messages(messages, worker_fn) for msg in self._messages_to_commit: self.consumer.commit(message=msg) self._messages_to_commit.clear() return nb_processed, at_eof class ParallelJournalProcessor: """ Reads the given object type from the journal in parallel. It creates one JournalExportWorker per process. """ def __init__( self, config, exporters: Sequence[Tuple[Type[Exporter], Dict[str, Any]]], export_id: str, obj_type: str, node_sets_path: Path, processes: int = 1, ): """ Args: config: the exporter config, which should also include the JournalClient configuration. exporters: a list of Exporter to process the objects export_id: a unique identifier for the export that will be used as part of a Kafka consumer group ID. obj_type: The type of SWH object to export. node_sets_path: A directory where to store the node sets. processes: The number of processes to run. """ self.config = config self.exporters = exporters self.group_id = "swh-dataset-export-{}".format(export_id) self.obj_type = obj_type self.processes = processes self.node_sets_path = node_sets_path self.offsets = None def get_offsets(self): """ Compute (lo, high) offset boundaries for all partitions. First pass to fetch all the current low and high watermark offsets of each partition to define the consumption boundaries. If available, use committed offsets as lo offset boundaries. """ if self.offsets is None: client = JournalClient( **self.config["journal"], object_types=[self.obj_type], group_id=self.group_id, ) topic_name = client.subscription[0] topics = client.consumer.list_topics(topic_name).topics partitions = topics[topic_name].partitions self.offsets = {} # LOW watermark offset: The offset of the earliest message in the # topic/partition. If no messages have been written to the topic, # the low watermark offset is set to 0. The low watermark will also # be 0 if one message has been written to the partition (with # offset 0). # HIGH watermark offset: the offset of the latest message in the # topic/partition available for consumption + 1 def fetch_insert_partition_id(partition_id): tp = TopicPartition(topic_name, partition_id) (lo, hi) = client.consumer.get_watermark_offsets(tp) if hi > lo: # hi == low means there is nothing in the partition to consume. # If the partition is not empty, retrieve the committed offset, # if any, to use it at lo offset. committed = client.consumer.committed([tp])[0] lo = max(lo, committed.offset) if hi > lo: # do only process the partition is there are actually new # messages to process (partition not empty and committed # offset is behind the high watermark). self.offsets[partition_id] = (lo, hi) with concurrent.futures.ThreadPoolExecutor( max_workers=self.processes ) as executor: list( tqdm.tqdm( executor.map(fetch_insert_partition_id, partitions.keys()), total=len(partitions), desc=" - Partition offsets", ) ) client.close() return self.offsets def run(self): """ Run the parallel export. """ offsets = self.get_offsets() to_assign = list(offsets.keys()) if not to_assign: print(f" - Export ({self.obj_type}): skipped (nothing to export)") return manager = multiprocessing.Manager() q = manager.Queue() with ProcessPoolExecutor(self.processes + 1) as pool: futures = [] for i in range(self.processes): futures.append( pool.submit( self.export_worker, assignment=to_assign[i :: self.processes], progress_queue=q, ) ) futures.append(pool.submit(self.progress_worker, queue=q)) concurrent.futures.wait(futures, return_when=FIRST_EXCEPTION) for f in futures: if f.running(): continue exc = f.exception() if exc: pool.shutdown(wait=False) f.result() raise exc def progress_worker(self, queue=None): """ An additional worker process that reports the current progress of the export between all the different parallel consumers and across all the partitions, by consuming the shared progress reporting Queue. """ d = {} active_workers = self.processes offset_diff = sum((hi - lo) for lo, hi in self.offsets.values()) desc = f" - Export ({self.obj_type})" with tqdm.tqdm(total=offset_diff, desc=desc) as pbar: while active_workers: item = queue.get() if item is None: active_workers -= 1 continue d.update(item) progress = sum(n + 1 - self.offsets[p][0] for p, n in d.items()) pbar.set_postfix(workers=f"{active_workers}/{self.processes}",) pbar.update(progress - pbar.n) def export_worker(self, assignment, progress_queue): worker = JournalProcessorWorker( self.config, self.exporters, self.group_id, self.obj_type, self.offsets, assignment, progress_queue, self.node_sets_path, ) with worker: worker.run() class JournalProcessorWorker: """ Worker process that processes all the messages and calls the given exporters for each object read from the journal. """ def __init__( self, config, exporters: Sequence[Tuple[Type[Exporter], Dict[str, Any]]], group_id: str, obj_type: str, offsets: Dict[int, Tuple[int, int]], assignment: Sequence[int], progress_queue: multiprocessing.Queue, node_sets_path: Path, ): self.config = config self.group_id = group_id self.obj_type = obj_type self.offsets = offsets self.assignment = assignment self.progress_queue = progress_queue self.node_sets_path = node_sets_path self.node_sets_path.mkdir(exist_ok=True, parents=True) self.node_sets: Dict[Tuple[int, str], LevelDBSet] = {} self.exporters = [ exporter_class(config, **kwargs) for exporter_class, kwargs in exporters ] self.exit_stack: contextlib.ExitStack = contextlib.ExitStack() def __enter__(self): self.exit_stack.__enter__() for exporter in self.exporters: self.exit_stack.enter_context(exporter) return self def __exit__(self, exc_type, exc_value, traceback): self.exit_stack.__exit__(exc_type, exc_value, traceback) def get_node_set_for_object(self, partition_id: int, object_id: bytes): """ Return an on-disk set object, which stores the nodes that have already been processed. Node sets are sharded by partition ID (as each object is guaranteed to be assigned to a deterministic Kafka partition) then by object ID prefix. The sharding path of each file looks like: .node_sets/{origin..content}/part-{0..256}/nodes-{0..f}.sqlite """ # obj_id_prefix = "{:x}".format(object_id[0] % 16) obj_id_prefix = "all" # disable sharding for now shard_id = (partition_id, obj_id_prefix) if shard_id not in self.node_sets: node_set_dir = ( self.node_sets_path / self.obj_type / ("part-{}".format(str(partition_id))) ) node_set_dir.mkdir(exist_ok=True, parents=True) node_set_file = node_set_dir / "nodes-{}.db".format(obj_id_prefix) node_set = LevelDBSet(node_set_file) self.exit_stack.enter_context(node_set) self.node_sets[shard_id] = node_set return self.node_sets[shard_id] def run(self): """ Start a Journal client on the given assignment and process all the incoming messages. """ client = JournalClientOffsetRanges( **self.config["journal"], object_types=[self.obj_type], group_id=self.group_id, debug="cgrp,broker", offset_ranges=self.offsets, assignment=self.assignment, progress_queue=self.progress_queue, **{"message.max.bytes": str(500 * 1024 * 1024)}, ) client.process(self.process_messages) def process_messages(self, messages): """ Process the incoming Kafka messages. """ for object_type, message_list in messages.items(): fixed_objects_by_partition = collections.defaultdict(list) for message in message_list: fixed_objects_by_partition[message.partition()].extend( fix_objects(object_type, [kafka_to_value(message.value())]) ) for partition, objects in fixed_objects_by_partition.items(): for obj in objects: self.process_message(object_type, partition, obj) def process_message(self, object_type, partition, obj): """ Process a single incoming Kafka message if the object it refers to has not been processed yet. It uses an on-disk set to make sure that each object is only ever processed once. """ if object_type == "origin_visit": - origin_id = origin_identifier({"url": obj["origin"]}) + origin_id = hash_to_hex(Origin(url=obj["origin"]).id) visit = obj["visit"] node_id = sha1(f"{origin_id}:{visit}".encode()).digest() elif object_type == "origin_visit_status": if obj["status"] not in ("partial", "full"): # Temporary visit object, not useful for the exports return - origin_id = origin_identifier({"url": obj["origin"]}) + origin_id = hash_to_hex(Origin(url=obj["origin"]).id) visit = obj["visit"] ts = obj["date"].timestamp() node_id = sha1(f"{origin_id}:{visit}:{ts}".encode()).digest() elif object_type == "origin": node_id = sha1(obj["url"].encode()).digest() elif object_type in ("content", "skipped_content"): node_id = obj["sha1_git"] else: node_id = obj["id"] node_set = self.get_node_set_for_object(partition, node_id) if not node_set.add(node_id): # Node already processed, skipping. return for exporter in self.exporters: try: exporter.process_object(object_type, obj) except Exception: logging.exception( "Exporter %s: error while exporting the object: %s", exporter.__class__.__name__, str(obj), )