|
49 | 49 | import org.elasticsearch.common.transport.TransportAddress;
|
50 | 50 | import org.elasticsearch.common.unit.TimeValue;
|
51 | 51 | import org.elasticsearch.common.util.CancellableThreads;
|
| 52 | +import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; |
52 | 53 | import org.elasticsearch.common.util.concurrent.ThreadContext;
|
53 | 54 | import org.elasticsearch.common.xcontent.XContentBuilder;
|
54 | 55 | import org.elasticsearch.common.xcontent.XContentFactory;
|
|
97 | 98 | import static org.hamcrest.Matchers.iterableWithSize;
|
98 | 99 | import static org.hamcrest.Matchers.not;
|
99 | 100 | import static org.hamcrest.Matchers.notNullValue;
|
| 101 | +import static org.hamcrest.Matchers.nullValue; |
100 | 102 | import static org.hamcrest.Matchers.sameInstance;
|
101 | 103 | import static org.hamcrest.Matchers.startsWith;
|
102 | 104 |
|
@@ -1204,6 +1206,67 @@ public void onFailure(Exception e) {
|
1204 | 1206 | }
|
1205 | 1207 | }
|
1206 | 1208 |
|
| 1209 | + public void testPendingConnectListeners() throws IOException, InterruptedException { |
| 1210 | + List<DiscoveryNode> knownNodes = new CopyOnWriteArrayList<>(); |
| 1211 | + try (MockTransportService seedTransport = startTransport("seed_node", knownNodes, Version.CURRENT); |
| 1212 | + MockTransportService discoverableTransport = startTransport("discoverable_node", knownNodes, Version.CURRENT)) { |
| 1213 | + DiscoveryNode seedNode = seedTransport.getLocalDiscoNode(); |
| 1214 | + knownNodes.add(seedTransport.getLocalDiscoNode()); |
| 1215 | + knownNodes.add(discoverableTransport.getLocalDiscoNode()); |
| 1216 | + Collections.shuffle(knownNodes, random()); |
| 1217 | + try (MockTransportService service = MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, threadPool, null)) { |
| 1218 | + service.start(); |
| 1219 | + service.acceptIncomingRequests(); |
| 1220 | + final Settings settings = Settings.builder() |
| 1221 | + .put(RemoteClusterConnection.REMOTE_MAX_PENDING_CONNECTION_LISTENERS.getKey(), 1).build(); |
| 1222 | + try (RemoteClusterConnection connection = new RemoteClusterConnection(settings, "test-cluster", |
| 1223 | + seedNodes(seedNode), service, Integer.MAX_VALUE, n -> true, null, profile)) { |
| 1224 | + ConnectionManager connectionManager = connection.getConnectionManager(); |
| 1225 | + CountDownLatch connectionOpenedLatch = new CountDownLatch(1); |
| 1226 | + CountDownLatch connectionBlockedLatch = new CountDownLatch(1); |
| 1227 | + connectionManager.addListener(new TransportConnectionListener() { |
| 1228 | + @Override |
| 1229 | + public void onConnectionOpened(Transport.Connection connection) { |
| 1230 | + connectionOpenedLatch.countDown(); |
| 1231 | + try { |
| 1232 | + connectionBlockedLatch.await(); |
| 1233 | + } catch (InterruptedException e) { |
| 1234 | + throw new AssertionError(e); |
| 1235 | + } |
| 1236 | + } |
| 1237 | + }); |
| 1238 | + |
| 1239 | + Thread thread = new Thread(() -> connection.ensureConnected(ActionListener.wrap(() -> {}))); |
| 1240 | + thread.start(); |
| 1241 | + connectionOpenedLatch.await(); |
| 1242 | + connection.ensureConnected(ActionListener.wrap(() -> {})); |
| 1243 | + try { |
| 1244 | + int pendingConnections = randomIntBetween(1, 5); |
| 1245 | + for (int i = 0; i < pendingConnections; i++) { |
| 1246 | + AtomicReference<Exception> error = new AtomicReference<>(); |
| 1247 | + connection.ensureConnected(new ActionListener<Void>() { |
| 1248 | + @Override |
| 1249 | + public void onResponse(Void aVoid) { |
| 1250 | + |
| 1251 | + } |
| 1252 | + |
| 1253 | + @Override |
| 1254 | + public void onFailure(Exception e) { |
| 1255 | + error.set(e); |
| 1256 | + } |
| 1257 | + }); |
| 1258 | + assertThat(error.get(), not(nullValue())); |
| 1259 | + assertThat(error.get(), instanceOf(EsRejectedExecutionException.class)); |
| 1260 | + } |
| 1261 | + } finally { |
| 1262 | + connectionBlockedLatch.countDown(); |
| 1263 | + thread.join(); |
| 1264 | + } |
| 1265 | + } |
| 1266 | + } |
| 1267 | + } |
| 1268 | + } |
| 1269 | + |
1207 | 1270 | public void testCollectNodes() throws Exception {
|
1208 | 1271 | List<DiscoveryNode> knownNodes = new CopyOnWriteArrayList<>();
|
1209 | 1272 | try (MockTransportService seedTransport = startTransport("seed_node", knownNodes, Version.CURRENT)) {
|
|
0 commit comments