Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UPDATE] Upgrade mysql connector version and remove mxj #1531

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions deploy-service/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
Expand Down Expand Up @@ -155,9 +155,21 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-mxj</artifactId>
<version>5.0.12</version>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016 Pinterest, Inc.
* Copyright (c) 2016-2024 Pinterest, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,159 +16,188 @@
package com.pinterest.deployservice.db;

import com.pinterest.deployservice.bean.UpdateStatement;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
* Utility class to insertOrUpdate MySQL datasource.
*/
/** Utility class to insertOrUpdate MySQL datasource. */
public class DatabaseUtil {

public static final int MAX_WAIT_TIME_FOR_CONN_IN_MS = 200;
public static final String MYSQL_JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final Logger LOG = LoggerFactory.getLogger(DatabaseUtil.class);
public static final int MAX_WAIT_TIME_FOR_CONN_IN_MS = 200;
public static final String MYSQL_JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final Logger LOG = LoggerFactory.getLogger(DatabaseUtil.class);

public static BasicDataSource createMysqlDataSource(String host, int port,
String user, String passwd, String poolSize) {
// autoReconnect by default is false
// TODO: queryTimeoutKillsConnection to true, "?queryTimeoutKillsConnection=true"
String url = String.format(
"jdbc:mysql://%s:%d/deploy?connectTimeout=5000&socketTimeout=3000&characterEncoding=UTF-8"
+ "&connectionCollation=utf8mb4_general_ci",
host, port);
LOG.info("mtls is disabled --- host:{}, port:{}, user:{}", host, port, user);
return createDataSource(MYSQL_JDBC_DRIVER, url, user, passwd, poolSize,
MAX_WAIT_TIME_FOR_CONN_IN_MS);
}
public static BasicDataSource createMysqlDataSource(
String host, int port, String user, String passwd, String poolSize) {
// autoReconnect by default is false
// TODO: queryTimeoutKillsConnection to true, "?queryTimeoutKillsConnection=true"
String url =
String.format(
"jdbc:mysql://%s:%d/deploy?connectTimeout=5000&socketTimeout=3000&characterEncoding=UTF-8"
+ "&connectionCollation=utf8mb4_general_ci",
host, port);
LOG.info("mtls is disabled --- host:{}, port:{}, user:{}", host, port, user);
return createDataSource(
MYSQL_JDBC_DRIVER, url, user, passwd, poolSize, MAX_WAIT_TIME_FOR_CONN_IN_MS);
}

public static BasicDataSource createMysqlDataSource(String host, int port,
String user, String passwd, String poolSize,
Map<String, String> connectionProperties) {
// autoReconnect by default is false
// TODO: queryTimeoutKillsConnection to true, "?queryTimeoutKillsConnection=true"
String url = String.format(
"jdbc:mysql://%s:%d/deploy?connectTimeout=5000&socketTimeout=3000&characterEncoding=UTF-8"
+ "&connectionCollation=utf8mb4_general_ci",
host, port);
LOG.info("mtls is enabled --- host:{}, port:{}, user:{}", host, port, user);
return createDataSource(MYSQL_JDBC_DRIVER, url, user, passwd, poolSize,
MAX_WAIT_TIME_FOR_CONN_IN_MS, connectionProperties);
}
public static BasicDataSource createMysqlDataSource(
String host,
int port,
String user,
String passwd,
String poolSize,
Map<String, String> connectionProperties) {
// autoReconnect by default is false
// TODO: queryTimeoutKillsConnection to true, "?queryTimeoutKillsConnection=true"
String url =
String.format(
"jdbc:mysql://%s:%d/deploy?connectTimeout=5000&socketTimeout=3000&characterEncoding=UTF-8"
+ "&connectionCollation=utf8mb4_general_ci",
host, port);
LOG.info("mtls is enabled --- host:{}, port:{}, user:{}", host, port, user);
return createDataSource(
MYSQL_JDBC_DRIVER,
url,
user,
passwd,
poolSize,
MAX_WAIT_TIME_FOR_CONN_IN_MS,
connectionProperties);
}

// Embedded mysql source, for unit test only
public static BasicDataSource createMXJDataSource(String dbName, String baseDir, int port) {
String
url =
String.format(
"jdbc:mysql:mxj://localhost:%d/%s?server"
+ ".basedir=%s&createDatabaseIfNotExist=true&server.initialize-user=true",
port, dbName, baseDir);
return createDataSource(MYSQL_JDBC_DRIVER, url, "tester", "passwd", "0:8:8:0",
MAX_WAIT_TIME_FOR_CONN_IN_MS);
}
// Embedded mysql source, for unit test only
public static BasicDataSource createMXJDataSource(String dbName, String baseDir, int port) {
String url =
String.format(
"jdbc:mysql:mxj://localhost:%d/%s?server"
+ ".basedir=%s&createDatabaseIfNotExist=true&server.initialize-user=true",
port, dbName, baseDir);
return createDataSource(
MYSQL_JDBC_DRIVER,
url,
"tester",
"passwd",
"0:8:8:0",
MAX_WAIT_TIME_FOR_CONN_IN_MS);
}

/**
* Create a MySQL datasource.
*
* @param url the url of the DB.
* @param user the user name to connect to MySQL as.
* @param passwd the password for the corresponding MySQL user.
* @param poolSize the connection pool size string, in the format of
* initialSize:maxActive:maxIdle:minIdle.
* @param maxWaitInMillis the max wait time in milliseconds to get a connection from the pool.
* @return a BasicDataSource for the target MySQL instance.
*/
public static BasicDataSource createDataSource(String driverClassName, String url,
String user, String passwd, String poolSize,
int maxWaitInMillis) {
return createDataSource(driverClassName, url, user, passwd, poolSize, maxWaitInMillis, null);
}
public static BasicDataSource createLocalDataSource(String url) {
return createDataSource(
MYSQL_JDBC_DRIVER, url, "root", "", "0:8:8:0", MAX_WAIT_TIME_FOR_CONN_IN_MS, null);
}

/**
* Create a MySQL datasource.
*
* @param url the url of the DB.
* @param user the user name to connect to MySQL as.
* @param passwd the password for the corresponding MySQL user.
* @param poolSize the connection pool size string, in the format of
* initialSize:maxActive:maxIdle:minIdle.
* @param maxWaitInMillis the max wait time in milliseconds to get a connection from the pool.
* @return a BasicDataSource for the target MySQL instance.
*/
public static BasicDataSource createDataSource(
String driverClassName,
String url,
String user,
String passwd,
String poolSize,
int maxWaitInMillis) {
return createDataSource(
driverClassName, url, user, passwd, poolSize, maxWaitInMillis, null);
}

public static BasicDataSource createDataSource(String driverClassName, String url,
String user, String passwd, String poolSize,
int maxWaitInMillis,
Map<String, String> connectionProperties) {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(passwd);
dataSource.setDefaultAutoCommit(true);
dataSource.setDefaultReadOnly(false);
public static BasicDataSource createDataSource(
String driverClassName,
String url,
String user,
String passwd,
String poolSize,
int maxWaitInMillis,
Map<String, String> connectionProperties) {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(passwd);
dataSource.setDefaultAutoCommit(true);
dataSource.setDefaultReadOnly(false);

// poolSize parsing, the poolsize string passed in the following format
// initialSize:maxActive:maxIdle:minIdle
String[] sizeStrs = poolSize.split(":");
dataSource.setInitialSize(Integer.parseInt(sizeStrs[0]));
dataSource.setMaxActive(Integer.parseInt(sizeStrs[1]));
dataSource.setMaxIdle(Integer.parseInt(sizeStrs[2]));
dataSource.setMinIdle(Integer.parseInt(sizeStrs[3]));
// poolSize parsing, the poolsize string passed in the following format
// initialSize:maxActive:maxIdle:minIdle
String[] sizeStrs = poolSize.split(":");
dataSource.setInitialSize(Integer.parseInt(sizeStrs[0]));
dataSource.setMaxActive(Integer.parseInt(sizeStrs[1]));
dataSource.setMaxIdle(Integer.parseInt(sizeStrs[2]));
dataSource.setMinIdle(Integer.parseInt(sizeStrs[3]));

dataSource.setValidationQuery("SELECT 1");
dataSource.setTestOnBorrow(true);
dataSource.setTestOnReturn(false);
dataSource.setTestWhileIdle(true);
dataSource.setMinEvictableIdleTimeMillis(5 * 60 * 1000);
dataSource.setTimeBetweenEvictionRunsMillis(3 * 60 * 1000);
// dataSource.setNumTestsPerEvictionRun(3);
// max wait in milliseconds for a connection.
dataSource.setMaxWait(maxWaitInMillis);
dataSource.setValidationQuery("SELECT 1");
dataSource.setTestOnBorrow(true);
dataSource.setTestOnReturn(false);
dataSource.setTestWhileIdle(true);
dataSource.setMinEvictableIdleTimeMillis(5 * 60 * 1000);
dataSource.setTimeBetweenEvictionRunsMillis(3 * 60 * 1000);
// dataSource.setNumTestsPerEvictionRun(3);
// max wait in milliseconds for a connection.
dataSource.setMaxWait(maxWaitInMillis);

if (connectionProperties != null) {
for (Map.Entry<String, String> entry : connectionProperties.entrySet()) {
LOG.info(
String.format("Add connection properties %s=%s", entry.getKey(), entry.getValue()));
dataSource.addConnectionProperty(
entry.getKey(),
entry.getValue()
);
}
}
if (connectionProperties != null) {
for (Map.Entry<String, String> entry : connectionProperties.entrySet()) {
LOG.info(
String.format(
"Add connection properties %s=%s",
entry.getKey(), entry.getValue()));
dataSource.addConnectionProperty(entry.getKey(), entry.getValue());
}
}

// force connection pool initialization.
Connection conn = null;
try {
// Here not getting the connection from ThreadLocal no need to worry about that.
conn = dataSource.getConnection();
LOG.info("mysql conn: {}", conn);
} catch (SQLException e) {
LOG.error(
String.format("Failed to get a db connection when creating DataSource, url = %s", url),
e);
} finally {
DbUtils.closeQuietly(conn);
// force connection pool initialization.
Connection conn = null;
try {
// Here not getting the connection from ThreadLocal no need to worry about that.
conn = dataSource.getConnection();
LOG.info("mysql conn: {}", conn);
} catch (SQLException e) {
LOG.error(
String.format(
"Failed to get a db connection when creating DataSource, url = %s",
url),
e);
} finally {
DbUtils.closeQuietly(conn);
}
LOG.info("mysql dataSource: {}", dataSource);
return dataSource;
}
LOG.info("mysql dataSource: {}", dataSource);
return dataSource;
}

public static void transactionalUpdate(BasicDataSource dataSource,
List<UpdateStatement> updateStatements) throws Exception {
QueryRunner queryRunner = new QueryRunner();
Connection connection = dataSource.getConnection();
boolean autoStatus = connection.getAutoCommit();
connection.setAutoCommit(false);
try {
for (UpdateStatement updateStatement : updateStatements) {
queryRunner
.update(connection, updateStatement.getStatement(), updateStatement.getValueArray());
}
connection.commit();
} catch (SQLException e) {
connection.rollback();
throw e;
} finally {
connection.setAutoCommit(autoStatus);
DbUtils.closeQuietly(connection);
public static void transactionalUpdate(
BasicDataSource dataSource, List<UpdateStatement> updateStatements) throws Exception {
QueryRunner queryRunner = new QueryRunner();
Connection connection = dataSource.getConnection();
boolean autoStatus = connection.getAutoCommit();
connection.setAutoCommit(false);
try {
for (UpdateStatement updateStatement : updateStatements) {
queryRunner.update(
connection,
updateStatement.getStatement(),
updateStatement.getValueArray());
}
connection.commit();
} catch (SQLException e) {
connection.rollback();
throw e;
} finally {
connection.setAutoCommit(autoStatus);
DbUtils.closeQuietly(connection);
}
}
}
}
Loading
Loading