Skip to content

Commit 21f5da4

Browse files
committed
Removing all copy/paste instances of the runMysqlScript sub and accompanying subs to enable it, and replacing them
with calls to the appropriate MySQLRunner calls.
1 parent a864ded commit 21f5da4

File tree

3 files changed

+40
-226
lines changed

3 files changed

+40
-226
lines changed

mysql-replicator/src/test/java/com/booking/replication/it/activeschema/ActiveSchemaTest.java

+14-79
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
import com.booking.replication.commons.services.ServicesProvider;
1414
import com.booking.replication.controller.WebServer;
1515
import com.booking.replication.coordinator.Coordinator;
16+
import com.booking.replication.it.util.MySQLRunner;
1617
import com.booking.replication.supplier.Supplier;
1718
import com.booking.replication.supplier.mysql.binlog.BinaryLogSupplier;
18-
import com.mysql.jdbc.Driver;
19-
import org.apache.commons.dbcp2.BasicDataSource;
2019
import org.apache.logging.log4j.LogManager;
2120
import org.apache.logging.log4j.Logger;
2221
import org.junit.AfterClass;
@@ -25,14 +24,22 @@
2524
import org.junit.Test;
2625

2726
import java.io.*;
28-
import java.sql.Connection;
29-
import java.sql.Statement;
3027
import java.util.*;
3128
import java.util.concurrent.TimeUnit;
3229

