use of org.opensearch.common.util.concurrent.AbstractRunnable in project OpenSearch by opensearch-project.
the class HandshakingTransportAddressConnector method connectToRemoteMasterNode.
@Override
public void connectToRemoteMasterNode(TransportAddress transportAddress, ActionListener<DiscoveryNode> listener) {
transportService.getThreadPool().generic().execute(new AbstractRunnable() {
private final AbstractRunnable thisConnectionAttempt = this;
@Override
protected void doRun() {
// We could skip this if the transportService were already connected to the given address, but the savings would be minimal
// so we open a new connection anyway.
final DiscoveryNode targetNode = new DiscoveryNode("", transportAddress.toString(), // generated deterministically for reproducible tests
UUIDs.randomBase64UUID(Randomness.get()), transportAddress.address().getHostString(), transportAddress.getAddress(), transportAddress, emptyMap(), emptySet(), Version.CURRENT.minimumCompatibilityVersion());
logger.trace("[{}] opening probe connection", thisConnectionAttempt);
transportService.openConnection(targetNode, ConnectionProfile.buildSingleChannelProfile(Type.REG, probeConnectTimeout, probeHandshakeTimeout, TimeValue.MINUS_ONE, null), new ActionListener<Connection>() {
@Override
public void onResponse(Connection connection) {
logger.trace("[{}] opened probe connection", thisConnectionAttempt);
// use NotifyOnceListener to make sure the following line does not result in onFailure being called when
// the connection is closed in the onResponse handler
transportService.handshake(connection, probeHandshakeTimeout.millis(), new NotifyOnceListener<DiscoveryNode>() {
@Override
protected void innerOnResponse(DiscoveryNode remoteNode) {
try {
// success means (amongst other things) that the cluster names match
logger.trace("[{}] handshake successful: {}", thisConnectionAttempt, remoteNode);
IOUtils.closeWhileHandlingException(connection);
if (remoteNode.equals(transportService.getLocalNode())) {
listener.onFailure(new ConnectTransportException(remoteNode, "local node found"));
} else if (remoteNode.isMasterNode() == false) {
listener.onFailure(new ConnectTransportException(remoteNode, "non-cluster-manager-eligible node found"));
} else {
transportService.connectToNode(remoteNode, new ActionListener<Void>() {
@Override
public void onResponse(Void ignored) {
logger.trace("[{}] completed full connection with [{}]", thisConnectionAttempt, remoteNode);
listener.onResponse(remoteNode);
}
@Override
public void onFailure(Exception e) {
// we opened a connection and successfully performed a handshake, so we're definitely
// talking to a cluster-manager-eligible node with a matching cluster name and a good
// version,
// but the attempt to open a full connection to its publish address failed; a common
// reason is that the remote node is listening on 0.0.0.0 but has made an inappropriate
// choice for its publish address.
logger.warn(new ParameterizedMessage("[{}] completed handshake with [{}] but followup connection failed", thisConnectionAttempt, remoteNode), e);
listener.onFailure(e);
}
});
}
} catch (Exception e) {
listener.onFailure(e);
}
}
@Override
protected void innerOnFailure(Exception e) {
// we opened a connection and successfully performed a low-level handshake, so we were definitely
// talking to an Elasticsearch node, but the high-level handshake failed indicating some kind of
// mismatched configurations (e.g. cluster name) that the user should address
logger.warn(new ParameterizedMessage("handshake failed for [{}]", thisConnectionAttempt), e);
IOUtils.closeWhileHandlingException(connection);
listener.onFailure(e);
}
});
}
@Override
public void onFailure(Exception e) {
listener.onFailure(e);
}
});
}
@Override
public void onFailure(Exception e) {
listener.onFailure(e);
}
@Override
public String toString() {
return "connectToRemoteMasterNode[" + transportAddress + "]";
}
});
}
use of org.opensearch.common.util.concurrent.AbstractRunnable in project OpenSearch by opensearch-project.
the class AsyncBulkByScrollActionTests method testThreadPoolRejectionsAbortRequest.
/**
* Mimicks a ThreadPool rejecting execution of the task.
*/
public void testThreadPoolRejectionsAbortRequest() throws Exception {
worker.rethrottle(1);
setupClient(new TestThreadPool(getTestName()) {
@Override
public ScheduledCancellable schedule(Runnable command, TimeValue delay, String name) {
// While we're here we can check that the sleep made it through
assertThat(delay.nanos(), greaterThan(0L));
assertThat(delay.seconds(), lessThanOrEqualTo(10L));
final OpenSearchRejectedExecutionException exception = new OpenSearchRejectedExecutionException("test");
if (command instanceof AbstractRunnable) {
((AbstractRunnable) command).onRejection(exception);
return null;
} else {
throw exception;
}
}
});
ScrollableHitSource.Response response = new ScrollableHitSource.Response(false, emptyList(), 0, emptyList(), null);
simulateScrollResponse(new DummyAsyncBulkByScrollAction(), System.nanoTime(), 10, response);
ExecutionException e = expectThrows(ExecutionException.class, () -> listener.get());
assertThat(e.getCause(), instanceOf(OpenSearchRejectedExecutionException.class));
assertThat(e.getCause(), hasToString(containsString("test")));
assertThat(client.scrollsCleared, contains(scrollId));
// When the task is rejected we don't increment the throttled timer
assertEquals(timeValueMillis(0), testTask.getStatus().getThrottled());
}
use of org.opensearch.common.util.concurrent.AbstractRunnable in project OpenSearch by opensearch-project.
the class EvilThreadPoolTests method checkExecutionError.
private void checkExecutionError(Consumer<Runnable> runner) throws InterruptedException {
logger.info("checking error for {}", runner);
final Runnable runnable;
if (randomBoolean()) {
runnable = () -> {
throw new Error("future error");
};
} else {
runnable = new AbstractRunnable() {
@Override
public void onFailure(Exception e) {
}
@Override
protected void doRun() {
throw new Error("future error");
}
};
}
runExecutionTest(runner, runnable, true, o -> {
assertTrue(o.isPresent());
assertThat(o.get(), instanceOf(Error.class));
assertThat(o.get(), hasToString(containsString("future error")));
});
}
use of org.opensearch.common.util.concurrent.AbstractRunnable in project OpenSearch by opensearch-project.
the class EvilThreadPoolTests method checkExecutionException.
private void checkExecutionException(Consumer<Runnable> runner, boolean expectException) throws InterruptedException {
final Runnable runnable;
final boolean willThrow;
if (randomBoolean()) {
logger.info("checking direct exception for {}", runner);
runnable = () -> {
throw new IllegalStateException("future exception");
};
willThrow = expectException;
} else {
logger.info("checking abstract runnable exception for {}", runner);
runnable = new AbstractRunnable() {
@Override
public void onFailure(Exception e) {
}
@Override
protected void doRun() {
throw new IllegalStateException("future exception");
}
};
willThrow = false;
}
runExecutionTest(runner, runnable, willThrow, o -> {
assertEquals(willThrow, o.isPresent());
if (willThrow) {
if (o.get() instanceof Error)
throw (Error) o.get();
assertThat(o.get(), instanceOf(IllegalStateException.class));
assertThat(o.get(), hasToString(containsString("future exception")));
}
});
}
use of org.opensearch.common.util.concurrent.AbstractRunnable in project OpenSearch by opensearch-project.
the class RecoveryIT method asyncIndexDocs.
private Future<Void> asyncIndexDocs(String index, final int idStart, final int numDocs) throws IOException {
PlainActionFuture<Void> future = new PlainActionFuture<>();
Thread background = new Thread(new AbstractRunnable() {
@Override
public void onFailure(Exception e) {
future.onFailure(e);
}
@Override
protected void doRun() throws Exception {
indexDocs(index, idStart, numDocs);
future.onResponse(null);
}
});
background.start();
return future;
}
Aggregations