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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2010, 2015 Oracle and/or its affiliates. All rights reserved.
*
*/
package repmgrtests;
import com.sleepycat.db.CheckpointConfig;
import com.sleepycat.db.Database;
import com.sleepycat.db.DatabaseConfig;
import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseType;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
import com.sleepycat.db.EventHandlerAdapter;
import com.sleepycat.db.ReplicationConfig;
import com.sleepycat.db.ReplicationHostAddress;
import com.sleepycat.db.ReplicationManagerAckPolicy;
import com.sleepycat.db.ReplicationManagerConnectionStatus;
import com.sleepycat.db.ReplicationManagerSiteConfig;
import com.sleepycat.db.ReplicationManagerSiteInfo;
import com.sleepycat.db.ReplicationManagerStartPolicy;
import com.sleepycat.db.ReplicationTimeoutType;
import com.sleepycat.db.VerboseConfig;
import static org.junit.Assert.*;
public class ConnectScript implements SimpleConnectTest.Ops {
private SimpleConnectTest.Config conf;
private Environment client;
public void setConfig(SimpleConnectTest.Config c) {
conf = c;
}
public void upgradeClient() throws Exception {
int[] remotePorts = new int[1];
remotePorts[0] = conf.masterPort;
EnvironmentConfig ec = makeBasicConfig(conf.clientPort, remotePorts);
client = new Environment(conf.clientDir, ec);
CheckpointConfig cc = new CheckpointConfig();
cc.setForce(true);
client.checkpoint(cc);
client.close();
MyEventHandler mon = new MyEventHandler();
ec.setEventHandler(mon);
client = new Environment(conf.clientDir, ec);
// For the "reverse" test, make it practically impossible that
// the client will retry connecting to the master after its
// initial failed attempt.
client.setReplicationTimeout(ReplicationTimeoutType.CONNECTION_RETRY,
conf.reverse ? Integer.MAX_VALUE : 1000000);
client.setReplicationConfig(ReplicationConfig.STRICT_2SITE, true);
client.replicationManagerStart(1, ReplicationManagerStartPolicy.REP_CLIENT);
}
public void shutdownClient() throws Exception {
client.close();
}
private EnvironmentConfig makeBasicConfig(int myPort, int[] remotePorts)
throws Exception
{
EnvironmentConfig ec = new EnvironmentConfig();
ec.setAllowCreate(true);
ec.setInitializeCache(true);
ec.setInitializeLocking(true);
ec.setInitializeLogging(true);
ec.setInitializeReplication(true);
ec.setTransactional(true);
ec.setReplicationManagerAckPolicy(ReplicationManagerAckPolicy.ALL);
ec.setRunRecovery(true);
ec.setThreaded(true);
ReplicationManagerSiteConfig conf =
new ReplicationManagerSiteConfig("localhost", myPort);
conf.setLocalSite(true);
conf.setLegacy(true);
ec.addReplicationManagerSite(conf);
for (int p : remotePorts) {
conf = new ReplicationManagerSiteConfig("localhost", p);
conf.setLegacy(true);
ec.addReplicationManagerSite(conf);
}
if (Boolean.getBoolean("VERB_REPLICATION"))
ec.setVerbose(VerboseConfig.REPLICATION, true);
return (ec);
}
class MyEventHandler extends EventHandlerAdapter {
private boolean done = false;
private boolean panic = false;
private boolean connFail = false;
@Override synchronized public void handleRepStartupDoneEvent() {
done = true;
notifyAll();
}
@Override synchronized public void handlePanicEvent() {
panic = true;
done = true;
notifyAll();
}
@Override
synchronized public void handleRepConnectTryFailedEvent() {
connFail = true;
notifyAll();
}
synchronized void awaitConnFailure() throws Exception {
for (;;) {
if (connFail) { break; }
wait();
if (panic)
throw new Exception("aborted by panic in DB");
}
}
synchronized void awaitStartupDone() throws Exception {
long deadline = System.currentTimeMillis() + 10000;
while (!done) {
long now = System.currentTimeMillis();
if (now >= deadline)
throw new Exception("timeout expired");
long duration = deadline - now;
wait(duration);
}
if (panic)
throw new Exception("aborted by panic in DB");
}
}
public void awaitClientConnFailure() throws Exception {
((MyEventHandler)client.getConfig().getEventHandler()).awaitConnFailure();
}
public void verifyClientConnect() throws Exception {
int pollLimit = 10;
for (int i=0; i<pollLimit; i++) {
ReplicationManagerSiteInfo[] si = client.getReplicationManagerSiteList();
ReplicationManagerSiteInfo inf = null;
for (ReplicationManagerSiteInfo in : si) {
ReplicationHostAddress addr = in.addr;
if (addr.port == conf.masterPort) {
inf = in;
break;
}
}
assertNotNull("other port not in site list", inf);
if (inf.getConnectionStatus() == ReplicationManagerConnectionStatus.CONNECTED) { return; }
Thread.sleep(1000);
}
fail("was not connected to remote site within " + pollLimit + " seconds");
}
}
|