Skip to content

Commit

Permalink
[#11577] Update mariadb-plugin to support mariadb java client 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jaehong-kim committed Oct 15, 2024
1 parent 68e3525 commit eb07dd9
Show file tree
Hide file tree
Showing 15 changed files with 963 additions and 96 deletions.
16 changes: 16 additions & 0 deletions agent-module/agent-testweb/mariadb-jdbc-plugin-testweb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

## Install
```
$ mvnw -P pinpoint-mariadb-jdbc-plugin-testweb install -Dmaven.test.skip=true
```

## Run
```
$ mvnw -P pinpoint-mariadb-jdbc-plugin-testweb spring-boot:start
```
You can then access here: http://localhost:18080/

## Stop
```
$ mvnw -P pinpoint-mariadb-jdbc-plugin-testweb spring-boot:stop
```
62 changes: 62 additions & 0 deletions agent-module/agent-testweb/mariadb-jdbc-plugin-testweb/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-agent-testweb</artifactId>
<version>3.0.1-SNAPSHOT</version>
</parent>

<artifactId>pinpoint-mariadb-jdbc-plugin-testweb</artifactId>

<packaging>jar</packaging>

<properties>
<pinpoint.agent.jvmargument>
${pinpoint.agent.default.jvmargument}
</pinpoint.agent.jvmargument>
<spring-boot.version>${spring-boot2.version}</spring-boot.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-agent-testweb-commons</artifactId>
</dependency>

<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.4.1</version>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mariadb</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* Copyright 2022 NAVER Corp.
*
* Licensed 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 com.pinpoint.test.plugin;

import com.pinpoint.test.common.view.ApiLinkPage;
import com.pinpoint.test.common.view.HrefTag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

@RestController
public class MariaDBPluginController {
static final String STATEMENT_QUERY = "SELECT count(1) FROM playground";
static final String PREPARED_STATEMENT_QUERY = "SELECT * FROM playground where id = ?";
static final String PROCEDURE_NAME = "getPlaygroundByName";
static final String CALLABLE_STATEMENT_QUERY = "{ CALL " + PROCEDURE_NAME + "(?, ?) }";
static final String CALLABLE_STATEMENT_INPUT_PARAM = "TWO";
static final int CALLABLE_STATMENT_OUTPUT_PARAM_TYPE = Types.INTEGER;

private final RequestMappingHandlerMapping handlerMapping;

@Autowired
public MariaDBPluginController(RequestMappingHandlerMapping handlerMapping) {
this.handlerMapping = handlerMapping;
}

@GetMapping("/")
String welcome() {
Map<RequestMappingInfo, HandlerMethod> handlerMethods = this.handlerMapping.getHandlerMethods();
List<HrefTag> list = new ArrayList<>();
for (RequestMappingInfo info : handlerMethods.keySet()) {
for (String path : info.getDirectPaths()) {
list.add(HrefTag.of(path));
}
}
list.sort(Comparator.comparing(HrefTag::getPath));
return new ApiLinkPage("mariadb-jdbc-plugin-testweb")
.addHrefTag(list)
.build();
}


@RequestMapping(value = "/mariadb/execute1")
public String execute1() throws Exception {
executeStatement();
return "OK";
}

@RequestMapping(value = "/mariadb/execute2")
public String execute2() throws Exception {
executePreparedStatement();
return "OK";
}

@RequestMapping(value = "/mariadb/execute3")
public String execute3() throws Exception {
executeCallableStatement();
return "OK";
}

private final void executeStatement() throws Exception {
final int expectedResultSize = 1;
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
connection = getConnection();
statement = connection.createStatement();
rs = statement.executeQuery(STATEMENT_QUERY);
int resultCount = 0;
while (rs.next()) {
++resultCount;
}
} finally {
closeQuietly(rs);
closeQuietly(statement);
closeQuietly(connection);
}
}

private void executePreparedStatement() throws Exception {
final int expectedResultSize = 1;

Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = getConnection();
ps = connection.prepareStatement(PREPARED_STATEMENT_QUERY);
ps.setInt(1, 3);
rs = ps.executeQuery();
int resultCount = 0;
while (rs.next()) {
++resultCount;
}
} finally {
closeQuietly(rs);
closeQuietly(ps);
closeQuietly(connection);
}
}

