diff --git a/.claude/skills/test/SKILL.md b/.claude/skills/test/SKILL.md index cca8166bfcf3..e29b55972ae8 100644 --- a/.claude/skills/test/SKILL.md +++ b/.claude/skills/test/SKILL.md @@ -30,13 +30,19 @@ CI runs this on JDK 11, 17, 21, 25 (ubuntu only). ### `slow` — slow integration tests +The slow matrix has two entries, each building the whole project once: + ```bash +# ElasticSearch / OpenSearch: the Elasticsearch client against Elasticsearch 7, 8 and 9, and OpenSearch 1 and 2 ./mvnw -B clean integration-test -Dcheckstyle.skip -DskipUTs=true \ -Dit.test=org.apache.skywalking.library.elasticsearch.ElasticSearchIT \ -Dfailsafe.failIfNoSpecifiedTests=false + +# Elastic IT: the storage-elasticsearch-plugin ITs tagged @Tag("elastic-it") (Zipkin query, span-attached events) +./mvnw -B clean integration-test -Dcheckstyle.skip -DskipUTs=true -Dgroups=elastic-it ``` -CI runs on JDK 11 (ubuntu only). Currently only ElasticSearch/OpenSearch IT is in the slow matrix. +CI runs on JDK 11 (ubuntu only). ### Module name — single module tests @@ -64,7 +70,7 @@ CI runs on JDK 11 (ubuntu only). Currently only ElasticSearch/OpenSearch IT is i ## Slow test tagging -Tests tagged with `@Tag("slow")` are excluded from the normal integration-test job and run separately in the slow-integration-test job. Use this tag for tests that spin up external services (Elasticsearch, etc.) and take significant time. +Tests tagged with `@Tag("slow")` are excluded from the normal integration-test job and run separately in the slow-integration-test job. Use this tag for tests that spin up external services (Elasticsearch, etc.) and take significant time. A slow IT must also be selected by a slow-matrix entry (by class, or by a second tag such as `@Tag("elastic-it")`), or it never runs in CI. ## CI retry behavior diff --git a/.github/workflows/skywalking.yaml b/.github/workflows/skywalking.yaml index 4777055fc562..0644e008dca2 100644 --- a/.github/workflows/skywalking.yaml +++ b/.github/workflows/skywalking.yaml @@ -260,6 +260,13 @@ jobs: - name: Unit test run: ./mvnw clean test -q -B -D"checkstyle.skip" || ./mvnw clean test -q -B -D"checkstyle.skip" + # Every `*IT` not tagged `@Tag("slow")`, on each supported JDK. Each IT starts its own server in Docker + # (Testcontainers): + # - BanyanDB storage: storage-banyandb-plugin's `BanyanDB*IT`, OAP's BanyanDB DAOs on a BanyanDB container. + # - JDBC storage: storage-jdbc-hikaricp-plugin's `JDBC*PostgreSQLIT` and `JDBC*MariaDBIT`, OAP's JDBC DAOs on + # PostgreSQL and on MariaDB, and `JDBCHistoryDeleteDAOIT`. + # - Cluster coordinators and dynamic configuration: the ZooKeeper, etcd, Consul, Nacos and Apollo plugins' ITs. + # - library-client's Elasticsearch bulk processor IT. integration-test: if: | ( always() && ! cancelled() ) && @@ -294,7 +301,13 @@ jobs: ./mvnw -B clean integration-test -Dcheckstyle.skip -DskipUTs=true -DexcludedGroups=slow || \ ./mvnw -B clean integration-test -Dcheckstyle.skip -DskipUTs=true -DexcludedGroups=slow - # Slow tests + # Slow tests: the `@Tag("slow")` ITs, all on Elasticsearch and OpenSearch containers, on JDK 11. Each matrix entry + # builds the whole project once and runs its own ITs: + # - `ElasticSearchIT`, by class: library-elasticsearch-client against Elasticsearch 7, 8 and 9, and OpenSearch 1 + # and 2. + # - Elastic IT, by the `@Tag("elastic-it")` the storage-elasticsearch-plugin's ITs also carry: the Zipkin query DAO + # (`ZipkinQueryEsDAO*IT`) and the span-attached event queries (`SpanAttachedEventQuery*IT`), each on + # Elasticsearch 8, 9 and 9 sharded, and OpenSearch 2, 3 and 3 sharded. slow-integration-test: if: | ( always() && ! cancelled() ) && @@ -307,11 +320,9 @@ jobs: matrix: test: - name: ElasticSearch / OpenSearch - class: org.apache.skywalking.library.elasticsearch.ElasticSearchIT - - name: Zipkin query on ElasticSearch / OpenSearch - class: ZipkinQueryEsDAO*IT - - name: Span attached events on ElasticSearch / OpenSearch - class: SpanAttachedEventQuery*IT + select: -Dit.test=org.apache.skywalking.library.elasticsearch.ElasticSearchIT -Dfailsafe.failIfNoSpecifiedTests=false + - name: Elastic IT + select: -Dgroups=elastic-it steps: - uses: actions/checkout@v7 with: @@ -329,8 +340,8 @@ jobs: distribution: temurin - name: ${{ matrix.test.name }} run: | - ./mvnw -B clean integration-test -Dcheckstyle.skip -DskipUTs=true -Dit.test=${{ matrix.test.class }} -Dfailsafe.failIfNoSpecifiedTests=false || \ - ./mvnw -B clean integration-test -Dcheckstyle.skip -DskipUTs=true -Dit.test=${{ matrix.test.class }} -Dfailsafe.failIfNoSpecifiedTests=false + ./mvnw -B clean integration-test -Dcheckstyle.skip -DskipUTs=true ${{ matrix.test.select }} || \ + ./mvnw -B clean integration-test -Dcheckstyle.skip -DskipUTs=true ${{ matrix.test.select }} e2e-test: if: | diff --git a/docs/en/changes/changes.md b/docs/en/changes/changes.md index 5d8da3455d70..f58f1b5f00f1 100644 --- a/docs/en/changes/changes.md +++ b/docs/en/changes/changes.md @@ -46,6 +46,26 @@ * LAL: save the logs of the OpenTelemetry services that set `service.layer=OTLP_GENERAL` through the new `otlp-general` rule in `lal/default.yaml`; no rule matched that layer, so these logs were dropped. A LAL rule declaring an unregistered or missing layer (names are case-sensitive) is now skipped with a warning at boot and rejected when applied as a runtime rule, instead of loading silently under `UNDEFINED` and never running. * LAL: a log without a layer that no `layer: auto` rule claims runs the new `fallback: true` auto rules, instead of the rules declared for `GENERAL`, and is dropped when none claims it either, so where such a log is filed is decided by a rule, not hard-coded. The new `default-fallback` rule in `lal/default.yaml` files it under `GENERAL`, as before; edit its extractor to file it under another layer. A fallback rule must set the layer, as any auto rule. A custom `layer: GENERAL` rule written for the logs without a layer must declare `layer: auto` and `fallback: true` and set `layer "GENERAL"` in its extractor; it then runs next to `default-fallback`, as it ran next to `default` before. * LAL: handle the mini-program error logs with the `miniprogram-errors-wechat` and `miniprogram-errors-alipay` rules in `lal/miniprogram.yaml`, declared for the `WECHAT_MINI_PROGRAM` and `ALIPAY_MINI_PROGRAM` layers, replacing the `layer: auto` rule `miniprogram-errors`. The mini-program SDK v0.5.0 sets `service.layer`, which routes its error logs to these rules; it is now the minimum supported SDK version, since an older SDK's error logs carry no layer and are filed under `GENERAL` without the `error_count` metric. +* Fix bugs of the JDBC storage, `mysql` (MySQL and MariaDB) and `postgresql`. Many showed only when a query's time range crossed midnight, as this storage keeps records and metrics in one table per day: + - The trace list came out of order when its time range crossed midnight, and its pages skipped or repeated traces. + - A Zipkin trace search returned more traces than its limit, and a trace with spans on both days came back as two partial traces, in a search and in a lookup of several trace IDs (`traceMany`). + - A heatmap had extra, zeroed columns. + - A Top N list ranked an entity by the average of its daily averages and could miss the top entities, and it failed on values above 2147483647, such as memory in bytes. + - The slow statement list was not ranked across the days. + - The async-profiler and pprof task lists could leave out the newest tasks. + - A trace profiling task scheduled to start on a later day than it was created could fail to reach the agent, and a new task could be created over it. + - Trace profiling of a Go service read its thread snapshots as Java, and the profile analysis could read thread snapshots outside the selected time range. + - Querying the targets of a continuous profiling policy saved without targets failed with an SQL syntax error, and an eBPF profiling analysis with no schedule logged one. + - The continuous profiling instance list missed instances first seen on an earlier day. + - Rover could receive eBPF profiling tasks for processes that had stopped reporting. + - Tag autocomplete returned more keys or values than its limit. + - Querying browser error logs without a time range failed. + - Filtering events by layer failed on PostgreSQL, and an event at the first millisecond of a time range was left out of it. + - Disabling a UI template always failed with "Can't find the template", and changing a template that does not exist reported success. + - A metric whose row was gone from its table while OAP still cached it was not written again: its update found no row and OAP kept the cached copy. It is now dropped from the cache, so the next flush inserts the row again. + - The table check at boot logged wrong table and column states. +* Fix creating an async-profiler task for a service while another one runs on it: the check that refuses it never found the running task on the JDBC and Elasticsearch storage. +* Fix the async-profiler task OAP sends to an agent whose service has two tasks created within five minutes of each other: it could send the older one. #### UI diff --git a/oap-server-bom/pom.xml b/oap-server-bom/pom.xml index bbbf0381a960..654a0b2fc482 100644 --- a/oap-server-bom/pom.xml +++ b/oap-server-bom/pom.xml @@ -65,6 +65,7 @@ 1.11.0 1.12.0 42.7.14 + 3.5.1 0.6.1 1.17.6 1.34.2 @@ -557,6 +558,13 @@ postgresql ${postgresql.version} + + + org.mariadb.jdbc + mariadb-java-client + ${mariadb-java-client.version} + test + com.linecorp.armeria @@ -623,6 +631,12 @@ ${testcontainers.version} test + + org.testcontainers + mariadb + ${testcontainers.version} + test + org.testcontainers junit-jupiter diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/cache/AsyncProfilerTaskCache.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/cache/AsyncProfilerTaskCache.java index 119cb957623d..19651277085a 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/cache/AsyncProfilerTaskCache.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/cache/AsyncProfilerTaskCache.java @@ -69,12 +69,19 @@ public AsyncProfilerTask getAsyncProfilerTask(String serviceId) { return serviceId2taskCache.getIfPresent(serviceId); } + /** + * Caches the newest task of the service. A refresh lists every task created within five minutes of now, newest + * first, so a task replaces the cached one only when it was created no earlier. + */ public void saveTask(String serviceId, AsyncProfilerTask task) { if (task == null) { return ; } - serviceId2taskCache.put(serviceId, task); + final AsyncProfilerTask cached = serviceId2taskCache.getIfPresent(serviceId); + if (cached == null || cached.getCreateTime() <= task.getCreateTime()) { + serviceId2taskCache.put(serviceId, task); + } } /** diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/profiling/asyncprofiler/AsyncProfilerMutationService.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/profiling/asyncprofiler/AsyncProfilerMutationService.java index 1b35c12cd2db..700022e526b3 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/profiling/asyncprofiler/AsyncProfilerMutationService.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/profiling/asyncprofiler/AsyncProfilerMutationService.java @@ -130,7 +130,8 @@ private String checkArgumentError(String serviceId, private String checkTaskProfiling(String serviceId, long createTime) throws IOException { // Each service can only enable one task at a time - long endTimeBucket = TimeBucket.getMinuteTimeBucket(createTime); + // Task rows carry the second bucket of their creation (createTask), so bound them by a second bucket too. + final long endTimeBucket = TimeBucket.getRecordTimeBucket(createTime); final List alreadyHaveTaskList = getAsyncProfileTaskDAO().getTaskList( serviceId, null, endTimeBucket, 1 ); diff --git a/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/cache/AsyncProfilerTaskCacheTest.java b/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/cache/AsyncProfilerTaskCacheTest.java new file mode 100644 index 000000000000..67f9d11b5db4 --- /dev/null +++ b/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/cache/AsyncProfilerTaskCacheTest.java @@ -0,0 +1,57 @@ +/* + * 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.core.cache; + +import org.apache.skywalking.oap.server.core.CoreModuleConfig; +import org.apache.skywalking.oap.server.core.query.type.AsyncProfilerTask; +import org.apache.skywalking.oap.server.library.module.ModuleManager; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; + +class AsyncProfilerTaskCacheTest { + private static final String SERVICE_ID = "c2VydmljZQ==.1"; + + /** + * A refresh lists the tasks created within five minutes of now, newest first as the storage DAOs return them: the + * task created a moment ago, then one that just ended. The service's agent must be sent the newer one, whichever + * order the list has. + */ + @Test + void keepsTheNewestTaskOfAServiceWhateverTheListOrder() { + final long now = System.currentTimeMillis(); + final AsyncProfilerTask ended = task("ended", now - 180_000L); + final AsyncProfilerTask created = task("created", now - 30_000L); + + final AsyncProfilerTaskCache newestFirst = new AsyncProfilerTaskCache(mock(ModuleManager.class), new CoreModuleConfig()); + newestFirst.saveTask(SERVICE_ID, created); + newestFirst.saveTask(SERVICE_ID, ended); + assertEquals("created", newestFirst.getAsyncProfilerTask(SERVICE_ID).getId()); + + final AsyncProfilerTaskCache oldestFirst = new AsyncProfilerTaskCache(mock(ModuleManager.class), new CoreModuleConfig()); + oldestFirst.saveTask(SERVICE_ID, ended); + oldestFirst.saveTask(SERVICE_ID, created); + assertEquals("created", oldestFirst.getAsyncProfilerTask(SERVICE_ID).getId()); + } + + private static AsyncProfilerTask task(final String id, final long createTime) { + return AsyncProfilerTask.builder().id(id).serviceId(SERVICE_ID).createTime(createTime).duration(60).build(); + } +} diff --git a/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/profiling/asyncprofiler/AsyncProfilerMutationServiceTest.java b/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/profiling/asyncprofiler/AsyncProfilerMutationServiceTest.java new file mode 100644 index 000000000000..3de35d213781 --- /dev/null +++ b/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/profiling/asyncprofiler/AsyncProfilerMutationServiceTest.java @@ -0,0 +1,78 @@ +/* + * 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.core.profiling.asyncprofiler; + +import java.util.Collections; +import java.util.List; +import org.apache.skywalking.oap.server.core.analysis.TimeBucket; +import org.apache.skywalking.oap.server.core.query.type.AsyncProfilerEventType; +import org.apache.skywalking.oap.server.core.query.type.AsyncProfilerTask; +import org.apache.skywalking.oap.server.core.query.type.AsyncProfilerTaskCreationResult; +import org.apache.skywalking.oap.server.core.query.type.AsyncProfilerTaskCreationType; +import org.apache.skywalking.oap.server.core.storage.StorageModule; +import org.apache.skywalking.oap.server.core.storage.profiling.asyncprofiler.IAsyncProfilerTaskQueryDAO; +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.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class AsyncProfilerMutationServiceTest { + private static final String SERVICE_ID = "c2VydmljZQ==.1"; + + /** + * A task created ten seconds ago runs for a minute. Its row carries the second bucket of its creation, as + * {@code createTask} stamps it, and the DAO keeps the rows whose time bucket is not past the bound, as the JDBC + * and Elasticsearch DAOs filter. + */ + @Test + void refusesASecondTaskForTheServiceWhileTheFirstRuns() throws Exception { + final long createTime = System.currentTimeMillis() - 10_000L; + final AsyncProfilerTask running = AsyncProfilerTask.builder() + .id(createTime + "_" + SERVICE_ID) + .serviceId(SERVICE_ID) + .createTime(createTime) + .duration(60) + .build(); + final long storedTimeBucket = TimeBucket.getRecordTimeBucket(createTime); + final IAsyncProfilerTaskQueryDAO dao = mock(IAsyncProfilerTaskQueryDAO.class); + when(dao.getTaskList(eq(SERVICE_ID), any(), anyLong(), any())).thenAnswer(invocation -> { + final long endTimeBucket = invocation.getArgument(2); + return storedTimeBucket <= endTimeBucket + ? Collections.singletonList(running) : Collections.emptyList(); + }); + final ModuleManager moduleManager = mock(ModuleManager.class); + final ModuleProviderHolder providerHolder = mock(ModuleProviderHolder.class); + final ModuleServiceHolder serviceHolder = mock(ModuleServiceHolder.class); + when(moduleManager.find(StorageModule.NAME)).thenReturn(providerHolder); + when(providerHolder.provider()).thenReturn(serviceHolder); + when(serviceHolder.getService(IAsyncProfilerTaskQueryDAO.class)).thenReturn(dao); + + final AsyncProfilerTaskCreationResult result = new AsyncProfilerMutationService(moduleManager).createTask( + SERVICE_ID, List.of("instance-1"), 60, List.of(AsyncProfilerEventType.CPU), ""); + + assertEquals(AsyncProfilerTaskCreationType.ALREADY_PROFILING_ERROR, result.getCode()); + } +} diff --git a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryElasticsearch8IT.java b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryElasticsearch8IT.java index 32f301a06a1e..26120e0b8dd6 100644 --- a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryElasticsearch8IT.java +++ b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryElasticsearch8IT.java @@ -25,6 +25,7 @@ import org.testcontainers.junit.jupiter.Testcontainers; @Tag("slow") +@Tag("elastic-it") @Testcontainers public class SpanAttachedEventQueryElasticsearch8IT extends SpanAttachedEventQueryITBase { @Container diff --git a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryElasticsearch9IT.java b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryElasticsearch9IT.java index b3ae322c204a..b828e8459aee 100644 --- a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryElasticsearch9IT.java +++ b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryElasticsearch9IT.java @@ -25,6 +25,7 @@ import org.testcontainers.junit.jupiter.Testcontainers; @Tag("slow") +@Tag("elastic-it") @Testcontainers public class SpanAttachedEventQueryElasticsearch9IT extends SpanAttachedEventQueryITBase { @Container diff --git a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryElasticsearch9ShardedIT.java b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryElasticsearch9ShardedIT.java index 8473b2f42044..9ef8d8b65944 100644 --- a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryElasticsearch9ShardedIT.java +++ b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryElasticsearch9ShardedIT.java @@ -25,6 +25,7 @@ import org.testcontainers.junit.jupiter.Testcontainers; @Tag("slow") +@Tag("elastic-it") @Testcontainers public class SpanAttachedEventQueryElasticsearch9ShardedIT extends SpanAttachedEventQueryITBase { @Container diff --git a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryOpenSearch2IT.java b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryOpenSearch2IT.java index 2346b372a124..5812ac6a107b 100644 --- a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryOpenSearch2IT.java +++ b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryOpenSearch2IT.java @@ -25,6 +25,7 @@ import org.testcontainers.junit.jupiter.Testcontainers; @Tag("slow") +@Tag("elastic-it") @Testcontainers public class SpanAttachedEventQueryOpenSearch2IT extends SpanAttachedEventQueryITBase { @Container diff --git a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryOpenSearch3IT.java b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryOpenSearch3IT.java index bc76448a56a5..980152a062a2 100644 --- a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryOpenSearch3IT.java +++ b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryOpenSearch3IT.java @@ -25,6 +25,7 @@ import org.testcontainers.junit.jupiter.Testcontainers; @Tag("slow") +@Tag("elastic-it") @Testcontainers public class SpanAttachedEventQueryOpenSearch3IT extends SpanAttachedEventQueryITBase { @Container diff --git a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryOpenSearch3ShardedIT.java b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryOpenSearch3ShardedIT.java index 6e23ff32900e..0d364005e7b9 100644 --- a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryOpenSearch3ShardedIT.java +++ b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/SpanAttachedEventQueryOpenSearch3ShardedIT.java @@ -25,6 +25,7 @@ import org.testcontainers.junit.jupiter.Testcontainers; @Tag("slow") +@Tag("elastic-it") @Testcontainers public class SpanAttachedEventQueryOpenSearch3ShardedIT extends SpanAttachedEventQueryITBase { @Container diff --git a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOElasticsearch8IT.java b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOElasticsearch8IT.java index 105209f42a6a..718c31adb0d5 100644 --- a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOElasticsearch8IT.java +++ b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOElasticsearch8IT.java @@ -25,6 +25,7 @@ import org.testcontainers.junit.jupiter.Testcontainers; @Tag("slow") +@Tag("elastic-it") @Testcontainers public class ZipkinQueryEsDAOElasticsearch8IT extends ZipkinQueryEsDAOITBase { @Container diff --git a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOElasticsearch9IT.java b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOElasticsearch9IT.java index d954be19dd88..7ccd47e3f450 100644 --- a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOElasticsearch9IT.java +++ b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOElasticsearch9IT.java @@ -25,6 +25,7 @@ import org.testcontainers.junit.jupiter.Testcontainers; @Tag("slow") +@Tag("elastic-it") @Testcontainers public class ZipkinQueryEsDAOElasticsearch9IT extends ZipkinQueryEsDAOITBase { @Container diff --git a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOElasticsearch9ShardedIT.java b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOElasticsearch9ShardedIT.java index 0506925841a2..0f91b43c9b1d 100644 --- a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOElasticsearch9ShardedIT.java +++ b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOElasticsearch9ShardedIT.java @@ -25,6 +25,7 @@ import org.testcontainers.junit.jupiter.Testcontainers; @Tag("slow") +@Tag("elastic-it") @Testcontainers public class ZipkinQueryEsDAOElasticsearch9ShardedIT extends ZipkinQueryEsDAOITBase { @Container diff --git a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOOpenSearch2IT.java b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOOpenSearch2IT.java index 487d7070a8f7..bf59e6f85d95 100644 --- a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOOpenSearch2IT.java +++ b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOOpenSearch2IT.java @@ -25,6 +25,7 @@ import org.testcontainers.junit.jupiter.Testcontainers; @Tag("slow") +@Tag("elastic-it") @Testcontainers public class ZipkinQueryEsDAOOpenSearch2IT extends ZipkinQueryEsDAOITBase { @Container diff --git a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOOpenSearch3IT.java b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOOpenSearch3IT.java index 35bf3a897452..15beb8072d40 100644 --- a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOOpenSearch3IT.java +++ b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOOpenSearch3IT.java @@ -25,6 +25,7 @@ import org.testcontainers.junit.jupiter.Testcontainers; @Tag("slow") +@Tag("elastic-it") @Testcontainers public class ZipkinQueryEsDAOOpenSearch3IT extends ZipkinQueryEsDAOITBase { @Container diff --git a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOOpenSearch3ShardedIT.java b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOOpenSearch3ShardedIT.java index afcc4dfc43f8..99d34994bb9d 100644 --- a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOOpenSearch3ShardedIT.java +++ b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/zipkin/ZipkinQueryEsDAOOpenSearch3ShardedIT.java @@ -25,6 +25,7 @@ import org.testcontainers.junit.jupiter.Testcontainers; @Tag("slow") +@Tag("elastic-it") @Testcontainers public class ZipkinQueryEsDAOOpenSearch3ShardedIT extends ZipkinQueryEsDAOITBase { @Container diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/pom.xml b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/pom.xml index 7e030b22d032..886696cb5098 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/pom.xml +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/pom.xml @@ -54,6 +54,16 @@ postgresql test + + org.testcontainers + mariadb + test + + + org.mariadb.jdbc + mariadb-java-client + test + org.apache.skywalking server-testing diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/BatchSQLExecutor.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/BatchSQLExecutor.java index 10c6f1558e0e..76eae262fe5a 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/BatchSQLExecutor.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/BatchSQLExecutor.java @@ -83,13 +83,12 @@ private void executeBatch(PreparedStatement preparedStatement, List bulkRequest) throws SQLException { final var start = System.currentTimeMillis(); final var executeBatchResults = preparedStatement.executeBatch(); - final var isInsert = bulkRequest.get(0) instanceof InsertRequest; + // A SQLExecutor is both an InsertRequest and an UpdateRequest, so its type can't tell an insert from an + // update. The row count can: an INSERT adds its row or fails, so zero rows is an UPDATE that found no row. for (int i = 0; i < executeBatchResults.length; i++) { - if ((executeBatchResults[i] == 1 || executeBatchResults[i] == Statement.SUCCESS_NO_INFO) && isInsert) { - // Insert successfully. + if (executeBatchResults[i] == 1 || executeBatchResults[i] == Statement.SUCCESS_NO_INFO) { ((InsertRequest) bulkRequest.get(i)).onInsertCompleted(); - } else if (executeBatchResults[i] == 0 && !isInsert) { - // Update Failure. + } else if (executeBatchResults[i] == 0) { ((UpdateRequest) bulkRequest.get(i)).onUpdateFailure(); } } diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/JDBCTableInstaller.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/JDBCTableInstaller.java index 6d8a46b779ac..9f5001ee3ec1 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/JDBCTableInstaller.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/JDBCTableInstaller.java @@ -87,7 +87,8 @@ public InstallInfo isExists(Model model, StorageManipulationOpt opt) { .map(ModelColumn::getColumnName) .map(ColumnName::getStorageName) .anyMatch(not(databaseColumns::contains)); - installInfo.setAllColumnsExist(isAnyColumnNotCreated); + installInfo.setTableExist(true); + installInfo.setAllColumnsExist(!isAnyColumnNotCreated); installInfo.setAllExist(!isAnyColumnNotCreated); return installInfo; } diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCAggregationQueryDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCAggregationQueryDAO.java index 1a74a1c30494..bacf23661cd2 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCAggregationQueryDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCAggregationQueryDAO.java @@ -33,13 +33,15 @@ import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.SQLAndParameters; import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.TableHelper; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; -import static java.util.Comparator.comparing; -import static java.util.stream.Collectors.groupingBy; - @RequiredArgsConstructor public class JDBCAggregationQueryDAO implements IAggregationQueryDAO { protected final JDBCClient jdbcClient; @@ -51,44 +53,52 @@ public List sortMetrics(final TopNCondition metrics, final String valueColumnName, final Duration duration, final List additionalConditions) { - final var results = new ArrayList(); final var tables = tableHelper.getTablesForRead( metrics.getName(), duration.getStartTimeBucket(), duration.getEndTimeBucket() ); - + // One table ranks and cuts in SQL. Across day tables the ranking needs each entity's mean over the whole + // range, so every table returns every entity's summation and sample count, kept exact until the division. + final Map summations = new HashMap<>(); + final Map samples = new HashMap<>(); + final boolean oneTable = tables.size() == 1; for (final var table : tables) { - final var sqlAndParameters = buildSQL(metrics, valueColumnName, duration, additionalConditions, table); + final var sqlAndParameters = oneTable + ? buildSQL(metrics, valueColumnName, duration, additionalConditions, table) + : buildSummationSQL(metrics, valueColumnName, duration, additionalConditions, table); jdbcClient.executeQuery(sqlAndParameters.sql(), resultSet -> { while (resultSet.next()) { - final var topNEntity = new SelectedRecord(); - topNEntity.setId(resultSet.getString(Metrics.ENTITY_ID)); - topNEntity.setValue(String.valueOf(resultSet.getInt("result"))); - results.add(topNEntity); + final var entityId = resultSet.getString(Metrics.ENTITY_ID); + if (oneTable) { + summations.put(entityId, resultSet.getBigDecimal("result")); + samples.put(entityId, 1L); + } else { + summations.merge(entityId, resultSet.getBigDecimal("summation"), BigDecimal::add); + samples.merge(entityId, resultSet.getLong("samples"), Long::sum); + } } return null; }, sqlAndParameters.parameters()); } - final var comparator = - Order.ASC.equals(metrics.getOrder()) ? - comparing((SelectedRecord it) -> Long.parseLong(it.getValue())) : - comparing((SelectedRecord it) -> Long.parseLong(it.getValue())).reversed(); - return results - .stream() - .collect(groupingBy(SelectedRecord::getId)) - .entrySet() - .stream() - .map(entry -> { - final var selectedRecord = new SelectedRecord(); - final var average = (int) entry.getValue().stream().map(SelectedRecord::getValue).mapToLong(Long::parseLong).average().orElse(0); - selectedRecord.setId(entry.getKey()); - selectedRecord.setValue(String.valueOf(average)); - return selectedRecord; - }) - .sorted(comparator) - .limit(metrics.getTopN()) - .collect(Collectors.toList()); + // Ranked by the exact mean, as the database ranks avg(): comparing summation x the other's samples keeps + // fractional means apart even where they truncate to the same value. The value returned is the truncated mean. + final Comparator byMean = (a, b) -> summations.get(a).multiply(BigDecimal.valueOf(samples.get(b))) + .compareTo(summations.get(b).multiply( + BigDecimal.valueOf(samples.get(a)))); + return summations.keySet() + .stream() + .sorted(Order.ASC.equals(metrics.getOrder()) ? byMean : byMean.reversed()) + .limit(metrics.getTopN()) + .map(entityId -> { + final var selectedRecord = new SelectedRecord(); + selectedRecord.setId(entityId); + final BigDecimal mean = summations.get(entityId).divide( + BigDecimal.valueOf(samples.get(entityId)), 0, RoundingMode.DOWN); + selectedRecord.setValue(String.valueOf(mean.longValue())); + return selectedRecord; + }) + .collect(Collectors.toList()); } protected SQLAndParameters buildSQL( @@ -103,8 +113,42 @@ protected SQLAndParameters buildSQL( sql.append("select result, ").append(Metrics.ENTITY_ID) .append(" from (select avg(").append(valueColumnName).append(") as result,") .append(Metrics.ENTITY_ID) - .append(" from ").append(table) - .append(" where ") + .append(" from ").append(table); + appendConditions(sql, parameters, metrics, duration, queries); + sql.append(" group by ").append(Metrics.ENTITY_ID); + sql.append(") as T order by result") + .append(Order.ASC.equals(metrics.getOrder()) ? " asc" : " desc") + .append(" limit ") + .append(metrics.getTopN()); + return new SQLAndParameters(sql.toString(), parameters); + } + + /** + * The summation and sample count of every entity in one day table, unranked and uncut. + */ + protected SQLAndParameters buildSummationSQL( + final TopNCondition metrics, + final String valueColumnName, + final Duration duration, + final List queries, + final String table) { + + final var sql = new StringBuilder(); + final var parameters = new ArrayList<>(10); + sql.append("select sum(").append(valueColumnName).append(") as summation, count(") + .append(valueColumnName).append(") as samples, ").append(Metrics.ENTITY_ID) + .append(" from ").append(table); + appendConditions(sql, parameters, metrics, duration, queries); + sql.append(" group by ").append(Metrics.ENTITY_ID); + return new SQLAndParameters(sql.toString(), parameters); + } + + private void appendConditions(final StringBuilder sql, + final List parameters, + final TopNCondition metrics, + final Duration duration, + final List queries) { + sql.append(" where ") .append(JDBCTableInstaller.TABLE_COLUMN).append(" = ?") .append(" and ") .append(Metrics.TIME_BUCKET).append(" >= ? ") @@ -130,11 +174,5 @@ protected SQLAndParameters buildSQL( parameters.add(attr.getValue()); }); } - sql.append(" group by ").append(Metrics.ENTITY_ID); - sql.append(") as T order by result") - .append(Order.ASC.equals(metrics.getOrder()) ? " asc" : " desc") - .append(" limit ") - .append(metrics.getTopN()); - return new SQLAndParameters(sql.toString(), parameters); } } diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCAsyncProfilerTaskQueryDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCAsyncProfilerTaskQueryDAO.java index e7d6e23a992b..061ca1f05c91 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCAsyncProfilerTaskQueryDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCAsyncProfilerTaskQueryDAO.java @@ -37,6 +37,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import static java.util.stream.Collectors.toList; @@ -96,12 +97,12 @@ public List getTaskList(String serviceId, Long startTimeBucke condition.toArray(new Object[0])) ); } - return limit == null ? - results : - results - .stream() - .limit(limit) - .collect(toList()); + // Each day table is sorted on its own: sort them together before the limit picks the newest. + return results + .stream() + .sorted(Comparator.comparingLong(AsyncProfilerTask::getCreateTime).reversed()) + .limit(limit == null ? Long.MAX_VALUE : limit) + .collect(toList()); } @Override diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCBrowserLogQueryDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCBrowserLogQueryDAO.java index 2dbb1ca95387..012160bcb95c 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCBrowserLogQueryDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCBrowserLogQueryDAO.java @@ -52,9 +52,10 @@ public BrowserErrorLogs queryBrowserErrorLogs(String serviceId, int limit, int from) { final var logs = new ArrayList(); - final var tables = tableHelper.getTablesForRead( - BrowserErrorLogRecord.INDEX_NAME, duration.getStartTimeBucket(), duration.getEndTimeBucket() - ); + final var tables = nonNull(duration) + ? tableHelper.getTablesForRead( + BrowserErrorLogRecord.INDEX_NAME, duration.getStartTimeBucket(), duration.getEndTimeBucket()) + : tableHelper.getTablesWithinTTL(BrowserErrorLogRecord.INDEX_NAME); for (String table : tables) { final var sqlAndParameters = buildSQL( diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCEBPFProfilingDataDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCEBPFProfilingDataDAO.java index efe96ec90f79..186f359d1bd8 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCEBPFProfilingDataDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCEBPFProfilingDataDAO.java @@ -25,6 +25,7 @@ import org.apache.skywalking.oap.server.core.profiling.ebpf.storage.EBPFProfilingDataRecord; import org.apache.skywalking.oap.server.core.storage.profiling.ebpf.IEBPFProfilingDataDAO; import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCClient; +import org.apache.skywalking.oap.server.library.util.CollectionUtils; import org.apache.skywalking.oap.server.library.util.StringUtil; import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.JDBCTableInstaller; import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.SQLAndParameters; @@ -34,6 +35,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Base64; +import java.util.Collections; import java.util.List; import static java.util.stream.Collectors.joining; @@ -46,6 +48,9 @@ public class JDBCEBPFProfilingDataDAO implements IEBPFProfilingDataDAO { @Override @SneakyThrows public List queryData(List scheduleIdList, long beginTime, long endTime) { + if (CollectionUtils.isEmpty(scheduleIdList)) { + return Collections.emptyList(); + } final var tables = tableHelper.getTablesForRead( EBPFProfilingDataRecord.INDEX_NAME, TimeBucket.getTimeBucket(beginTime, DownSampling.Day), diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCEBPFProfilingTaskDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCEBPFProfilingTaskDAO.java index 1df3aa413150..887fc2c12222 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCEBPFProfilingTaskDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCEBPFProfilingTaskDAO.java @@ -26,6 +26,7 @@ import org.apache.skywalking.oap.server.core.storage.StorageData; import org.apache.skywalking.oap.server.core.storage.profiling.ebpf.IEBPFProfilingTaskDAO; import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCClient; +import org.apache.skywalking.oap.server.library.util.CollectionUtils; import org.apache.skywalking.oap.server.library.util.StringUtil; import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.JDBCTableInstaller; import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.SQLAndParameters; @@ -102,6 +103,10 @@ public List queryTasksByServices(List serviceId public List queryTasksByTargets(String serviceId, String serviceInstanceId, List targetTypes, EBPFProfilingTriggerType triggerType, long taskStartTime, long latestUpdateTime) throws IOException { final var results = new ArrayList(); + // No target type asked for (a continuous-profiling policy saved without targets): no task. + if (CollectionUtils.isEmpty(targetTypes)) { + return results; + } final var tables = tableHelper.getTablesWithinTTL(EBPFProfilingTaskRecord.INDEX_NAME); for (final var table : tables) { diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCEventQueryDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCEventQueryDAO.java index 6fe70c14e008..34c64035352f 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCEventQueryDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCEventQueryDAO.java @@ -214,8 +214,9 @@ protected Tuple2, Stream> buildQuery(final EventQueryCond final Duration time = condition.getTime(); if (time != null) { + // The window is [start, end): end is the first millisecond after the last step of the duration. if (time.getStartTimestamp() > 0) { - conditions.add(Event.TIMESTAMP + ">?"); + conditions.add(Event.TIMESTAMP + ">=?"); parameters.add(time.getStartTimestamp()); } if (time.getEndTimestamp() > 0) { @@ -226,7 +227,7 @@ protected Tuple2, Stream> buildQuery(final EventQueryCond if (!isNullOrEmpty(condition.getLayer())) { conditions.add(Event.LAYER + "=?"); - parameters.add(String.valueOf(Layer.nameOf(condition.getLayer()).value())); + parameters.add(Layer.nameOf(condition.getLayer()).value()); } return Tuple.of(conditions.build(), parameters.build()); diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetadataQueryDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetadataQueryDAO.java index fd334d5273d8..f5a2d1fab182 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetadataQueryDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetadataQueryDAO.java @@ -186,6 +186,8 @@ public ServiceInstance getInstance(final String instanceId) { @Override public List getInstances(List instanceIds) throws IOException { final var tables = tableHelper.getTablesWithinTTL(InstanceTraffic.INDEX_NAME); + // An instance stays in the day table it was first stored in, so every table may hold some of the ids. + final var results = new ArrayList(); for (String table : tables) { StringBuilder sql = new StringBuilder(); @@ -200,13 +202,10 @@ public List getInstances(List instanceIds) throws IOExc condition.addAll(instanceIds); sql.append(" limit ").append(instanceIds.size()); - final var result = jdbcClient.executeQuery(sql.toString(), resultSet -> buildInstances(resultSet), condition.toArray(new Object[0])); - if (result != null) { - return result; - } + results.addAll(jdbcClient.executeQuery(sql.toString(), this::buildInstances, condition.toArray(new Object[0]))); } - return null; + return results; } @Override @@ -346,7 +345,7 @@ public List listProcesses(String agentId, long startPingTimeBucket, lon sql.append("select * from ").append(table); sql.append(" where ").append(JDBCTableInstaller.TABLE_COLUMN).append(" = ? "); condition.add(ProcessTraffic.INDEX_NAME); - appendProcessWhereQuery(sql, condition, null, null, agentId, null, 0, 0, false); + appendProcessWhereQuery(sql, condition, null, null, agentId, null, startPingTimeBucket, endPingTimeBucket, false); sql.append(" limit ").append(metadataQueryMaxSize); results.addAll( diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetricsQueryDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetricsQueryDAO.java index 9277422c7d81..f2bd0e682ffc 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetricsQueryDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetricsQueryDAO.java @@ -290,17 +290,17 @@ public HeatMap readHeatMap(final MetricsCondition condition, duration.getStartTimeBucket(), duration.getEndTimeBucket() ); - final var heatMap = new HeatMap(); + final var pointOfTimes = duration.assembleDurationPoints(); + final var entityId = condition.getEntity().buildId(); + final var ids = + pointOfTimes + .stream() + .map(pointOfTime -> TableHelper.generateId(condition.getName(), pointOfTime.id(entityId))) + .collect(Collectors.toList()); + final int defaultValue = ValueColumnMetadata.INSTANCE.getDefaultValue(condition.getName()); + final var datasets = new HashMap(); for (String table : tables) { - final var pointOfTimes = duration.assembleDurationPoints(); - final var entityId = condition.getEntity().buildId(); - final var ids = - pointOfTimes - .stream() - .map(pointOfTime -> TableHelper.generateId(condition.getName(), pointOfTime.id(entityId))) - .collect(Collectors.toList()); - final var sql = new StringBuilder("select id, " + valueColumnName + " dataset, id from " + table) .append(" where id in ") .append( @@ -309,22 +309,28 @@ public HeatMap readHeatMap(final MetricsCondition condition, .collect(Collectors.joining(", ", "(", ")")) ); - final int defaultValue = ValueColumnMetadata.INSTANCE.getDefaultValue(condition.getName()); - jdbcClient.executeQuery( sql.toString(), resultSet -> { while (resultSet.next()) { - heatMap.buildColumn( - resultSet.getString("id"), resultSet.getString("dataset"), defaultValue); + datasets.put(resultSet.getString("id"), resultSet.getString("dataset")); } - heatMap.fixMissingColumns(ids, defaultValue); - return null; }, ids.toArray(new Object[0])); } + // The points of a range can sit in several day tables; the columns follow the points, whichever table holds + // them, and the points no table holds are filled once all are read. + final var heatMap = new HeatMap(); + for (final var id : ids) { + final var dataset = datasets.get(id); + if (dataset != null) { + heatMap.buildColumn(id, dataset, defaultValue); + } + } + heatMap.fixMissingColumns(ids, defaultValue); + return heatMap; } } diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCPprofTaskQueryDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCPprofTaskQueryDAO.java index 1dd991a25279..70f9cd516b3c 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCPprofTaskQueryDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCPprofTaskQueryDAO.java @@ -26,6 +26,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; @@ -99,12 +100,12 @@ public List getTaskList(String serviceId, ) ); } - return limit == null ? - results : - results - .stream() - .limit(limit) - .collect(toList()); + // Each day table is sorted on its own: sort them together before the limit picks the newest. + return results + .stream() + .sorted(Comparator.comparingLong(PprofTask::getCreateTime).reversed()) + .limit(limit == null ? Long.MAX_VALUE : limit) + .collect(toList()); } @Override diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfileTaskQueryDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfileTaskQueryDAO.java index 97d5133e1ca7..434cc634833f 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfileTaskQueryDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfileTaskQueryDAO.java @@ -31,6 +31,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import static java.util.stream.Collectors.toList; @@ -49,9 +50,9 @@ public JDBCProfileTaskQueryDAO(JDBCClient jdbcClient, ModuleManager moduleManage public List getTaskList(String serviceId, String endpointName, Long startTimeBucket, Long endTimeBucket, Integer limit) { final var results = new ArrayList(); - final var tables = startTimeBucket == null || endTimeBucket == null ? - tableHelper.getTablesWithinTTL(ProfileTaskRecord.INDEX_NAME) : - tableHelper.getTablesForRead(ProfileTaskRecord.INDEX_NAME, startTimeBucket, endTimeBucket); + // A task is stored in the table of the day it was created, while its time bucket is its scheduled start, + // which can be days later: the start-time bounds do not tell the tables, so read every table within the TTL. + final var tables = tableHelper.getTablesWithinTTL(ProfileTaskRecord.INDEX_NAME); for (final var table : tables) { final var condition = new ArrayList<>(4); final var sql = new StringBuilder() @@ -98,12 +99,11 @@ public List getTaskList(String serviceId, String endpointName, Long condition.toArray(new Object[0])) ); } - return limit == null ? - results : - results - .stream() - .limit(limit) - .collect(toList()); + return results + .stream() + .sorted(Comparator.comparingLong(ProfileTask::getStartTime).reversed()) + .limit(limit == null ? Long.MAX_VALUE : limit) + .collect(toList()); } @Override diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfileThreadSnapshotQueryDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfileThreadSnapshotQueryDAO.java index ae02da58cc62..d6e9c54e14ca 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfileThreadSnapshotQueryDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfileThreadSnapshotQueryDAO.java @@ -20,6 +20,7 @@ import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; +import org.apache.skywalking.oap.server.core.profiling.trace.ProfileLanguageType; import org.apache.skywalking.oap.server.core.profiling.trace.ProfileThreadSnapshotRecord; import org.apache.skywalking.oap.server.core.storage.profiling.trace.IProfileThreadSnapshotQueryDAO; import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCClient; @@ -74,6 +75,7 @@ protected ArrayList querySegments(String taskId, St record.setSegmentId(resultSet.getString(ProfileThreadSnapshotRecord.SEGMENT_ID)); record.setDumpTime(resultSet.getLong(ProfileThreadSnapshotRecord.DUMP_TIME)); record.setSequence(resultSet.getInt(ProfileThreadSnapshotRecord.SEQUENCE)); + record.setLanguage(ProfileLanguageType.fromValue(resultSet.getInt(ProfileThreadSnapshotRecord.LANGUAGE_TYPE))); String dataBinaryBase64 = resultSet.getString(ProfileThreadSnapshotRecord.STACK_BINARY); if (StringUtil.isNotEmpty(dataBinaryBase64)) { record.setStackBinary(Base64.getDecoder().decode(dataBinaryBase64)); @@ -125,6 +127,7 @@ public List queryRecords(String segmentId, record.setSegmentId(resultSet.getString(ProfileThreadSnapshotRecord.SEGMENT_ID)); record.setDumpTime(resultSet.getLong(ProfileThreadSnapshotRecord.DUMP_TIME)); record.setSequence(resultSet.getInt(ProfileThreadSnapshotRecord.SEQUENCE)); + record.setLanguage(ProfileLanguageType.fromValue(resultSet.getInt(ProfileThreadSnapshotRecord.LANGUAGE_TYPE))); String dataBinaryBase64 = resultSet.getString(ProfileThreadSnapshotRecord.STACK_BINARY); if (StringUtil.isNotEmpty(dataBinaryBase64)) { record.setStackBinary(Base64.getDecoder().decode(dataBinaryBase64)); @@ -168,7 +171,11 @@ private int querySequenceWithAgg(String aggType, String segmentId, long start, l jdbcClient.executeQuery(sql.toString(), resultSet -> { if (resultSet.next()) { - result.add(resultSet.getInt(1)); + final int sequence = resultSet.getInt(1); + // An aggregate over no row is SQL NULL, which getInt reads as 0: a day table without the segment. + if (!resultSet.wasNull()) { + result.add(sequence); + } } return null; }, params); diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCRecordsQueryDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCRecordsQueryDAO.java index ee57e166595a..e20c8a1c9e6b 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCRecordsQueryDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCRecordsQueryDAO.java @@ -33,7 +33,9 @@ import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.TableHelper; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; +import java.util.stream.Collectors; @RequiredArgsConstructor public class JDBCRecordsQueryDAO implements IRecordsQueryDAO { @@ -71,7 +73,12 @@ public List readRecords(final RecordCondition condition, sqlAndParameters.parameters()); } - return results; + // Each day table returns its own top N; the top N of the range is taken over all of them. + final Comparator byValue = Comparator.comparingLong(record -> Long.parseLong(record.getValue())); + return results.stream() + .sorted(Order.DES.equals(condition.getOrder()) ? byValue.reversed() : byValue) + .limit(condition.getTopN()) + .collect(Collectors.toList()); } protected static SQLAndParameters buildSQL( diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCRuntimeRuleManagementDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCRuntimeRuleManagementDAO.java index 82d5bbf62207..27fdbeda25c9 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCRuntimeRuleManagementDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCRuntimeRuleManagementDAO.java @@ -84,15 +84,8 @@ public List getAll() { public void save(final RuntimeRule rule) throws IOException { final Model model = TableMetaInfo.get(RuntimeRule.INDEX_NAME); final RuntimeRule.Builder builder = new RuntimeRule.Builder(); - // The shared {@link JDBCSQLExecutor#getByID} is unusable for ManagementData like - // RuntimeRule: it always passes the lookup id through {@code TableHelper.generateId( - // String, String)} which prefixes with the model name, but the INSERT path uses - // {@code TableHelper.generateId(Model, String)} which returns the RAW id for - // non-record / non-function-metric types. RuntimeRule is non-record + non-metric, so - // its row is stored with the raw composite id while getByID looks up "runtimerule_" - // and never finds it. Without this workaround the second save() always falls through - // to INSERT and trips the primary-key constraint, breaking every /addOrUpdate update - // and every /inactivate after the first persist. + // An upsert by the id the INSERT path stores: every /addOrUpdate after the first and every + // /inactivate must update the existing row rather than trip the primary-key constraint. final String storedId = TableHelper.generateId(model, rule.id().build()); try (Connection connection = jdbcClient.getConnection()) { final boolean exists = rowExists(connection, storedId); @@ -111,9 +104,7 @@ public void save(final RuntimeRule rule) throws IOException { } /** - * Probe every TTL-shadow table for a row with the given stored id. Direct SELECT on the - * id column rather than the shared getByID helper for the prefix-mismatch reason - * documented in {@link #save(RuntimeRule)}. + * Probe every TTL-shadow table for a row with the given stored id. */ private boolean rowExists(final Connection connection, final String storedId) throws SQLException { final List tables = tableHelper.getTablesWithinTTL(RuntimeRule.INDEX_NAME); diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCSQLExecutor.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCSQLExecutor.java index bc422dcb61f8..22e58648edab 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCSQLExecutor.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCSQLExecutor.java @@ -79,7 +79,7 @@ protected StorageData getByID(JDBCClient h2Client, Strin final var result = h2Client.executeQuery( "SELECT * FROM " + table + " WHERE id = ?", resultSet -> toStorageData(resultSet, modelName, storageBuilder), - TableHelper.generateId(modelName, id) + TableHelper.generateId(TableMetaInfo.get(modelName), id) ); if (result != null) { return result; diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTagAutoCompleteQueryDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTagAutoCompleteQueryDAO.java index f4ddca69f89f..3b6b3cd26708 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTagAutoCompleteQueryDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTagAutoCompleteQueryDAO.java @@ -33,6 +33,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import static java.util.Objects.nonNull; @@ -65,7 +66,7 @@ public Set queryTagAutocompleteKeys(final TagType tagType, }, sqlAndParameters.parameters()); } - return results; + return limit(results, limit); } protected SQLAndParameters buildSQLForQueryKeys(TagType tagType, int limit, Duration duration, String table) { @@ -107,7 +108,14 @@ public Set queryTagAutocompleteValues(final TagType tagType, sqlAndParameters.parameters() ); } - return results; + return limit(results, limit); + } + + /** + * Each day table is read up to the limit, so their union can hold more; the limit applies to the whole window. + */ + private static Set limit(final Set results, final int limit) { + return results.stream().limit(limit).collect(Collectors.toSet()); } protected SQLAndParameters buildSQLForQueryValues(TagType tagType, String tagKey, int limit, Duration duration, String table) { diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTraceQueryDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTraceQueryDAO.java index 2fb99095ed59..a320211628bd 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTraceQueryDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCTraceQueryDAO.java @@ -50,6 +50,7 @@ import java.util.ArrayList; import java.util.Base64; import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @@ -192,7 +193,8 @@ public TraceBrief queryBasicTraces(Duration duration, break; } - buildLimit(sql, from, limit); + // Every day table answers its own first `from + limit`; the page is cut from all of them together. + buildLimit(sql, 0, from + limit); jdbcClient.executeQuery( "select " + @@ -223,7 +225,10 @@ public TraceBrief queryBasicTraces(Duration duration, parameters.toArray(new Object[0])); } - return new TraceBrief(traces); // TODO: sort, + final Comparator order = queryOrder == QueryOrder.BY_DURATION + ? Comparator.comparingInt(BasicTrace::getDuration).reversed() + : Comparator.comparingLong((BasicTrace trace) -> Long.parseLong(trace.getStart())).reversed(); + return new TraceBrief(traces.stream().sorted(order).skip(from).limit(limit).collect(Collectors.toList())); } protected void buildLimit(StringBuilder sql, int from, int limit) { @@ -283,6 +288,9 @@ public List queryBySegmentIdList(List segmentIdList, @Nul @SneakyThrows @Override public List queryByTraceIdWithInstanceId(List traceIdList, List instanceIdList, @Nullable Duration duration) throws IOException { + if (CollectionUtils.isEmpty(traceIdList) || CollectionUtils.isEmpty(instanceIdList)) { + return Collections.emptyList(); + } final var tables = tableHelper.getTablesWithinTTL(SegmentRecord.INDEX_NAME); final var segmentRecords = new ArrayList(); final ArrayList conditions = new ArrayList<>(); diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCUITemplateManagementDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCUITemplateManagementDAO.java index 81b04c33e5b8..af9dc6d1be12 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCUITemplateManagementDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCUITemplateManagementDAO.java @@ -136,6 +136,13 @@ public TemplateChangeStatus addTemplate(final DashboardSetting setting) throws I @Override public TemplateChangeStatus changeTemplate(final DashboardSetting setting) throws IOException { + if (getByID(h2Client, UITemplate.INDEX_NAME, setting.getId(), new UITemplate.Builder()) == null) { + return TemplateChangeStatus.builder() + .status(false) + .id(setting.getId()) + .message("Can't find the template") + .build(); + } final UITemplate uiTemplate = setting.toEntity(); return executeUpdate(uiTemplate); } diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCZipkinQueryDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCZipkinQueryDAO.java index a35eb463995e..1f4184b7da76 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCZipkinQueryDAO.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCZipkinQueryDAO.java @@ -43,12 +43,14 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; +import java.util.HashMap; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; +import static java.util.stream.Collectors.toCollection; import static java.util.stream.Collectors.toList; @RequiredArgsConstructor @@ -182,7 +184,7 @@ public List> getTraces(final QueryRequest request, Duration duration) ); final long startTimeMillis = duration.getStartTimestamp(); final long endTimeMillis = duration.getEndTimestamp(); - final var traceIds = new HashSet(); + final Map firstMatchByTraceId = new HashMap<>(); for (String table : tables) { StringBuilder sql = new StringBuilder(); @@ -260,12 +262,20 @@ public List> getTraces(final QueryRequest request, Duration duration) sql.append(" limit ").append(request.limit()); h2Client.executeQuery(sql.toString(), resultSet -> { while (resultSet.next()) { - traceIds.add(resultSet.getString(ZipkinSpanRecord.TRACE_ID)); + firstMatchByTraceId.merge( + resultSet.getString(ZipkinSpanRecord.TRACE_ID), resultSet.getLong(2), Math::min); } return null; }, condition.toArray(new Object[0])); } + // Each day table answers its own top `limit`; the traces of every table are ranked together and cut once. + final Set traceIds = firstMatchByTraceId.entrySet() + .stream() + .sorted(Map.Entry.comparingByValue().reversed()) + .limit(request.limit()) + .map(Map.Entry::getKey) + .collect(toCollection(LinkedHashSet::new)); return getTraces(traceIds, duration); } @@ -277,7 +287,8 @@ public List> getTraces(final Set traceIds, final Duration dur } final var tables = tableHelper.getTablesWithinTTL(ZipkinSpanRecord.INDEX_NAME); - final var traces = new ArrayList>(); + // A trace that crosses midnight has spans in two day tables; they are grouped into one trace. + final Map> traces = new LinkedHashMap<>(); for (String table : tables) { StringBuilder sql = new StringBuilder(); @@ -293,22 +304,15 @@ public List> getTraces(final Set traceIds, final Duration dur sql.append(" order by ").append(ZipkinSpanRecord.TIMESTAMP_MILLIS).append(" desc"); - traces.addAll( - h2Client.executeQuery(sql.toString(), this::buildTraces, condition.toArray(new Object[0])) - ); - } - return traces; - } - - private List> buildTraces(ResultSet resultSet) throws SQLException { - Map> groupedByTraceId = new LinkedHashMap>(); - while (resultSet.next()) { - Span span = buildSpan(resultSet); - String traceId = span.traceId(); - groupedByTraceId.putIfAbsent(traceId, new ArrayList<>()); - groupedByTraceId.get(traceId).add(span); + h2Client.executeQuery(sql.toString(), resultSet -> { + while (resultSet.next()) { + final Span span = buildSpan(resultSet); + traces.computeIfAbsent(span.traceId(), traceId -> new ArrayList<>()).add(span); + } + return null; + }, condition.toArray(new Object[0])); } - return new ArrayList<>(groupedByTraceId.values()); + return new ArrayList<>(traces.values()); } private Span buildSpan(ResultSet resultSet) throws SQLException { 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/JDBCAggregationQueryDAOTest.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCAggregationQueryDAOTest.java index 381d30123887..91febe4c1eb6 100644 --- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCAggregationQueryDAOTest.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCAggregationQueryDAOTest.java @@ -24,6 +24,7 @@ import org.apache.skywalking.oap.server.core.query.input.Duration; import org.apache.skywalking.oap.server.core.query.input.TopNCondition; import org.apache.skywalking.oap.server.core.query.type.KeyValue; +import org.apache.skywalking.oap.server.core.query.type.SelectedRecord; import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCClient; import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.JDBCTableInstaller; import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.SQLAndParameters; @@ -36,9 +37,20 @@ import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; +import java.math.BigDecimal; +import java.sql.ResultSet; +import java.util.Arrays; import java.util.Collections; +import java.util.Iterator; +import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) @@ -131,4 +143,70 @@ void buildSQL_withAdditionalConditions_shouldAppendConditions() { assertThat(result.sql()).contains("service_id = ?"); assertThat(result.parameters()).contains("svc-1"); } + + /** + * A {@code doubleAvg} metric such as {@code instance_jvm_cpu} stores fractional values, so each day table's + * summation is fractional: the mean of 1.9 and 2.9 over two days is 2, not the 1 of truncating each day first. + */ + @Test + void sortMetrics_shouldMergeFractionalSummationsOfEveryDayBeforeTheMean() throws Exception { + when(tableHelper.getTablesForRead(anyString(), anyLong(), anyLong())) + .thenReturn(Arrays.asList("instance_jvm_cpu_20230101", "instance_jvm_cpu_20230102")); + final Iterator days = Arrays.asList( + summations(new String[] {"instance-1"}, "1.9"), summations(new String[] {"instance-1"}, "2.9")).iterator(); + doAnswer(invocation -> invocation.>getArgument(1).handle(days.next())) + .when(jdbcClient).executeQuery(anyString(), any(), any(Object[].class)); + + final List top = dao.sortMetrics( + buildCondition("instance_jvm_cpu", 10, Order.DES), "value", buildDuration(), null); + + assertThat(top).hasSize(1); + assertThat(top.get(0).getId()).isEqualTo("instance-1"); + assertThat(top.get(0).getValue()).isEqualTo("2"); + } + + /** + * Two idle instances, 0.1 and 0.9 on both days: their means truncate to the same 0, yet the ranking keeps them + * apart, as the database ranks {@code avg()}. + */ + @Test + void sortMetrics_shouldRankByTheExactMeanBeforeTruncatingIt() throws Exception { + when(tableHelper.getTablesForRead(anyString(), anyLong(), anyLong())) + .thenReturn(Arrays.asList("instance_jvm_cpu_20230101", "instance_jvm_cpu_20230102")); + final String[] instances = {"instance-1", "instance-2"}; + // Two day tables for each of the two queries below. + final Iterator days = Arrays.asList( + summations(instances, "0.1", "0.9"), summations(instances, "0.1", "0.9"), + summations(instances, "0.1", "0.9"), summations(instances, "0.1", "0.9")).iterator(); + doAnswer(invocation -> invocation.>getArgument(1).handle(days.next())) + .when(jdbcClient).executeQuery(anyString(), any(), any(Object[].class)); + + final List busiest = dao.sortMetrics( + buildCondition("instance_jvm_cpu", 1, Order.DES), "value", buildDuration(), null); + assertThat(busiest).extracting(SelectedRecord::getId).containsExactly("instance-2"); + assertThat(busiest).extracting(SelectedRecord::getValue).containsExactly("0"); + + final List idlest = dao.sortMetrics( + buildCondition("instance_jvm_cpu", 1, Order.ASC), "value", buildDuration(), null); + assertThat(idlest).extracting(SelectedRecord::getId).containsExactly("instance-1"); + } + + /** + * One day table's summation and sample count per entity, one sample each. + */ + private static ResultSet summations(final String[] entityIds, final String... summations) throws Exception { + final ResultSet resultSet = mock(ResultSet.class); + // A row per entity: true for each, then false. + final Boolean[] afterFirst = new Boolean[entityIds.length]; + Arrays.fill(afterFirst, true); + afterFirst[entityIds.length - 1] = false; + when(resultSet.next()).thenReturn(true, afterFirst); + when(resultSet.getString(Metrics.ENTITY_ID)) + .thenReturn(entityIds[0], Arrays.copyOfRange(entityIds, 1, entityIds.length)); + final BigDecimal[] values = Arrays.stream(summations).map(BigDecimal::new).toArray(BigDecimal[]::new); + when(resultSet.getBigDecimal("summation")).thenReturn(values[0], Arrays.copyOfRange(values, 1, values.length)); + when(resultSet.getLong("samples")).thenReturn(1L); + return resultSet; + } + } 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/JDBCLogQueryITBase.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCLogQueryITBase.java new file mode 100644 index 000000000000..0ddb87c62c26 --- /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/JDBCLogQueryITBase.java @@ -0,0 +1,222 @@ +/* + * 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.stream.Collectors; +import org.apache.skywalking.apm.network.language.agent.v3.BrowserErrorLog; +import org.apache.skywalking.apm.network.language.agent.v3.ErrorCategory; +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.Layer; +import org.apache.skywalking.oap.server.core.analysis.TimeBucket; +import org.apache.skywalking.oap.server.core.analysis.record.Event; +import org.apache.skywalking.oap.server.core.browser.manual.errorlog.BrowserErrorLogRecord; +import org.apache.skywalking.oap.server.core.browser.source.BrowserErrorCategory; +import org.apache.skywalking.oap.server.core.query.enumeration.Order; +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.Pagination; +import org.apache.skywalking.oap.server.core.query.type.event.EventQueryCondition; +import org.apache.skywalking.oap.server.core.query.type.event.EventType; +import org.apache.skywalking.oap.server.core.query.type.event.Events; +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 events the way the event list and the alarm list do, and browser error logs the way the browser log query + * does, on rows written on both sides of midnight, so in two day tables. A subclass names the server. + */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +abstract class JDBCLogQueryITBase { + private static final String SERVICE = "checkout"; + /** + * Event uuid, offset from midnight in seconds, layer. Kubernetes reports events to the second, so an event often + * starts on a minute's first millisecond, as {@code e3} at midnight and {@code e5} a minute later do. + */ + private static final Object[][] EVENTS = { + {"e1", -90, Layer.MESH}, + {"e2", -30, Layer.GENERAL}, + {"e3", 0, Layer.MESH}, + {"e4", 30, Layer.GENERAL}, + {"e5", 60, Layer.MESH}, + }; + private static final String BROWSER_SERVICE = "shop-web"; + private static final String BROWSER_SERVICE_ID = IDManager.ServiceID.buildId(BROWSER_SERVICE, true); + /** Browser error unique id, offset from midnight in seconds. Two before midnight, two after. */ + private static final Object[][] BROWSER_ERRORS = { + {"b1", -20}, + {"b2", -10}, + {"b3", 10}, + {"b4", 20}, + }; + + private final JdbcDatabaseContainer server; + private final JDBCQueryITSupport.Target target; + private JDBCQueryITSupport support; + private JDBCEventQueryDAO eventDAO; + private JDBCBrowserLogQueryDAO browserLogDAO; + + JDBCLogQueryITBase(final JdbcDatabaseContainer server, final JDBCQueryITSupport.Target target) { + this.server = server; + this.target = target; + } + + @BeforeAll + public void writeEventsAndBrowserErrors() throws Exception { + support = JDBCQueryITSupport.connect(server, target); + final var eventModel = support.install( + Event.class, DefaultScopeDefine.EVENT, Event.INDEX_NAME, DownSampling.Second); + final List events = new ArrayList<>(); + for (final Object[] event : EVENTS) { + events.add(event((String) event[0], MIDNIGHT + (int) event[1] * 1000L, (Layer) event[2])); + } + support.writeRecords(eventModel, new Event.Builder(), events); + + final var browserErrorModel = support.install( + BrowserErrorLogRecord.class, DefaultScopeDefine.BROWSER_ERROR_LOG, BrowserErrorLogRecord.INDEX_NAME, + DownSampling.Second); + final List errors = new ArrayList<>(); + for (final Object[] error : BROWSER_ERRORS) { + errors.add(browserError((String) error[0], MIDNIGHT + (int) error[1] * 1000L)); + } + support.writeRecords(browserErrorModel, new BrowserErrorLogRecord.Builder(), errors); + + eventDAO = new JDBCEventQueryDAO(support.getClient(), support.getTableHelper()); + browserLogDAO = new JDBCBrowserLogQueryDAO(support.getClient(), support.getTableHelper()); + } + + @AfterAll + public void close() { + if (support != null) { + support.close(); + } + } + + /** + * The layer column is an INT; PostgreSQL compares it with an INT parameter only. + */ + @Test + public void readsTheEventsOfALayerOnBothDays() throws Exception { + final EventQueryCondition mesh = condition( + JDBCQueryITSupport.duration(MIDNIGHT - 120_000L, MIDNIGHT + 60_000L, Step.MINUTE)); + mesh.setLayer(Layer.MESH.name()); + assertEquals(Arrays.asList("e5", "e3", "e1"), uuids(eventDAO.queryEvents(mesh))); + assertEquals(Arrays.asList("e5", "e3", "e1"), uuids(eventDAO.queryEvents(Collections.singletonList(mesh)))); + } + + /** + * A minute window holds the events from its first millisecond up to, not including, the next minute's first, so + * an event on a minute's first millisecond belongs to that minute alone. + */ + @Test + public void readsAnEventOnItsMinutesFirstMillisecond() throws Exception { + assertEquals(Collections.singletonList("e2"), uuids(eventDAO.queryEvents(minute(MIDNIGHT - 60_000L)))); + assertEquals(Arrays.asList("e4", "e3"), uuids(eventDAO.queryEvents(minute(MIDNIGHT)))); + assertEquals(Collections.singletonList("e5"), uuids(eventDAO.queryEvents(minute(MIDNIGHT + 60_000L)))); + } + + /** + * The query protocol's duration of a browser error log query is optional; without one the read covers every + * day table within the record TTL. + */ + @Test + public void readsTheBrowserErrorsOfBothDaysWithoutADuration() { + assertEquals(Arrays.asList("b4", "b3", "b2", "b1"), browserErrors(10, 0)); + assertEquals(Arrays.asList("b3", "b2"), browserErrors(2, 1)); + } + + private List browserErrors(final int limit, final int from) { + return browserLogDAO.queryBrowserErrorLogs(BROWSER_SERVICE_ID, null, null, null, null, limit, from) + .getLogs() + .stream() + .map(log -> log.getMessage()) + .collect(Collectors.toList()); + } + + private static EventQueryCondition minute(final long start) { + return condition(JDBCQueryITSupport.duration(start, start, Step.MINUTE)); + } + + private static EventQueryCondition condition(final Duration time) { + final EventQueryCondition condition = new EventQueryCondition(); + condition.setTime(time); + condition.setOrder(Order.DES); + condition.setPaging(new Pagination(1, 10)); + return condition; + } + + private static List uuids(final Events events) { + return events.getEvents() + .stream() + .map(event -> event.getUuid()) + .collect(Collectors.toList()); + } + + private static Event event(final String uuid, final long startTime, final Layer layer) { + final Event event = new Event(); + event.setUuid(uuid); + event.setService(SERVICE); + event.setServiceInstance(SERVICE + "-1"); + event.setEndpoint("POST /orders"); + event.setName(layer == Layer.MESH ? "Reboot" : "Upgrade"); + event.setType(EventType.Normal.name()); + event.setMessage("event " + uuid); + event.setParameters("{}"); + event.setStartTime(startTime); + event.setEndTime(startTime + 5_000L); + event.setLayer(layer); + event.setTimestamp(startTime); + event.setTimeBucket(TimeBucket.getRecordTimeBucket(startTime)); + return event; + } + + private static BrowserErrorLogRecord browserError(final String uniqueId, final long time) { + final String version = IDManager.ServiceInstanceID.buildId(BROWSER_SERVICE_ID, "v1.0.0"); + final BrowserErrorLogRecord record = new BrowserErrorLogRecord(); + record.setUniqueId(uniqueId); + record.setServiceId(BROWSER_SERVICE_ID); + record.setServiceVersionId(version); + record.setPagePathId(IDManager.EndpointID.buildId(BROWSER_SERVICE_ID, "/cart")); + record.setErrorCategory(BrowserErrorCategory.JS.getValue()); + record.setTimestamp(time); + record.setTimeBucket(TimeBucket.getRecordTimeBucket(time)); + record.setDataBinary(BrowserErrorLog.newBuilder() + .setUniqueId(uniqueId) + .setService(BROWSER_SERVICE) + .setServiceVersion("v1.0.0") + .setPagePath("/cart") + .setCategory(ErrorCategory.js) + .setMessage(uniqueId) + .setTime(time) + .build() + .toByteArray()); + 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/JDBCLogQueryMariaDBIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCLogQueryMariaDBIT.java new file mode 100644 index 000000000000..309fc3fd90fe --- /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/JDBCLogQueryMariaDBIT.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 JDBCLogQueryMariaDBIT extends JDBCLogQueryITBase { + @Container + private static final MariaDBContainer SERVER = JDBCQueryITSupport.mariaDB(); + + public JDBCLogQueryMariaDBIT() { + 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/JDBCLogQueryPostgreSQLIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCLogQueryPostgreSQLIT.java new file mode 100644 index 000000000000..25e802b300a9 --- /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/JDBCLogQueryPostgreSQLIT.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 JDBCLogQueryPostgreSQLIT extends JDBCLogQueryITBase { + @Container + private static final PostgreSQLContainer SERVER = JDBCQueryITSupport.postgreSQL(); + + public JDBCLogQueryPostgreSQLIT() { + 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/JDBCManagementITBase.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCManagementITBase.java new file mode 100644 index 000000000000..ebd59483829f --- /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/JDBCManagementITBase.java @@ -0,0 +1,256 @@ +/* + * 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.Arrays; +import java.util.Collections; +import java.util.List; +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.relation.instance.ServiceInstanceRelationServerSideMetrics; +import org.apache.skywalking.oap.server.core.analysis.manual.relation.service.ServiceRelationClientSideMetrics; +import org.apache.skywalking.oap.server.core.analysis.manual.relation.service.ServiceRelationServerSideMetrics; +import org.apache.skywalking.oap.server.core.analysis.metrics.IntList; +import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics; +import org.apache.skywalking.oap.server.core.management.ui.template.UITemplate; +import org.apache.skywalking.oap.server.core.query.input.DashboardSetting; +import org.apache.skywalking.oap.server.core.query.type.DashboardConfiguration; +import org.apache.skywalking.oap.server.core.query.type.TemplateChangeStatus; +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.model.Model; +import org.apache.skywalking.oap.server.core.storage.model.StorageManipulationOpt; +import org.apache.skywalking.oap.server.core.storage.type.StorageBuilder; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.TableHelper; +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.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +/** + * Manages UI templates the way the UI management API does, persists management rows and updates metrics the way + * OAP's workers do, and inspects installed tables the way the boot and the runtime-rule reconciler do. A subclass + * names the server. + */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +abstract class JDBCManagementITBase { + private static final String GENERAL_SERVICE = "general-service"; + private static final String MESH_SERVICE = "mesh-service"; + private static final String K8S_SERVICE = "k8s-service"; + private static final String CHECKOUT_ID = IDManager.ServiceID.buildId("checkout", true); + private static final String PAYMENT_ID = IDManager.ServiceID.buildId("payment", true); + private static final String INVENTORY_ID = IDManager.ServiceID.buildId("inventory", true); + /** Component ids of the Spring MVC and Feign plugins. */ + private static final int SPRING_MVC = 14; + private static final int FEIGN = 11; + + private final JdbcDatabaseContainer server; + private final JDBCQueryITSupport.Target target; + private JDBCQueryITSupport support; + private Model templateModel; + private Model relationModel; + private JDBCUITemplateManagementDAO templateDAO; + + JDBCManagementITBase(final JdbcDatabaseContainer server, final JDBCQueryITSupport.Target target) { + this.server = server; + this.target = target; + } + + @BeforeAll + public void writeTemplatesAndRelation() throws Exception { + support = JDBCQueryITSupport.connect(server, target); + templateModel = support.install( + UITemplate.class, DefaultScopeDefine.UI_TEMPLATE, UITemplate.INDEX_NAME, DownSampling.None); + templateDAO = new JDBCUITemplateManagementDAO(support.getClient(), support.getTableHelper()); + for (final String id : Arrays.asList(GENERAL_SERVICE, MESH_SERVICE, K8S_SERVICE)) { + assertTrue(templateDAO.addTemplate(setting(id, configuration(id, 1))).isStatus(), id); + } + + relationModel = support.install( + ServiceRelationServerSideMetrics.class, DefaultScopeDefine.SERVICE_RELATION, + ServiceRelationServerSideMetrics.INDEX_NAME, DownSampling.Minute); + support.writeMetrics( + relationModel, new ServiceRelationServerSideMetrics.Builder(), + Collections.singletonList(relation(CHECKOUT_ID, PAYMENT_ID, SPRING_MVC))); + } + + @AfterAll + public void close() { + if (support != null) { + support.close(); + } + } + + @Test + public void disablesAnAddedTemplate() throws Exception { + final TemplateChangeStatus disabled = templateDAO.disableTemplate(GENERAL_SERVICE); + assertTrue(disabled.isStatus(), disabled.getMessage()); + assertFalse(templateIds(false).contains(GENERAL_SERVICE)); + assertTrue(templateIds(true).contains(GENERAL_SERVICE)); + assertTrue(templateDAO.getTemplate(GENERAL_SERVICE).isDisabled()); + + final TemplateChangeStatus disabledAgain = templateDAO.disableTemplate(GENERAL_SERVICE); + assertTrue(disabledAgain.isStatus(), disabledAgain.getMessage()); + } + + /** + * A change of a template never added is refused. A change replaces the whole template, so it also enables a + * disabled one, the only way the API has to. + */ + @Test + public void changesAnExistingTemplateOnly() throws Exception { + final TemplateChangeStatus missing = + templateDAO.changeTemplate(setting("not-added", configuration("not-added", 1))); + assertAll( + () -> assertFalse(missing.isStatus()), + () -> assertEquals("not-added", missing.getId()), + () -> assertEquals("Can't find the template", missing.getMessage()), + () -> assertNull(templateDAO.getTemplate("not-added")) + ); + + assertTrue(templateDAO.changeTemplate(setting(MESH_SERVICE, configuration(MESH_SERVICE, 2))).isStatus()); + assertEquals(configuration(MESH_SERVICE, 2), templateDAO.getTemplate(MESH_SERVICE).getConfiguration()); + + assertTrue(templateDAO.disableTemplate(K8S_SERVICE).isStatus()); + assertTrue(templateDAO.changeTemplate(setting(K8S_SERVICE, configuration(K8S_SERVICE, 2))).isStatus()); + final DashboardConfiguration changed = templateDAO.getTemplate(K8S_SERVICE); + assertFalse(changed.isDisabled()); + assertEquals(configuration(K8S_SERVICE, 2), changed.getConfiguration()); + } + + /** + * An update that finds no row, as for a metric the session cache holds but the table lost, fails, so the cache + * drops the metric and the next flush inserts it again; an update of a written row does not. + */ + @Test + @SuppressWarnings({"unchecked", "rawtypes"}) + public void failsAnUpdateOfAMissingRowOnly() throws Exception { + final JDBCMetricsDAO dao = new JDBCMetricsDAO( + support.getClient(), + (StorageBuilder) (StorageBuilder) new ServiceRelationServerSideMetrics.Builder()); + final ServiceRelationServerSideMetrics written = relation(CHECKOUT_ID, PAYMENT_ID, SPRING_MVC, FEIGN); + final ServiceRelationServerSideMetrics missing = relation(CHECKOUT_ID, INVENTORY_ID, FEIGN); + final SessionCacheCallback writtenCallback = mock(SessionCacheCallback.class); + final SessionCacheCallback missingCallback = mock(SessionCacheCallback.class); + + support.flush(Arrays.asList( + dao.prepareBatchUpdate(relationModel, written, writtenCallback), + dao.prepareBatchUpdate(relationModel, missing, missingCallback) + )); + + verify(missingCallback).onUpdateFailure(); + verify(writtenCallback, never()).onUpdateFailure(); + final List stored = dao.multiGet(relationModel, Arrays.asList(written, missing)); + assertEquals(1, stored.size()); + assertEquals( + written.getComponentIds().toStorageData(), + ((ServiceRelationServerSideMetrics) stored.get(0)).getComponentIds().toStorageData()); + } + + @Test + public void reportsTheTableAndColumnsOfAnInstalledModel() throws Exception { + for (final Model model : Arrays.asList(templateModel, relationModel)) { + final var info = support.installer().isExists(model, StorageManipulationOpt.withSchemaChange()); + assertTrue(info.isAllExist(), model.getName()); + assertTrue( + info.buildInstallInfoMsg().contains("allResourcesExist=true [tableExist=true, allColumnsExist=true]"), + info.buildInstallInfoMsg()); + } + } + + @Test + public void reportsAModelWithoutATable() throws Exception { + final Model model = support.register( + ServiceInstanceRelationServerSideMetrics.class, DefaultScopeDefine.SERVICE_INSTANCE_RELATION, + ServiceInstanceRelationServerSideMetrics.INDEX_NAME, DownSampling.Minute); + final var info = support.installer().isExists(model, StorageManipulationOpt.withSchemaChange()); + assertFalse(info.isAllExist()); + assertTrue( + info.buildInstallInfoMsg().contains("allResourcesExist=false [tableExist=false, allColumnsExist=false]"), + info.buildInstallInfoMsg()); + } + + /** + * The table an older OAP installed before the model gained a column. + */ + @Test + public void reportsATableMissingAColumn() throws Exception { + final Model model = support.install( + ServiceRelationClientSideMetrics.class, DefaultScopeDefine.SERVICE_RELATION, + ServiceRelationClientSideMetrics.INDEX_NAME, DownSampling.Minute); + support.getClient().execute( + "ALTER TABLE " + TableHelper.getLatestTableForWrite(model) + " DROP COLUMN " + + ServiceRelationClientSideMetrics.COMPONENT_IDS); + final var info = support.installer().isExists(model, StorageManipulationOpt.withSchemaChange()); + assertFalse(info.isAllExist()); + assertTrue( + info.buildInstallInfoMsg().contains("allResourcesExist=false [tableExist=true, allColumnsExist=false]"), + info.buildInstallInfoMsg()); + } + + private List templateIds(final boolean includingDisabled) { + return templateDAO.getAllTemplates(includingDisabled) + .stream() + .map(DashboardConfiguration::getId) + .collect(Collectors.toList()); + } + + private static DashboardSetting setting(final String id, final String configuration) { + final DashboardSetting setting = new DashboardSetting(); + setting.setId(id); + setting.setConfiguration(configuration); + return setting; + } + + private static String configuration(final String name, final int version) { + return "{\"name\":\"" + name + "\",\"version\":" + version + ",\"children\":[]}"; + } + + /** + * The relation of the first minute after midnight, as the topology analysis aggregates it. + */ + private static ServiceRelationServerSideMetrics relation(final String sourceServiceId, final String destServiceId, + final int... componentIds) { + final ServiceRelationServerSideMetrics relation = new ServiceRelationServerSideMetrics(); + relation.setSourceServiceId(sourceServiceId); + relation.setDestServiceId(destServiceId); + relation.setEntityId(IDManager.ServiceID.buildRelationId( + new IDManager.ServiceID.ServiceRelationDefine(sourceServiceId, destServiceId))); + final IntList components = new IntList(componentIds.length); + for (final int componentId : componentIds) { + components.add(componentId); + } + relation.setComponentIds(components); + relation.setTimeBucket(TimeBucket.getMinuteTimeBucket(MIDNIGHT)); + return relation; + } +} 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/JDBCManagementMariaDBIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCManagementMariaDBIT.java new file mode 100644 index 000000000000..a6f7a7bf85d6 --- /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/JDBCManagementMariaDBIT.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 JDBCManagementMariaDBIT extends JDBCManagementITBase { + @Container + private static final MariaDBContainer SERVER = JDBCQueryITSupport.mariaDB(); + + public JDBCManagementMariaDBIT() { + 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/JDBCManagementPostgreSQLIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCManagementPostgreSQLIT.java new file mode 100644 index 000000000000..d36dad84bd85 --- /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/JDBCManagementPostgreSQLIT.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 JDBCManagementPostgreSQLIT extends JDBCManagementITBase { + @Container + private static final PostgreSQLContainer SERVER = JDBCQueryITSupport.postgreSQL(); + + public JDBCManagementPostgreSQLIT() { + 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/JDBCMetadataQueryITBase.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetadataQueryITBase.java new file mode 100644 index 000000000000..475360a0b2d3 --- /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/JDBCMetadataQueryITBase.java @@ -0,0 +1,239 @@ +/* + * 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 com.google.gson.JsonObject; +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.Const; +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.instance.InstanceTraffic; +import org.apache.skywalking.oap.server.core.analysis.manual.process.ProcessDetectType; +import org.apache.skywalking.oap.server.core.analysis.manual.process.ProcessTraffic; +import org.apache.skywalking.oap.server.core.analysis.manual.searchtag.TagAutocompleteData; +import org.apache.skywalking.oap.server.core.analysis.manual.searchtag.TagType; +import org.apache.skywalking.oap.server.core.query.enumeration.ProfilingSupportStatus; +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.Process; +import org.apache.skywalking.oap.server.core.query.type.ServiceInstance; +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; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Reads instances, the processes an eBPF agent reports and searchable tags the way the metadata query, the eBPF + * receiver and tag autocompletion do, on traffic first stored on both sides of midnight, so in two day tables: a + * traffic row stays in the table of the day it was first stored, however long it keeps pinging. A subclass names the + * server. + */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +abstract class JDBCMetadataQueryITBase { + private static final int METADATA_QUERY_MAX_SIZE = 5000; + private static final String SERVICE_ID = IDManager.ServiceID.buildId("checkout", true); + private static final String INSTANCE_FIRST_SEEN_YESTERDAY = "checkout-1"; + private static final String INSTANCE_FIRST_SEEN_YESTERDAY_ID = + IDManager.ServiceInstanceID.buildId(SERVICE_ID, INSTANCE_FIRST_SEEN_YESTERDAY); + private static final String INSTANCE_FIRST_SEEN_TODAY = "checkout-2"; + private static final String INSTANCE_FIRST_SEEN_TODAY_ID = + IDManager.ServiceInstanceID.buildId(SERVICE_ID, INSTANCE_FIRST_SEEN_TODAY); + private static final String AGENT_ID = "rover-node-1"; + /** Searchable trace tags first seen yesterday, as key and value. */ + private static final String[][] TAGS_YESTERDAY = { + {"http.method", "GET"}, + {"http.status_code", "200"}, + {"http.status_code", "404"}, + {"db.type", "MySQL"}, + {"db.instance", "orders"}, + }; + /** Searchable trace tags first seen today, as key and value. */ + private static final String[][] TAGS_TODAY = { + {"rpc.status_code", "OK"}, + {"mq.queue", "orders"}, + {"mq.topic", "payments"}, + {"mq.broker", "kafka:9092"}, + {"http.status_code", "500"}, + {"http.status_code", "503"}, + }; + + private final JdbcDatabaseContainer server; + private final JDBCQueryITSupport.Target target; + private JDBCQueryITSupport support; + private JDBCMetadataQueryDAO metadataDAO; + private JDBCTagAutoCompleteQueryDAO tagDAO; + private long now; + private Duration aroundMidnight; + + JDBCMetadataQueryITBase(final JdbcDatabaseContainer server, final JDBCQueryITSupport.Target target) { + this.server = server; + this.target = target; + } + + @BeforeAll + public void writeTraffic() throws Exception { + support = JDBCQueryITSupport.connect(server, target); + // The fixtures' "now", anchored to the captured midnight, not the clock, so a run that crosses midnight + // writes its rows into the day tables its reads look at. + now = MIDNIGHT + 20 * 60_000L; + final long yesterday = MIDNIGHT - 2 * 3_600_000L; + + support.writeMetrics( + support.install(InstanceTraffic.class, DefaultScopeDefine.SERVICE_INSTANCE, InstanceTraffic.INDEX_NAME, + DownSampling.Minute), + new InstanceTraffic.Builder(), + Arrays.asList( + instance(INSTANCE_FIRST_SEEN_YESTERDAY, yesterday, now), + instance(INSTANCE_FIRST_SEEN_TODAY, now, now))); + + support.writeMetrics( + support.install(ProcessTraffic.class, DefaultScopeDefine.PROCESS, ProcessTraffic.INDEX_NAME, + DownSampling.Minute), + new ProcessTraffic.Builder(), + Arrays.asList( + process(INSTANCE_FIRST_SEEN_YESTERDAY_ID, "java-app", yesterday, now), + process(INSTANCE_FIRST_SEEN_TODAY_ID, "envoy", now, now), + process(INSTANCE_FIRST_SEEN_YESTERDAY_ID, "batch-job", yesterday, now - 30 * 60_000L))); + + final List tags = new ArrayList<>(); + for (final String[] tag : TAGS_YESTERDAY) { + tags.add(tag(tag, MIDNIGHT - 30 * 60_000L)); + } + for (final String[] tag : TAGS_TODAY) { + tags.add(tag(tag, now)); + } + support.writeMetrics( + support.install(TagAutocompleteData.class, DefaultScopeDefine.TAG_AUTOCOMPLETE, + TagAutocompleteData.INDEX_NAME, DownSampling.Minute), + new TagAutocompleteData.Builder(), tags); + + metadataDAO = new JDBCMetadataQueryDAO(support.getClient(), METADATA_QUERY_MAX_SIZE, support.getModuleManager()); + tagDAO = new JDBCTagAutoCompleteQueryDAO(support.getClient(), support.getTableHelper()); + aroundMidnight = JDBCQueryITSupport.duration(MIDNIGHT - 30 * 60_000L, MIDNIGHT + 30 * 60_000L, Step.MINUTE); + } + + @AfterAll + public void close() { + if (support != null) { + support.close(); + } + } + + @Test + public void readsTheInstancesOfEveryDayTable() throws Exception { + assertEquals(new TreeSet<>(Arrays.asList(INSTANCE_FIRST_SEEN_YESTERDAY, INSTANCE_FIRST_SEEN_TODAY)), + instanceNames(INSTANCE_FIRST_SEEN_YESTERDAY_ID, INSTANCE_FIRST_SEEN_TODAY_ID)); + assertEquals(Collections.singleton(INSTANCE_FIRST_SEEN_YESTERDAY), + instanceNames(INSTANCE_FIRST_SEEN_YESTERDAY_ID)); + } + + /** + * The eBPF receiver asks for the processes of an agent that pinged in the last five minutes; batch-job stopped + * pinging half an hour ago. + */ + @Test + public void listsTheProcessesOfAnAgentThatPingedInTheRange() throws Exception { + final List processes = metadataDAO.listProcesses( + AGENT_ID, TimeBucket.getMinuteTimeBucket(now - 5 * 60_000L), TimeBucket.getMinuteTimeBucket(now)); + assertEquals(new TreeSet<>(Arrays.asList("envoy", "java-app")), + processes.stream().map(Process::getName).collect(Collectors.toCollection(TreeSet::new))); + } + + @Test + public void limitsTheTagKeysOfBothDays() throws Exception { + final Set keys = new TreeSet<>(); + for (final String[] tag : TAGS_YESTERDAY) { + keys.add(tag[0]); + } + for (final String[] tag : TAGS_TODAY) { + keys.add(tag[0]); + } + assertEquals(keys, tagDAO.queryTagAutocompleteKeys(TagType.TRACE, 100, aroundMidnight)); + + final Set limited = tagDAO.queryTagAutocompleteKeys(TagType.TRACE, 5, aroundMidnight); + assertEquals(5, limited.size()); + assertTrue(keys.containsAll(limited)); + } + + @Test + public void limitsTheTagValuesOfBothDays() throws Exception { + final Set values = new TreeSet<>(Arrays.asList("200", "404", "500", "503")); + assertEquals(values, tagDAO.queryTagAutocompleteValues(TagType.TRACE, "http.status_code", 100, aroundMidnight)); + + final Set limited = tagDAO.queryTagAutocompleteValues(TagType.TRACE, "http.status_code", 3, aroundMidnight); + assertEquals(3, limited.size()); + assertTrue(values.containsAll(limited)); + } + + private Set instanceNames(final String... ids) throws Exception { + return metadataDAO.getInstances(Arrays.asList(ids)) + .stream() + .map(ServiceInstance::getName) + .collect(Collectors.toCollection(TreeSet::new)); + } + + private static InstanceTraffic instance(final String name, final long firstSeen, final long lastPing) { + final JsonObject properties = new JsonObject(); + properties.addProperty(InstanceTraffic.PropertyUtil.LANGUAGE, "java"); + final InstanceTraffic instance = new InstanceTraffic(); + instance.setServiceId(SERVICE_ID); + instance.setName(name); + instance.setProperties(properties); + instance.setTimeBucket(TimeBucket.getMinuteTimeBucket(firstSeen)); + instance.setLastPingTimestamp(TimeBucket.getMinuteTimeBucket(lastPing)); + return instance; + } + + private static ProcessTraffic process(final String instanceId, final String name, final long firstSeen, + final long lastPing) { + final ProcessTraffic process = new ProcessTraffic(); + process.setServiceId(SERVICE_ID); + process.setInstanceId(instanceId); + process.setName(name); + process.setAgentId(AGENT_ID); + process.setDetectType(ProcessDetectType.VM.value()); + process.setProfilingSupportStatus(ProfilingSupportStatus.SUPPORT_EBPF_PROFILING.value()); + process.setLabelsJson(Const.EMPTY_STRING); + process.setTimeBucket(TimeBucket.getMinuteTimeBucket(firstSeen)); + process.setLastPingTimestamp(TimeBucket.getMinuteTimeBucket(lastPing)); + return process; + } + + private static TagAutocompleteData tag(final String[] tag, final long firstSeen) { + final TagAutocompleteData data = new TagAutocompleteData(); + data.setTagKey(tag[0]); + data.setTagValue(tag[1]); + data.setTagType(TagType.TRACE.name()); + data.setTimeBucket(TimeBucket.retainToDay4MinuteBucket(TimeBucket.getMinuteTimeBucket(firstSeen))); + return data; + } +} 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/JDBCMetadataQueryMariaDBIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetadataQueryMariaDBIT.java new file mode 100644 index 000000000000..04c673d4913f --- /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/JDBCMetadataQueryMariaDBIT.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 JDBCMetadataQueryMariaDBIT extends JDBCMetadataQueryITBase { + @Container + private static final MariaDBContainer SERVER = JDBCQueryITSupport.mariaDB(); + + public JDBCMetadataQueryMariaDBIT() { + 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/JDBCMetadataQueryPostgreSQLIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetadataQueryPostgreSQLIT.java new file mode 100644 index 000000000000..b4a290342986 --- /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/JDBCMetadataQueryPostgreSQLIT.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 JDBCMetadataQueryPostgreSQLIT extends JDBCMetadataQueryITBase { + @Container + private static final PostgreSQLContainer SERVER = JDBCQueryITSupport.postgreSQL(); + + public JDBCMetadataQueryPostgreSQLIT() { + 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/JDBCMetricsQueryITBase.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetricsQueryITBase.java new file mode 100644 index 000000000000..b484bbed919d --- /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/JDBCMetricsQueryITBase.java @@ -0,0 +1,299 @@ +/* + * 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.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.Layer; +import org.apache.skywalking.oap.server.core.analysis.TimeBucket; +import org.apache.skywalking.oap.server.core.analysis.manual.database.TopNDatabaseStatement; +import org.apache.skywalking.oap.server.core.analysis.meter.MeterEntity; +import org.apache.skywalking.oap.server.core.analysis.meter.function.AcceptableValue; +import org.apache.skywalking.oap.server.core.analysis.meter.function.BucketedValues; +import org.apache.skywalking.oap.server.core.analysis.meter.function.avg.AvgFunction; +import org.apache.skywalking.oap.server.core.analysis.meter.function.avg.AvgHistogramFunction; +import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics; +import org.apache.skywalking.oap.server.core.analysis.topn.TopN; +import org.apache.skywalking.oap.server.core.config.NamingControl; +import org.apache.skywalking.oap.server.core.config.group.EndpointNameGrouping; +import org.apache.skywalking.oap.server.core.query.enumeration.Order; +import org.apache.skywalking.oap.server.core.query.enumeration.Scope; +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.input.Entity; +import org.apache.skywalking.oap.server.core.query.input.MetricsCondition; +import org.apache.skywalking.oap.server.core.query.input.RecordCondition; +import org.apache.skywalking.oap.server.core.query.input.TopNCondition; +import org.apache.skywalking.oap.server.core.query.type.HeatMap; +import org.apache.skywalking.oap.server.core.query.type.Record; +import org.apache.skywalking.oap.server.core.query.type.SelectedRecord; +import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine; +import org.apache.skywalking.oap.server.core.storage.annotation.ValueColumnMetadata; +import org.apache.skywalking.oap.server.core.storage.model.Model; +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 metrics the way the metrics query service, the top-N ranking and the top-N records do, over ranges that + * cross midnight, so over two day tables: a heat map of a MAL histogram, MAL {@code vm.yaml} metrics ranked across + * the hosts, and the slow statements of a database. A subclass names the server. + */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +abstract class JDBCMetricsQueryITBase { + private static final String LATENCY_HISTOGRAM = "meter_checkout_latency_histogram"; + private static final String CPU = "meter_vm_cpu_total_percentage"; + private static final String MEMORY = "meter_vm_memory_used"; + private static final String SERVICE = "checkout"; + private static final long[] HISTOGRAM_BUCKETS = {0, 50, 100}; + /** Minute from midnight, then the count of each bucket. Minute -1 has no histogram. */ + private static final long[][] HISTOGRAMS = { + {-2, 3, 5, 1}, + {0, 4, 6, 2}, + {1, 5, 7, 3}, + }; + /** + * Host, then the CPU percentage of each minute from -3 to 2. web-1 is steady at 50; batch-1 is busy until + * midnight and backup-1 from midnight, each with one quiet minute on the other day, so each tops one day and + * the whole range averages them to 46 and 47. A missing minute is -1. + */ + private static final Object[][] CPU_MINUTES = { + {"web-1", new long[] {-1, -1, 50, 50, 50, 50}}, + {"batch-1", new long[] {60, 60, 60, 5, -1, -1}}, + {"backup-1", new long[] {-1, -1, 5, 62, 62, 62}}, + }; + private static final long GIB = 1L << 30; + /** Host, then the used memory in bytes of each minute from -2 to 0: db-1 averages 26 GiB, web-1 4 GiB. */ + private static final Object[][] MEMORY_MINUTES = { + {"db-1", new long[] {30 * GIB, 30 * GIB, 18 * GIB}}, + {"web-1", new long[] {-1, 3 * GIB, 5 * GIB}}, + }; + private static final String DATABASE = "orders-db:3306"; + /** Second from midnight, latency in ms. Three before midnight, three after. */ + private static final long[][] STATEMENTS = { + {-110, 1200}, + {-80, 300}, + {-40, 900}, + {15, 1500}, + {45, 100}, + {90, 700}, + }; + + private final JdbcDatabaseContainer server; + private final JDBCQueryITSupport.Target target; + private JDBCQueryITSupport support; + + JDBCMetricsQueryITBase(final JdbcDatabaseContainer server, final JDBCQueryITSupport.Target target) { + this.server = server; + this.target = target; + } + + @BeforeAll + public void writeMetrics() throws Exception { + support = JDBCQueryITSupport.connect(server, target); + MeterEntity.setNamingControl(new NamingControl(70, 100, 150, new EndpointNameGrouping())); + + final Model histogram = support.install( + HistogramMeter.class, DefaultScopeDefine.SERVICE, LATENCY_HISTOGRAM, DownSampling.Minute); + final List histograms = new ArrayList<>(); + for (final long[] point : HISTOGRAMS) { + final HistogramMeter meter = new HistogramMeter(); + meter.setTimeBucket(minuteBucket(point[0])); + meter.accept(MeterEntity.newService(SERVICE, Layer.GENERAL), + new BucketedValues(HISTOGRAM_BUCKETS, Arrays.copyOfRange(point, 1, point.length))); + meter.calculate(); + histograms.add(meter); + } + support.writeMetrics(histogram, new AvgHistogramFunction.AvgHistogramFunctionBuilder(), histograms); + + writeHostMetric(CPU, CPU_MINUTES, -3); + writeHostMetric(MEMORY, MEMORY_MINUTES, -2); + + final Model statementModel = support.install( + TopNDatabaseStatement.class, DefaultScopeDefine.DATABASE_SLOW_STATEMENT, TopNDatabaseStatement.INDEX_NAME, + DownSampling.Second); + final List statements = new ArrayList<>(); + for (int i = 0; i < STATEMENTS.length; i++) { + statements.add(statement(DATABASE, "segment-" + i, STATEMENTS[i][0], STATEMENTS[i][1])); + } + support.writeRecords(statementModel, new TopNDatabaseStatement.Builder(), statements); + } + + @AfterAll + public void close() { + if (support != null) { + support.close(); + } + } + + /** + * One column per minute of the range, in minute order, each from whichever day table holds it, and an empty + * column for the minute none holds. + */ + @Test + public void readsOneHeatMapColumnPerMinuteAcrossMidnight() throws Exception { + final HeatMap heatMap = support.metricsQueryDAO().readHeatMap( + serviceCondition(LATENCY_HISTOGRAM, SERVICE), ValueColumnMetadata.INSTANCE.getValueCName(LATENCY_HISTOGRAM), + minutes(-2, 1)); + assertEquals( + Arrays.asList( + Arrays.asList(3L, 5L, 1L), Arrays.asList(0L, 0L, 0L), Arrays.asList(4L, 6L, 2L), Arrays.asList(5L, 7L, 3L)), + heatMap.getValues().stream().map(HeatMap.HeatMapColumn::getValues).collect(Collectors.toList())); + } + + /** + * Each host's mean over the whole range, not the mean of its two daily means (32 for batch-1, 33 for backup-1). + */ + @Test + public void ranksTheMeanOfTheWholeRangeAcrossMidnight() throws Exception { + assertEquals(Arrays.asList("web-1=50", "backup-1=47", "batch-1=46"), top(CPU, 3, minutes(-3, 2))); + } + + /** + * web-1 tops neither day, as batch-1 and backup-1 each top one, yet tops the range. + */ + @Test + public void keepsTheTopOfTheRangeThatTopsNoDay() throws Exception { + assertEquals(Collections.singletonList("web-1=50"), top(CPU, 1, minutes(-3, 2))); + } + + @Test + public void ranksAMetricAboveTheIntRange() throws Exception { + assertEquals(Arrays.asList("db-1=" + 26 * GIB, "web-1=" + 4 * GIB), top(MEMORY, 2, minutes(-2, 0))); + // Within one day the ranking is done in SQL. + assertEquals(Arrays.asList("db-1=" + 18 * GIB, "web-1=" + 5 * GIB), top(MEMORY, 2, minutes(0, 0))); + } + + @Test + public void readsTheSlowestStatementsAcrossMidnight() throws Exception { + assertEquals(Arrays.asList("1500", "1200", "900"), latencies(DATABASE, 3, Order.DES)); + } + + @Test + public void readsTheFastestStatementsAcrossMidnight() throws Exception { + assertEquals(Arrays.asList("100", "300"), latencies(DATABASE, 2, Order.ASC)); + } + + private void writeHostMetric(final String name, final Object[][] hosts, final long firstMinute) throws Exception { + final Model model = support.install(AvgMeter.class, DefaultScopeDefine.SERVICE, name, DownSampling.Minute); + final List metrics = new ArrayList<>(); + for (final Object[] host : hosts) { + final long[] values = (long[]) host[1]; + for (int i = 0; i < values.length; i++) { + if (values[i] < 0) { + continue; + } + final AvgMeter meter = new AvgMeter(); + meter.setTimeBucket(minuteBucket(firstMinute + i)); + meter.accept(MeterEntity.newService((String) host[0], Layer.OS_LINUX), values[i]); + meter.calculate(); + metrics.add(meter); + } + } + support.writeMetrics(model, new AvgFunction.AvgStorageBuilder(), metrics); + } + + /** The ranked hosts as {@code host=value}. */ + private List top(final String metric, final int topN, final Duration duration) throws Exception { + final TopNCondition condition = new TopNCondition(); + condition.setName(metric); + condition.setScope(Scope.Service); + condition.setTopN(topN); + condition.setOrder(Order.DES); + final List ranked = support.aggregationQueryDAO().sortMetrics( + condition, ValueColumnMetadata.INSTANCE.getValueCName(metric), duration, null); + return ranked.stream() + .map(record -> IDManager.ServiceID.analysisId(record.getId()).getName() + "=" + record.getValue()) + .collect(Collectors.toList()); + } + + private List latencies(final String database, final int topN, final Order order) throws Exception { + final Entity entity = new Entity(); + entity.setScope(Scope.Service); + entity.setServiceName(database); + entity.setNormal(false); + final RecordCondition condition = new RecordCondition(); + condition.setName(TopNDatabaseStatement.INDEX_NAME); + condition.setParentEntity(entity); + condition.setTopN(topN); + condition.setOrder(order); + final List records = new JDBCRecordsQueryDAO(support.getClient(), support.getTableHelper()) + .readRecords(condition, TopN.LATENCY, minutes(-2, 1)); + return records.stream().map(Record::getValue).collect(Collectors.toList()); + } + + private static MetricsCondition serviceCondition(final String metric, final String service) { + final Entity entity = new Entity(); + entity.setScope(Scope.Service); + entity.setServiceName(service); + entity.setNormal(true); + final MetricsCondition condition = new MetricsCondition(); + condition.setName(metric); + condition.setEntity(entity); + return condition; + } + + private static Duration minutes(final long first, final long last) { + return JDBCQueryITSupport.duration(MIDNIGHT + first * 60_000L, MIDNIGHT + last * 60_000L, Step.MINUTE); + } + + private static long minuteBucket(final long minute) { + return TimeBucket.getMinuteTimeBucket(MIDNIGHT + minute * 60_000L); + } + + private static TopNDatabaseStatement statement(final String database, final String segmentId, + final long second, final long latency) { + final long timestamp = MIDNIGHT + second * 1000L; + final TopNDatabaseStatement statement = new TopNDatabaseStatement(); + statement.setId(segmentId + "-1"); + statement.setEntityId(IDManager.ServiceID.buildId(database, false)); + statement.setStatement("select * from orders where customer_id = ?"); + statement.setLatency(latency); + statement.setTraceId("trace-" + segmentId); + statement.setTimestamp(timestamp); + statement.setTimeBucket(TimeBucket.getRecordTimeBucket(timestamp)); + return statement; + } + + /** A MAL histogram meter: the class MAL generates adds nothing to its function but {@code createNew}. */ + static class HistogramMeter extends AvgHistogramFunction { + @Override + public AcceptableValue createNew() { + return new HistogramMeter(); + } + } + + /** A MAL meter of the default {@code avg} function. */ + static class AvgMeter extends AvgFunction { + @Override + public AcceptableValue createNew() { + return new AvgMeter(); + } + } +} 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/JDBCMetricsQueryMariaDBIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetricsQueryMariaDBIT.java new file mode 100644 index 000000000000..8a2608cad010 --- /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/JDBCMetricsQueryMariaDBIT.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 JDBCMetricsQueryMariaDBIT extends JDBCMetricsQueryITBase { + @Container + private static final MariaDBContainer SERVER = JDBCQueryITSupport.mariaDB(); + + public JDBCMetricsQueryMariaDBIT() { + 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/JDBCMetricsQueryPostgreSQLIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCMetricsQueryPostgreSQLIT.java new file mode 100644 index 000000000000..9f0420632397 --- /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/JDBCMetricsQueryPostgreSQLIT.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 JDBCMetricsQueryPostgreSQLIT extends JDBCMetricsQueryITBase { + @Container + private static final PostgreSQLContainer SERVER = JDBCQueryITSupport.postgreSQL(); + + public JDBCMetricsQueryPostgreSQLIT() { + 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/JDBCProfilingDataQueryITBase.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfilingDataQueryITBase.java new file mode 100644 index 000000000000..36a330221799 --- /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/JDBCProfilingDataQueryITBase.java @@ -0,0 +1,232 @@ +/* + * 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.Comparator; +import java.util.List; +import java.util.stream.Collectors; +import org.apache.skywalking.oap.server.core.Const; +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.profiling.ebpf.storage.EBPFProfilingDataRecord; +import org.apache.skywalking.oap.server.core.profiling.ebpf.storage.EBPFProfilingTargetType; +import org.apache.skywalking.oap.server.core.profiling.trace.ProfileLanguageType; +import org.apache.skywalking.oap.server.core.profiling.trace.ProfileThreadSnapshotRecord; +import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine; +import org.apache.skywalking.oap.server.core.storage.model.Model; +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 trace-profiling thread snapshots the way the profile analysis does, and eBPF profiling data the way the eBPF + * analysis does, on data dumped and uploaded on both sides of midnight, so in two day tables. A subclass names the + * server. + */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +abstract class JDBCProfilingDataQueryITBase { + private static final String JAVA_TASK = + (MIDNIGHT - 600_000L) + Const.ID_CONNECTOR + IDManager.ServiceID.buildId("checkout", true); + private static final String GO_TASK = + (MIDNIGHT - 300_000L) + Const.ID_CONNECTOR + IDManager.ServiceID.buildId("payment", true); + private static final String JAVA_SEGMENT = "segment-java"; + /** The Java agent dumps the thread of a profiled segment every dump period, the first three before midnight. */ + private static final int JAVA_SNAPSHOTS = 6; + private static final long DUMP_PERIOD = 10L; + /** The Go agent reports one snapshot per segment, at the time of its profile. */ + private static final String GO_SEGMENT_YESTERDAY = "segment-go-1"; + private static final String GO_SEGMENT_TODAY = "segment-go-2"; + private static final String SCHEDULE = "schedule-checkout"; + + private final JdbcDatabaseContainer server; + private final JDBCQueryITSupport.Target target; + private JDBCQueryITSupport support; + private JDBCProfileThreadSnapshotQueryDAO snapshotDAO; + private ProfileThreadSnapshotRecord goSnapshotToday; + + JDBCProfilingDataQueryITBase(final JdbcDatabaseContainer server, final JDBCQueryITSupport.Target target) { + this.server = server; + this.target = target; + } + + @BeforeAll + public void writeData() throws Exception { + support = JDBCQueryITSupport.connect(server, target); + final Model snapshots = support.install( + ProfileThreadSnapshotRecord.class, DefaultScopeDefine.PROFILE_TASK_SEGMENT_SNAPSHOT, + ProfileThreadSnapshotRecord.INDEX_NAME, DownSampling.Second); + final List written = new ArrayList<>(); + for (int sequence = 0; sequence < JAVA_SNAPSHOTS; sequence++) { + written.add(javaSnapshot(sequence)); + } + goSnapshotToday = goSnapshot(GO_SEGMENT_TODAY, MIDNIGHT + 5_000L); + written.add(goSnapshot(GO_SEGMENT_YESTERDAY, MIDNIGHT - 5_000L)); + written.add(goSnapshotToday); + support.writeRecords(snapshots, new ProfileThreadSnapshotRecord.Builder(), written); + snapshotDAO = new JDBCProfileThreadSnapshotQueryDAO(support.getClient(), support.getTableHelper()); + + final Model ebpfData = support.install( + EBPFProfilingDataRecord.class, DefaultScopeDefine.EBPF_PROFILING_DATA, EBPFProfilingDataRecord.INDEX_NAME, + DownSampling.Second); + support.writeRecords(ebpfData, new EBPFProfilingDataRecord.Builder(), Arrays.asList( + ebpfData("stack-1", MIDNIGHT - 3_000L), ebpfData("stack-2", MIDNIGHT + 3_000L))); + } + + @AfterAll + public void close() { + if (support != null) { + support.close(); + } + } + + /** + * The analysis of a span reads the sequences dumped within the span's time. The segment has snapshots in both + * day tables, but none of the window in one of them. + */ + @Test + public void findsTheSequenceRangeOfAWindowOnEachSideOfMidnight() throws Exception { + assertEquals(Arrays.asList(1, 2), sequenceRange(JAVA_SEGMENT, dumpTime(1), dumpTime(2))); + assertEquals(Arrays.asList(3, 5), sequenceRange(JAVA_SEGMENT, dumpTime(3), dumpTime(5))); + } + + @Test + public void findsNoSequenceInAWindowWithoutSnapshots() throws Exception { + final long afterLastDump = dumpTime(JAVA_SNAPSHOTS - 1) + 1; + assertEquals(Arrays.asList(-1, -1), sequenceRange(JAVA_SEGMENT, afterLastDump, afterLastDump + 1_000L)); + } + + /** + * The profiled segments of a task, each with the language of its agent. + */ + @Test + public void readsTheLanguageOfTheProfiledSegmentsOfATask() throws Exception { + assertEquals(Collections.singletonList(JAVA_SEGMENT + "|JAVA"), + segmentLanguages(snapshotDAO.queryRecords(JAVA_TASK))); + assertEquals(Arrays.asList(GO_SEGMENT_YESTERDAY + "|GO", GO_SEGMENT_TODAY + "|GO"), + segmentLanguages(snapshotDAO.queryRecords(GO_TASK))); + } + + /** + * The analysis falls back to the Go profile of a segment, which it keeps only when the snapshot reads as Go. + */ + @Test + public void readsTheSnapshotsOfASegmentWhole() throws Exception { + final List java = new ArrayList<>(); + for (int sequence = 0; sequence < JAVA_SNAPSHOTS; sequence++) { + java.add(describe(javaSnapshot(sequence))); + } + assertEquals(java, describe(snapshotDAO.queryRecords(JAVA_SEGMENT, 0, JAVA_SNAPSHOTS))); + assertEquals(Collections.singletonList(describe(goSnapshotToday)), + describe(snapshotDAO.queryRecords(GO_SEGMENT_TODAY, 0, 1))); + } + + /** + * An eBPF analysis asked for no schedule reads nothing. + */ + @Test + public void readsNoEBPFProfilingDataForNoSchedule() throws Exception { + final JDBCEBPFProfilingDataDAO dao = new JDBCEBPFProfilingDataDAO(support.getClient(), support.getTableHelper()); + final long begin = MIDNIGHT - 10_000L; + final long end = MIDNIGHT + 10_000L; + assertEquals(Arrays.asList("stack-1", "stack-2"), dao.queryData(Collections.singletonList(SCHEDULE), begin, end) + .stream() + .map(EBPFProfilingDataRecord::getStackIdList) + .sorted() + .collect(Collectors.toList())); + assertEquals(Collections.emptyList(), dao.queryData(Collections.emptyList(), begin, end)); + } + + private List sequenceRange(final String segmentId, final long start, final long end) throws Exception { + return Arrays.asList(snapshotDAO.queryMinSequence(segmentId, start, end), + snapshotDAO.queryMaxSequence(segmentId, start, end)); + } + + private static List segmentLanguages(final List snapshots) { + return snapshots.stream() + .map(snapshot -> snapshot.getSegmentId() + "|" + snapshot.getLanguage()) + .sorted() + .collect(Collectors.toList()); + } + + private static List describe(final List snapshots) { + return snapshots.stream() + .sorted(Comparator.comparingInt(ProfileThreadSnapshotRecord::getSequence)) + .map(JDBCProfilingDataQueryITBase::describe) + .collect(Collectors.toList()); + } + + private static String describe(final ProfileThreadSnapshotRecord snapshot) { + return String.join("|", snapshot.getTaskId(), snapshot.getSegmentId(), String.valueOf(snapshot.getSequence()), + String.valueOf(snapshot.getDumpTime()), new String(snapshot.getStackBinary()), + snapshot.getLanguage().name()); + } + + private static long dumpTime(final int sequence) { + return MIDNIGHT + (sequence - JAVA_SNAPSHOTS / 2) * DUMP_PERIOD; + } + + private static ProfileThreadSnapshotRecord javaSnapshot(final int sequence) { + return snapshot(JAVA_TASK, JAVA_SEGMENT, sequence, dumpTime(sequence), ProfileLanguageType.JAVA); + } + + private static ProfileThreadSnapshotRecord goSnapshot(final String segmentId, final long dumpTime) { + return snapshot(GO_TASK, segmentId, 0, dumpTime, ProfileLanguageType.GO); + } + + /** + * A snapshot as the profile receiver writes it, the stack being the dumped thread stack of a Java agent or the + * segment's samples of a Go agent's pprof. + */ + private static ProfileThreadSnapshotRecord snapshot(final String taskId, final String segmentId, final int sequence, + final long dumpTime, final ProfileLanguageType language) { + final ProfileThreadSnapshotRecord snapshot = new ProfileThreadSnapshotRecord(); + snapshot.setTaskId(taskId); + snapshot.setSegmentId(segmentId); + snapshot.setDumpTime(dumpTime); + snapshot.setSequence(sequence); + snapshot.setStackBinary(("stack " + segmentId + " " + sequence).getBytes()); + snapshot.setLanguage(language); + snapshot.setTimeBucket(TimeBucket.getRecordTimeBucket(dumpTime)); + return snapshot; + } + + /** + * A stack as the eBPF profiling data dispatcher writes it. + */ + private static EBPFProfilingDataRecord ebpfData(final String stackIdList, final long uploadTime) { + final EBPFProfilingDataRecord data = new EBPFProfilingDataRecord(); + data.setScheduleId(SCHEDULE); + data.setTaskId("ebpf-task-checkout"); + data.setStackIdList(stackIdList); + data.setTargetType(EBPFProfilingTargetType.ON_CPU.value()); + data.setDataBinary(new byte[] {1}); + data.setUploadTime(uploadTime); + data.setTimeBucket(TimeBucket.getRecordTimeBucket(uploadTime)); + return data; + } +} 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/JDBCProfilingDataQueryMariaDBIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfilingDataQueryMariaDBIT.java new file mode 100644 index 000000000000..dc134d4e50dc --- /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/JDBCProfilingDataQueryMariaDBIT.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 JDBCProfilingDataQueryMariaDBIT extends JDBCProfilingDataQueryITBase { + @Container + private static final MariaDBContainer SERVER = JDBCQueryITSupport.mariaDB(); + + public JDBCProfilingDataQueryMariaDBIT() { + 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/JDBCProfilingDataQueryPostgreSQLIT.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfilingDataQueryPostgreSQLIT.java new file mode 100644 index 000000000000..847822a2d295 --- /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/JDBCProfilingDataQueryPostgreSQLIT.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 JDBCProfilingDataQueryPostgreSQLIT extends JDBCProfilingDataQueryITBase { + @Container + private static final PostgreSQLContainer SERVER = JDBCQueryITSupport.postgreSQL(); + + public JDBCProfilingDataQueryPostgreSQLIT() { + 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/JDBCProfilingTaskQueryITBase.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/test/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/common/dao/JDBCProfilingTaskQueryITBase.java new file mode 100644 index 000000000000..06811963fef6 --- /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/JDBCProfilingTaskQueryITBase.java @@ -0,0 +1,309 @@ +/* + * 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 com.google.gson.Gson; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.function.ToLongFunction; +import java.util.stream.Collectors; +import org.apache.skywalking.oap.server.core.Const; +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.profiling.asyncprofiler.storage.AsyncProfilerTaskRecord; +import org.apache.skywalking.oap.server.core.profiling.ebpf.storage.EBPFProfilingTargetType; +import org.apache.skywalking.oap.server.core.profiling.ebpf.storage.EBPFProfilingTaskRecord; +import org.apache.skywalking.oap.server.core.profiling.ebpf.storage.EBPFProfilingTriggerType; +import org.apache.skywalking.oap.server.core.profiling.pprof.storage.PprofTaskRecord; +import org.apache.skywalking.oap.server.core.profiling.trace.ProfileTaskRecord; +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.AsyncProfilerEventType; +import org.apache.skywalking.oap.server.core.query.type.AsyncProfilerTask; +import org.apache.skywalking.oap.server.core.query.type.EBPFProfilingTaskContinuousProfiling; +import org.apache.skywalking.oap.server.core.query.type.PprofEventType; +import org.apache.skywalking.oap.server.core.query.type.PprofTask; +import org.apache.skywalking.oap.server.core.query.type.ProfileTask; +import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine; +import org.apache.skywalking.oap.server.core.storage.model.Model; +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 profiling tasks the way the profiling services and the agent task caches do. OAP writes a task into the + * table of the day it creates it, so the tasks created before midnight are in yesterday's table and the others in + * today's. A subclass names the server. + */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +abstract class JDBCProfilingTaskQueryITBase { + 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 int TRACE_PROFILING_MINUTES = 10; + /** When each async-profiler and each pprof task was created, in minutes from midnight. */ + private static final int[] CREATED_MINUTES = {-30, -20, -10, 10, 20}; + private static final Gson GSON = new Gson(); + + private final JdbcDatabaseContainer server; + private final JDBCQueryITSupport.Target target; + private JDBCQueryITSupport support; + /** Created yesterday evening to start today at 10:00. */ + private ProfileTaskRecord scheduledForToday; + /** Created and started a minute after midnight. */ + private ProfileTaskRecord startedToday; + /** Created today to start tomorrow at 10:00. */ + private ProfileTaskRecord scheduledForTomorrow; + private JDBCProfileTaskQueryDAO traceProfilingDAO; + private Duration aroundMidnight; + + JDBCProfilingTaskQueryITBase(final JdbcDatabaseContainer server, final JDBCQueryITSupport.Target target) { + this.server = server; + this.target = target; + } + + @BeforeAll + public void writeTasks() throws Exception { + support = JDBCQueryITSupport.connect(server, target); + + final Model traceProfiling = support.install( + ProfileTaskRecord.class, DefaultScopeDefine.PROFILE_TASK, ProfileTaskRecord.INDEX_NAME, DownSampling.Minute); + scheduledForToday = traceProfilingTask( + "/checkout", MIDNIGHT - TimeUnit.HOURS.toMillis(2), MIDNIGHT + TimeUnit.HOURS.toMillis(10)); + startedToday = traceProfilingTask("/pay", MIDNIGHT + 60_000L, MIDNIGHT + 60_000L); + scheduledForTomorrow = traceProfilingTask( + "/checkout", MIDNIGHT + 120_000L, MIDNIGHT + TimeUnit.DAYS.toMillis(1) + TimeUnit.HOURS.toMillis(10)); + for (final ProfileTaskRecord task : Arrays.asList(scheduledForToday, startedToday, scheduledForTomorrow)) { + support.writeNoneStream(traceProfiling, new ProfileTaskRecord.Builder(), task, task.getCreateTime()); + } + traceProfilingDAO = new JDBCProfileTaskQueryDAO(support.getClient(), support.getModuleManager()); + + final Model asyncProfiler = support.install( + AsyncProfilerTaskRecord.class, DefaultScopeDefine.ASYNC_PROFILER_TASK, AsyncProfilerTaskRecord.INDEX_NAME, + DownSampling.Minute); + final Model pprof = support.install( + PprofTaskRecord.class, DefaultScopeDefine.PPROF_TASK, PprofTaskRecord.INDEX_NAME, DownSampling.Minute); + for (final int minute : CREATED_MINUTES) { + final long createTime = MIDNIGHT + minute * 60_000L; + support.writeNoneStream( + asyncProfiler, new AsyncProfilerTaskRecord.Builder(), asyncProfilerTask(createTime), createTime); + support.writeNoneStream(pprof, new PprofTaskRecord.Builder(), pprofTask(createTime), createTime); + } + + final Model ebpf = support.install( + EBPFProfilingTaskRecord.class, DefaultScopeDefine.EBPF_PROFILING_TASK, EBPFProfilingTaskRecord.INDEX_NAME, + DownSampling.Minute); + for (final EBPFProfilingTaskRecord ebpfTask : Arrays.asList( + continuousProfilingTask(EBPFProfilingTargetType.ON_CPU, MIDNIGHT + 30_000L), + continuousProfilingTask(EBPFProfilingTargetType.OFF_CPU, MIDNIGHT + 60_000L))) { + support.writeNoneStream(ebpf, new EBPFProfilingTaskRecord.Builder(), ebpfTask, ebpfTask.getCreateTime()); + } + + aroundMidnight = JDBCQueryITSupport.duration( + MIDNIGHT - TimeUnit.HOURS.toMillis(1), MIDNIGHT + TimeUnit.HOURS.toMillis(1), Step.MINUTE); + } + + @AfterAll + public void close() { + if (support != null) { + support.close(); + } + } + + /** + * The async-profiler task list of a time range, cut to a limit, is the newest tasks of both day tables ranked + * together. + */ + @Test + public void listsTheNewestAsyncProfilerTasksAcrossMidnight() throws Exception { + final JDBCAsyncProfilerTaskQueryDAO dao = + new JDBCAsyncProfilerTaskQueryDAO(support.getClient(), support.getTableHelper()); + final long start = aroundMidnight.getStartTimeBucketInSec(); + final long end = aroundMidnight.getEndTimeBucketInSec(); + assertEquals(Arrays.asList(20, 10), + createdMinutes(dao.getTaskList(SERVICE_ID, start, end, 2), AsyncProfilerTask::getCreateTime)); + assertEquals(Arrays.asList(20, 10, -10), + createdMinutes(dao.getTaskList(SERVICE_ID, start, end, 3), AsyncProfilerTask::getCreateTime)); + } + + @Test + public void listsTheNewestPprofTasksAcrossMidnight() throws Exception { + final JDBCPprofTaskQueryDAO dao = new JDBCPprofTaskQueryDAO(support.getClient(), support.getTableHelper()); + final long start = aroundMidnight.getStartTimeBucketInSec(); + final long end = aroundMidnight.getEndTimeBucketInSec(); + assertEquals(Arrays.asList(20, 10), + createdMinutes(dao.getTaskList(SERVICE_ID, start, end, 2), PprofTask::getCreateTime)); + assertEquals(Arrays.asList(20, 10, -10), + createdMinutes(dao.getTaskList(SERVICE_ID, start, end, 3), PprofTask::getCreateTime)); + } + + /** + * The task list of a service is latest start first across both day tables: the task created yesterday for today + * comes before the task started today, though it is in yesterday's table. + */ + @Test + public void listsTraceProfilingTasksLatestStartFirst() { + assertEquals(ids(scheduledForTomorrow, scheduledForToday, startedToday), + ids(traceProfilingDAO.getTaskList(SERVICE_ID, null, null, null, null))); + } + + /** + * Creating a task checks the latest-starting task that starts before the new one ends for an overlap. A new task + * five minutes into the one scheduled for today must find that one, not the one started after midnight. + */ + @Test + public void checksANewTraceProfilingTaskAgainstTheLatestStartingTask() { + final long newTaskEnd = scheduledForToday.getStartTime() + + TimeUnit.MINUTES.toMillis(5 + TRACE_PROFILING_MINUTES); + assertEquals(ids(scheduledForToday), ids(traceProfilingDAO.getTaskList( + SERVICE_ID, null, null, TimeBucket.getMinuteTimeBucket(newTaskEnd), 1))); + } + + /** + * The agent task cache reads the tasks that start within five minutes of now. A task is in the table of the day + * it was created while its time bucket is its start, so the task created today for tomorrow is in today's table. + */ + @Test + public void findsATaskCreatedTodayForALaterDayAroundItsStart() { + assertEquals(ids(scheduledForTomorrow), ids(readAroundStart(scheduledForTomorrow))); + } + + /** + * The same read on the day a task is scheduled for, as the cache makes it today for the task created yesterday. + */ + @Test + public void findsATaskCreatedYesterdayForTodayAroundItsStart() { + assertEquals(ids(scheduledForToday), ids(readAroundStart(scheduledForToday))); + } + + /** + * A continuous-profiling policy saved without targets asks for the tasks of no target type. + */ + @Test + public void readsNoContinuousProfilingTaskForAPolicyWithoutTargets() throws Exception { + final JDBCEBPFProfilingTaskDAO dao = new JDBCEBPFProfilingTaskDAO(support.getClient(), support.getTableHelper()); + final long triggeredSince = MIDNIGHT - TimeUnit.HOURS.toMillis(48); + final List policyTargets = + Arrays.asList(EBPFProfilingTargetType.ON_CPU, EBPFProfilingTargetType.OFF_CPU); + assertEquals(Arrays.asList(EBPFProfilingTargetType.ON_CPU.value(), EBPFProfilingTargetType.OFF_CPU.value()), + dao.queryTasksByTargets(SERVICE_ID, null, policyTargets, + EBPFProfilingTriggerType.CONTINUOUS_PROFILING, triggeredSince, 0) + .stream() + .map(EBPFProfilingTaskRecord::getTargetType) + .sorted() + .collect(Collectors.toList())); + assertEquals(Collections.emptyList(), + dao.queryTasksByTargets(SERVICE_ID, null, Collections.emptyList(), + EBPFProfilingTriggerType.CONTINUOUS_PROFILING, triggeredSince, 0)); + } + + private List readAroundStart(final ProfileTaskRecord task) { + final long start = task.getStartTime(); + return traceProfilingDAO.getTaskList( + null, null, + TimeBucket.getMinuteTimeBucket(start - TimeUnit.MINUTES.toMillis(5)), + TimeBucket.getMinuteTimeBucket(start + TimeUnit.MINUTES.toMillis(5)), null); + } + + private static List createdMinutes(final List tasks, final ToLongFunction createTime) { + return tasks.stream() + .map(task -> (int) ((createTime.applyAsLong(task) - MIDNIGHT) / 60_000L)) + .collect(Collectors.toList()); + } + + private static List ids(final ProfileTaskRecord... tasks) { + return Arrays.stream(tasks).map(ProfileTaskRecord::getTaskId).collect(Collectors.toList()); + } + + private static List ids(final List 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 builder, + final List 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 builder, + final List 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 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); + } +}