From c45bb361ee150c4295dce7ef174c7d1eaa834a78 Mon Sep 17 00:00:00 2001
From: haoyann <1064645534@qq.com>
Date: Sat, 20 Feb 2021 20:59:51 +0800
Subject: [PATCH] Storage plugin supports PostgreSQL (#6345)
---
.github/workflows/e2e.storages.yaml | 2 +-
.github/workflows/e2e.ttl.yaml | 2 +-
CHANGES.md | 1 +
dist-material/release-docs/LICENSE | 1 +
.../licenses/LICENSE-postgresql.txt | 23 +++
docs/en/setup/backend/backend-storage.md | 24 +++
.../setup/backend/configuration-vocabulary.md | 5 +
oap-server/pom.xml | 7 +
.../src/main/resources/application.yml | 12 ++
.../storage-jdbc-hikaricp-plugin/pom.xml | 4 +
.../jdbc/h2/dao/H2AggregationQueryDAO.java | 21 +-
.../plugin/jdbc/h2/dao/H2MetricsQueryDAO.java | 8 +-
.../plugin/jdbc/mysql/MySQLAlarmQueryDAO.java | 8 +-
.../postgresql/PostgreSQLStorageConfig.java | 29 +++
.../postgresql/PostgreSQLStorageProvider.java | 193 ++++++++++++++++++
.../postgresql/PostgreSQLTableInstaller.java | 87 ++++++++
.../dao/PostgreSQLAggregationQueryDAO.java | 43 ++++
.../dao/PostgreSQLAlarmQueryDAO.java | 40 ++++
.../dao/PostgreSQLBrowserLogQueryDAO.java | 34 +++
.../postgresql/dao/PostgreSQLLogQueryDAO.java | 37 ++++
.../dao/PostgreSQLMetricsQueryDAO.java | 36 ++++
.../dao/PostgreSQLTraceQueryDAO.java | 37 ++++
...g.oap.server.library.module.ModuleProvider | 3 +-
.../storage/docker-compose.postgresql.yml | 48 +++++
.../docker/ttl/docker-compose.postgresql.yml | 48 +++++
.../known-oap-backend-dependencies-es7.txt | 1 +
.../known-oap-backend-dependencies.txt | 1 +
27 files changed, 740 insertions(+), 15 deletions(-)
create mode 100644 dist-material/release-docs/licenses/LICENSE-postgresql.txt
create mode 100644 oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/postgresql/PostgreSQLStorageConfig.java
create mode 100644 oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/postgresql/PostgreSQLStorageProvider.java
create mode 100644 oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/postgresql/PostgreSQLTableInstaller.java
create mode 100644 oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/postgresql/dao/PostgreSQLAggregationQueryDAO.java
create mode 100644 oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/postgresql/dao/PostgreSQLAlarmQueryDAO.java
create mode 100644 oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/postgresql/dao/PostgreSQLBrowserLogQueryDAO.java
create mode 100644 oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/postgresql/dao/PostgreSQLLogQueryDAO.java
create mode 100644 oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/postgresql/dao/PostgreSQLMetricsQueryDAO.java
create mode 100644 oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/postgresql/dao/PostgreSQLTraceQueryDAO.java
create mode 100644 test/e2e/e2e-test/docker/storage/docker-compose.postgresql.yml
create mode 100644 test/e2e/e2e-test/docker/ttl/docker-compose.postgresql.yml
diff --git a/.github/workflows/e2e.storages.yaml b/.github/workflows/e2e.storages.yaml
index 06006407f..c8aa2c1ae 100644
--- a/.github/workflows/e2e.storages.yaml
+++ b/.github/workflows/e2e.storages.yaml
@@ -31,7 +31,7 @@ jobs:
timeout-minutes: 90
strategy:
matrix:
- storage: ['mysql', 'es6', 'es7.0', 'es7.10', 'influxdb', 'tidb']
+ storage: ['mysql', 'es6', 'es7.0', 'es7.10', 'influxdb', 'tidb', 'postgresql']
env:
SW_STORAGE: ${{ matrix.storage }}
steps:
diff --git a/.github/workflows/e2e.ttl.yaml b/.github/workflows/e2e.ttl.yaml
index 35ecbfae4..1f2d0c753 100644
--- a/.github/workflows/e2e.ttl.yaml
+++ b/.github/workflows/e2e.ttl.yaml
@@ -34,7 +34,7 @@ jobs:
timeout-minutes: 90
strategy:
matrix:
- storage: ['es6', 'es7', 'influxdb', 'tidb']
+ storage: ['es6', 'es7', 'influxdb', 'tidb', 'postgresql']
env:
SW_STORAGE: ${{ matrix.storage }}
steps:
diff --git a/CHANGES.md b/CHANGES.md
index 81a547928..3f350ed4c 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -22,6 +22,7 @@ Release Notes.
* Support Zabbix protocol to receive agent metrics.
* Update the Apdex metric combine calculator.
* Enhance `MeterSystem` to allow creating metrics with same `metricName` / `function` / `scope`.
+* Storage plugin supports postgresql.
#### UI
* Update selector scroller to show in all pages.
diff --git a/dist-material/release-docs/LICENSE b/dist-material/release-docs/LICENSE
index 09be5e25a..4b6dcc286 100755
--- a/dist-material/release-docs/LICENSE
+++ b/dist-material/release-docs/LICENSE
@@ -392,6 +392,7 @@ The text of each license is also included at licenses/LICENSE-[project].txt.
proto files from gogo: https://github.com/gogo/protobuf BSD-2
proto files from grpc-gateway, https://github.com/grpc-ecosystem/grpc-gateway/tree/master/protoc-gen-swagger/options BSD-3
zstd-jni 1.4.3-1: https://github.com/luben/zstd-jni, BSD-3-Clause
+ postgresql 42.2.18: https://jdbc.postgresql.org/about/license.html, BSD-2-Clause
========================================================================
MPL 2.0 licenses
diff --git a/dist-material/release-docs/licenses/LICENSE-postgresql.txt b/dist-material/release-docs/licenses/LICENSE-postgresql.txt
new file mode 100644
index 000000000..98dff7b6e
--- /dev/null
+++ b/dist-material/release-docs/licenses/LICENSE-postgresql.txt
@@ -0,0 +1,23 @@
+Copyright (c) 1997, PostgreSQL Global Development Group
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file
diff --git a/docs/en/setup/backend/backend-storage.md b/docs/en/setup/backend/backend-storage.md
index a2090c352..da2acb033 100644
--- a/docs/en/setup/backend/backend-storage.md
+++ b/docs/en/setup/backend/backend-storage.md
@@ -13,6 +13,7 @@ Native supported storage
- MySQL
- TiDB
- InfluxDB
+- PostgreSQL
Redistribution version with supported storage.
- ElasticSearch 5
@@ -265,6 +266,29 @@ storage:
```
All connection related settings including link url, username and password are in `application.yml`. The Metadata storage provider settings can refer to the configuration of **H2/MySQL** above.
+## PostgreSQL
+PostgreSQL jdbc driver uses version 42.2.18, it supports PostgreSQL 8.2 or newer.
+Active PostgreSQL as storage, set storage provider to **postgresql**.
+
+```yaml
+storage:
+ selector: ${SW_STORAGE:postgresql}
+ postgresql:
+ properties:
+ jdbcUrl: ${SW_JDBC_URL:"jdbc:postgresql://localhost:5432/skywalking"}
+ dataSource.user: ${SW_DATA_SOURCE_USER:postgres}
+ dataSource.password: ${SW_DATA_SOURCE_PASSWORD:123456}
+ dataSource.cachePrepStmts: ${SW_DATA_SOURCE_CACHE_PREP_STMTS:true}
+ dataSource.prepStmtCacheSize: ${SW_DATA_SOURCE_PREP_STMT_CACHE_SQL_SIZE:250}
+ dataSource.prepStmtCacheSqlLimit: ${SW_DATA_SOURCE_PREP_STMT_CACHE_SQL_LIMIT:2048}
+ dataSource.useServerPrepStmts: ${SW_DATA_SOURCE_USE_SERVER_PREP_STMTS:true}
+ metadataQueryMaxSize: ${SW_STORAGE_MYSQL_QUERY_MAX_SIZE:5000}
+ maxSizeOfArrayColumn: ${SW_STORAGE_MAX_SIZE_OF_ARRAY_COLUMN:20}
+ numOfSearchableValuesPerTag: ${SW_STORAGE_NUM_OF_SEARCHABLE_VALUES_PER_TAG:2}
+```
+All connection related settings including link url, username and password are in `application.yml`.
+Here are some of the settings, please follow [HikariCP](https://github.com/brettwooldridge/HikariCP) connection pool document for all the settings.
+
## ElasticSearch 5
ElasticSearch 5 is incompatible with ElasticSearch 6 Java client jar, so it could not be included in native distribution.
[OpenSkyWalking/SkyWalking-With-Es5x-Storage](https://github.com/OpenSkywalking/SkyWalking-With-Es5x-Storage) repo includes the distribution version.
diff --git a/docs/en/setup/backend/configuration-vocabulary.md b/docs/en/setup/backend/configuration-vocabulary.md
index a26447db6..428bad3a9 100644
--- a/docs/en/setup/backend/configuration-vocabulary.md
+++ b/docs/en/setup/backend/configuration-vocabulary.md
@@ -132,6 +132,11 @@ core|default|role|Option values, `Mixed/Receiver/Aggregator`. **Receiver** mode
| - | - | metadataQueryMaxSize | The max size of metadata per query. | SW_STORAGE_MYSQL_QUERY_MAX_SIZE | 5000 |
| - | - | maxSizeOfArrayColumn | Some entities, such as trace segment, include the logic column with multiple values. In the MySQL, we use multiple physical columns to host the values, such as, Change column_a with values [1,2,3,4,5] to `column_a_0 = 1, column_a_1 = 2, column_a_2 = 3 , column_a_3 = 4, column_a_4 = 5` | SW_STORAGE_MAX_SIZE_OF_ARRAY_COLUMN | 20 |
| - | - | numOfSearchableValuesPerTag | In a trace segment, it includes multiple spans with multiple tags. Different spans could have same tag keys, such as multiple HTTP exit spans all have their own `http.method` tag. This configuration set the limitation of max num of values for the same tag key. | SW_STORAGE_NUM_OF_SEARCHABLE_VALUES_PER_TAG | 2 |
+| - |postgresql| - | PostgreSQL storage. | - | - |
+| - | - | properties | Hikari connection pool configurations | - | Listed in the `application.yaml`. |
+| - | - | metadataQueryMaxSize | The max size of metadata per query. | SW_STORAGE_MYSQL_QUERY_MAX_SIZE | 5000 |
+| - | - | maxSizeOfArrayColumn | Some entities, such as trace segment, include the logic column with multiple values. In the PostgreSQL, we use multiple physical columns to host the values, such as, Change column_a with values [1,2,3,4,5] to `column_a_0 = 1, column_a_1 = 2, column_a_2 = 3 , column_a_3 = 4, column_a_4 = 5` | SW_STORAGE_MAX_SIZE_OF_ARRAY_COLUMN | 20 |
+| - | - | numOfSearchableValuesPerTag | In a trace segment, it includes multiple spans with multiple tags. Different spans could have same tag keys, such as multiple HTTP exit spans all have their own `http.method` tag. This configuration set the limitation of max num of values for the same tag key. | SW_STORAGE_NUM_OF_SEARCHABLE_VALUES_PER_TAG | 2 |
| - |influxdb| - | InfluxDB storage. |- | - |
| - | - | url| InfluxDB connection URL. | SW_STORAGE_INFLUXDB_URL | http://localhost:8086|
| - | - | user | User name of InfluxDB. | SW_STORAGE_INFLUXDB_USER | root|
diff --git a/oap-server/pom.xml b/oap-server/pom.xml
index 46839825a..15d76ca8f 100755
--- a/oap-server/pom.xml
+++ b/oap-server/pom.xml
@@ -101,6 +101,7 @@
2.4.6.RELEASE1.9.41.12.0
+ 42.2.18
@@ -561,6 +562,12 @@
flatbuffers-java${flatbuffers-java.version}
+
+
+ org.postgresql
+ postgresql
+ ${postgresql.version}
+
diff --git a/oap-server/server-bootstrap/src/main/resources/application.yml b/oap-server/server-bootstrap/src/main/resources/application.yml
index 943e03e4a..34b4bef10 100755
--- a/oap-server/server-bootstrap/src/main/resources/application.yml
+++ b/oap-server/server-bootstrap/src/main/resources/application.yml
@@ -201,6 +201,18 @@ storage:
batchEnabled: ${SW_STORAGE_INFLUXDB_BATCH_ENABLED:true}
fetchTaskLogMaxSize: ${SW_STORAGE_INFLUXDB_FETCH_TASK_LOG_MAX_SIZE:5000} # the max number of fetch task log in a request
connectionResponseFormat: ${SW_STORAGE_INFLUXDB_CONNECTION_RESPONSE_FORMAT:MSGPACK} # the response format of connection to influxDB, cannot be anything but MSGPACK or JSON.
+ postgresql:
+ properties:
+ jdbcUrl: ${SW_JDBC_URL:"jdbc:postgresql://localhost:5432/skywalking"}
+ dataSource.user: ${SW_DATA_SOURCE_USER:postgres}
+ dataSource.password: ${SW_DATA_SOURCE_PASSWORD:123456}
+ dataSource.cachePrepStmts: ${SW_DATA_SOURCE_CACHE_PREP_STMTS:true}
+ dataSource.prepStmtCacheSize: ${SW_DATA_SOURCE_PREP_STMT_CACHE_SQL_SIZE:250}
+ dataSource.prepStmtCacheSqlLimit: ${SW_DATA_SOURCE_PREP_STMT_CACHE_SQL_LIMIT:2048}
+ dataSource.useServerPrepStmts: ${SW_DATA_SOURCE_USE_SERVER_PREP_STMTS:true}
+ metadataQueryMaxSize: ${SW_STORAGE_MYSQL_QUERY_MAX_SIZE:5000}
+ maxSizeOfArrayColumn: ${SW_STORAGE_MAX_SIZE_OF_ARRAY_COLUMN:20}
+ numOfSearchableValuesPerTag: ${SW_STORAGE_NUM_OF_SEARCHABLE_VALUES_PER_TAG:2}
agent-analyzer:
selector: ${SW_AGENT_ANALYZER:default}
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 68f1d874e..4a8af4e64 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
@@ -48,6 +48,10 @@
+
+ org.postgresql
+ postgresql
+
diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2AggregationQueryDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2AggregationQueryDAO.java
index ce7bad021..d686cc51c 100644
--- a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2AggregationQueryDAO.java
+++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2AggregationQueryDAO.java
@@ -49,15 +49,8 @@ public class H2AggregationQueryDAO implements IAggregationQueryDAO {
final String valueColumnName,
final Duration duration,
List additionalConditions) throws IOException {
- StringBuilder sql = new StringBuilder();
List