private final void executeCallableStatement() throws Exception {

final int expectedResultSize = 1;
final int expectedTotalCount = 3;
final int expectedMatchingId = 2;
final String outputParamCountName = "outputParamCount";

Connection conn = null;
CallableStatement cs = null;
ResultSet rs = null;
try {
conn = getConnection();

cs = conn.prepareCall(CALLABLE_STATEMENT_QUERY);
cs.setString(1, CALLABLE_STATEMENT_INPUT_PARAM);
cs.registerOutParameter(2, CALLABLE_STATMENT_OUTPUT_PARAM_TYPE);

rs = cs.executeQuery();
int resultCount = 0;
while (rs.next()) {
++resultCount;
}
final int totalCount = cs.getInt(outputParamCountName);

} finally {
closeQuietly(rs);
closeQuietly(cs);
closeQuietly(conn);
}
}

private Connection getConnection() throws SQLException {
return DriverManager.getConnection(MariaDBServer.getUri(), "root", "");
}

private void closeQuietly(AutoCloseable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception ignored) {
// empty
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2022 NAVER Corp.
*
* Licensed 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 com.pinpoint.test.plugin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MariaDBPluginTestStarter {

public static void main(String[] args) {
SpringApplication.run(MariaDBPluginTestStarter.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.pinpoint.test.plugin;

public class MariaDBServer {
private static final int PORT = 33001;

public static String getUri() {
return "jdbc:mariadb://localhost:" + PORT + "/test";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Defined in commandlineArgument of agent-test pom.xml

server:
port: 18080

logging:
level:
root: info

springdoc:
swagger-ui:
path: /
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
USE test;
CREATE TABLE IF NOT EXISTS playground (id int(5) NOT NULL, name varchar(50) DEFAULT NULL);
INSERT INTO playground VALUES (1, 'ONE');
INSERT INTO playground VALUES (2, 'TWO');
INSERT INTO playground VALUES (3, 'THREE');

CREATE PROCEDURE getPlaygroundByName (IN inputParamName VARCHAR(50), OUT outputParamCount INT)
BEGIN
SELECT count(*) INTO outputParamCount FROM playground;
SELECT id, name FROM playground WHERE name = inputParamName ORDER BY id ASC;
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2022 NAVER Corp.
*
* Licensed 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 com.pinpoint.test.plugin.mongo;


import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.MariaDBContainer;
import org.testcontainers.containers.wait.strategy.Wait;

@Ignore
public class MariaDBTest {
public static final String DATABASE_NAME = "test";
public static final String USERNAME = "root";
public static final String PASSWORD = "";

private static MariaDBContainer<?> container;

@BeforeClass
public static void beforeClass() {
Assume.assumeTrue("Docker not enabled", DockerClientFactory.instance().isDockerAvailable());
container = new MariaDBContainer<>("mariadb:10.6.17");
container.waitingFor(Wait.forListeningPort());
container.withDatabaseName(DATABASE_NAME);
container.withUsername(USERNAME);
container.withPassword(PASSWORD);
container.withInitScript("init.sql");
container.start();

System.out.println("##host=" + container.getHost());
System.out.println("##port=" + container.getFirstMappedPort());
}

@AfterClass
public static void select() {
if (container != null) {
container.stop();
}
}

@Test
public void test() throws Exception {
System.out.println("TEST");
}
}
1 change: 1 addition & 0 deletions agent-module/agent-testweb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<module>ktor-plugin-testweb</module>
<module>closed-module-testweb</module>
<module>closed-module-testlib</module>
<module>mariadb-jdbc-plugin-testweb</module>
</modules>

<dependencyManagement>
Expand Down
Loading

0 comments on commit eb07dd9

Please sign in to comment.