|
1 | | -import { ClickHouse, type TaskEventV2Input } from "@internal/clickhouse"; |
| 1 | +import { |
| 2 | + ClickHouse, |
| 3 | + TASK_EVENT_SEARCH_V2_INSERT_COLUMNS, |
| 4 | + toTaskEventSearchV2Row, |
| 5 | + type TaskEventSearchV2Input, |
| 6 | + type TaskEventV2Input, |
| 7 | +} from "@internal/clickhouse"; |
2 | 8 | import { clickhouseTest } from "@internal/testcontainers"; |
3 | 9 | import { describe, expect, vi } from "vitest"; |
4 | 10 | import { z } from "zod"; |
5 | 11 | import { |
6 | 12 | ClickhouseEventRepository, |
7 | 13 | logsSearchRolloutSelectedRowCount, |
8 | 14 | } from "~/v3/eventRepository/clickhouseEventRepository.server"; |
| 15 | +import { insertLogsSearchRows } from "~/v3/eventRepository/insertLogsSearchRows.server"; |
| 16 | +import { |
| 17 | + INVALID_UTF16_SENTINEL, |
| 18 | + insertWithBadRowSkip, |
| 19 | +} from "~/v3/eventRepository/sanitizeRowsOnParseError.server"; |
9 | 20 | import { latestMetrics, metricSum } from "./otlpMetrics.helpers"; |
10 | 21 | import { createInMemoryMetrics } from "./utils/tracing"; |
11 | 22 |
|
@@ -86,7 +97,10 @@ describe("ClickhouseEventRepository logs search dual writer", () => { |
86 | 97 |
|
87 | 98 | try { |
88 | 99 | const allowedEvents = Array.from({ length: 1_000 }, (_, index) => |
89 | | - event({ span_id: `span_allowed_${index}` }) |
| 100 | + event({ |
| 101 | + span_id: `span_allowed_${index}`, |
| 102 | + message: index === 500 ? "broken \uD800 escape" : "source write survives", |
| 103 | + }) |
90 | 104 | ); |
91 | 105 | (repository as any).addToBatch([ |
92 | 106 | ...allowedEvents, |
@@ -127,6 +141,63 @@ describe("ClickhouseEventRepository logs search dual writer", () => { |
127 | 141 | 60_000 |
128 | 142 | ); |
129 | 143 |
|
| 144 | + clickhouseTest( |
| 145 | + "recovers a strict search insert without losing neighboring rows", |
| 146 | + async ({ clickhouseContainer }) => { |
| 147 | + const clickhouse = new ClickHouse({ |
| 148 | + url: clickhouseContainer.getConnectionUrl(), |
| 149 | + logLevel: "error", |
| 150 | + }); |
| 151 | + const strictInsert = clickhouse.writer.insertUnsafe<TaskEventSearchV2Input>({ |
| 152 | + name: "strict-search-insert", |
| 153 | + table: "trigger_dev.task_events_search_v2", |
| 154 | + columns: TASK_EVENT_SEARCH_V2_INSERT_COLUMNS, |
| 155 | + settings: { input_format_json_throw_on_bad_escape_sequence: 1 }, |
| 156 | + }); |
| 157 | + const rows = ["clean prefix", "broken \uD800 escape", "clean suffix"].map((message, index) => |
| 158 | + toTaskEventSearchV2Row(event({ message, span_id: `span_${index}` }), new Date()) |
| 159 | + ); |
| 160 | + const readMessages = clickhouse.reader.query({ |
| 161 | + name: "read-recovered-search-messages", |
| 162 | + query: `SELECT message FROM trigger_dev.task_events_search_v2 |
| 163 | + WHERE environment_id = {environmentId: String} ORDER BY span_id`, |
| 164 | + params: z.object({ environmentId: z.string() }), |
| 165 | + schema: z.object({ message: z.string() }), |
| 166 | + }); |
| 167 | + |
| 168 | + try { |
| 169 | + const [error] = await strictInsert(rows); |
| 170 | + expect(error?.clickhouseErrorType).toBe("CANNOT_PARSE_ESCAPE_SEQUENCE"); |
| 171 | + const insert = async (batch: TaskEventSearchV2Input[]) => { |
| 172 | + const [insertError, result] = await strictInsert(batch); |
| 173 | + if (insertError) throw insertError; |
| 174 | + return result; |
| 175 | + }; |
| 176 | + await expect( |
| 177 | + insertWithBadRowSkip({ |
| 178 | + rows, |
| 179 | + contextLabel: "default-recovery", |
| 180 | + logger: console, |
| 181 | + insert, |
| 182 | + insertAllowingBadRows: insert, |
| 183 | + }) |
| 184 | + ).rejects.toMatchObject({ clickhouseErrorType: "CANNOT_PARSE_ESCAPE_SEQUENCE" }); |
| 185 | + const outcome = await insertLogsSearchRows(strictInsert, "strict-recovery", rows, console); |
| 186 | + expect(outcome.kind).toBe("sanitized"); |
| 187 | + const [queryError, messages] = await readMessages({ environmentId: "env_dual_write_test" }); |
| 188 | + expect(queryError).toBeNull(); |
| 189 | + expect(messages).toEqual([ |
| 190 | + { message: "clean prefix" }, |
| 191 | + { message: INVALID_UTF16_SENTINEL }, |
| 192 | + { message: "clean suffix" }, |
| 193 | + ]); |
| 194 | + } finally { |
| 195 | + await clickhouse.close(); |
| 196 | + } |
| 197 | + }, |
| 198 | + 60_000 |
| 199 | + ); |
| 200 | + |
130 | 201 | clickhouseTest( |
131 | 202 | "records mapping failures as dropped search rows", |
132 | 203 | async ({ clickhouseContainer }) => { |
@@ -154,6 +225,12 @@ describe("ClickhouseEventRepository logs search dual writer", () => { |
154 | 225 | }); |
155 | 226 |
|
156 | 227 | try { |
| 228 | + expect( |
| 229 | + metricSum(await latestMetrics(metrics), "logs_search.dual_write.rows_dropped", { |
| 230 | + table: "task_events_search_v2", |
| 231 | + reason: "mapping_failed", |
| 232 | + }) |
| 233 | + ).toBe(0); |
157 | 234 | (repository as any).addToBatch([ |
158 | 235 | event({ start_time: new Date().toISOString().replace("T", " ").replace("Z", "") }), |
159 | 236 | ]); |
|
0 commit comments