/
MultiHostsConnectionTest.java
423 lines (359 loc) · 14.6 KB
/
MultiHostsConnectionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* Copyright (c) 2007, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.test.hostchooser;
import static java.lang.Integer.parseInt;
import static java.util.Arrays.asList;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import static org.postgresql.hostchooser.HostRequirement.any;
import static org.postgresql.hostchooser.HostRequirement.preferSecondary;
import static org.postgresql.hostchooser.HostRequirement.primary;
import static org.postgresql.hostchooser.HostRequirement.secondary;
import static org.postgresql.hostchooser.HostStatus.Primary;
import static org.postgresql.hostchooser.HostStatus.Secondary;
import static org.postgresql.test.TestUtil.closeDB;
import org.postgresql.PGProperty;
import org.postgresql.hostchooser.GlobalHostStatusTracker;
import org.postgresql.hostchooser.HostRequirement;
import org.postgresql.test.TestUtil;
import org.postgresql.util.HostSpec;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class MultiHostsConnectionTest {
private static final String user = TestUtil.getUser();
private static final String password = TestUtil.getPassword();
private static final String primary1 = TestUtil.getServer() + ":" + TestUtil.getPort();
private static final String secondary1 = getSecondaryServer1() + ":" + getSecondaryPort1();
private static final String secondary2 = getSecondaryServer2() + ":" + getSecondaryPort2();
private static final String fake1 = "127.127.217.217:1";
private String primaryIp;
private String secondaryIP;
private String secondaryIP2;
private Connection con;
private Map<HostSpec, Object> hostStatusMap;
@BeforeClass
public static void setUpClass() {
assumeTrue(isReplicationInstanceAvailable());
}
@Before
public void setUp() throws Exception {
Field field = GlobalHostStatusTracker.class.getDeclaredField("hostStatusMap");
field.setAccessible(true);
hostStatusMap = (Map<HostSpec, Object>) field.get(null);
con = TestUtil.openDB();
primaryIp = getRemoteHostSpec();
closeDB(con);
con = openSecondaryDB();
secondaryIP = getRemoteHostSpec();
closeDB(con);
con = openSecondaryDB2();
secondaryIP2 = getRemoteHostSpec();
closeDB(con);
}
private static boolean isReplicationInstanceAvailable() {
try {
Connection connection = openSecondaryDB();
closeDB(connection);
return true;
} catch (Exception e) {
return false;
}
}
private static Connection openSecondaryDB() throws Exception {
TestUtil.initDriver();
Properties props = userAndPassword();
return DriverManager.getConnection(TestUtil.getURL(getSecondaryServer1(), getSecondaryPort1()), props);
}
private static Properties userAndPassword() {
Properties props = new Properties();
PGProperty.USER.set(props,TestUtil.getUser());
PGProperty.PASSWORD.set(props,TestUtil.getPassword());
return props;
}
private static String getSecondaryServer1() {
return System.getProperty("secondaryServer1", TestUtil.getServer());
}
private static int getSecondaryPort1() {
return Integer.parseInt(System.getProperty("secondaryPort1", String.valueOf(TestUtil.getPort() + 1)));
}
private static Connection openSecondaryDB2() throws Exception {
TestUtil.initDriver();
Properties props = userAndPassword();
return DriverManager.getConnection(TestUtil.getURL(getSecondaryServer2(), getSecondaryPort2()), props);
}
private static String getSecondaryServer2() {
return System.getProperty("secondaryServer2", TestUtil.getServer());
}
private static int getSecondaryPort2() {
return Integer.parseInt(System.getProperty("secondaryPort2", String.valueOf(TestUtil.getPort() + 2)));
}
private Connection getConnection(HostRequirement hostType, String... targets)
throws SQLException {
return getConnection(hostType, true, targets);
}
private static HostSpec hostSpec(String host) {
int split = host.indexOf(':');
return new HostSpec(host.substring(0, split), parseInt(host.substring(split + 1)));
}
private Connection getConnection(HostRequirement hostType, boolean reset,
String... targets) throws SQLException {
return getConnection(hostType, reset, false, targets);
}
private Connection getConnection(HostRequirement hostType, boolean reset, boolean lb,
String... targets) throws SQLException {
TestUtil.closeDB(con);
if (reset) {
resetGlobalState();
}
Properties props = new Properties();
PGProperty.USER.set(props,user);
PGProperty.PASSWORD.set(props,password);
PGProperty.TARGET_SERVER_TYPE.set(props,hostType.name());
PGProperty.HOST_RECHECK_SECONDS.set(props, 2);
if (lb) {
PGProperty.LOAD_BALANCE_HOSTS.set(props,"true");
}
StringBuilder sb = new StringBuilder();
sb.append("jdbc:postgresql://");
for (String target : targets) {
sb.append(target).append(',');
}
sb.setLength(sb.length() - 1);
sb.append("/");
sb.append(TestUtil.getDatabase());
return con = DriverManager.getConnection(sb.toString(), props);
}
private void assertRemote(String expectedHost) throws SQLException {
assertEquals(expectedHost, getRemoteHostSpec());
}
private String getRemoteHostSpec() throws SQLException {
ResultSet rs = con.createStatement()
.executeQuery("select inet_server_addr() || ':' || inet_server_port()");
rs.next();
return rs.getString(1);
}
public static boolean isMaster(Connection con) throws SQLException {
ResultSet rs = con.createStatement().executeQuery("show transaction_read_only");
rs.next();
return "off".equals(rs.getString(1));
}
private void assertGlobalState(String host, String status) {
HostSpec spec = hostSpec(host);
if (status == null) {
assertNull(hostStatusMap.get(spec));
} else {
assertEquals(host + "=" + status, hostStatusMap.get(spec).toString());
}
}
private void resetGlobalState() {
hostStatusMap.clear();
}
@Test
public void testConnectToAny() throws SQLException {
getConnection(any, fake1, primary1);
assertRemote(primaryIp);
assertGlobalState(primary1, "ConnectOK");
assertGlobalState(fake1, "ConnectFail");
getConnection(any, fake1, secondary1);
assertRemote(secondaryIP);
assertGlobalState(secondary1, "ConnectOK");
getConnection(any, fake1, primary1);
assertRemote(primaryIp);
assertGlobalState(primary1, "ConnectOK");
assertGlobalState(fake1, "ConnectFail");
}
@Test
public void testConnectToMaster() throws SQLException {
getConnection(primary, true, fake1, primary1, secondary1);
assertRemote(primaryIp);
assertGlobalState(fake1, "ConnectFail");
assertGlobalState(primary1, "Primary");
assertGlobalState(secondary1, null);
getConnection(primary, false, fake1, secondary1, primary1);
assertRemote(primaryIp);
assertGlobalState(fake1, "ConnectFail"); // cached
assertGlobalState(primary1, "Primary"); // connected to primary
assertGlobalState(secondary1, "Secondary"); // was unknown, so tried to connect in order
}
@Test
public void testConnectToPrimaryWithReadonlyTransactionMode() throws SQLException {
con = TestUtil.openPrivilegedDB();
con.createStatement().execute("ALTER DATABASE " + TestUtil.getDatabase() + " SET default_transaction_read_only=on;");
try {
getConnection(primary, true, fake1, primary1, secondary1);
} catch (PSQLException e) {
assertEquals(PSQLState.CONNECTION_UNABLE_TO_CONNECT.getState(), e.getSQLState());
assertGlobalState(fake1, "ConnectFail");
assertGlobalState(primary1, "Secondary");
assertGlobalState(secondary1, "Secondary");
} finally {
con = TestUtil.openPrivilegedDB();
con.createStatement().execute(
"BEGIN;"
+ "SET TRANSACTION READ WRITE;"
+ "ALTER DATABASE " + TestUtil.getDatabase() + " SET default_transaction_read_only=off;"
+ "COMMIT;"
);
TestUtil.closeDB(con);
}
}
@Test
public void testConnectToSecondary() throws SQLException {
getConnection(secondary, true, fake1, secondary1, primary1);
assertRemote(secondaryIP);
assertGlobalState(fake1, "ConnectFail");
assertGlobalState(secondary1, "Secondary");
assertGlobalState(primary1, null);
getConnection(secondary, false, fake1, primary1, secondary1);
assertRemote(secondaryIP);
assertGlobalState(fake1, "ConnectFail"); // cached
assertGlobalState(secondary1, "Secondary"); // connected
assertGlobalState(primary1, "Primary"); // tried as it was unknown
}
@Test
public void testConnectToSecondaryFirst() throws SQLException {
getConnection(preferSecondary, true, fake1, secondary1, primary1);
assertRemote(secondaryIP);
assertGlobalState(fake1, "ConnectFail");
assertGlobalState(secondary1, "Secondary");
assertGlobalState(primary1, null);
getConnection(secondary, false, fake1, primary1, secondary1);
assertRemote(secondaryIP);
assertGlobalState(fake1, "ConnectFail");
assertGlobalState(secondary1, "Secondary");
assertGlobalState(primary1, "Primary"); // tried as it was unknown
getConnection(preferSecondary, true, fake1, primary1, secondary1);
assertRemote(secondaryIP);
assertGlobalState(fake1, "ConnectFail");
assertGlobalState(secondary1, "Secondary");
assertGlobalState(primary1, "Primary");
}
@Test
public void testFailedConnection() throws SQLException {
try {
getConnection(any, true, fake1);
fail();
} catch (PSQLException ex) {
}
}
@Test
public void testLoadBalancing() throws SQLException {
Set<String> connectedHosts = new HashSet<String>();
boolean fake1FoundTried = false;
for (int i = 0; i < 20; ++i) {
getConnection(any, true, true, fake1, primary1, secondary1);
connectedHosts.add(getRemoteHostSpec());
fake1FoundTried |= hostStatusMap.containsKey(hostSpec(fake1));
if (connectedHosts.size() == 2 && fake1FoundTried) {
break;
}
}
assertEquals("Never connected to all hosts", new HashSet<String>(asList(primaryIp, secondaryIP)),
connectedHosts);
assertTrue("Never tried to connect to fake node", fake1FoundTried);
}
@Test
public void testLoadBalancing_preferSecondary() throws SQLException {
Set<String> connectedHosts = new HashSet<String>();
Set<HostSpec> tryConnectedHosts = new HashSet<HostSpec>();
for (int i = 0; i < 20; ++i) {
getConnection(preferSecondary, true, true, fake1, primary1, secondary1, secondary2);
connectedHosts.add(getRemoteHostSpec());
tryConnectedHosts.addAll(hostStatusMap.keySet());
if (tryConnectedHosts.size() == 4) {
break;
}
}
assertEquals("Never connected to all secondary hosts", new HashSet<String>(asList(secondaryIP, secondaryIP2)),
connectedHosts);
assertEquals("Never tried to connect to fake node",4, tryConnectedHosts.size());
getConnection(preferSecondary, false, true, fake1, primary1, secondary1);
assertRemote(secondaryIP);
connectedHosts.clear();
for (int i = 0; i < 20; ++i) {
getConnection(preferSecondary, false, true, fake1, primary1, secondary1, secondary2);
connectedHosts.add(getRemoteHostSpec());
if (connectedHosts.size() == 2) {
break;
}
}
assertEquals("Never connected to all secondary hosts", new HashSet<String>(asList(secondaryIP, secondaryIP2)),
connectedHosts);
// connect to primary when there's no secondary
getConnection(preferSecondary, true, true, fake1, primary1);
assertRemote(primaryIp);
getConnection(preferSecondary, false, true, fake1, primary1);
assertRemote(primaryIp);
}
@Test
public void testLoadBalancing_secondary() throws SQLException {
Set<String> connectedHosts = new HashSet<String>();
Set<HostSpec> tryConnectedHosts = new HashSet<HostSpec>();
for (int i = 0; i < 20; ++i) {
getConnection(secondary, true, true, fake1, primary1, secondary1, secondary2);
connectedHosts.add(getRemoteHostSpec());
tryConnectedHosts.addAll(hostStatusMap.keySet());
if (tryConnectedHosts.size() == 4) {
break;
}
}
assertEquals("Did not attempt to connect to all secondary hosts", new HashSet<String>(asList(secondaryIP, secondaryIP2)),
connectedHosts);
assertEquals("Did not attempt to connect to primary and fake node", 4, tryConnectedHosts.size());
getConnection(preferSecondary, false, true, fake1, primary1, secondary1);
assertRemote(secondaryIP);
connectedHosts.clear();
for (int i = 0; i < 20; ++i) {
getConnection(secondary, false, true, fake1, primary1, secondary1, secondary2);
connectedHosts.add(getRemoteHostSpec());
if (connectedHosts.size() == 2) {
break;
}
}
assertEquals("Did not connect to all secondary hosts", new HashSet<String>(asList(secondaryIP, secondaryIP2)),
connectedHosts);
}
@Test
public void testHostRechecks() throws SQLException, InterruptedException {
GlobalHostStatusTracker.reportHostStatus(hostSpec(primary1), Secondary);
GlobalHostStatusTracker.reportHostStatus(hostSpec(secondary1), Primary);
GlobalHostStatusTracker.reportHostStatus(hostSpec(fake1), Secondary);
try {
getConnection(primary, false, fake1, secondary1, primary1);
fail();
} catch (SQLException ex) {
}
GlobalHostStatusTracker.reportHostStatus(hostSpec(primary1), Secondary);
GlobalHostStatusTracker.reportHostStatus(hostSpec(secondary1), Primary);
GlobalHostStatusTracker.reportHostStatus(hostSpec(fake1), Secondary);
SECONDS.sleep(3);
getConnection(primary, false, secondary1, fake1, primary1);
assertRemote(primaryIp);
}
@Test
public void testNoGoodHostsRechecksEverything() throws SQLException, InterruptedException {
GlobalHostStatusTracker.reportHostStatus(hostSpec(primary1), Secondary);
GlobalHostStatusTracker.reportHostStatus(hostSpec(secondary1), Secondary);
GlobalHostStatusTracker.reportHostStatus(hostSpec(fake1), Secondary);
getConnection(primary, false, secondary1, fake1, primary1);
assertRemote(primaryIp);
}
}