|
| 1 | +# JSON flattening limits |
| 2 | + |
| 3 | +Generic flattening turns every nested array of an event into rows: an event becomes one row |
| 4 | +for each combination of the values of its arrays. Sibling arrays multiply, so |
| 5 | +`{"a": [0..999], "b": [0..999], "c": [0..999]}`, about 11 KiB of JSON, would expand into |
| 6 | +10^9 rows. Three limits cap that expansion. |
| 7 | + |
| 8 | +Parseable checks each event against the limits before it builds the rows of that event: it |
| 9 | +counts the rows and bytes the event expands into without building them. When an event would |
| 10 | +go over a limit, the whole request is rejected with `400 Bad Request`. None of its events |
| 11 | +are stored, and none of its fields are added to the dataset schema. Events that came before |
| 12 | +it in the same request may already have been expanded in memory by then, within the |
| 13 | +limits, and are dropped with the rest: in a dataset with a custom or time partition, and |
| 14 | +for Kinesis and OTEL, the events of a request are expanded one after the other. An |
| 15 | +endpoint that creates the dataset on first use, such as `/api/v1/ingest`, does that before |
| 16 | +it flattens the request, so a rejected request can still leave a new, empty dataset. |
| 17 | + |
| 18 | +| Environment variable | Flag | Default | Limit | |
| 19 | +| --------------------------------- | --------------------------------- | -------------------- | --------------------------------------------------------- | |
| 20 | +| `P_MAX_FLATTEN_ROWS_PER_EVENT` | `--max-flatten-rows-per-event` | `1000` | Rows a single event may expand into | |
| 21 | +| `P_MAX_FLATTEN_ROWS_PER_REQUEST` | `--max-flatten-rows-per-request` | `10000` | Rows all events of one request may expand into together | |
| 22 | +| `P_MAX_FLATTEN_BYTES_PER_REQUEST` | `--max-flatten-bytes-per-request` | `67108864` (64 MiB) | Bytes all those rows may add up to, measured as compact JSON | |
| 23 | + |
| 24 | +Each value must be a positive integer. A limit cannot be set to zero or switched off. |
| 25 | + |
| 26 | +These limits are new, and they reject some requests that earlier versions accepted, |
| 27 | +including batches of small events without any arrays. Read |
| 28 | +[Upgrading](#upgrading-requests-that-are-now-rejected) before you upgrade. |
| 29 | + |
| 30 | +The byte limit counts the size of the expanded rows as compact JSON, including keys, |
| 31 | +quotes, escapes and delimiters. It also counts what is copied into every row afterwards: |
| 32 | +Kinesis request metadata and OTEL body key prefixes (see below). It is not a measure of the |
| 33 | +memory a request uses. It is independent of `P_MAX_EVENT_PAYLOAD_SIZE` (10 MiB by default, |
| 34 | +at most 100 MiB), which limits the size of the request body as it is received: a request |
| 35 | +within one of them can be over the other. |
| 36 | + |
| 37 | +After generic flattening, every row is flattened structurally: nested objects become fields |
| 38 | +named after the keys on their path, joined with `_`. A parent's name is repeated in every |
| 39 | +field below it, so `{"<128 character name>": {"f0": 0, ..., "f31": 31}, "r": [0, 1, 2, 3]}` |
| 40 | +expands into 4 rows of about 400 bytes, but each of them structurally flattened takes about |
| 41 | +4.4 KiB. Every event is therefore charged the larger of the two: the compact JSON of its |
| 42 | +generic rows, or of those rows structurally flattened. Both are computed in one pass over |
| 43 | +the event, before any row or field name of it is built. The structurally flattened size |
| 44 | +counts every scalar as a field of its own, even when its joined name collides with another |
| 45 | +field (for example `{"a_b": 1, "a": {"b": 2}}`, where one `a_b` overwrites the other), so |
| 46 | +with such collisions it is an overestimate. Empty objects add no field. |
| 47 | + |
| 48 | +## What counts as an event |
| 49 | + |
| 50 | +The limits apply only where generic flattening runs. There, every event takes at least one |
| 51 | +row, including an event without arrays, which generic flattening leaves as it is: |
| 52 | + |
| 53 | +- **JSON and custom sources** (`/api/v1/ingest`, `/api/v1/logstream/{name}`) on datasets using |
| 54 | + schema version V1. Every object in the request body is one event, after any nested |
| 55 | + top-level arrays are spliced in, so a batch (a top-level array) of 10,001 objects takes at |
| 56 | + least 10,001 rows. Bodies nested deeper than `P_MAX_FLATTEN_LEVEL` skip generic |
| 57 | + flattening, so these limits do not apply to them. |
| 58 | +- **Kinesis**: every decoded `data` record is one event, even when it holds a JSON array. |
| 59 | + Each of its rows also gets the request's `requestId` and `timestamp`, so their compact |
| 60 | + JSON size, with keys and delimiters, is counted once per row, on top of the larger of the |
| 61 | + generic and structurally flattened sizes. It is counted in full even for rows that |
| 62 | + already have these fields. A record nested deeper than `P_MAX_FLATTEN_LEVEL` is ingested |
| 63 | + as it is, and not counted. |
| 64 | +- **OTEL logs**: a log record whose body (or a body field) is a string holding a JSON object |
| 65 | + has that object flattened. All flattened bodies of one log record together are one event, |
| 66 | + of at least one row per flattened body. A log record without such a body takes no rows. |
| 67 | + This counts every intermediate row, even though the rows end up merged into the single |
| 68 | + log record. Each top-level field of a flattened body is renamed to `{body key}_{field}` |
| 69 | + before the merge, so the body key, with its escapes and the `_`, is counted again for |
| 70 | + every such field in every row. Structurally flattening the record afterwards repeats the |
| 71 | + body key in the name of every field nested below those too, `{body key}_{field}_{name}`. |
| 72 | + A body is charged the larger of its generic rows with the renamed top-level fields, or |
| 73 | + of its structurally flattened rows with the body key in every field name. This counts |
| 74 | + the rows of a body as if they were all kept, though only the last value of each field |
| 75 | + ends up in the record. |
| 76 | +- **Schema detection** (`/api/v1/logstream/schema/detect`) counts the request body the same |
| 77 | + way as JSON ingestion, structurally flattened rows included. |
| 78 | + |
| 79 | +Every request has a budget of its own. All events of a request share that budget, across |
| 80 | +custom partition elements, Kinesis records, and OTEL resources and scopes. OTEL metrics and |
| 81 | +traces, Kafka, and V0 datasets do not use generic flattening, so these limits do not apply |
| 82 | +to them. Internal dataset statistics are bounded differently, see |
| 83 | +[below](#internal-dataset-statistics-pstats). |
| 84 | + |
| 85 | +## Upgrading: requests that are now rejected |
| 86 | + |
| 87 | +Earlier versions flattened every event without a limit. With the defaults, these requests |
| 88 | +are now rejected: |
| 89 | + |
| 90 | +- **More than 10,000 events in one request.** Every event counts toward |
| 91 | + `P_MAX_FLATTEN_ROWS_PER_REQUEST`, including an event without arrays. A JSON batch of |
| 92 | + 10,001 `{"a":1}` events is about 80 KB, far below the 10 MiB default of |
| 93 | + `P_MAX_EVENT_PAYLOAD_SIZE`, and it is rejected. So is a Kinesis request of more than |
| 94 | + 10,000 records, an OTEL logs request of more than 10,000 log records with JSON object |
| 95 | + bodies, and schema detection of more than 10,000 events. Shippers that batch by size or |
| 96 | + by time, rather than by a number of events, can send such requests. |
| 97 | +- **More than 1,000 values in an array of one event.** An event such as `{"items": [...]}` |
| 98 | + with 1,001 items expands into 1,001 rows, over `P_MAX_FLATTEN_ROWS_PER_EVENT`. That |
| 99 | + includes an event that wraps its records in an array, like `{"records": [...]}`, and a |
| 100 | + Kinesis record that decodes to an array of more than 1,000 objects. The elements of a |
| 101 | + top-level array in a request body are events of their own, so this limit does not apply |
| 102 | + to a top-level array (the limit per request does). |
| 103 | +- **Rows over 64 MiB in one request.** For a JSON batch of events without arrays or nested |
| 104 | + objects, the rows take about the size of the batch without whitespace. With the 10 MiB |
| 105 | + default of `P_MAX_EVENT_PAYLOAD_SIZE`, such batches stay well below |
| 106 | + `P_MAX_FLATTEN_BYTES_PER_REQUEST`; they can reach it once `P_MAX_EVENT_PAYLOAD_SIZE` is |
| 107 | + raised close to or above 64 MiB. Nested arrays, and parent names repeated in the |
| 108 | + structurally flattened field names, make the rows larger than the request. |
| 109 | + |
| 110 | +Nothing is truncated: a request over a limit is rejected whole, with a `400` that names the |
| 111 | +limit, and no part of it is stored. Many shippers treat a `400` as permanent and drop the |
| 112 | +batch instead of retrying it, so compare these limits with your largest requests before you |
| 113 | +upgrade, and look for these errors afterwards. |
| 114 | + |
| 115 | +To migrate, do one of these: |
| 116 | + |
| 117 | +- Configure each shipper to send at most 10,000 events per request (Kinesis records, or |
| 118 | + OTEL log records with JSON object bodies), and to send each record as an event of its own |
| 119 | + (an element of a top-level array, or a Kinesis record of its own) instead of wrapping |
| 120 | + many of them in the array of one event. |
| 121 | +- Raise the limits to fit your largest requests. `P_MAX_FLATTEN_ROWS_PER_REQUEST` must be at |
| 122 | + least the number of events in a request, plus the rows their arrays add. |
| 123 | + `P_MAX_FLATTEN_ROWS_PER_EVENT` must be at least the length of the longest array of one |
| 124 | + event (the product of the lengths, for sibling arrays). The request size limit and the |
| 125 | + byte limit are independent. Raising `P_MAX_EVENT_PAYLOAD_SIZE`, which can go up to |
| 126 | + 100 MiB, lets larger requests in, but the rows they expand into are still held to |
| 127 | + `P_MAX_FLATTEN_BYTES_PER_REQUEST` (64 MiB by default) and to the row limits, so raise |
| 128 | + those too for larger flat batches. Higher limits let a request use more memory. |
| 129 | + |
| 130 | +## Internal dataset statistics (`pstats`) |
| 131 | + |
| 132 | +Parseable computes statistics for every parquet file it uploads (unless |
| 133 | +`P_COLLECT_DATASET_STATS` is off) and ingests them into the internal `pstats` dataset, one |
| 134 | +row for every distinct value of every field (up to `P_MAX_FIELD_STATISTICS` per field), and |
| 135 | +one row for a field without distinct values. These rows are not generically flattened. |
| 136 | +They are built straight from the statistics, with the fields and values that flattening |
| 137 | +gave them before, one row at a time, and each row is ingested before the next is built. So |
| 138 | +the row limits do not apply, and the statistics of a file are ingested in full however many |
| 139 | +rows they take. |
| 140 | + |
| 141 | +The byte limit applies to each row on its own. A row is measured before it is built. A row |
| 142 | +larger than `P_MAX_FLATTEN_BYTES_PER_REQUEST` as compact JSON, which takes a distinct value |
| 143 | +or a field name of about that size, is skipped with a warning that names the stream, the |
| 144 | +file and the field (cut to 128 characters), but no value. The other rows of the file are |
| 145 | +still ingested. A failure to compute or ingest the statistics of a file is logged as a |
| 146 | +warning, with the stream, the tenant and the file. |
| 147 | + |
| 148 | +## When a request is rejected |
| 149 | + |
| 150 | +The error message names the limit that was exceeded and its value. To get under the limit, |
| 151 | +reduce the number or length of the nested arrays in each event, or split the batch into |
| 152 | +smaller requests. You can also raise the limit, see |
| 153 | +[Upgrading](#upgrading-requests-that-are-now-rejected). |
| 154 | + |
| 155 | +`tools/flatten-bench/http_smoke.py` checks these responses against a built server binary, |
| 156 | +including flat batches at and just over the rows per request. |
0 commit comments