|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2026 NetBox Labs Inc |
| 3 | +""" |
| 4 | +Message chunking utilities for Diode SDK. |
| 5 | +
|
| 6 | +This module provides utilities for chunking large lists of entities into |
| 7 | +size-appropriate chunks for gRPC ingestion, ensuring no chunk exceeds |
| 8 | +the gRPC message size limit. |
| 9 | +""" |
| 10 | + |
| 11 | +from collections.abc import Iterable |
| 12 | + |
| 13 | +from .diode.v1 import ingester_pb2 |
| 14 | + |
| 15 | + |
| 16 | +def create_message_chunks( |
| 17 | + entities: Iterable[ingester_pb2.Entity], max_chunk_size_mb: float = 3.0 |
| 18 | +) -> list[list[ingester_pb2.Entity]]: |
| 19 | + """ |
| 20 | + Create size-aware chunks from entities using greedy bin-packing. |
| 21 | +
|
| 22 | + This function chunks entities to ensure each chunk stays under the specified |
| 23 | + size limit. It uses a greedy bin-packing algorithm that accumulates entities |
| 24 | + until adding the next entity would exceed the limit, then starts a new chunk. |
| 25 | +
|
| 26 | + The default chunk size of 3.0 MB provides a safe margin below the gRPC 4 MB |
| 27 | + message size limit, accounting for protobuf serialization overhead. |
| 28 | +
|
| 29 | + Args: |
| 30 | + entities: Iterable of Entity protobuf messages to chunk |
| 31 | + max_chunk_size_mb: Maximum chunk size in MB (default 3.0) |
| 32 | +
|
| 33 | + Returns: |
| 34 | + List of entity chunks, each under max_chunk_size_mb. Returns at least |
| 35 | + one chunk even if the input is empty. |
| 36 | +
|
| 37 | + Examples: |
| 38 | + >>> entities = [entity1, entity2, entity3, ...] |
| 39 | + >>> chunks = create_message_chunks(entities) |
| 40 | + >>> for chunk in chunks: |
| 41 | + ... client.ingest(chunk) |
| 42 | +
|
| 43 | + >>> # Use a custom chunk size |
| 44 | + >>> chunks = create_message_chunks(entities, max_chunk_size_mb=3.5) |
| 45 | +
|
| 46 | + """ |
| 47 | + # Convert iterable to list if necessary for size estimation |
| 48 | + if not isinstance(entities, list): |
| 49 | + entities = list(entities) |
| 50 | + |
| 51 | + if not entities: |
| 52 | + return [entities] |
| 53 | + |
| 54 | + # Convert MB to bytes |
| 55 | + max_chunk_size_bytes = int(max_chunk_size_mb * 1024 * 1024) |
| 56 | + |
| 57 | + # Quick check: if all entities fit in one chunk, return early |
| 58 | + total_size = estimate_message_size(entities) |
| 59 | + if total_size <= max_chunk_size_bytes: |
| 60 | + return [entities] |
| 61 | + |
| 62 | + # Greedy bin-packing: accumulate entities until limit reached |
| 63 | + base_overhead = ingester_pb2.IngestRequest().ByteSize() |
| 64 | + chunks = [] |
| 65 | + current_chunk: list[ingester_pb2.Entity] = [] |
| 66 | + current_chunk_size = base_overhead # Start with overhead for the chunk |
| 67 | + |
| 68 | + for entity in entities: |
| 69 | + entity_size = entity.ByteSize() |
| 70 | + projected_size = current_chunk_size + entity_size |
| 71 | + |
| 72 | + # Check if adding this entity would exceed limit |
| 73 | + if current_chunk and projected_size > max_chunk_size_bytes: |
| 74 | + # Finalize current chunk and start new one |
| 75 | + chunks.append(current_chunk) |
| 76 | + current_chunk = [entity] |
| 77 | + current_chunk_size = base_overhead + entity_size |
| 78 | + else: |
| 79 | + # Add entity to current chunk |
| 80 | + current_chunk.append(entity) |
| 81 | + current_chunk_size = projected_size |
| 82 | + |
| 83 | + # Add final chunk if not empty |
| 84 | + if current_chunk: |
| 85 | + chunks.append(current_chunk) |
| 86 | + |
| 87 | + return chunks if chunks else [entities] |
| 88 | + |
| 89 | + |
| 90 | +def estimate_message_size(entities: Iterable[ingester_pb2.Entity]) -> int: |
| 91 | + """ |
| 92 | + Estimate the serialized size of entities in bytes. |
| 93 | +
|
| 94 | + Calculates the total size by summing individual entity sizes plus the |
| 95 | + IngestRequest protobuf overhead. |
| 96 | +
|
| 97 | + Args: |
| 98 | + entities: Iterable of Entity protobuf messages |
| 99 | +
|
| 100 | + Returns: |
| 101 | + Estimated size in bytes including IngestRequest overhead |
| 102 | +
|
| 103 | + Examples: |
| 104 | + >>> entities = [entity1, entity2, entity3] |
| 105 | + >>> size_bytes = estimate_message_size(entities) |
| 106 | + >>> size_mb = size_bytes / (1024 * 1024) |
| 107 | + >>> print(f"Estimated size: {size_mb:.2f} MB") |
| 108 | +
|
| 109 | + """ |
| 110 | + # Convert iterable to list if necessary |
| 111 | + if not isinstance(entities, list): |
| 112 | + entities = list(entities) |
| 113 | + |
| 114 | + base_overhead = ingester_pb2.IngestRequest().ByteSize() |
| 115 | + entity_sizes_sum = sum(entity.ByteSize() for entity in entities) |
| 116 | + return base_overhead + entity_sizes_sum |
0 commit comments