3330
public class ActiveSchemaTest {
3431

3532
private static final Logger LOG = LogManager.getLogger(ActiveSchemaTest.class);
33+
private static MySQLConfiguration MYSQL_CONFIG = new MySQLConfiguration(
34+
ActiveSchemaTest.MYSQL_SCHEMA,
35+
ActiveSchemaTest.MYSQL_USERNAME,
36+
ActiveSchemaTest.MYSQL_PASSWORD,
37+
ActiveSchemaTest.MYSQL_CONF_FILE,
38+
Collections.emptyList(),
39+
null,
40+
null
41+
);
42+
private static final MySQLRunner MYSQL_RUNNER = new MySQLRunner();
3643

3744
private static final String ZOOKEEPER_CHECKPOINT_PATH = "/replicator/checkpoint";
3845

@@ -55,18 +62,7 @@ public class ActiveSchemaTest {
5562
public static void before() throws InterruptedException {
5663
ServicesProvider servicesProvider = ServicesProvider.build(ServicesProvider.Type.CONTAINERS);
5764

58-
MySQLConfiguration mySQLConfiguration = new MySQLConfiguration(
59-
ActiveSchemaTest.MYSQL_SCHEMA,
60-
ActiveSchemaTest.MYSQL_USERNAME,
61-
ActiveSchemaTest.MYSQL_PASSWORD,
62-
ActiveSchemaTest.MYSQL_CONF_FILE,
63-
Collections.emptyList(),
64-
null,
65-
null
66-
);
67-
68-
69-
ActiveSchemaTest.mysqlBinaryLog = servicesProvider.startMySQL(mySQLConfiguration);
65+
ActiveSchemaTest.mysqlBinaryLog = servicesProvider.startMySQL(MYSQL_CONFIG);
7066

7167
try {
7268
Thread.sleep(15000L);
@@ -81,74 +77,13 @@ public void testReplicator() throws Exception {
8177

8278
File file = new File("src/test/resources/" + ActiveSchemaTest.MYSQL_TEST_SCRIPT);
8379

84-
runMysqlScripts(this.getConfiguration(), file.getAbsolutePath());
80+
MYSQL_RUNNER.runMysqlScript(this.mysqlBinaryLog, this.MYSQL_CONFIG, file.getAbsolutePath(), null, true);
8581

86-
replicator.wait(1L, TimeUnit.MINUTES);
82+
replicator.wait(15L, TimeUnit.SECONDS);
8783

8884
replicator.stop();
8985
}
9086

91-
private boolean runMysqlScripts(Map<String, Object> configuration, String scriptFilePath) {
92-
BufferedReader reader;
93-
Statement statement;
94-
BasicDataSource dataSource = initDatasource(configuration, Driver.class.getName());
95-
try (Connection connection = dataSource.getConnection()) {
96-
statement = connection.createStatement();
97-
reader = new BufferedReader(new FileReader(scriptFilePath));
98-
String line;
99-
// read script line by line
100-
ActiveSchemaTest.LOG.info("Executing query from " + scriptFilePath);
101-
String s;
102-
StringBuilder sb = new StringBuilder();
103-
104-
FileReader fr = new FileReader(new File(scriptFilePath));
105-
BufferedReader br = new BufferedReader(fr);
106-
while ((s = br.readLine()) != null) {
107-
sb.append(s);
108-
}
109-
br.close();
110-
111-
String[] inst = sb.toString().split(";");
112-
for (String query : inst) {
113-
if (!query.trim().equals("")) {
114-
statement.execute(query);
115-
ActiveSchemaTest.LOG.info(query);
116-
}
117-
}
118-
return true;
119-
} catch (Exception exception) {
120-
ActiveSchemaTest.LOG.warn(String.format("error executing query \"%s\": %s", scriptFilePath, exception.getMessage()));
121-
return false;
122-
}
123-
124-
}
125-
126-
private BasicDataSource initDatasource(Map<String, Object> configuration, Object driverClass) {
127-
List<String> hostnames = (List<String>) configuration.get(BinaryLogSupplier.Configuration.MYSQL_HOSTNAME);
128-
Object port = configuration.getOrDefault(BinaryLogSupplier.Configuration.MYSQL_PORT, "3306");
129-
Object schema = configuration.get(BinaryLogSupplier.Configuration.MYSQL_SCHEMA);
130-
Object username = configuration.get(BinaryLogSupplier.Configuration.MYSQL_USERNAME);
131-
Object password = configuration.get(BinaryLogSupplier.Configuration.MYSQL_PASSWORD);
132-
133-
Objects.requireNonNull(hostnames, String.format("Configuration required: %s", BinaryLogSupplier.Configuration.MYSQL_HOSTNAME));
134-
Objects.requireNonNull(schema, String.format("Configuration required: %s", BinaryLogSupplier.Configuration.MYSQL_SCHEMA));
135-
Objects.requireNonNull(username, String.format("Configuration required: %s", BinaryLogSupplier.Configuration.MYSQL_USERNAME));
136-
Objects.requireNonNull(password, String.format("Configuration required: %s", BinaryLogSupplier.Configuration.MYSQL_PASSWORD));
137-
138-
return this.getDataSource(driverClass.toString(), hostnames.get(0), Integer.parseInt(port.toString()), schema.toString(), username.toString(), password.toString());
139-
}
140-
141-
private BasicDataSource getDataSource(String driverClass, String hostname, int port, String schema, String username, String password) {
142-
BasicDataSource dataSource = new BasicDataSource();
143-
144-
dataSource.setDriverClassName(driverClass);
145-
dataSource.setUrl(String.format(CONNECTION_URL_FORMAT, hostname, port, schema));
146-
dataSource.setUsername(username);
147-
dataSource.setPassword(password);
148-
149-
return dataSource;
150-
}
151-
15287
private Map<String, Object> getConfiguration() {
15388
Map<String, Object> configuration = new HashMap<>();
15489

mysql-replicator/src/test/java/com/booking/replication/it/kafka/ReplicatorKafkaAvroTest.java

+13-73
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.booking.replication.controller.WebServer;
1717
import com.booking.replication.coordinator.Coordinator;
1818
import com.booking.replication.coordinator.ZookeeperCoordinator;
19+
import com.booking.replication.it.util.MySQLRunner;
1920
import com.booking.replication.supplier.Supplier;
2021
import com.booking.replication.supplier.mysql.binlog.BinaryLogSupplier;
2122

@@ -60,6 +61,16 @@
6061
public class ReplicatorKafkaAvroTest {
6162

6263
private static final Logger LOG = LogManager.getLogger(ReplicatorKafkaAvroTest.class);
64+
private static final MySQLRunner MYSQL_RUNNER = new MySQLRunner();
65+
private static final MySQLConfiguration MYSQL_CONFIG = new MySQLConfiguration(
66+
ReplicatorKafkaAvroTest.MYSQL_SCHEMA,
67+
ReplicatorKafkaAvroTest.MYSQL_USERNAME,
68+
ReplicatorKafkaAvroTest.MYSQL_PASSWORD,
69+
ReplicatorKafkaAvroTest.MYSQL_CONF_FILE,
70+
Collections.singletonList(ReplicatorKafkaAvroTest.MYSQL_INIT_SCRIPT),
71+
null,
72+
null
73+
);
6374

6475
private static final String ZOOKEEPER_LEADERSHIP_PATH = "/replicator/leadership";
6576
private static final String ZOOKEEPER_CHECKPOINT_PATH = "/replicator/checkpoint";
@@ -96,16 +107,6 @@ public static void before() {
96107

97108
ReplicatorKafkaAvroTest.zookeeper = servicesProvider.startZookeeper();
98109

99-
MySQLConfiguration mySQLConfiguration = new MySQLConfiguration(
100-
ReplicatorKafkaAvroTest.MYSQL_SCHEMA,
101-
ReplicatorKafkaAvroTest.MYSQL_USERNAME,
102-
ReplicatorKafkaAvroTest.MYSQL_PASSWORD,
103-
ReplicatorKafkaAvroTest.MYSQL_CONF_FILE,
104-
Collections.singletonList(ReplicatorKafkaAvroTest.MYSQL_INIT_SCRIPT),
105-
null,
106-
null
107-
);
108-
109110
MySQLConfiguration mySQLActiveSchemaConfiguration = new MySQLConfiguration(
110111
ReplicatorKafkaAvroTest.MYSQL_ACTIVE_SCHEMA,
111112
ReplicatorKafkaAvroTest.MYSQL_USERNAME,
@@ -116,7 +117,7 @@ public static void before() {
116117
null
117118
);
118119

119-
ReplicatorKafkaAvroTest.mysqlBinaryLog = servicesProvider.startMySQL(mySQLConfiguration);
120+
ReplicatorKafkaAvroTest.mysqlBinaryLog = servicesProvider.startMySQL(MYSQL_CONFIG);
120121
ReplicatorKafkaAvroTest.mysqlActiveSchema = servicesProvider.startMySQL(mySQLActiveSchemaConfiguration);
121122
Network network = Network.newNetwork();
122123
ReplicatorKafkaAvroTest.kafkaZk = servicesProvider.startZookeeper(network, "kafkaZk");
@@ -132,7 +133,7 @@ public void testReplicator() throws Exception {
132133

133134
File file = new File("src/test/resources/" + ReplicatorKafkaAvroTest.MYSQL_TEST_SCRIPT);
134135

135-
runMysqlScripts(this.getConfiguration(), file.getAbsolutePath());
136+
MYSQL_RUNNER.runMysqlScript(ReplicatorKafkaAvroTest.mysqlBinaryLog, MYSQL_CONFIG, file.getAbsolutePath(), null, true);
136137

137138
replicator.wait(1L, TimeUnit.MINUTES);
138139

@@ -166,67 +167,6 @@ public void testReplicator() throws Exception {
166167
replicator.stop();
167168
}
168169

169-
private boolean runMysqlScripts(Map<String, Object> configuration, String scriptFilePath) {
170-
BufferedReader reader;
171-
Statement statement;
172-
BasicDataSource dataSource = initDatasource(configuration, Driver.class.getName());
173-
try (Connection connection = dataSource.getConnection()) {
174-
statement = connection.createStatement();
175-
reader = new BufferedReader(new FileReader(scriptFilePath));
176-
String line;
177-
// read script line by line
178-
ReplicatorKafkaAvroTest.LOG.info("Executing query from " + scriptFilePath);
179-
String s;
180-
StringBuilder sb = new StringBuilder();
181-
182-
FileReader fr = new FileReader(new File(scriptFilePath));
183-
BufferedReader br = new BufferedReader(fr);
184-
while ((s = br.readLine()) != null) {
185-
sb.append(s);
186-
}
187-
br.close();
188-
189-
String[] inst = sb.toString().split(";");
190-
for (String query : inst) {
191-
if (!query.trim().equals("")) {
192-
statement.execute(query);
193-
ReplicatorKafkaAvroTest.LOG.info(query);
194-
}
195-
}
196-
return true;
197-
} catch (Exception exception) {
198-
ReplicatorKafkaAvroTest.LOG.warn(String.format("error executing query \"%s\": %s", scriptFilePath, exception.getMessage()));
199-
return false;
200-
}
201-
202-
}
203-
204-
private BasicDataSource initDatasource(Map<String, Object> configuration, Object driverClass) {
205-
List<String> hostnames = (List<String>) configuration.get(BinaryLogSupplier.Configuration.MYSQL_HOSTNAME);
206-
Object port = configuration.getOrDefault(BinaryLogSupplier.Configuration.MYSQL_PORT, "3306");
207-
Object schema = configuration.get(BinaryLogSupplier.Configuration.MYSQL_SCHEMA);
208-
Object username = configuration.get(BinaryLogSupplier.Configuration.MYSQL_USERNAME);
209-
Object password = configuration.get(BinaryLogSupplier.Configuration.MYSQL_PASSWORD);
210-
211-
Objects.requireNonNull(hostnames, String.format("Configuration required: %s", BinaryLogSupplier.Configuration.MYSQL_HOSTNAME));
212-
Objects.requireNonNull(schema, String.format("Configuration required: %s", BinaryLogSupplier.Configuration.MYSQL_SCHEMA));
213-
Objects.requireNonNull(username, String.format("Configuration required: %s", BinaryLogSupplier.Configuration.MYSQL_USERNAME));
214-
Objects.requireNonNull(password, String.format("Configuration required: %s", BinaryLogSupplier.Configuration.MYSQL_PASSWORD));
215-
216-
return this.getDataSource(driverClass.toString(), hostnames.get(0), Integer.parseInt(port.toString()), schema.toString(), username.toString(), password.toString());
217-
}
218-
219-
private BasicDataSource getDataSource(String driverClass, String hostname, int port, String schema, String username, String password) {
220-
BasicDataSource dataSource = new BasicDataSource();
221-
222-
dataSource.setDriverClassName(driverClass);
223-
dataSource.setUrl(String.format(CONNECTION_URL_FORMAT, hostname, port, schema));
224-
dataSource.setUsername(username);
225-
dataSource.setPassword(password);
226-
227-
return dataSource;
228-
}
229-
230170
private Map<String, Object> getConfiguration() {
231171
Map<String, Object> configuration = new HashMap<>();
232172

mysql-replicator/src/test/java/com/booking/replication/it/kafka/ReplicatorKafkaJSONTest.java

+13-74
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.booking.replication.controller.WebServer;
2121
import com.booking.replication.coordinator.Coordinator;
2222
import com.booking.replication.coordinator.ZookeeperCoordinator;
23+
import com.booking.replication.it.util.MySQLRunner;
2324
import com.booking.replication.supplier.Supplier;
2425
import com.booking.replication.supplier.mysql.binlog.BinaryLogSupplier;
2526
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -51,12 +52,22 @@
5152
public class ReplicatorKafkaJSONTest {
5253

5354
private static final Logger LOG = LogManager.getLogger(ReplicatorKafkaJSONTest.class);
55+
private static final MySQLRunner MYSQL_RUNNER = new MySQLRunner();
5456

5557
private static final String ZOOKEEPER_LEADERSHIP_PATH = "/replicator/leadership";
5658
private static final String ZOOKEEPER_CHECKPOINT_PATH = "/replicator/checkpoint";
5759

5860
private static final String CHECKPOINT_DEFAULT = "{\"timestamp\": 0, \"serverId\": 1, \"gtid\": null, \"binlog\": {\"filename\": \"binlog.000001\", \"position\": 4}}";
5961

62+
private static MySQLConfiguration MYSQL_CONFIG = new MySQLConfiguration(
63+
ReplicatorKafkaJSONTest.MYSQL_SCHEMA,
64+
ReplicatorKafkaJSONTest.MYSQL_USERNAME,
65+
ReplicatorKafkaJSONTest.MYSQL_PASSWORD,
66+
ReplicatorKafkaJSONTest.MYSQL_CONF_FILE,
67+
Collections.singletonList(ReplicatorKafkaJSONTest.MYSQL_INIT_SCRIPT),
68+
null,
69+
null
70+
);
6071
private static final String MYSQL_SCHEMA = "replicator";
6172
private static final String MYSQL_ROOT_USERNAME = "root";
6273
private static final String MYSQL_USERNAME = "replicator";
@@ -83,19 +94,8 @@ public class ReplicatorKafkaJSONTest {
8394
@BeforeClass
8495
public static void before() {
8596
ServicesProvider servicesProvider = ServicesProvider.build(ServicesProvider.Type.CONTAINERS);
86-
8797
ReplicatorKafkaJSONTest.zookeeper = servicesProvider.startZookeeper();
8898

89-
MySQLConfiguration mySQLConfiguration = new MySQLConfiguration(
90-
ReplicatorKafkaJSONTest.MYSQL_SCHEMA,
91-
ReplicatorKafkaJSONTest.MYSQL_USERNAME,
92-
ReplicatorKafkaJSONTest.MYSQL_PASSWORD,
93-
ReplicatorKafkaJSONTest.MYSQL_CONF_FILE,
94-
Collections.singletonList(ReplicatorKafkaJSONTest.MYSQL_INIT_SCRIPT),
95-
null,
96-
null
97-
);
98-
9999
MySQLConfiguration mySQLActiveSchemaConfiguration = new MySQLConfiguration(
100100
ReplicatorKafkaJSONTest.MYSQL_ACTIVE_SCHEMA,
101101
ReplicatorKafkaJSONTest.MYSQL_USERNAME,
@@ -106,7 +106,7 @@ public static void before() {
106106
null
107107
);
108108

109-
ReplicatorKafkaJSONTest.mysqlBinaryLog = servicesProvider.startMySQL(mySQLConfiguration);
109+
ReplicatorKafkaJSONTest.mysqlBinaryLog = servicesProvider.startMySQL(MYSQL_CONFIG);
110110
ReplicatorKafkaJSONTest.mysqlActiveSchema = servicesProvider.startMySQL(mySQLActiveSchemaConfiguration);
111111
Network network = Network.newNetwork();
112112
ReplicatorKafkaJSONTest.kafkaZk = servicesProvider.startZookeeper(network, "kafkaZk");
@@ -121,7 +121,7 @@ public void testReplicator() throws Exception {
121121

122122
File file = new File("src/test/resources/" + ReplicatorKafkaJSONTest.MYSQL_TEST_SCRIPT);
123123

124-
runMysqlScripts(this.getConfiguration(), file.getAbsolutePath());
124+
MYSQL_RUNNER.runMysqlScript( ReplicatorKafkaJSONTest.mysqlBinaryLog, MYSQL_CONFIG, file.getAbsolutePath(), null, true);
125125

126126
replicator.wait(1L, TimeUnit.MINUTES);
127127

@@ -211,67 +211,6 @@ public void testReplicator() throws Exception {
211211
replicator.stop();
212212
}
213213

214-
private boolean runMysqlScripts(Map<String, Object> configuration, String scriptFilePath) {
215-
BufferedReader reader;
216-
Statement statement;
217-
BasicDataSource dataSource = initDatasource(configuration, Driver.class.getName());
218-
try (Connection connection = dataSource.getConnection()) {
219-
statement = connection.createStatement();
220-
reader = new BufferedReader(new FileReader(scriptFilePath));
221-
String line;
222-
// read script line by line
223-
ReplicatorKafkaJSONTest.LOG.info("Executing query from " + scriptFilePath);
224-
String s;
225-
StringBuilder sb = new StringBuilder();
226-
227-
FileReader fr = new FileReader(new File(scriptFilePath));
228-
BufferedReader br = new BufferedReader(fr);
229-
while ((s = br.readLine()) != null) {
230-
sb.append(s);
231-
}
232-
br.close();
233-
234-
String[] inst = sb.toString().split(";");
235-
for (String query : inst) {
236-
if (!query.trim().equals("")) {
237-
statement.execute(query);
238-
ReplicatorKafkaJSONTest.LOG.info(query);
239-
}
240-
}
241-
return true;
242-
} catch (Exception exception) {
243-
ReplicatorKafkaJSONTest.LOG.warn(String.format("error executing query \"%s\": %s", scriptFilePath, exception.getMessage()));
244-
return false;
245-
}
246-
247-
}
248-
249-
private BasicDataSource initDatasource(Map<String, Object> configuration, Object driverClass) {
250-
List<String> hostnames = (List<String>) configuration.get(BinaryLogSupplier.Configuration.MYSQL_HOSTNAME);
251-
Object port = configuration.getOrDefault(BinaryLogSupplier.Configuration.MYSQL_PORT, "3306");
252-
Object schema = configuration.get(BinaryLogSupplier.Configuration.MYSQL_SCHEMA);
253-
Object username = configuration.get(BinaryLogSupplier.Configuration.MYSQL_USERNAME);
254-
Object password = configuration.get(BinaryLogSupplier.Configuration.MYSQL_PASSWORD);
255-
256-
Objects.requireNonNull(hostnames, String.format("Configuration required: %s", BinaryLogSupplier.Configuration.MYSQL_HOSTNAME));
257-
Objects.requireNonNull(schema, String.format("Configuration required: %s", BinaryLogSupplier.Configuration.MYSQL_SCHEMA));
258-
Objects.requireNonNull(username, String.format("Configuration required: %s", BinaryLogSupplier.Configuration.MYSQL_USERNAME));
259-
Objects.requireNonNull(password, String.format("Configuration required: %s", BinaryLogSupplier.Configuration.MYSQL_PASSWORD));
260-
261-
return this.getDataSource(driverClass.toString(), hostnames.get(0), Integer.parseInt(port.toString()), schema.toString(), username.toString(), password.toString());
262-
}
263-
264-
private BasicDataSource getDataSource(String driverClass, String hostname, int port, String schema, String username, String password) {
265-
BasicDataSource dataSource = new BasicDataSource();
266-
267-
dataSource.setDriverClassName(driverClass);
268-
dataSource.setUrl(String.format(CONNECTION_URL_FORMAT, hostname, port, schema));
269-
dataSource.setUsername(username);
270-
dataSource.setPassword(password);
271-
272-
return dataSource;
273-
}
274-
275214
private Map<String, Object> getConfiguration() {
276215
Map<String, Object> configuration = new HashMap<>();
277216

0 commit comments

Comments
 (0)