This is an automated email from the ASF dual-hosted git repository. wusheng pushed a commit to branch h2-storage in repository https://gitbox.apache.org/repos/asf/incubator-skywalking.git
commit c3d7350251fb7c23e777f4df9f85e57e98a73f2e Author: Wu Sheng <[email protected]> AuthorDate: Mon Sep 17 23:28:14 2018 +0800 H2 storage code skeleton. --- .../network/trace/component/ComponentsDefine.java | 2 +- .../apm/plugin/dubbo/DubboInterceptor.java | 4 +- .../jdbc/h2/define/DriverInstrumentation.java | 2 +- .../h2/define/JdbcConnectionInstrumentation.java | 2 +- .../h2/define/JdbcXAConnectionInstrumentation.java | 4 +- .../PooledJdbcConnectionInstrumentation.java | 4 +- .../jdbc/connectionurl/parser/URLParser.java | 2 +- .../apm/plugin/jdbc/trace/ConnectionInfo.java | 2 +- .../jdbc/connectionurl/parser/URLParserTest.java | 8 +- oap-server/pom.xml | 6 + .../oap/server/core/storage/StorageException.java | 4 + oap-server/server-library/library-client/pom.xml | 4 +- .../oap/server/library/client/h2/H2Client.java | 113 ---------------- .../JDBCClientException.java} | 11 +- .../client/jdbc/hikaricp/JDBCHikariCPClient.java | 127 +++++++++++++++++ .../client/shardingjdbc/ShardingjdbcClient.java | 150 --------------------- .../shardingjdbc/ShardingjdbcClientConfig.java | 73 ---------- oap-server/server-starter/pom.xml | 2 +- .../src/main/resources/application.yml | 31 ++--- oap-server/server-storage-plugin/pom.xml | 2 +- .../pom.xml | 19 ++- .../server/storage/plugin/jdbc/SQLBuilder.java} | 40 +++--- .../server/storage/plugin/jdbc/SQLExecutor.java} | 24 ++-- .../server/storage/plugin/jdbc/TableMetaInfo.java} | 19 +-- .../storage/plugin/jdbc/h2/H2StorageConfig.java} | 12 +- .../storage/plugin/jdbc/h2/H2StorageProvider.java | 118 ++++++++++++++++ .../storage/plugin/jdbc/h2/dao/H2BatchDAO.java} | 13 +- .../jdbc/h2/dao/H2EndpointInventoryCacheDAO.java} | 18 +-- .../storage/plugin/jdbc/h2/dao/H2IndicatorDAO.java | 124 +++++++++++++++++ .../h2/dao/H2NetworkAddressInventoryCacheDAO.java} | 18 +-- .../plugin/jdbc/h2/dao/H2RegisterLockDAO.java} | 20 +-- .../dao/H2ServiceInstanceInventoryCacheDAO.java} | 38 ++---- .../jdbc/h2/dao/H2ServiceInventoryCacheDAO.java} | 20 +-- .../storage/plugin/jdbc/h2/dao/H2StorageDAO.java | 46 +++++++ .../plugin/jdbc/h2/dao/H2TableInstaller.java | 108 +++++++++++++++ .../plugin/jdbc/h2/dao/H2TopologyQueryDAO.java | 69 ++++++++++ ...alking.oap.server.library.module.ModuleProvider | 19 +++ 37 files changed, 791 insertions(+), 487 deletions(-) diff --git a/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java b/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java index 02a6730..e6427be 100644 --- a/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java +++ b/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java @@ -68,7 +68,7 @@ public class ComponentsDefine { public static final OfficialComponent JEDIS = new OfficialComponent(30, "Jedis"); - public static final OfficialComponent H2_JDBC_DRIVER = new OfficialComponent(32, "h2-jdbc-driver"); + public static final OfficialComponent H2_JDBC_DRIVER = new OfficialComponent(32, "jdbc-jdbc-driver"); public static final OfficialComponent MYSQL_JDBC_DRIVER = new OfficialComponent(33, "mysql-connector-java"); diff --git a/apm-sniffer/apm-sdk-plugin/dubbo-plugin/src/main/java/org/apache/skywalking/apm/plugin/dubbo/DubboInterceptor.java b/apm-sniffer/apm-sdk-plugin/dubbo-plugin/src/main/java/org/apache/skywalking/apm/plugin/dubbo/DubboInterceptor.java index 994bc89..5f447e0 100644 --- a/apm-sniffer/apm-sdk-plugin/dubbo-plugin/src/main/java/org/apache/skywalking/apm/plugin/dubbo/DubboInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/dubbo-plugin/src/main/java/org/apache/skywalking/apm/plugin/dubbo/DubboInterceptor.java @@ -45,10 +45,10 @@ import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; */ public class DubboInterceptor implements InstanceMethodsAroundInterceptor { /** - * <h2>Consumer:</h2> The serialized trace context data will + * <jdbc>Consumer:</jdbc> The serialized trace context data will * inject to the {@link RpcContext#attachments} for transport to provider side. * <p> - * <h2>Provider:</h2> The serialized trace context data will extract from + * <jdbc>Provider:</jdbc> The serialized trace context data will extract from * {@link RpcContext#attachments}. current trace segment will ref if the serialize context data is not null. */ @Override diff --git a/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/DriverInstrumentation.java b/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/DriverInstrumentation.java index 3d4fafd..1fd69d6 100644 --- a/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/DriverInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/DriverInstrumentation.java @@ -30,7 +30,7 @@ import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName * @author zhangxin */ public class DriverInstrumentation extends AbstractDriverInstrumentation { - private static final String CLASS_OF_INTERCEPT_H2_DRIVER = "org.h2.Driver"; + private static final String CLASS_OF_INTERCEPT_H2_DRIVER = "org.jdbc.Driver"; @Override protected ClassMatch enhanceClass() { diff --git a/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/JdbcConnectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/JdbcConnectionInstrumentation.java index 0b778b2..7ee6970 100644 --- a/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/JdbcConnectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/JdbcConnectionInstrumentation.java @@ -29,7 +29,7 @@ import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName * @author zhangxin */ public class JdbcConnectionInstrumentation extends AbstractConnectionInstrumentation { - public static final String ENHANCE_CLASS = "org.h2.jdbc.JdbcConnection"; + public static final String ENHANCE_CLASS = "org.jdbc.jdbc.JdbcConnection"; @Override protected ClassMatch enhanceClass() { return byName(ENHANCE_CLASS); diff --git a/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/JdbcXAConnectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/JdbcXAConnectionInstrumentation.java index 000f7ba..6e6945c 100644 --- a/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/JdbcXAConnectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/JdbcXAConnectionInstrumentation.java @@ -34,8 +34,8 @@ import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName */ public class JdbcXAConnectionInstrumentation extends AbstractConnectionInstrumentation { - public static final String ENHANCE_CLASS = "org.h2.jdbcx.JdbcXAConnection"; - public static final String CONSTRUCTOR_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.jdbc.h2.JdbcXAConnectionConstructorInterceptor"; + public static final String ENHANCE_CLASS = "org.jdbc.jdbcx.JdbcXAConnection"; + public static final String CONSTRUCTOR_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.jdbc.jdbc.JdbcXAConnectionConstructorInterceptor"; @Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { return new ConstructorInterceptPoint[] { diff --git a/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/PooledJdbcConnectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/PooledJdbcConnectionInstrumentation.java index 0e07a66..dc50ff6 100644 --- a/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/PooledJdbcConnectionInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/h2-1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jdbc/h2/define/PooledJdbcConnectionInstrumentation.java @@ -35,8 +35,8 @@ import static net.bytebuddy.matcher.ElementMatchers.any; */ public class PooledJdbcConnectionInstrumentation extends AbstractConnectionInstrumentation { - public static final String CONSTRUCTOR_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.jdbc.h2.PooledJdbcConnectionConstructorInterceptor"; - public static final String ENHANCE_CLASS = "org.h2.jdbcx.JdbcXAConnection$PooledJdbcConnection"; + public static final String CONSTRUCTOR_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.jdbc.jdbc.PooledJdbcConnectionConstructorInterceptor"; + public static final String ENHANCE_CLASS = "org.jdbc.jdbcx.JdbcXAConnection$PooledJdbcConnection"; @Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { return new ConstructorInterceptPoint[] { diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParser.java b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParser.java index b978121..721d177 100644 --- a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParser.java +++ b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParser.java @@ -31,7 +31,7 @@ public class URLParser { private static final String MYSQL_JDBC_URL_PREFIX = "jdbc:mysql"; private static final String ORACLE_JDBC_URL_PREFIX = "jdbc:oracle"; - private static final String H2_JDBC_URL_PREFIX = "jdbc:h2"; + private static final String H2_JDBC_URL_PREFIX = "jdbc:jdbc"; private static final String POSTGRESQL_JDBC_URL_PREFIX = "jdbc:postgresql"; public static ConnectionInfo parser(String url) { diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/trace/ConnectionInfo.java b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/trace/ConnectionInfo.java index bed5f6b..d5a556e 100644 --- a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/trace/ConnectionInfo.java +++ b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/trace/ConnectionInfo.java @@ -28,7 +28,7 @@ import org.apache.skywalking.apm.network.trace.component.OfficialComponent; */ public class ConnectionInfo { /** - * DB type, such as mysql, oracle, h2. + * DB type, such as mysql, oracle, jdbc. */ private final String dbType; /** diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParserTest.java b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParserTest.java index a8d7017..7eca4db 100644 --- a/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParserTest.java +++ b/apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParserTest.java @@ -116,7 +116,7 @@ public class URLParserTest { @Test public void testParseH2JDBCURLWithEmbedded() { - ConnectionInfo connectionInfo = new URLParser().parser("jdbc:h2:file:/data/sample"); + ConnectionInfo connectionInfo = new URLParser().parser("jdbc:jdbc:file:/data/sample"); assertThat(connectionInfo.getDBType(), is("H2")); assertThat(connectionInfo.getDatabaseName(), is("/data/sample")); assertThat(connectionInfo.getDatabasePeer(), is("localhost:-1")); @@ -124,7 +124,7 @@ public class URLParserTest { @Test public void testParseH2JDBCURLWithEmbeddedRunningInWindows() { - ConnectionInfo connectionInfo = new URLParser().parser("jdbc:h2:file:C:/data/sample"); + ConnectionInfo connectionInfo = new URLParser().parser("jdbc:jdbc:file:C:/data/sample"); assertThat(connectionInfo.getDBType(), is("H2")); assertThat(connectionInfo.getDatabaseName(), is("C:/data/sample")); assertThat(connectionInfo.getDatabasePeer(), is("localhost:-1")); @@ -132,7 +132,7 @@ public class URLParserTest { @Test public void testParseH2JDBCURLWithMemoryMode() { - ConnectionInfo connectionInfo = new URLParser().parser("jdbc:h2:mem:test_mem"); + ConnectionInfo connectionInfo = new URLParser().parser("jdbc:jdbc:mem:test_mem"); assertThat(connectionInfo.getDBType(), is("H2")); assertThat(connectionInfo.getDatabaseName(), is("test_mem")); assertThat(connectionInfo.getDatabasePeer(), is("localhost:-1")); @@ -140,7 +140,7 @@ public class URLParserTest { @Test public void testParseH2JDBCURL() { - ConnectionInfo connectionInfo = new URLParser().parser("jdbc:h2:tcp://localhost:8084/~/sample"); + ConnectionInfo connectionInfo = new URLParser().parser("jdbc:jdbc:tcp://localhost:8084/~/sample"); assertThat(connectionInfo.getDBType(), is("H2")); assertThat(connectionInfo.getDatabaseName(), is("sample")); assertThat(connectionInfo.getDatabasePeer(), is("localhost:8084")); diff --git a/oap-server/pom.xml b/oap-server/pom.xml index c2995ce..8652efa 100644 --- a/oap-server/pom.xml +++ b/oap-server/pom.xml @@ -60,6 +60,7 @@ <elasticsearch.version>6.3.2</elasticsearch.version> <joda-time.version>2.9.9</joda-time.version> <kubernetes.version>2.0.0</kubernetes.version> + <hikaricp.version>3.1.0</hikaricp.version> </properties> <dependencies> @@ -261,6 +262,11 @@ <artifactId>client-java</artifactId> <version>${kubernetes.version}</version> </dependency> + <dependency> + <groupId>com.zaxxer</groupId> + <artifactId>HikariCP</artifactId> + <version>${hikaricp.version}</version> + </dependency> </dependencies> </dependencyManagement> </project> \ No newline at end of file diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/StorageException.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/StorageException.java index b9a4d94..8f321a1 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/StorageException.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/StorageException.java @@ -26,4 +26,8 @@ public class StorageException extends Exception { public StorageException(String message) { super(message); } + + public StorageException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/oap-server/server-library/library-client/pom.xml b/oap-server/server-library/library-client/pom.xml index a8a6bd5..26cc854 100644 --- a/oap-server/server-library/library-client/pom.xml +++ b/oap-server/server-library/library-client/pom.xml @@ -36,8 +36,8 @@ <artifactId>grpc-core</artifactId> </dependency> <dependency> - <groupId>com.h2database</groupId> - <artifactId>h2</artifactId> + <groupId>com.zaxxer</groupId> + <artifactId>HikariCP</artifactId> </dependency> <dependency> <groupId>io.shardingjdbc</groupId> diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/h2/H2Client.java b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/h2/H2Client.java deleted file mode 100644 index 41a24ed..0000000 --- a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/h2/H2Client.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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.library.client.h2; - -import java.sql.*; -import org.apache.skywalking.oap.server.library.client.Client; -import org.h2.util.IOUtils; -import org.slf4j.*; - -/** - * @author peng-yongsheng - */ -public class H2Client implements Client { - - private final Logger logger = LoggerFactory.getLogger(H2Client.class); - - private Connection conn; - private String url; - private String userName; - private String password; - - public H2Client() { - this.url = "jdbc:h2:mem:collector"; - this.userName = ""; - this.password = ""; - } - - public H2Client(String url, String userName, String password) { - this.url = url; - this.userName = userName; - this.password = password; - } - - @Override public void initialize() throws H2ClientException { - try { - Class.forName("org.h2.Driver"); - conn = DriverManager. - getConnection(this.url, this.userName, this.password); - } catch (Exception e) { - throw new H2ClientException(e.getMessage(), e); - } - } - - @Override public void shutdown() { - IOUtils.closeSilently(conn); - } - - public Connection getConnection() { - return conn; - } - - public void execute(String sql) throws H2ClientException { - try (Statement statement = getConnection().createStatement()) { - statement.execute(sql); - statement.closeOnCompletion(); - } catch (SQLException e) { - throw new H2ClientException(e.getMessage(), e); - } - } - - public ResultSet executeQuery(String sql, Object[] params) throws H2ClientException { - logger.debug("execute query with result: {}", sql); - ResultSet rs; - PreparedStatement statement; - try { - statement = getConnection().prepareStatement(sql); - if (params != null) { - for (int i = 0; i < params.length; i++) { - statement.setObject(i + 1, params[i]); - } - } - rs = statement.executeQuery(); - statement.closeOnCompletion(); - } catch (SQLException e) { - throw new H2ClientException(e.getMessage(), e); - } - return rs; - } - - public boolean execute(String sql, Object[] params) throws H2ClientException { - logger.debug("execute insert/update/delete: {}", sql); - boolean flag; - Connection conn = getConnection(); - try (PreparedStatement statement = conn.prepareStatement(sql)) { - conn.setAutoCommit(true); - if (params != null) { - for (int i = 0; i < params.length; i++) { - statement.setObject(i + 1, params[i]); - } - } - flag = statement.execute(); - } catch (SQLException e) { - throw new H2ClientException(e.getMessage(), e); - } - return flag; - } -} diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/h2/H2ClientException.java b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/jdbc/JDBCClientException.java similarity index 78% rename from oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/h2/H2ClientException.java rename to oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/jdbc/JDBCClientException.java index 4c2d8c7..7a96711 100644 --- a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/h2/H2ClientException.java +++ b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/jdbc/JDBCClientException.java @@ -16,20 +16,21 @@ * */ -package org.apache.skywalking.oap.server.library.client.h2; +package org.apache.skywalking.oap.server.library.client.jdbc; import org.apache.skywalking.oap.server.library.client.ClientException; /** - * @author peng-yongsheng + * @author wusheng */ -public class H2ClientException extends ClientException { +public class JDBCClientException extends ClientException { - public H2ClientException(String message) { + public JDBCClientException(String message) { super(message); } - public H2ClientException(String message, Throwable cause) { + public JDBCClientException(String message, Throwable cause) { super(message, cause); } } + diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/jdbc/hikaricp/JDBCHikariCPClient.java b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/jdbc/hikaricp/JDBCHikariCPClient.java new file mode 100644 index 0000000..07e474e --- /dev/null +++ b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/jdbc/hikaricp/JDBCHikariCPClient.java @@ -0,0 +1,127 @@ +/* + * 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.library.client.jdbc.hikaricp; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Properties; +import org.apache.skywalking.oap.server.library.client.Client; +import org.apache.skywalking.oap.server.library.client.ClientException; +import org.apache.skywalking.oap.server.library.client.jdbc.JDBCClientException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * JDBC Client uses HikariCP connection management lib to execute SQL. + * + * @author wusheng + */ +public class JDBCHikariCPClient implements Client { + private final Logger logger = LoggerFactory.getLogger(JDBCHikariCPClient.class); + + private HikariDataSource dataSource; + private HikariConfig hikariConfig; + + public JDBCHikariCPClient(Properties properties) { + hikariConfig = new HikariConfig(properties); + } + + @Override public void initialize() throws ClientException { + dataSource = new HikariDataSource(hikariConfig); + } + + @Override public void shutdown() { + } + + public Connection getConnection() throws JDBCClientException { + try { + return dataSource.getConnection(); + } catch (SQLException e) { + throw new JDBCClientException(e.getMessage(), e); + } + } + + public void execute(String sql) throws JDBCClientException { + try (Connection conn = getConnection()) { + try (Statement statement = conn.createStatement()) { + statement.execute(sql); + statement.closeOnCompletion(); + } catch (SQLException e) { + throw new JDBCClientException(e.getMessage(), e); + } + } catch (SQLException e) { + throw new JDBCClientException(e.getMessage(), e); + } + } + + public ResultSet executeQuery(String sql, Object[] params) throws JDBCClientException { + logger.debug("execute query with result: {}", sql); + ResultSet rs; + try (Connection conn = getConnection()) { + conn.setReadOnly(true); + try (PreparedStatement statement = conn.prepareStatement(sql)) { + if (params != null) { + for (int i = 0; i < params.length; i++) { + statement.setObject(i + 1, params[i]); + } + } + rs = statement.executeQuery(); + statement.closeOnCompletion(); + } catch (SQLException e) { + throw new JDBCClientException(e.getMessage(), e); + } + } catch (SQLException e) { + throw new JDBCClientException(e.getMessage(), e); + } + + return rs; + } + + public boolean execute(String sql, Object[] params) throws JDBCClientException { + logger.debug("execute insert/update/delete: {}", sql); + boolean flag; + try (Connection conn = getConnection()) { + /** + * Notice, SkyWalking is an observability system, + * no transaction required. + */ + conn.setAutoCommit(true); + try (PreparedStatement statement = conn.prepareStatement(sql)) { + conn.setAutoCommit(true); + if (params != null) { + for (int i = 0; i < params.length; i++) { + statement.setObject(i + 1, params[i]); + } + } + flag = statement.execute(); + statement.closeOnCompletion(); + } catch (SQLException e) { + throw new JDBCClientException(e.getMessage(), e); + } + } catch (SQLException e) { + throw new JDBCClientException(e.getMessage(), e); + } + return flag; + } +} diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClient.java b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClient.java deleted file mode 100644 index 3fb7559..0000000 --- a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClient.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * 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.library.client.shardingjdbc; - -import io.shardingjdbc.core.api.ShardingDataSourceFactory; -import io.shardingjdbc.core.api.config.ShardingRuleConfiguration; -import java.sql.*; -import java.util.*; -import javax.sql.DataSource; -import org.apache.commons.dbcp.BasicDataSource; -import org.apache.skywalking.oap.server.library.client.Client; -import org.slf4j.*; - -/** - * @author linjiaqi - */ -public class ShardingjdbcClient implements Client { - - private static final Logger logger = LoggerFactory.getLogger(ShardingjdbcClient.class); - - private Map<String, ShardingjdbcClientConfig> shardingjdbcClientConfig; - - private ShardingRuleConfiguration shardingRuleConfiguration; - - private Map<String, DataSource> shardingDataSource = new HashMap<String, DataSource>(); - - private DataSource dataSource; - - public ShardingjdbcClient(Map<String, ShardingjdbcClientConfig> shardingjdbcClientConfig, - ShardingRuleConfiguration shardingRuleConfiguration) { - this.shardingjdbcClientConfig = shardingjdbcClientConfig; - this.shardingRuleConfiguration = shardingRuleConfiguration; - } - - @Override public void initialize() throws ShardingjdbcClientException { - try { - shardingjdbcClientConfig.forEach((key, value) -> { - BasicDataSource basicDataSource = new BasicDataSource(); - basicDataSource.setDriverClassName(value.getDriverClass()); - basicDataSource.setUrl(value.getUrl()); - basicDataSource.setUsername(value.getUserName()); - basicDataSource.setPassword(value.getPassword()); - shardingDataSource.put(key, basicDataSource); - logger.info("add sharding datasource: {}, url: {}", key, value.getUrl()); - }); - dataSource = ShardingDataSourceFactory.createDataSource(shardingDataSource, shardingRuleConfiguration, - new HashMap<String, Object>(), new Properties()); - } catch (Exception e) { - logger.error("case the exception is 'Cannot load JDBC driver class', please add the driver mysql-connector-java-5.1.36.jar to collector-libs manual"); - throw new ShardingjdbcClientException(e.getMessage(), e); - } - } - - @Override public void shutdown() { - - } - - public Connection getConnection() throws SQLException { - return dataSource.getConnection(); - } - - public void execute(String sql) throws ShardingjdbcClientException { - Connection conn = null; - Statement statement = null; - try { - conn = getConnection(); - statement = conn.createStatement(); - statement.execute(sql); - } catch (SQLException e) { - throw new ShardingjdbcClientException(e.getMessage(), e); - } finally { - try { - if (statement != null) { - statement.close(); - } - if (conn != null) { - conn.close(); - } - } catch (SQLException e) { - throw new ShardingjdbcClientException(e.getMessage(), e); - } - } - } - - public ResultSet executeQuery(String sql, Object[] params) throws ShardingjdbcClientException { - logger.debug("execute query with result: {}", sql); - ResultSet rs; - PreparedStatement statement; - try { - statement = getConnection().prepareStatement(sql); - if (params != null) { - for (int i = 0; i < params.length; i++) { - statement.setObject(i + 1, params[i]); - } - } - rs = statement.executeQuery(); - } catch (SQLException e) { - throw new ShardingjdbcClientException(e.getMessage(), e); - } - return rs; - } - - public boolean execute(String sql, Object[] params) throws ShardingjdbcClientException { - logger.debug("execute insert/update/delete: {}", sql); - boolean flag; - Connection conn = null; - PreparedStatement statement = null; - try { - conn = getConnection(); - conn.setAutoCommit(true); - statement = conn.prepareStatement(sql); - if (params != null) { - for (int i = 0; i < params.length; i++) { - statement.setObject(i + 1, params[i]); - } - } - flag = statement.execute(); - } catch (SQLException e) { - throw new ShardingjdbcClientException(e.getMessage(), e); - } finally { - try { - if (statement != null) { - statement.close(); - } - if (conn != null) { - conn.close(); - } - } catch (SQLException e) { - throw new ShardingjdbcClientException(e.getMessage(), e); - } - } - return flag; - } -} diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientConfig.java b/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientConfig.java deleted file mode 100644 index 08edb35..0000000 --- a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientConfig.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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.library.client.shardingjdbc; - -/** - * @author linjiaqi - */ -public class ShardingjdbcClientConfig { - - private String driverClass; - private String url; - private String userName; - private String password; - - public ShardingjdbcClientConfig() { - - } - - public ShardingjdbcClientConfig(String driverClass, String url, String username, String password) { - this.driverClass = driverClass; - this.url = url; - this.userName = username; - this.password = password; - } - - public String getDriverClass() { - return driverClass; - } - - public void setDriverClass(String driverClass) { - this.driverClass = driverClass; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public String getUserName() { - return userName; - } - - public void setUserName(String userName) { - this.userName = userName; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } -} diff --git a/oap-server/server-starter/pom.xml b/oap-server/server-starter/pom.xml index b4845a7..8d99daf 100644 --- a/oap-server/server-starter/pom.xml +++ b/oap-server/server-starter/pom.xml @@ -85,7 +85,7 @@ <!-- storage module --> <dependency> <groupId>org.apache.skywalking</groupId> - <artifactId>storage-h2-plugin</artifactId> + <artifactId>storage-jdbc-hikaricp-plugin</artifactId> <version>${project.version}</version> </dependency> <dependency> diff --git a/oap-server/server-starter/src/main/resources/application.yml b/oap-server/server-starter/src/main/resources/application.yml index 9290a93..9a58c5b 100644 --- a/oap-server/server-starter/src/main/resources/application.yml +++ b/oap-server/server-starter/src/main/resources/application.yml @@ -34,21 +34,22 @@ core: gRPCHost: 0.0.0.0 gRPCPort: 11800 storage: - elasticsearch: - clusterNodes: localhost:9200 - indexShardsNumber: 2 - indexReplicasNumber: 0 - # Batch process setting, refer to https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-docs-bulk-processor.html - bulkActions: 2000 # Execute the bulk every 2000 requests - bulkSize: 20 # flush the bulk every 20mb - flushInterval: 10 # flush the bulk every 10 seconds whatever the number of requests - concurrentRequests: 2 # the number of concurrent requests - # Set a timeout on metric data. After the timeout has expired, the metric data will automatically be deleted. - traceDataTTL: 90 # Unit is minute - minuteMetricDataTTL: 90 # Unit is minute - hourMetricDataTTL: 36 # Unit is hour - dayMetricDataTTL: 45 # Unit is day - monthMetricDataTTL: 18 # Unit is month + h2: +# elasticsearch: +# clusterNodes: localhost:9200 +# indexShardsNumber: 2 +# indexReplicasNumber: +# # Batch process setting, refer to https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-docs-bulk-processor.html +# bulkActions: 2000 # Execute the bulk every 2000 requests +# bulkSize: 20 # flush the bulk every 20mb +# flushInterval: 10 # flush the bulk every 10 seconds whatever the number of requests +# concurrentRequests: 2 # the number of concurrent requests +# # Set a timeout on metric data. After the timeout has expired, the metric data will automatically be deleted. +# traceDataTTL: 90 # Unit is minute +# minuteMetricDataTTL: 90 # Unit is minute +# hourMetricDataTTL: 36 # Unit is hour +# dayMetricDataTTL: 45 # Unit is day +# monthMetricDataTTL: 18 # Unit is month receiver-register: default: receiver-trace: diff --git a/oap-server/server-storage-plugin/pom.xml b/oap-server/server-storage-plugin/pom.xml index ff237b0..48d5ac9 100644 --- a/oap-server/server-storage-plugin/pom.xml +++ b/oap-server/server-storage-plugin/pom.xml @@ -30,7 +30,7 @@ <artifactId>server-storage-plugin</artifactId> <packaging>pom</packaging> <modules> - <module>storage-h2-plugin</module> + <module>storage-jdbc-hikaricp-plugin</module> <module>storage-elasticsearch-plugin</module> </modules> diff --git a/oap-server/server-storage-plugin/storage-h2-plugin/pom.xml b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/pom.xml similarity index 67% rename from oap-server/server-storage-plugin/storage-h2-plugin/pom.xml rename to oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/pom.xml index 2cb644e..523467e 100644 --- a/oap-server/server-storage-plugin/storage-h2-plugin/pom.xml +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/pom.xml @@ -27,7 +27,24 @@ </parent> <modelVersion>4.0.0</modelVersion> - <artifactId>storage-h2-plugin</artifactId> + <artifactId>storage-jdbc-hikaricp-plugin</artifactId> <packaging>jar</packaging> + <dependencies> + <dependency> + <groupId>org.apache.skywalking</groupId> + <artifactId>server-core</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.skywalking</groupId> + <artifactId>library-client</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>com.h2database</groupId> + <artifactId>h2</artifactId> + </dependency> + </dependencies> + </project> \ No newline at end of file diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/h2/H2ClientConfig.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/SQLBuilder.java similarity index 53% copy from oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/h2/H2ClientConfig.java copy to oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/SQLBuilder.java index 764d906..f74b1b6 100644 --- a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/h2/H2ClientConfig.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/SQLBuilder.java @@ -16,38 +16,42 @@ * */ -package org.apache.skywalking.oap.server.library.client.h2; +package org.apache.skywalking.oap.server.storage.plugin.jdbc; /** - * @author peng-yongsheng + * SQLBuilder + * + * @author wusheng */ -public abstract class H2ClientConfig { +public class SQLBuilder { + private static String LINE_END = System.lineSeparator(); - private String url; - private String userName; - private String password; + private StringBuilder text; - public String getUrl() { - return url; + public SQLBuilder() { + this.text = new StringBuilder(); } - public void setUrl(String url) { - this.url = url; + public SQLBuilder(String initLine) { + this(); + this.appendLine(initLine); } - public String getUserName() { - return userName; + public SQLBuilder append(String fragment) { + text.append(fragment); + return this; } - public void setUserName(String userName) { - this.userName = userName; + public SQLBuilder appendLine(String line) { + text.append(line).append(LINE_END); + return this; } - public String getPassword() { - return password; + public String toStringInNewLine() { + return LINE_END + toString(); } - public void setPassword(String password) { - this.password = password; + @Override public String toString() { + return text.toString(); } } diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/SQLExecutor.java similarity index 66% copy from oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java copy to oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/SQLExecutor.java index e8ee50a..4cd33c8 100644 --- a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/SQLExecutor.java @@ -16,20 +16,24 @@ * */ -package org.apache.skywalking.oap.server.library.client.shardingjdbc; +package org.apache.skywalking.oap.server.storage.plugin.jdbc; -import org.apache.skywalking.oap.server.library.client.ClientException; +import java.util.List; +import lombok.AccessLevel; +import lombok.Getter; /** - * @author linjiaqi + * A SQL executor. + * + * @author wusheng */ -public class ShardingjdbcClientException extends ClientException { - - public ShardingjdbcClientException(String message) { - super(message); - } +@Getter(AccessLevel.PUBLIC) +public class SQLExecutor { + private String sql; + private List<Object> param; - public ShardingjdbcClientException(String message, Throwable cause) { - super(message, cause); + public SQLExecutor(String sql, List<Object> param) { + this.sql = sql; + this.param = param; } } diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/TableMetaInfo.java similarity index 64% copy from oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java copy to oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/TableMetaInfo.java index e8ee50a..752728e 100644 --- a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/TableMetaInfo.java @@ -16,20 +16,23 @@ * */ -package org.apache.skywalking.oap.server.library.client.shardingjdbc; +package org.apache.skywalking.oap.server.storage.plugin.jdbc; -import org.apache.skywalking.oap.server.library.client.ClientException; +import java.util.HashMap; +import java.util.Map; +import org.apache.skywalking.oap.server.core.storage.model.Model; /** - * @author linjiaqi + * @author wusheng */ -public class ShardingjdbcClientException extends ClientException { +public class TableMetaInfo { + private static Map<String, Model> TABLES = new HashMap<>(); - public ShardingjdbcClientException(String message) { - super(message); + public static void addModel(Model model) { + TABLES.put(model.getName(), model); } - public ShardingjdbcClientException(String message, Throwable cause) { - super(message, cause); + public static Model get(String moduleName) { + return TABLES.get(moduleName); } } diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/StorageException.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/H2StorageConfig.java similarity index 79% copy from oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/StorageException.java copy to oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/H2StorageConfig.java index b9a4d94..a2d2aad 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/StorageException.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/H2StorageConfig.java @@ -16,14 +16,12 @@ * */ -package org.apache.skywalking.oap.server.core.storage; +package org.apache.skywalking.oap.server.storage.plugin.jdbc.h2; + +import org.apache.skywalking.oap.server.library.module.ModuleConfig; /** - * @author peng-yongsheng + * @author wusheng */ -public class StorageException extends Exception { - - public StorageException(String message) { - super(message); - } +public class H2StorageConfig extends ModuleConfig { } diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/H2StorageProvider.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/H2StorageProvider.java new file mode 100644 index 0000000..feb0d9d --- /dev/null +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/H2StorageProvider.java @@ -0,0 +1,118 @@ +/* + * 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.h2; + +import java.util.Properties; +import org.apache.skywalking.oap.server.core.storage.IBatchDAO; +import org.apache.skywalking.oap.server.core.storage.IRegisterLockDAO; +import org.apache.skywalking.oap.server.core.storage.StorageDAO; +import org.apache.skywalking.oap.server.core.storage.StorageException; +import org.apache.skywalking.oap.server.core.storage.StorageModule; +import org.apache.skywalking.oap.server.core.storage.cache.IEndpointInventoryCacheDAO; +import org.apache.skywalking.oap.server.core.storage.cache.INetworkAddressInventoryCacheDAO; +import org.apache.skywalking.oap.server.core.storage.cache.IServiceInstanceInventoryCacheDAO; +import org.apache.skywalking.oap.server.core.storage.cache.IServiceInventoryCacheDAO; +import org.apache.skywalking.oap.server.core.storage.query.ITopologyQueryDAO; +import org.apache.skywalking.oap.server.library.client.ClientException; +import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCHikariCPClient; +import org.apache.skywalking.oap.server.library.module.ModuleConfig; +import org.apache.skywalking.oap.server.library.module.ModuleDefine; +import org.apache.skywalking.oap.server.library.module.ModuleProvider; +import org.apache.skywalking.oap.server.library.module.ModuleStartException; +import org.apache.skywalking.oap.server.library.module.ServiceNotProvidedException; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2BatchDAO; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2EndpointInventoryCacheDAO; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2NetworkAddressInventoryCacheDAO; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2RegisterLockDAO; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2ServiceInstanceInventoryCacheDAO; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2ServiceInventoryCacheDAO; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2StorageDAO; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2TableInstaller; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao.H2TopologyQueryDAO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author wusheng + */ +public class H2StorageProvider extends ModuleProvider { + + private static final Logger logger = LoggerFactory.getLogger(H2StorageProvider.class); + + private H2StorageConfig config; + private JDBCHikariCPClient h2Client; + + public H2StorageProvider() { + config = new H2StorageConfig(); + } + + @Override public String name() { + return "h2"; + } + + @Override public Class<? extends ModuleDefine> module() { + return StorageModule.class; + } + + @Override public ModuleConfig createConfigBeanIfAbsent() { + return config; + } + + @Override public void prepare() throws ServiceNotProvidedException, ModuleStartException { + Properties settings = new Properties(); + settings.setProperty("dataSourceClassName", "org.h2.jdbcx.JdbcDataSource"); + settings.setProperty("dataSource.url", "jdbc:h2:mem:collector"); + settings.setProperty("dataSource.user", ""); + settings.setProperty("dataSource.password", ""); + h2Client = new JDBCHikariCPClient(settings); + + this.registerServiceImplementation(IBatchDAO.class, new H2BatchDAO()); + this.registerServiceImplementation(StorageDAO.class, new H2StorageDAO(h2Client)); + this.registerServiceImplementation(IRegisterLockDAO.class, new H2RegisterLockDAO()); + + this.registerServiceImplementation(IServiceInventoryCacheDAO.class, new H2ServiceInventoryCacheDAO()); + this.registerServiceImplementation(IServiceInstanceInventoryCacheDAO.class, new H2ServiceInstanceInventoryCacheDAO()); + this.registerServiceImplementation(IEndpointInventoryCacheDAO.class, new H2EndpointInventoryCacheDAO()); + this.registerServiceImplementation(INetworkAddressInventoryCacheDAO.class, new H2NetworkAddressInventoryCacheDAO()); + + this.registerServiceImplementation(ITopologyQueryDAO.class, new H2TopologyQueryDAO()); + + } + + @Override public void start() throws ServiceNotProvidedException, ModuleStartException { + try { + h2Client.initialize(); + + H2TableInstaller installer = new H2TableInstaller(getManager()); + installer.install(h2Client); + } catch (StorageException e) { + throw new ModuleStartException(e.getMessage(), e); + } catch (ClientException e) { + throw new ModuleStartException(e.getMessage(), e); + } + } + + @Override public void notifyAfterCompleted() throws ServiceNotProvidedException, ModuleStartException { + + } + + @Override public String[] requiredModules() { + return new String[0]; + } +} diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/StorageException.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2BatchDAO.java similarity index 74% copy from oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/StorageException.java copy to oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2BatchDAO.java index b9a4d94..a252733 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/StorageException.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2BatchDAO.java @@ -16,14 +16,13 @@ * */ -package org.apache.skywalking.oap.server.core.storage; +package org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao; -/** - * @author peng-yongsheng - */ -public class StorageException extends Exception { +import java.util.List; +import org.apache.skywalking.oap.server.core.storage.IBatchDAO; + +public class H2BatchDAO implements IBatchDAO { + @Override public void batchPersistence(List<?> batchCollection) { - public StorageException(String message) { - super(message); } } diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2EndpointInventoryCacheDAO.java similarity index 62% copy from oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java copy to oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2EndpointInventoryCacheDAO.java index e8ee50a..5f61d4a 100644 --- a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2EndpointInventoryCacheDAO.java @@ -16,20 +16,20 @@ * */ -package org.apache.skywalking.oap.server.library.client.shardingjdbc; +package org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao; -import org.apache.skywalking.oap.server.library.client.ClientException; +import org.apache.skywalking.oap.server.core.register.EndpointInventory; +import org.apache.skywalking.oap.server.core.storage.cache.IEndpointInventoryCacheDAO; /** - * @author linjiaqi + * @author wusheng */ -public class ShardingjdbcClientException extends ClientException { - - public ShardingjdbcClientException(String message) { - super(message); +public class H2EndpointInventoryCacheDAO implements IEndpointInventoryCacheDAO { + @Override public int getEndpointId(int serviceId, String endpointName) { + return 0; } - public ShardingjdbcClientException(String message, Throwable cause) { - super(message, cause); + @Override public EndpointInventory get(int endpointId) { + return null; } } 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/H2IndicatorDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2IndicatorDAO.java new file mode 100644 index 0000000..6043079 --- /dev/null +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2IndicatorDAO.java @@ -0,0 +1,124 @@ +/* + * 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.h2.dao; + +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.skywalking.oap.server.core.analysis.indicator.Indicator; +import org.apache.skywalking.oap.server.core.storage.IIndicatorDAO; +import org.apache.skywalking.oap.server.core.storage.StorageBuilder; +import org.apache.skywalking.oap.server.core.storage.model.ModelColumn; +import org.apache.skywalking.oap.server.core.storage.type.StorageDataType; +import org.apache.skywalking.oap.server.library.client.jdbc.JDBCClientException; +import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCHikariCPClient; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.SQLBuilder; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.SQLExecutor; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.TableMetaInfo; + +/** + * @author wusheng + */ +public class H2IndicatorDAO implements IIndicatorDAO<SQLExecutor, SQLExecutor> { + private JDBCHikariCPClient h2Client; + private StorageBuilder<Indicator> storageBuilder; + + public H2IndicatorDAO(JDBCHikariCPClient h2Client, StorageBuilder<Indicator> storageBuilder) { + this.h2Client = h2Client; + this.storageBuilder = storageBuilder; + } + + @Override public Indicator get(String modelName, Indicator indicator) throws IOException { + try (ResultSet rs = h2Client.executeQuery("SELECT * FROM " + modelName + " WHERE entity_id = ?", new Object[] {indicator.id()})) { + while (rs.next()) { + Map data = new HashMap(); + List<ModelColumn> columns = TableMetaInfo.get(modelName).getColumns(); + for (ModelColumn column : columns) { + data.put(column.getColumnName(), rs.getObject(column.getColumnName().getName())); + } + return storageBuilder.map2Data(data); + } + } catch (SQLException e) { + throw new IOException(e.getMessage(), e); + } catch (JDBCClientException e) { + throw new IOException(e.getMessage(), e); + } + return null; + } + + @Override public SQLExecutor prepareBatchInsert(String modelName, Indicator indicator) throws IOException { + Map<String, Object> objectMap = storageBuilder.data2Map(indicator); + + SQLBuilder sqlBuilder = new SQLBuilder("INSERT INTO " + modelName + " VALUES"); + List<ModelColumn> columns = TableMetaInfo.get(modelName).getColumns(); + List<Object> param = new ArrayList<>(); + sqlBuilder.append("("); + for (int i = 0; i < columns.size(); i++) { + ModelColumn column = columns.get(i); + sqlBuilder.append("?"); + if (i != columns.size()) { + sqlBuilder.append(","); + } + + Object value = objectMap.get(column.getColumnName().getName()); + if (value instanceof StorageDataType) { + param.add(((StorageDataType)value).toStorageData()); + } else { + param.add(value); + } + } + sqlBuilder.append(")"); + + return new SQLExecutor(sqlBuilder.toString(), param); + } + + @Override public SQLExecutor prepareBatchUpdate(String modelName, Indicator indicator) throws IOException { + Map<String, Object> objectMap = storageBuilder.data2Map(indicator); + + SQLBuilder sqlBuilder = new SQLBuilder("UPDATE " + modelName + " SET "); + List<ModelColumn> columns = TableMetaInfo.get(modelName).getColumns(); + List<Object> param = new ArrayList<>(); + for (int i = 0; i < columns.size(); i++) { + ModelColumn column = columns.get(i); + sqlBuilder.append(column.getColumnName().getName() + "= ?"); + if (i != columns.size()) { + sqlBuilder.append(","); + } + + Object value = objectMap.get(column.getColumnName().getName()); + if (value instanceof StorageDataType) { + param.add(((StorageDataType)value).toStorageData()); + } else { + param.add(value); + } + } + sqlBuilder.append(" WHERE entity_id = ?"); + param.add(indicator.id()); + + return new SQLExecutor(sqlBuilder.toString(), param); + } + + @Override public void deleteHistory(String modelName, Long timeBucketBefore) { + + } +} diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2NetworkAddressInventoryCacheDAO.java similarity index 61% copy from oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java copy to oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2NetworkAddressInventoryCacheDAO.java index e8ee50a..7a11ed6 100644 --- a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2NetworkAddressInventoryCacheDAO.java @@ -16,20 +16,20 @@ * */ -package org.apache.skywalking.oap.server.library.client.shardingjdbc; +package org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao; -import org.apache.skywalking.oap.server.library.client.ClientException; +import org.apache.skywalking.oap.server.core.register.NetworkAddressInventory; +import org.apache.skywalking.oap.server.core.storage.cache.INetworkAddressInventoryCacheDAO; /** - * @author linjiaqi + * @author wusheng */ -public class ShardingjdbcClientException extends ClientException { - - public ShardingjdbcClientException(String message) { - super(message); +public class H2NetworkAddressInventoryCacheDAO implements INetworkAddressInventoryCacheDAO { + @Override public int getAddressId(String networkAddress) { + return 0; } - public ShardingjdbcClientException(String message, Throwable cause) { - super(message, cause); + @Override public NetworkAddressInventory get(int addressId) { + return null; } } diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2RegisterLockDAO.java similarity index 61% copy from oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java copy to oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2RegisterLockDAO.java index e8ee50a..fba9e09 100644 --- a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2RegisterLockDAO.java @@ -16,20 +16,22 @@ * */ -package org.apache.skywalking.oap.server.library.client.shardingjdbc; +package org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao; -import org.apache.skywalking.oap.server.library.client.ClientException; +import org.apache.skywalking.oap.server.core.source.Scope; +import org.apache.skywalking.oap.server.core.storage.IRegisterLockDAO; /** - * @author linjiaqi + * No need to create any lock table. In SQL based database, could use `select... for update` to avoid lock table. + * + * @author wusheng */ -public class ShardingjdbcClientException extends ClientException { - - public ShardingjdbcClientException(String message) { - super(message); +public class H2RegisterLockDAO implements IRegisterLockDAO { + @Override public boolean tryLock(Scope scope) { + return false; } - public ShardingjdbcClientException(String message, Throwable cause) { - super(message, cause); + @Override public void releaseLock(Scope scope) { + } } diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/h2/H2ClientConfig.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2ServiceInstanceInventoryCacheDAO.java similarity index 56% rename from oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/h2/H2ClientConfig.java rename to oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2ServiceInstanceInventoryCacheDAO.java index 764d906..a715cb1 100644 --- a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/h2/H2ClientConfig.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2ServiceInstanceInventoryCacheDAO.java @@ -16,38 +16,24 @@ * */ -package org.apache.skywalking.oap.server.library.client.h2; +package org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao; + +import org.apache.skywalking.oap.server.core.register.ServiceInstanceInventory; +import org.apache.skywalking.oap.server.core.storage.cache.IServiceInstanceInventoryCacheDAO; /** - * @author peng-yongsheng + * @author wusheng */ -public abstract class H2ClientConfig { - - private String url; - private String userName; - private String password; - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public String getUserName() { - return userName; - } - - public void setUserName(String userName) { - this.userName = userName; +public class H2ServiceInstanceInventoryCacheDAO implements IServiceInstanceInventoryCacheDAO { + @Override public ServiceInstanceInventory get(int serviceInstanceId) { + return null; } - public String getPassword() { - return password; + @Override public int getServiceInstanceId(int serviceId, String serviceInstanceName) { + return 0; } - public void setPassword(String password) { - this.password = password; + @Override public int getServiceInstanceId(int serviceId, int addressId) { + return 0; } } diff --git a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2ServiceInventoryCacheDAO.java similarity index 59% rename from oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java rename to oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2ServiceInventoryCacheDAO.java index e8ee50a..50e882e 100644 --- a/oap-server/server-library/library-client/src/main/java/org/apache/skywalking/oap/server/library/client/shardingjdbc/ShardingjdbcClientException.java +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2ServiceInventoryCacheDAO.java @@ -16,20 +16,24 @@ * */ -package org.apache.skywalking.oap.server.library.client.shardingjdbc; +package org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao; -import org.apache.skywalking.oap.server.library.client.ClientException; +import org.apache.skywalking.oap.server.core.register.ServiceInventory; +import org.apache.skywalking.oap.server.core.storage.cache.IServiceInventoryCacheDAO; /** - * @author linjiaqi + * @author wusheng */ -public class ShardingjdbcClientException extends ClientException { +public class H2ServiceInventoryCacheDAO implements IServiceInventoryCacheDAO { + @Override public int getServiceId(String serviceName) { + return 0; + } - public ShardingjdbcClientException(String message) { - super(message); + @Override public int getServiceId(int addressId) { + return 0; } - public ShardingjdbcClientException(String message, Throwable cause) { - super(message, cause); + @Override public ServiceInventory get(int serviceId) { + return null; } } 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/H2StorageDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2StorageDAO.java new file mode 100644 index 0000000..2f18d2c --- /dev/null +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2StorageDAO.java @@ -0,0 +1,46 @@ +/* + * 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.h2.dao; + +import org.apache.skywalking.oap.server.core.analysis.indicator.Indicator; +import org.apache.skywalking.oap.server.core.register.RegisterSource; +import org.apache.skywalking.oap.server.core.storage.IIndicatorDAO; +import org.apache.skywalking.oap.server.core.storage.IRegisterDAO; +import org.apache.skywalking.oap.server.core.storage.StorageBuilder; +import org.apache.skywalking.oap.server.core.storage.StorageDAO; +import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCHikariCPClient; + +/** + * @author wusheng + */ +public class H2StorageDAO implements StorageDAO { + private JDBCHikariCPClient h2Client; + + public H2StorageDAO(JDBCHikariCPClient h2Client) { + this.h2Client = h2Client; + } + + @Override public IIndicatorDAO newIndicatorDao(StorageBuilder<Indicator> storageBuilder) { + return new H2IndicatorDAO(h2Client, storageBuilder); + } + + @Override public IRegisterDAO newRegisterDao(StorageBuilder<RegisterSource> storageBuilder) { + return null; + } +} 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/H2TableInstaller.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2TableInstaller.java new file mode 100644 index 0000000..815bf63 --- /dev/null +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2TableInstaller.java @@ -0,0 +1,108 @@ +/* + * 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.h2.dao; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import org.apache.skywalking.oap.server.core.analysis.indicator.IntKeyLongValueArray; +import org.apache.skywalking.oap.server.core.storage.StorageException; +import org.apache.skywalking.oap.server.core.storage.model.ColumnName; +import org.apache.skywalking.oap.server.core.storage.model.Model; +import org.apache.skywalking.oap.server.core.storage.model.ModelColumn; +import org.apache.skywalking.oap.server.core.storage.model.ModelInstaller; +import org.apache.skywalking.oap.server.library.client.Client; +import org.apache.skywalking.oap.server.library.client.jdbc.JDBCClientException; +import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCHikariCPClient; +import org.apache.skywalking.oap.server.library.module.ModuleManager; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.SQLBuilder; +import org.apache.skywalking.oap.server.storage.plugin.jdbc.TableMetaInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class H2TableInstaller extends ModelInstaller { + private static final Logger logger = LoggerFactory.getLogger(H2TableInstaller.class); + + public H2TableInstaller(ModuleManager moduleManager) { + super(moduleManager); + } + + @Override protected boolean isExists(Client client, Model model) throws StorageException { + JDBCHikariCPClient h2Client = (JDBCHikariCPClient)client; + try (Connection conn = h2Client.getConnection()) { + try (ResultSet rset = conn.getMetaData().getTables(null, null, model.getName(), null)) { + if (rset.next()) { + return true; + } + } + } catch (SQLException e) { + throw new StorageException(e.getMessage(), e); + } catch (JDBCClientException e) { + throw new StorageException(e.getMessage(), e); + } + return false; + } + + @Override protected void columnCheck(Client client, Model model) throws StorageException { + + } + + @Override protected void deleteTable(Client client, Model model) throws StorageException { + + } + + @Override protected void createTable(Client client, Model model) throws StorageException { + TableMetaInfo.addModel(model); + JDBCHikariCPClient h2Client = (JDBCHikariCPClient)client; + SQLBuilder tableCreateSQL = new SQLBuilder("CREATE TABLE " + model.getName() + " ("); + for (int i = 0; i < model.getColumns().size(); i++) { + ModelColumn column = model.getColumns().get(i); + ColumnName name = column.getColumnName(); + tableCreateSQL.appendLine(name.getName() + " " + getColumnType(column.getType()) + (i != model.getColumns().size() - 1 ? "," : "")); + } + tableCreateSQL.appendLine(")"); + + if (logger.isDebugEnabled()) { + logger.debug("creating table: " + tableCreateSQL.toStringInNewLine()); + } + + try { + h2Client.execute(tableCreateSQL.toString()); + } catch (JDBCClientException e) { + throw new StorageException(e.getMessage(), e); + } + + } + + private String getColumnType(Class<?> type) { + if (Integer.class.equals(type) || int.class.equals(type)) { + return "INT"; + } else if (Long.class.equals(type) || long.class.equals(type)) { + return "BIGINT"; + } else if (Double.class.equals(type) || double.class.equals(type)) { + return "DOUBLE"; + } else if (String.class.equals(type)) { + return "VARCHAR(2000)"; + } else if (IntKeyLongValueArray.class.equals(type)) { + return "VARCHAR(20000)"; + } else { + throw new IllegalArgumentException("Unsupported data type: " + type.getName()); + } + } +} 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/H2TopologyQueryDAO.java b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2TopologyQueryDAO.java new file mode 100644 index 0000000..06c0fcc --- /dev/null +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2TopologyQueryDAO.java @@ -0,0 +1,69 @@ +/* + * 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.h2.dao; + +import java.io.IOException; +import java.util.List; +import org.apache.skywalking.oap.server.core.query.entity.Call; +import org.apache.skywalking.oap.server.core.query.entity.Step; +import org.apache.skywalking.oap.server.core.source.ServiceComponent; +import org.apache.skywalking.oap.server.core.source.ServiceMapping; +import org.apache.skywalking.oap.server.core.storage.query.ITopologyQueryDAO; + +/** + * @author wusheng + */ +public class H2TopologyQueryDAO implements ITopologyQueryDAO { + @Override public List<Call> loadSpecifiedServerSideServiceRelations(Step step, long startTB, long endTB, + List<Integer> serviceIds) throws IOException { + return null; + } + + @Override public List<Call> loadSpecifiedClientSideServiceRelations(Step step, long startTB, long endTB, + List<Integer> serviceIds) throws IOException { + return null; + } + + @Override public List<Call> loadServerSideServiceRelations(Step step, long startTB, long endTB) throws IOException { + return null; + } + + @Override public List<Call> loadClientSideServiceRelations(Step step, long startTB, long endTB) throws IOException { + return null; + } + + @Override public List<ServiceMapping> loadServiceMappings(Step step, long startTB, long endTB) throws IOException { + return null; + } + + @Override + public List<ServiceComponent> loadServiceComponents(Step step, long startTB, long endTB) throws IOException { + return null; + } + + @Override public List<Call> loadSpecifiedDestOfServerSideEndpointRelations(Step step, long startTB, long endTB, + int destEndpointId) throws IOException { + return null; + } + + @Override public List<Call> loadSpecifiedSourceOfClientSideEndpointRelations(Step step, long startTB, long endTB, + int sourceEndpointId) throws IOException { + return null; + } +} diff --git a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/resources/META-INF/services/org.apache.skywalking.oap.server.library.module.ModuleProvider b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/resources/META-INF/services/org.apache.skywalking.oap.server.library.module.ModuleProvider new file mode 100644 index 0000000..86c2c56 --- /dev/null +++ b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/resources/META-INF/services/org.apache.skywalking.oap.server.library.module.ModuleProvider @@ -0,0 +1,19 @@ +# +# 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. +# +# + +org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.H2StorageProvider \ No newline at end of file
