forked from apache/solr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectionManagerTest.java
172 lines (147 loc) · 5.79 KB
/
ConnectionManagerTest.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.solr.cloud;
import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeoutException;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.cloud.ConnectionManager;
import org.apache.solr.common.cloud.SolrZkClient;
import org.apache.solr.common.util.SolrNamedThreadFactory;
import org.apache.zookeeper.TestableZooKeeper;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Ignore;
import org.junit.Test;
public class ConnectionManagerTest extends SolrTestCaseJ4 {
static final int TIMEOUT = 3000;
@Ignore
public void testConnectionManager() throws Exception {
// setup a SolrZkClient to do some getBaseUrlForNodeName testing
Path zkDir = createTempDir("zkData");
ZkTestServer server = new ZkTestServer(zkDir);
try {
server.run();
SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);
ConnectionManager cm = zkClient.getConnectionManager();
try {
assertFalse(cm.isLikelyExpired());
ZooKeeper zk = zkClient.getZooKeeper();
assertTrue(zk instanceof TestableZooKeeper);
((TestableZooKeeper) zk).testableConnloss();
server.expire(zkClient.getZooKeeper().getSessionId());
Thread.sleep(TIMEOUT);
assertTrue(cm.isLikelyExpired());
} finally {
cm.close();
zkClient.close();
}
} finally {
server.shutdown();
}
}
public void testLikelyExpired() throws Exception {
// setup a SolrZkClient to do some getBaseUrlForNodeName testing
Path zkDir = createTempDir("zkData");
ZkTestServer server = new ZkTestServer(zkDir);
try {
server.run();
SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT);
ConnectionManager cm = zkClient.getConnectionManager();
try {
assertFalse(cm.isLikelyExpired());
assertTrue(cm.isConnectedAndNotClosed());
cm.process(new WatchedEvent(EventType.None, KeeperState.Disconnected, ""));
// disconnect shouldn't immediately set likelyExpired
assertFalse(cm.isConnectedAndNotClosed());
assertFalse(cm.isLikelyExpired());
// but it should after the timeout
Thread.sleep((long) (zkClient.getZkClientTimeout() * 1.5));
assertFalse(cm.isConnectedAndNotClosed());
assertTrue(cm.isLikelyExpired());
// even if we disconnect immediately again
cm.process(new WatchedEvent(EventType.None, KeeperState.Disconnected, ""));
assertFalse(cm.isConnectedAndNotClosed());
assertTrue(cm.isLikelyExpired());
// reconnect -- should no longer be likely expired
cm.process(new WatchedEvent(EventType.None, KeeperState.SyncConnected, ""));
assertFalse(cm.isLikelyExpired());
assertTrue(cm.isConnectedAndNotClosed());
} finally {
cm.close();
zkClient.close();
}
} finally {
server.shutdown();
}
}
@Test
public void testReconnectWhenZkDisappeared() throws Exception {
ScheduledExecutorService executor =
Executors.newSingleThreadScheduledExecutor(
new SolrNamedThreadFactory("connectionManagerTest"));
// setup a SolrZkClient to do some getBaseUrlForNodeName testing
Path zkDir = createTempDir("zkData");
ZkTestServer server = new ZkTestServer(zkDir);
try {
server.run();
MockZkClientConnectionStrategy strategy = new MockZkClientConnectionStrategy();
SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT, strategy, null);
ConnectionManager cm = zkClient.getConnectionManager();
try {
assertFalse(cm.isLikelyExpired());
assertTrue(cm.isConnectedAndNotClosed());
// reconnect -- should no longer be likely expired
cm.process(new WatchedEvent(EventType.None, KeeperState.Expired, ""));
assertFalse(cm.isLikelyExpired());
assertTrue(cm.isConnectedAndNotClosed());
assertTrue(strategy.isExceptionThrow());
} finally {
cm.close();
zkClient.close();
executor.shutdown();
}
} finally {
server.shutdown();
}
}
private static class MockZkClientConnectionStrategy extends TestConnectionStrategy {
int called = 0;
boolean exceptionThrown = false;
@Override
public void reconnect(
final String serverAddress,
final int zkClientTimeout,
final Watcher watcher,
final ZkUpdate updater)
throws IOException, InterruptedException, TimeoutException {
if (called++ < 1) {
exceptionThrown = true;
throw new IOException("Testing");
}
super.reconnect(serverAddress, zkClientTimeout, watcher, updater);
}
public boolean isExceptionThrow() {
return exceptionThrown;
}
}
}