use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.
the class RemoteScrollableHitSourceTests method testCleanupSuccessful.
public void testCleanupSuccessful() throws Exception {
AtomicBoolean cleanupCallbackCalled = new AtomicBoolean();
RestClient client = mock(RestClient.class);
TestRemoteScrollableHitSource hitSource = new TestRemoteScrollableHitSource(client);
hitSource.cleanup(() -> cleanupCallbackCalled.set(true));
verify(client).close();
assertTrue(cleanupCallbackCalled.get());
}
use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.
the class RemoteScrollableHitSourceTests method sourceWithMockedClient.
private RemoteScrollableHitSource sourceWithMockedClient(boolean mockRemoteVersion, CloseableHttpAsyncClient httpClient) {
HttpAsyncClientBuilder clientBuilder = mock(HttpAsyncClientBuilder.class);
when(clientBuilder.build()).thenReturn(httpClient);
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).setHttpClientConfigCallback(httpClientBuilder -> clientBuilder).build();
TestRemoteScrollableHitSource hitSource = new TestRemoteScrollableHitSource(restClient) {
@Override
void lookupRemoteVersion(RejectAwareActionListener<Version> listener) {
if (mockRemoteVersion) {
listener.onResponse(Version.CURRENT);
} else {
super.lookupRemoteVersion(listener);
}
}
};
if (mockRemoteVersion) {
hitSource.remoteVersion = Version.CURRENT;
}
return hitSource;
}
use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.
the class Zen2RestApiIT method testRollingRestartOfTwoNodeCluster.
public void testRollingRestartOfTwoNodeCluster() throws Exception {
internalCluster().setBootstrapClusterManagerNodeIndex(1);
final List<String> nodes = internalCluster().startNodes(2);
createIndex("test", Settings.builder().put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), // assign shards
TimeValue.ZERO).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, // causes rebalancing
2).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1).build());
ensureGreen("test");
RestClient restClient = getRestClient();
internalCluster().rollingRestart(new InternalTestCluster.RestartCallback() {
@Override
public void doAfterNodes(int n, Client client) throws IOException {
ensureGreen("test");
Response response = restClient.performRequest(new Request("POST", "/_cluster/voting_config_exclusions/" + internalCluster().getNodeNames()[n]));
assertThat(response.getStatusLine().getStatusCode(), is(200));
}
@Override
public Settings onNodeStopped(String nodeName) throws IOException {
String viaNode = randomValueOtherThan(nodeName, () -> randomFrom(nodes));
List<Node> allNodes = restClient.getNodes();
try {
restClient.setNodes(Collections.singletonList(new Node(HttpHost.create(internalCluster().getInstance(HttpServerTransport.class, viaNode).boundAddress().publishAddress().toString()))));
Response deleteResponse = restClient.performRequest(new Request("DELETE", "/_cluster/voting_config_exclusions"));
assertThat(deleteResponse.getStatusLine().getStatusCode(), is(200));
ClusterHealthResponse clusterHealthResponse = client(viaNode).admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNodes(Integer.toString(1)).setTimeout(TimeValue.timeValueSeconds(30L)).setWaitForYellowStatus().get();
assertFalse(nodeName, clusterHealthResponse.isTimedOut());
return Settings.EMPTY;
} finally {
restClient.setNodes(allNodes);
}
}
});
ensureStableCluster(2);
ensureGreen("test");
assertThat(internalCluster().size(), is(2));
}
use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.
the class Reindexer method buildRestClient.
/**
* Build the {@link RestClient} used for reindexing from remote clusters.
*
* @param remoteInfo connection information for the remote cluster
* @param sslConfig configuration for potential outgoing HTTPS connections
* @param taskId the id of the current task. This is added to the thread name for easier tracking
* @param threadCollector a list in which we collect all the threads created by the client
* @param restInterceptor an optional HttpRequestInterceptor
*/
static RestClient buildRestClient(RemoteInfo remoteInfo, ReindexSslConfig sslConfig, long taskId, List<Thread> threadCollector, Optional<HttpRequestInterceptor> restInterceptor) {
Header[] clientHeaders = new Header[remoteInfo.getHeaders().size()];
int i = 0;
for (Map.Entry<String, String> header : remoteInfo.getHeaders().entrySet()) {
clientHeaders[i++] = new BasicHeader(header.getKey(), header.getValue());
}
final RestClientBuilder builder = RestClient.builder(new HttpHost(remoteInfo.getHost(), remoteInfo.getPort(), remoteInfo.getScheme())).setDefaultHeaders(clientHeaders).setRequestConfigCallback(c -> {
c.setConnectTimeout(Math.toIntExact(remoteInfo.getConnectTimeout().millis()));
c.setSocketTimeout(Math.toIntExact(remoteInfo.getSocketTimeout().millis()));
return c;
}).setHttpClientConfigCallback(c -> {
// Enable basic auth if it is configured
if (remoteInfo.getUsername() != null) {
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(remoteInfo.getUsername(), remoteInfo.getPassword());
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, creds);
c.setDefaultCredentialsProvider(credentialsProvider);
} else {
restInterceptor.ifPresent(interceptor -> c.addInterceptorLast(interceptor));
}
// Stick the task id in the thread name so we can track down tasks from stack traces
AtomicInteger threads = new AtomicInteger();
c.setThreadFactory(r -> {
String name = "es-client-" + taskId + "-" + threads.getAndIncrement();
Thread t = new Thread(r, name);
threadCollector.add(t);
return t;
});
// Limit ourselves to one reactor thread because for now the search process is single threaded.
c.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build());
c.setSSLStrategy(sslConfig.getStrategy());
return c;
});
if (Strings.hasLength(remoteInfo.getPathPrefix()) && "/".equals(remoteInfo.getPathPrefix()) == false) {
builder.setPathPrefix(remoteInfo.getPathPrefix());
}
return builder.build();
}
use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.
the class IndexingIT method testSeqNoCheckpoints.
public void testSeqNoCheckpoints() throws Exception {
Nodes nodes = buildNodeAndVersions();
assumeFalse("new nodes is empty", nodes.getNewNodes().isEmpty());
logger.info("cluster discovered: {}", nodes.toString());
final List<String> bwcNamesList = nodes.getBWCNodes().stream().map(Node::getNodeName).collect(Collectors.toList());
final String bwcNames = bwcNamesList.stream().collect(Collectors.joining(","));
Settings.Builder settings = Settings.builder().put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1).put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 2).put("index.routing.allocation.include._name", bwcNames);
final String index = "test";
createIndex(index, settings.build());
try (RestClient newNodeClient = buildClient(restClientSettings(), nodes.getNewNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new))) {
int numDocs = 0;
final int numberOfInitialDocs = 1 + randomInt(5);
logger.info("indexing [{}] docs initially", numberOfInitialDocs);
numDocs += indexDocs(index, 0, numberOfInitialDocs);
boolean compat = nodes.getBWCVersion().compareTo(LegacyESVersion.fromId(6000000)) >= 0;
assertSeqNoOnShards(index, nodes, compat ? numDocs : 0, newNodeClient);
logger.info("allowing shards on all nodes");
updateIndexSettings(index, Settings.builder().putNull("index.routing.allocation.include._name"));
ensureGreen(index);
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
for (final String bwcName : bwcNamesList) {
assertCount(index, "_only_nodes:" + bwcName, numDocs);
}
final int numberOfDocsAfterAllowingShardsOnAllNodes = 1 + randomInt(5);
logger.info("indexing [{}] docs after allowing shards on all nodes", numberOfDocsAfterAllowingShardsOnAllNodes);
numDocs += indexDocs(index, numDocs, numberOfDocsAfterAllowingShardsOnAllNodes);
compat = nodes.getBWCVersion().compareTo(LegacyESVersion.fromId(6000000)) >= 0;
assertSeqNoOnShards(index, nodes, compat ? numDocs : 0, newNodeClient);
Shard primary = buildShards(index, nodes, newNodeClient).stream().filter(Shard::isPrimary).findFirst().get();
logger.info("moving primary to new node by excluding {}", primary.getNode().getNodeName());
updateIndexSettings(index, Settings.builder().put("index.routing.allocation.exclude._name", primary.getNode().getNodeName()));
ensureGreen(index);
int numDocsOnNewPrimary = 0;
final int numberOfDocsAfterMovingPrimary = 1 + randomInt(5);
logger.info("indexing [{}] docs after moving primary", numberOfDocsAfterMovingPrimary);
numDocsOnNewPrimary += indexDocs(index, numDocs, numberOfDocsAfterMovingPrimary);
numDocs += numberOfDocsAfterMovingPrimary;
compat = nodes.getBWCVersion().compareTo(LegacyESVersion.fromId(6000000)) >= 0;
assertSeqNoOnShards(index, nodes, compat ? numDocs : numDocsOnNewPrimary, newNodeClient);
/*
* Dropping the number of replicas to zero, and then increasing it to one triggers a recovery thus exercising any BWC-logic in
* the recovery code.
*/
logger.info("setting number of replicas to 0");
updateIndexSettings(index, Settings.builder().put("index.number_of_replicas", 0));
final int numberOfDocsAfterDroppingReplicas = 1 + randomInt(5);
logger.info("indexing [{}] docs after setting number of replicas to 0", numberOfDocsAfterDroppingReplicas);
numDocsOnNewPrimary += indexDocs(index, numDocs, numberOfDocsAfterDroppingReplicas);
numDocs += numberOfDocsAfterDroppingReplicas;
logger.info("setting number of replicas to 1");
updateIndexSettings(index, Settings.builder().put("index.number_of_replicas", 1));
ensureGreen(index);
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
for (Shard shard : buildShards(index, nodes, newNodeClient)) {
assertCount(index, "_only_nodes:" + shard.node.nodeName, numDocs);
}
compat = nodes.getBWCVersion().compareTo(LegacyESVersion.fromId(6000000)) >= 0;
assertSeqNoOnShards(index, nodes, compat ? numDocs : numDocsOnNewPrimary, newNodeClient);
}
}
Aggregations