feat(alerting): ClickHouse projections for alerting read paths

Adds alerting_projections.sql with four projections (alerting_app_status,
alerting_route_status on executions; alerting_app_level on logs;
alerting_instance_metric on agent_metrics). ClickHouseSchemaInitializer now
runs both init.sql and alerting_projections.sql, with ADD PROJECTION and
MATERIALIZE treated as non-fatal — executions (ReplacingMergeTree) requires
deduplicate_merge_projection_mode=rebuild which is unavailable via JDBC pool.
MergeTree projections (logs, agent_metrics) always succeed and are asserted in IT.

Column names confirmed from init.sql: logs uses 'application' (not application_id),
agent_metrics uses 'collected_at' (not timestamp). All column names match the plan.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
hsiegeln
2026-04-19 19:18:58 +02:00
parent 7b79d3aa64
commit 7c0e94a425
3 changed files with 107 additions and 4 deletions

View File

@@ -26,9 +26,14 @@ public class ClickHouseSchemaInitializer {
@EventListener(ApplicationReadyEvent.class)
public void initializeSchema() {
runScript("clickhouse/init.sql");
runScript("clickhouse/alerting_projections.sql");
}
private void runScript(String classpathResource) {
try {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource script = resolver.getResource("classpath:clickhouse/init.sql");
Resource script = resolver.getResource("classpath:" + classpathResource);
String sql = script.getContentAsString(StandardCharsets.UTF_8);
log.info("Executing ClickHouse schema: {}", script.getFilename());
@@ -41,13 +46,28 @@ public class ClickHouseSchemaInitializer {
.filter(line -> !line.isEmpty())
.reduce("", (a, b) -> a + b);
if (!withoutComments.isEmpty()) {
clickHouseJdbc.execute(trimmed);
String upper = withoutComments.toUpperCase();
boolean isBestEffort = upper.contains("MATERIALIZE PROJECTION")
|| upper.contains("ADD PROJECTION");
try {
clickHouseJdbc.execute(trimmed);
} catch (Exception e) {
if (isBestEffort) {
// ADD PROJECTION on ReplacingMergeTree requires a session setting not available
// via JDBC pool; MATERIALIZE can fail on empty tables — both are non-fatal.
log.warn("Projection DDL step skipped (non-fatal): {} — {}",
trimmed.substring(0, Math.min(trimmed.length(), 120)), e.getMessage());
} else {
throw e;
}
}
}
}
log.info("ClickHouse schema initialization complete");
log.info("ClickHouse schema script complete: {}", script.getFilename());
} catch (Exception e) {
log.error("ClickHouse schema initialization failed — server will continue but ClickHouse features may not work", e);
log.error("ClickHouse schema script failed [{}] — server will continue but ClickHouse features may not work",
classpathResource, e);
}
}
}

View File

@@ -0,0 +1,33 @@
-- Alerting projections — additive and idempotent (IF NOT EXISTS).
-- Safe to run on every startup alongside init.sql.
--
-- NOTE: executions uses ReplacingMergeTree which requires deduplicate_merge_projection_mode='rebuild'
-- to support projections (ClickHouse 24.x). The ADD PROJECTION and MATERIALIZE statements for
-- executions are treated as best-effort by the schema initializer (non-fatal on failure).
-- logs and agent_metrics use plain MergeTree and always succeed.
--
-- MATERIALIZE statements are also wrapped as non-fatal to handle empty tables in fresh deployments.
-- Plain MergeTree tables: always succeed
ALTER TABLE logs
ADD PROJECTION IF NOT EXISTS alerting_app_level
(SELECT * ORDER BY (tenant_id, environment, application, level, timestamp));
ALTER TABLE agent_metrics
ADD PROJECTION IF NOT EXISTS alerting_instance_metric
(SELECT * ORDER BY (tenant_id, environment, instance_id, metric_name, collected_at));
-- ReplacingMergeTree tables: best-effort (requires deduplicate_merge_projection_mode='rebuild')
ALTER TABLE executions
ADD PROJECTION IF NOT EXISTS alerting_app_status
(SELECT * ORDER BY (tenant_id, environment, application_id, status, start_time));
ALTER TABLE executions
ADD PROJECTION IF NOT EXISTS alerting_route_status
(SELECT * ORDER BY (tenant_id, environment, route_id, status, start_time));
-- MATERIALIZE: best-effort on all tables (non-fatal if table is empty or already running)
ALTER TABLE logs MATERIALIZE PROJECTION alerting_app_level;
ALTER TABLE agent_metrics MATERIALIZE PROJECTION alerting_instance_metric;
ALTER TABLE executions MATERIALIZE PROJECTION alerting_app_status;
ALTER TABLE executions MATERIALIZE PROJECTION alerting_route_status;

View File

@@ -0,0 +1,50 @@
package com.cameleer.server.app.search;
import com.cameleer.server.app.ClickHouseTestHelper;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.testcontainers.clickhouse.ClickHouseContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@Testcontainers
class AlertingProjectionsIT {
@Container
static final ClickHouseContainer clickhouse =
new ClickHouseContainer("clickhouse/clickhouse-server:24.12");
private JdbcTemplate jdbc;
@BeforeEach
void setUp() throws Exception {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl(clickhouse.getJdbcUrl());
ds.setUsername(clickhouse.getUsername());
ds.setPassword(clickhouse.getPassword());
jdbc = new JdbcTemplate(ds);
ClickHouseTestHelper.executeInitSqlWithProjections(jdbc);
}
@Test
void mergeTreeProjectionsExistAfterInit() {
// logs and agent_metrics are plain MergeTree — projections always succeed.
// executions is ReplacingMergeTree; its projections require the session setting
// deduplicate_merge_projection_mode='rebuild' which is unavailable via JDBC pool,
// so they are best-effort and not asserted here.
List<String> names = jdbc.queryForList(
"SELECT name FROM system.projections WHERE table IN ('logs', 'agent_metrics')",
String.class);
assertThat(names).contains(
"alerting_app_level",
"alerting_instance_metric");
}
}