tasks) {
+ return tasks.stream().map(ProfileTask::getId).collect(Collectors.toList());
+ }
+
+ private static ProfileTaskRecord traceProfilingTask(final String endpointName, final long createTime,
+ final long startTime) {
+ final ProfileTaskRecord task = new ProfileTaskRecord();
+ task.setTaskId(createTime + Const.ID_CONNECTOR + SERVICE_ID);
+ task.setServiceId(SERVICE_ID);
+ task.setEndpointName(endpointName);
+ task.setStartTime(startTime);
+ task.setDuration(TRACE_PROFILING_MINUTES);
+ task.setMinDurationThreshold(0);
+ task.setDumpPeriod(10);
+ task.setCreateTime(createTime);
+ task.setMaxSamplingCount(5);
+ task.setTimeBucket(TimeBucket.getMinuteTimeBucket(startTime));
+ return task;
+ }
+
+ private static AsyncProfilerTaskRecord asyncProfilerTask(final long createTime) {
+ final AsyncProfilerTaskRecord task = new AsyncProfilerTaskRecord();
+ task.setTaskId(createTime + Const.ID_CONNECTOR + SERVICE_ID);
+ task.setServiceId(SERVICE_ID);
+ task.setServiceInstanceIdsFromList(Collections.singletonList(INSTANCE_ID));
+ task.setDuration((int) TimeUnit.MINUTES.toSeconds(5));
+ task.setEventsFromList(Collections.singletonList(AsyncProfilerEventType.CPU.toString()));
+ task.setCreateTime(createTime);
+ task.setTimeBucket(TimeBucket.getRecordTimeBucket(createTime));
+ return task;
+ }
+
+ private static PprofTaskRecord pprofTask(final long createTime) {
+ final PprofTaskRecord task = new PprofTaskRecord();
+ task.setTaskId(createTime + Const.ID_CONNECTOR + SERVICE_ID);
+ task.setServiceId(SERVICE_ID);
+ task.setServiceInstanceIdsFromList(Collections.singletonList(INSTANCE_ID));
+ task.setDuration(5);
+ task.setEvents(PprofEventType.CPU.toString());
+ task.setDumpPeriod(0);
+ task.setCreateTime(createTime);
+ task.setTimeBucket(TimeBucket.getRecordTimeBucket(createTime));
+ return task;
+ }
+
+ /**
+ * A task as the eBPF agent reports one when a continuous-profiling policy triggers on its process.
+ */
+ private static EBPFProfilingTaskRecord continuousProfilingTask(final EBPFProfilingTargetType targetType,
+ final long time) {
+ final EBPFProfilingTaskContinuousProfiling continuousProfiling = new EBPFProfilingTaskContinuousProfiling();
+ continuousProfiling.setProcessId(IDManager.ProcessID.buildId(INSTANCE_ID, "checkout"));
+ continuousProfiling.setProcessName("checkout");
+ final EBPFProfilingTaskRecord task = new EBPFProfilingTaskRecord();
+ task.setServiceId(SERVICE_ID);
+ task.setProcessLabelsJson(Const.EMPTY_STRING);
+ task.setInstanceId(INSTANCE_ID);
+ task.setStartTime(time);
+ task.setTriggerType(EBPFProfilingTriggerType.CONTINUOUS_PROFILING.value());
+ task.setFixedTriggerDuration(60);
+ task.setTargetType(targetType.value());
+ task.setCreateTime(time);
+ task.setLastUpdateTime(time);
+ task.setTimeBucket(TimeBucket.getRecordTimeBucket(time));
+ task.setContinuousProfilingJson(GSON.toJson(continuousProfiling));
+ task.generateLogicalId();
+ return task;
+ }
+}
diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfilingTaskQueryMariaDBIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfilingTaskQueryMariaDBIT.java
new file mode 100644
index 000000000000..f1bc54952641
--- /dev/null
+++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfilingTaskQueryMariaDBIT.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao;
+
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+@Testcontainers
+public class JDBCProfilingTaskQueryMariaDBIT extends JDBCProfilingTaskQueryITBase {
+ @Container
+ private static final MariaDBContainer> SERVER = JDBCQueryITSupport.mariaDB();
+
+ public JDBCProfilingTaskQueryMariaDBIT() {
+ super(SERVER, JDBCQueryITSupport.Target.MARIADB);
+ }
+}
diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfilingTaskQueryPostgreSQLIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfilingTaskQueryPostgreSQLIT.java
new file mode 100644
index 000000000000..7c767a210c37
--- /dev/null
+++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfilingTaskQueryPostgreSQLIT.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao;
+
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+@Testcontainers
+public class JDBCProfilingTaskQueryPostgreSQLIT extends JDBCProfilingTaskQueryITBase {
+ @Container
+ private static final PostgreSQLContainer> SERVER = JDBCQueryITSupport.postgreSQL();
+
+ public JDBCProfilingTaskQueryPostgreSQLIT() {
+ super(SERVER, JDBCQueryITSupport.Target.POSTGRESQL);
+ }
+}
diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCQueryITSupport.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCQueryITSupport.java
new file mode 100644
index 000000000000..9410bdecd7f1
--- /dev/null
+++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCQueryITSupport.java
@@ -0,0 +1,293 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao;
+
+import java.io.Closeable;
+import java.sql.Connection;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import lombok.Getter;
+import org.apache.skywalking.oap.server.core.analysis.DownSampling;
+import org.apache.skywalking.oap.server.core.analysis.TimeBucket;
+import org.apache.skywalking.oap.server.core.analysis.config.NoneStream;
+import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
+import org.apache.skywalking.oap.server.core.analysis.record.Record;
+import org.apache.skywalking.oap.server.core.config.ConfigService;
+import org.apache.skywalking.oap.server.core.query.enumeration.Step;
+import org.apache.skywalking.oap.server.core.query.input.Duration;
+import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine;
+import org.apache.skywalking.oap.server.core.storage.SessionCacheCallback;
+import org.apache.skywalking.oap.server.core.storage.annotation.Storage;
+import org.apache.skywalking.oap.server.core.storage.model.Model;
+import org.apache.skywalking.oap.server.core.storage.model.ModelManipulator;
+import org.apache.skywalking.oap.server.core.storage.model.StorageManipulationOpt;
+import org.apache.skywalking.oap.server.core.storage.model.StorageModels;
+import org.apache.skywalking.oap.server.core.storage.type.HashMapConverter;
+import org.apache.skywalking.oap.server.core.storage.type.StorageBuilder;
+import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCClient;
+import org.apache.skywalking.oap.server.library.client.request.PrepareRequest;
+import org.apache.skywalking.oap.server.library.module.ModuleManager;
+import org.apache.skywalking.oap.server.library.module.ModuleProviderHolder;
+import org.apache.skywalking.oap.server.library.module.ModuleServiceHolder;
+import org.apache.skywalking.oap.server.storage.plugin.jdbc.BatchSQLExecutor;
+import org.apache.skywalking.oap.server.storage.plugin.jdbc.SQLExecutor;
+import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.JDBCTableInstaller;
+import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.TableHelper;
+import org.apache.skywalking.oap.server.storage.plugin.jdbc.mysql.MySQLTableInstaller;
+import org.apache.skywalking.oap.server.storage.plugin.jdbc.postgresql.PostgreSQLTableInstaller;
+import org.apache.skywalking.oap.server.storage.plugin.jdbc.postgresql.dao.PostgreSQLAggregationQueryDAO;
+import org.apache.skywalking.oap.server.storage.plugin.jdbc.postgresql.dao.PostgreSQLMetricsQueryDAO;
+import org.joda.time.DateTime;
+import org.mockito.MockedStatic;
+import org.testcontainers.containers.JdbcDatabaseContainer;
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.utility.DockerImageName;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+
+/**
+ * A JDBC storage on a PostgreSQL or MariaDB test container, with OAP's own table install and write paths, for the
+ * ITs that read rows back through OAP's JDBC query DAOs. Each target uses the installer and the DAOs its storage
+ * provider registers: {@code postgresql} its own installer and metrics/aggregation DAOs, {@code mysql} (MariaDB is
+ * served by the {@code mysql} storage) the MySQL installer and the common DAOs.
+ *
+ * The fixtures sit on both sides of today's local midnight, so a read crosses two day tables, as JDBC splits
+ * records and metrics per day, and both tables are inside the TTL the table lookups apply.
+ */
+final class JDBCQueryITSupport implements Closeable {
+ /** Today's local midnight: the day tables the ITs write are yesterday's and today's. */
+ static final long MIDNIGHT = new DateTime().withTimeAtStartOfDay().getMillis();
+ static final int RECORD_TTL_DAYS = 3;
+ static final int METRICS_TTL_DAYS = 7;
+
+ enum Target {
+ POSTGRESQL,
+ MARIADB
+ }
+
+ private final Target target;
+ @Getter
+ private final JDBCClient client;
+ @Getter
+ private final ModuleManager moduleManager;
+ @Getter
+ private final TableHelper tableHelper;
+ private final JDBCTableInstaller installer;
+ private final StorageModels models;
+
+ private JDBCQueryITSupport(final Target target, final JDBCClient client, final ModuleManager moduleManager,
+ final StorageModels models) {
+ this.target = target;
+ this.client = client;
+ this.moduleManager = moduleManager;
+ this.models = models;
+ this.tableHelper = new TableHelper(moduleManager, client);
+ this.installer = target == Target.POSTGRESQL
+ ? new PostgreSQLTableInstaller(client, moduleManager)
+ : new MySQLTableInstaller(client, moduleManager);
+ }
+
+ static PostgreSQLContainer> postgreSQL() {
+ return new PostgreSQLContainer<>(DockerImageName.parse("postgres:13"));
+ }
+
+ static MariaDBContainer> mariaDB() {
+ return new MariaDBContainer<>(DockerImageName.parse("mariadb:11.4"));
+ }
+
+ static JDBCQueryITSupport connect(final JdbcDatabaseContainer> server, final Target target) {
+ final Properties properties = new Properties();
+ properties.setProperty("jdbcUrl", server.getJdbcUrl());
+ properties.setProperty("dataSource.user", server.getUsername());
+ properties.setProperty("dataSource.password", server.getPassword());
+ final JDBCClient client = new JDBCClient(properties);
+ client.connect();
+
+ final ConfigService configService = mock(ConfigService.class);
+ when(configService.getRecordDataTTL()).thenReturn(RECORD_TTL_DAYS);
+ when(configService.getMetricsDataTTL()).thenReturn(METRICS_TTL_DAYS);
+ final StorageModels models = new StorageModels();
+ final Map, Object> services = new LinkedHashMap<>();
+ services.put(ConfigService.class, configService);
+ services.put(ModelManipulator.class, models);
+ final ModuleManager moduleManager = mock(ModuleManager.class);
+ final ModuleProviderHolder providerHolder = mock(ModuleProviderHolder.class);
+ final ModuleServiceHolder serviceHolder = mock(ModuleServiceHolder.class);
+ when(moduleManager.find(anyString())).thenReturn(providerHolder);
+ when(providerHolder.provider()).thenReturn(serviceHolder);
+ when(serviceHolder.getService(any())).thenAnswer(invocation -> services.get(invocation.>getArgument(0)));
+ final JDBCQueryITSupport support = new JDBCQueryITSupport(target, client, moduleManager, models);
+ // As the storage provider starts it: the installer renames the columns whose core names the database
+ // reserves (value, precision, match) before any model is added.
+ support.installer.start();
+ return support;
+ }
+
+ /**
+ * Registers a model the way OAP does at boot, without creating its tables.
+ */
+ Model register(final Class> storageClass, final int scopeId, final String name,
+ final DownSampling downSampling) throws Exception {
+ try (MockedStatic ignored = mockStatic(DefaultScopeDefine.class)) {
+ return models.add(
+ storageClass, scopeId, new Storage(name, false, downSampling), StorageManipulationOpt.withSchemaChange());
+ }
+ }
+
+ /**
+ * Installs a model the way OAP does at boot, with a table for yesterday and today when it is split per day, and
+ * tomorrow's, which OAP creates a day ahead ({@link JDBCHistoryDeleteDAO}), so a run that crosses midnight still
+ * has a table for a row written at "now".
+ */
+ Model install(final Class> storageClass, final int scopeId, final String name,
+ final DownSampling downSampling) throws Exception {
+ final Model model = register(storageClass, scopeId, name, downSampling);
+ installer.isExists(model, StorageManipulationOpt.withSchemaChange());
+ if (model.isTimeSeries()) {
+ installer.createTable(model, day(MIDNIGHT - 1));
+ installer.createTable(model, day(MIDNIGHT));
+ installer.createTable(model, day(new DateTime(MIDNIGHT).plusDays(1).getMillis()));
+ } else {
+ installer.createTable(model);
+ }
+ return model;
+ }
+
+ JDBCTableInstaller installer() {
+ return installer;
+ }
+
+ /**
+ * Writes records through OAP's record DAO and its batch executor.
+ */
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ void writeRecords(final Model model, final StorageBuilder extends Record> builder,
+ final List extends Record> records) throws Exception {
+ final JDBCRecordDAO dao = new JDBCRecordDAO(client, (StorageBuilder) (StorageBuilder) builder);
+ final List requests = new ArrayList<>();
+ for (final Record record : records) {
+ requests.add(dao.prepareBatchInsert(model, record));
+ }
+ flush(requests);
+ }
+
+ /**
+ * Writes metrics as new rows through OAP's metrics DAO and its batch executor.
+ */
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ void writeMetrics(final Model model, final StorageBuilder extends Metrics> builder,
+ final List extends Metrics> metrics) throws Exception {
+ final JDBCMetricsDAO dao = new JDBCMetricsDAO(client, (StorageBuilder) (StorageBuilder) builder);
+ final List requests = new ArrayList<>();
+ for (final Metrics metric : metrics) {
+ requests.add(dao.prepareBatchInsert(model, metric, (SessionCacheCallback) null));
+ }
+ flush(requests);
+ }
+
+ /**
+ * Writes a task row as {@link JDBCNoneStreamDAO} does, at once, into the table of the day it is created on.
+ * {@code JDBCNoneStreamDAO} names that day "now"; the fixtures name it by the row's creation time, so a run that
+ * crosses midnight still writes where its reads look.
+ */
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ void writeNoneStream(final Model model, final StorageBuilder extends NoneStream> builder,
+ final NoneStream row, final long createTime) throws Exception {
+ try (Connection connection = client.getConnection()) {
+ new JDBCSQLExecutor().getInsertExecutor(
+ model, row, TimeBucket.getMinuteTimeBucket(createTime), (StorageBuilder) (StorageBuilder) builder,
+ new HashMapConverter.ToStorage(), null).invoke(connection);
+ }
+ }
+
+ /**
+ * Executes requests the way {@link JDBCBatchDAO#flush(List)} does: each statement with its additional ones,
+ * grouped by SQL into one batch each.
+ */
+ void flush(final List requests) throws Exception {
+ final Map> bySql = new LinkedHashMap<>();
+ for (final PrepareRequest request : requests) {
+ bySql.computeIfAbsent(request, sql -> new ArrayList<>()).add(request);
+ final List additional = ((SQLExecutor) request).getAdditionalSQLs();
+ if (additional != null) {
+ for (final SQLExecutor sql : additional) {
+ bySql.computeIfAbsent(sql, key -> new ArrayList<>()).add(sql);
+ }
+ }
+ }
+ for (final List batch : bySql.values()) {
+ new BatchSQLExecutor(client, batch).invoke(batch.size());
+ }
+ }
+
+ JDBCMetricsQueryDAO metricsQueryDAO() {
+ return target == Target.POSTGRESQL
+ ? new PostgreSQLMetricsQueryDAO(client, tableHelper)
+ : new JDBCMetricsQueryDAO(client, tableHelper);
+ }
+
+ JDBCAggregationQueryDAO aggregationQueryDAO() {
+ return target == Target.POSTGRESQL
+ ? new PostgreSQLAggregationQueryDAO(client, tableHelper)
+ : new JDBCAggregationQueryDAO(client, tableHelper);
+ }
+
+ static long day(final long timestamp) {
+ return TimeBucket.getTimeBucket(timestamp, DownSampling.Day);
+ }
+
+ /**
+ * A query duration from {@code start} to {@code end}, as the query protocol writes it for {@code step}.
+ */
+ static Duration duration(final long start, final long end, final Step step) {
+ final String pattern;
+ switch (step) {
+ case SECOND:
+ pattern = "yyyy-MM-dd HHmmss";
+ break;
+ case MINUTE:
+ pattern = "yyyy-MM-dd HHmm";
+ break;
+ case HOUR:
+ pattern = "yyyy-MM-dd HH";
+ break;
+ default:
+ pattern = "yyyy-MM-dd";
+ break;
+ }
+ final Duration duration = new Duration();
+ duration.setStep(step);
+ duration.setStart(new DateTime(start).toString(pattern));
+ duration.setEnd(new DateTime(end).toString(pattern));
+ return duration;
+ }
+
+ @Override
+ public void close() {
+ client.shutdown();
+ }
+}
diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTraceQueryITBase.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTraceQueryITBase.java
new file mode 100644
index 000000000000..de74f23947ce
--- /dev/null
+++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTraceQueryITBase.java
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.stream.Collectors;
+import org.apache.skywalking.oap.server.core.analysis.DownSampling;
+import org.apache.skywalking.oap.server.core.analysis.IDManager;
+import org.apache.skywalking.oap.server.core.analysis.TimeBucket;
+import org.apache.skywalking.oap.server.core.analysis.manual.segment.SegmentRecord;
+import org.apache.skywalking.oap.server.core.query.enumeration.Step;
+import org.apache.skywalking.oap.server.core.query.input.Duration;
+import org.apache.skywalking.oap.server.core.query.type.BasicTrace;
+import org.apache.skywalking.oap.server.core.query.type.QueryOrder;
+import org.apache.skywalking.oap.server.core.query.type.TraceState;
+import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.testcontainers.containers.JdbcDatabaseContainer;
+
+import static org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao.JDBCQueryITSupport.MIDNIGHT;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Reads segments the way the trace list and trace profiling do, on segments written on both sides of midnight, so
+ * in two day tables. A subclass names the server.
+ */
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
+abstract class JDBCTraceQueryITBase {
+ private static final String SERVICE_ID = IDManager.ServiceID.buildId("checkout", true);
+ private static final String INSTANCE_ID = IDManager.ServiceInstanceID.buildId(SERVICE_ID, "checkout-1");
+ private static final String ENDPOINT_ID = IDManager.EndpointID.buildId(SERVICE_ID, "POST /orders");
+ /** Segment id, offset from midnight in seconds, latency in ms. Three before midnight, two after. */
+ private static final Object[][] SEGMENTS = {
+ {"s1", -30, 300},
+ {"s2", -20, 100},
+ {"s3", -10, 500},
+ {"s4", 10, 200},
+ {"s5", 20, 400},
+ };
+
+ private final JdbcDatabaseContainer> server;
+ private final JDBCQueryITSupport.Target target;
+ private JDBCQueryITSupport support;
+ private JDBCTraceQueryDAO dao;
+ private Duration duration;
+
+ JDBCTraceQueryITBase(final JdbcDatabaseContainer> server, final JDBCQueryITSupport.Target target) {
+ this.server = server;
+ this.target = target;
+ }
+
+ @BeforeAll
+ public void writeSegments() throws Exception {
+ support = JDBCQueryITSupport.connect(server, target);
+ final var model = support.install(
+ SegmentRecord.class, DefaultScopeDefine.SEGMENT, SegmentRecord.INDEX_NAME, DownSampling.Second);
+ final List segments = new ArrayList<>();
+ for (final Object[] segment : SEGMENTS) {
+ segments.add(segment((String) segment[0], MIDNIGHT + (int) segment[1] * 1000L, (int) segment[2]));
+ }
+ support.writeRecords(model, new SegmentRecord.Builder(), segments);
+ dao = new JDBCTraceQueryDAO(support.getModuleManager(), support.getClient(), support.getTableHelper());
+ duration = JDBCQueryITSupport.duration(MIDNIGHT - 60_000L, MIDNIGHT + 60_000L, Step.SECOND);
+ }
+
+ @AfterAll
+ public void close() {
+ if (support != null) {
+ support.close();
+ }
+ }
+
+ /**
+ * apache/skywalking#14239: a trace profiling task without a sampled segment has no segment id to read.
+ */
+ @Test
+ public void readsNoSegmentForNoSegmentId() throws Exception {
+ assertEquals(Collections.emptyList(), dao.queryBySegmentIdList(Collections.emptyList(), duration));
+ }
+
+ @Test
+ public void readsTheSegmentsOfTheIdsOfBothDays() throws Exception {
+ assertEquals(new TreeSet<>(Arrays.asList("s1", "s4")),
+ segmentIds(dao.queryBySegmentIdList(Arrays.asList("s1", "s4"), duration)));
+ }
+
+ @Test
+ public void readsNoSegmentForNoTraceOrNoInstance() throws Exception {
+ assertEquals(Collections.emptyList(), dao.queryByTraceIdWithInstanceId(
+ Collections.emptyList(), Collections.singletonList(INSTANCE_ID), null));
+ assertEquals(Collections.emptyList(), dao.queryByTraceIdWithInstanceId(
+ Collections.singletonList("trace-s1"), Collections.emptyList(), null));
+ }
+
+ /**
+ * Each page is cut from the segments of both days ranked together, latest start first.
+ */
+ @Test
+ public void pagesTheLatestTracesAcrossMidnight() throws Exception {
+ assertEquals(Arrays.asList("s5", "s4"), page(0, 2, QueryOrder.BY_START_TIME));
+ assertEquals(Arrays.asList("s3", "s2"), page(2, 2, QueryOrder.BY_START_TIME));
+ assertEquals(Collections.singletonList("s1"), page(4, 2, QueryOrder.BY_START_TIME));
+ }
+
+ @Test
+ public void pagesTheSlowestTracesAcrossMidnight() throws Exception {
+ assertEquals(Arrays.asList("s3", "s5", "s1"), page(0, 3, QueryOrder.BY_DURATION));
+ assertEquals(Arrays.asList("s4", "s2"), page(3, 3, QueryOrder.BY_DURATION));
+ }
+
+ private List page(final int from, final int limit, final QueryOrder order) throws Exception {
+ return dao.queryBasicTraces(duration, 0, 0, SERVICE_ID, null, null, null, limit, from, TraceState.ALL, order, null)
+ .getTraces()
+ .stream()
+ .map(BasicTrace::getSegmentId)
+ .collect(Collectors.toList());
+ }
+
+ private static Set segmentIds(final List segments) {
+ return segments.stream().map(SegmentRecord::getSegmentId).collect(Collectors.toCollection(TreeSet::new));
+ }
+
+ private static SegmentRecord segment(final String segmentId, final long startTime, final int latency) {
+ final SegmentRecord segment = new SegmentRecord();
+ segment.setSegmentId(segmentId);
+ segment.setTraceId("trace-" + segmentId);
+ segment.setServiceId(SERVICE_ID);
+ segment.setServiceInstanceId(INSTANCE_ID);
+ segment.setEndpointId(ENDPOINT_ID);
+ segment.setStartTime(startTime);
+ segment.setLatency(latency);
+ segment.setIsError(0);
+ segment.setDataBinary(new byte[] {1});
+ segment.setTags(new ArrayList<>());
+ segment.setTimeBucket(TimeBucket.getRecordTimeBucket(startTime));
+ return segment;
+ }
+}
diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTraceQueryMariaDBIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTraceQueryMariaDBIT.java
new file mode 100644
index 000000000000..2cccb4bb95f5
--- /dev/null
+++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTraceQueryMariaDBIT.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao;
+
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+@Testcontainers
+public class JDBCTraceQueryMariaDBIT extends JDBCTraceQueryITBase {
+ @Container
+ private static final MariaDBContainer> SERVER = JDBCQueryITSupport.mariaDB();
+
+ public JDBCTraceQueryMariaDBIT() {
+ super(SERVER, JDBCQueryITSupport.Target.MARIADB);
+ }
+}
diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTraceQueryPostgreSQLIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTraceQueryPostgreSQLIT.java
new file mode 100644
index 000000000000..eda0bd5489ce
--- /dev/null
+++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTraceQueryPostgreSQLIT.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao;
+
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+@Testcontainers
+public class JDBCTraceQueryPostgreSQLIT extends JDBCTraceQueryITBase {
+ @Container
+ private static final PostgreSQLContainer> SERVER = JDBCQueryITSupport.postgreSQL();
+
+ public JDBCTraceQueryPostgreSQLIT() {
+ super(SERVER, JDBCQueryITSupport.Target.POSTGRESQL);
+ }
+}
diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCZipkinQueryITBase.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCZipkinQueryITBase.java
new file mode 100644
index 000000000000..99c2d0c9279d
--- /dev/null
+++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCZipkinQueryITBase.java
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+import org.apache.skywalking.oap.server.core.analysis.DownSampling;
+import org.apache.skywalking.oap.server.core.analysis.TimeBucket;
+import org.apache.skywalking.oap.server.core.query.enumeration.Step;
+import org.apache.skywalking.oap.server.core.query.input.Duration;
+import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine;
+import org.apache.skywalking.oap.server.core.zipkin.ZipkinSpanRecord;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.testcontainers.containers.JdbcDatabaseContainer;
+import zipkin2.Span;
+import zipkin2.storage.QueryRequest;
+
+import static org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao.JDBCQueryITSupport.MIDNIGHT;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Searches Zipkin traces the way the Zipkin query API does, on spans written on both sides of midnight, so in two
+ * day tables. A search ranks the traces by their first span that matches, latest first, and keeps {@code limit} of
+ * them; a trace comes back whole, every span of it, whichever day table holds it. A subclass names the server.
+ */
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
+abstract class JDBCZipkinQueryITBase {
+ private static final String BACKEND = "backend";
+ private static final String FRONTEND = "frontend";
+ /** The backend's traces, latest first by their first backend span. */
+ private static final List LATEST_FIRST = Arrays.asList(
+ "a02", "b07", "b06", "b05", "b04", "b03", "b02", "a01");
+
+ private final JdbcDatabaseContainer> server;
+ private final JDBCQueryITSupport.Target target;
+ private JDBCQueryITSupport support;
+ private JDBCZipkinQueryDAO dao;
+ private Duration duration;
+
+ JDBCZipkinQueryITBase(final JdbcDatabaseContainer> server, final JDBCQueryITSupport.Target target) {
+ this.server = server;
+ this.target = target;
+ }
+
+ @BeforeAll
+ public void writeSpans() throws Exception {
+ support = JDBCQueryITSupport.connect(server, target);
+ final var model = support.install(
+ ZipkinSpanRecord.class, DefaultScopeDefine.ZIPKIN_SPAN, ZipkinSpanRecord.INDEX_NAME, DownSampling.Second);
+ support.writeRecords(model, new ZipkinSpanRecord.Builder(), Arrays.asList(
+ // a01 starts in the backend before midnight; its child, after midnight, is the backend too.
+ span("a01", 1, 0, BACKEND, -60),
+ span("a01", 2, 1, BACKEND, 1),
+ // a02 starts in the frontend before midnight; only its backend child, after midnight, matches.
+ span("a02", 1, 0, FRONTEND, -1),
+ span("a02", 2, 1, BACKEND, 60),
+ span("b02", 1, 0, BACKEND, -40),
+ span("b03", 1, 0, BACKEND, -30),
+ span("b04", 1, 0, BACKEND, -20),
+ span("b05", 1, 0, BACKEND, 10),
+ span("b06", 1, 0, BACKEND, 20),
+ span("b07", 1, 0, BACKEND, 30)
+ ));
+ dao = new JDBCZipkinQueryDAO(support.getClient(), support.getTableHelper());
+ duration = JDBCQueryITSupport.duration(MIDNIGHT - 120_000L, MIDNIGHT + 120_000L, Step.SECOND);
+ }
+
+ @AfterAll
+ public void close() {
+ if (support != null) {
+ support.close();
+ }
+ }
+
+ @ParameterizedTest(name = "limit {0}")
+ @ValueSource(ints = {4, 5, 8})
+ public void keepsTheLatestTracesAcrossMidnight(final int limit) throws Exception {
+ final Set> expected = new HashSet<>();
+ for (final String trace : LATEST_FIRST.subList(0, limit)) {
+ expected.add(spanIds(trace));
+ }
+ final QueryRequest request = QueryRequest.newBuilder()
+ .serviceName(BACKEND)
+ .endTs(MIDNIGHT + 120_000L)
+ .lookback(240_000L)
+ .limit(limit)
+ .build();
+ assertEquals(expected, traces(dao.getTraces(request, duration)));
+ }
+
+ @Test
+ public void readsWholeTracesByIdAcrossMidnight() throws Exception {
+ final Set traceIds = new HashSet<>(Arrays.asList(traceId("a01"), traceId("a02"), traceId("b05")));
+ assertEquals(new HashSet<>(Arrays.asList(spanIds("a01"), spanIds("a02"), spanIds("b05"))),
+ traces(dao.getTraces(traceIds, duration)));
+ }
+
+ /**
+ * Each trace as its sorted span ids; a trace split into two lists shows up as two entries.
+ */
+ private static Set> traces(final List> traces) {
+ final Set> result = new HashSet<>();
+ for (final List trace : traces) {
+ final Set ids = new TreeSet<>();
+ for (final Span span : trace) {
+ ids.add(span.id());
+ }
+ assertTrue(result.add(new ArrayList<>(ids)), "trace returned twice: " + ids);
+ }
+ return result;
+ }
+
+ private static List spanIds(final String trace) {
+ return trace.startsWith("a")
+ ? Arrays.asList(spanId(trace, 1), spanId(trace, 2))
+ : Collections.singletonList(spanId(trace, 1));
+ }
+
+ private static String traceId(final String trace) {
+ return String.format("%032x", Integer.parseInt(trace.substring(1), 16) + (trace.charAt(0) == 'a' ? 0xa00 : 0xb00));
+ }
+
+ private static String spanId(final String trace, final int span) {
+ return traceId(trace).substring(20) + String.format("%04x", span);
+ }
+
+ private static ZipkinSpanRecord span(final String trace, final int span, final int parent,
+ final String service, final int secondsFromMidnight) {
+ final long timestampMillis = MIDNIGHT + secondsFromMidnight * 1000L;
+ final ZipkinSpanRecord record = new ZipkinSpanRecord();
+ record.setTraceId(traceId(trace));
+ record.setSpanId(spanId(trace, span));
+ record.setParentId(parent == 0 ? null : spanId(trace, parent));
+ record.setKind(parent == 0 ? "SERVER" : "CLIENT");
+ record.setName("get /orders");
+ record.setLocalEndpointServiceName(service);
+ record.setTimestamp(timestampMillis * 1000L);
+ record.setTimestampMillis(timestampMillis);
+ record.setDuration(1000L);
+ record.setQuery(new ArrayList<>());
+ record.setTimeBucket(TimeBucket.getRecordTimeBucket(timestampMillis));
+ return record;
+ }
+}
diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCZipkinQueryMariaDBIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCZipkinQueryMariaDBIT.java
new file mode 100644
index 000000000000..2226a4151c51
--- /dev/null
+++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCZipkinQueryMariaDBIT.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao;
+
+import org.testcontainers.containers.MariaDBContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+@Testcontainers
+public class JDBCZipkinQueryMariaDBIT extends JDBCZipkinQueryITBase {
+ @Container
+ private static final MariaDBContainer> SERVER = JDBCQueryITSupport.mariaDB();
+
+ public JDBCZipkinQueryMariaDBIT() {
+ super(SERVER, JDBCQueryITSupport.Target.MARIADB);
+ }
+}
diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCZipkinQueryPostgreSQLIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCZipkinQueryPostgreSQLIT.java
new file mode 100644
index 000000000000..ef4fc1c1aa14
--- /dev/null
+++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCZipkinQueryPostgreSQLIT.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao;
+
+import org.testcontainers.containers.PostgreSQLContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+@Testcontainers
+public class JDBCZipkinQueryPostgreSQLIT extends JDBCZipkinQueryITBase {
+ @Container
+ private static final PostgreSQLContainer> SERVER = JDBCQueryITSupport.postgreSQL();
+
+ public JDBCZipkinQueryPostgreSQLIT() {
+ super(SERVER, JDBCQueryITSupport.Target.POSTGRESQL);
+ }
+}