use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.
the class IndexingIT method testSyncedFlushTransition.
public void testSyncedFlushTransition() throws Exception {
Nodes nodes = buildNodeAndVersions();
assumeTrue("bwc version is on 1.x or Legacy 7.x", nodes.getBWCVersion().before(Version.V_2_0_0));
assumeFalse("no new node found", nodes.getNewNodes().isEmpty());
assumeFalse("no bwc node found", nodes.getBWCNodes().isEmpty());
// Allocate shards to new nodes then verify synced flush requests processed by old nodes/new nodes
String newNodes = nodes.getNewNodes().stream().map(Node::getNodeName).collect(Collectors.joining(","));
int numShards = randomIntBetween(1, 10);
int numOfReplicas = randomIntBetween(0, nodes.getNewNodes().size() - 1);
int totalShards = numShards * (numOfReplicas + 1);
final String index = "test_synced_flush";
createIndex(index, Settings.builder().put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), numShards).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numOfReplicas).put("index.routing.allocation.include._name", newNodes).build());
ensureGreen(index);
indexDocs(index, randomIntBetween(0, 100), between(1, 100));
try (RestClient oldNodeClient = buildClient(restClientSettings(), nodes.getBWCNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new))) {
Request request = new Request("POST", index + "/_flush/synced");
assertBusy(() -> {
ResponseException responseException = expectThrows(ResponseException.class, () -> oldNodeClient.performRequest(request));
assertThat(responseException.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.CONFLICT.getStatus()));
assertThat(responseException.getResponse().getWarnings(), contains("Synced flush is deprecated and will be removed in 8.0. Use flush at _/flush or /{index}/_flush instead."));
Map<String, Object> result = ObjectPath.createFromResponse(responseException.getResponse()).evaluate("_shards");
assertThat(result.get("total"), equalTo(totalShards));
assertThat(result.get("successful"), equalTo(0));
assertThat(result.get("failed"), equalTo(totalShards));
});
Map<String, Object> stats = entityAsMap(client().performRequest(new Request("GET", index + "/_stats?level=shards")));
assertThat(XContentMapValues.extractValue("indices." + index + ".total.translog.uncommitted_operations", stats), equalTo(0));
}
indexDocs(index, randomIntBetween(0, 100), between(1, 100));
try (RestClient newNodeClient = buildClient(restClientSettings(), nodes.getNewNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new))) {
Request request = new Request("POST", index + "/_flush/synced");
List<String> warningMsg = Arrays.asList("Synced flush was removed and a normal flush was performed instead. " + "This transition will be removed in a future version.");
RequestOptions.Builder requestOptionsBuilder = RequestOptions.DEFAULT.toBuilder();
requestOptionsBuilder.setWarningsHandler(warnings -> warnings.equals(warningMsg) == false);
request.setOptions(requestOptionsBuilder);
assertBusy(() -> {
Map<String, Object> result = ObjectPath.createFromResponse(newNodeClient.performRequest(request)).evaluate("_shards");
assertThat(result.get("total"), equalTo(totalShards));
assertThat(result.get("successful"), equalTo(totalShards));
assertThat(result.get("failed"), equalTo(0));
});
Map<String, Object> stats = entityAsMap(client().performRequest(new Request("GET", index + "/_stats?level=shards")));
assertThat(XContentMapValues.extractValue("indices." + index + ".total.translog.uncommitted_operations", stats), equalTo(0));
}
}
use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.
the class SnifferTests method testTaskCancelling.
public void testTaskCancelling() throws Exception {
RestClient restClient = mock(RestClient.class);
NodesSniffer nodesSniffer = mock(NodesSniffer.class);
Scheduler noOpScheduler = new Scheduler() {
@Override
public Future<?> schedule(Sniffer.Task task, long delayMillis) {
return null;
}
@Override
public void shutdown() {
}
};
Sniffer sniffer = new Sniffer(restClient, nodesSniffer, noOpScheduler, 0L, 0L);
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
try {
int numIters = randomIntBetween(50, 100);
for (int i = 0; i < numIters; i++) {
Sniffer.Task task = sniffer.new Task(0L);
TaskWrapper wrapper = new TaskWrapper(task);
Future<?> future;
if (rarely()) {
future = executor.schedule(wrapper, randomLongBetween(0L, 200L), TimeUnit.MILLISECONDS);
} else {
future = executor.submit(wrapper);
}
Sniffer.ScheduledTask scheduledTask = new Sniffer.ScheduledTask(task, future);
boolean skip = scheduledTask.skip();
try {
assertNull(future.get());
} catch (CancellationException ignore) {
assertTrue(future.isCancelled());
}
if (skip) {
// the task was either cancelled before starting, in which case it will never start (thanks to Future#cancel),
// or skipped, in which case it will run but do nothing (thanks to Task#skip).
// Here we want to make sure that whenever skip returns true, the task either won't run or it won't do anything,
// otherwise we may end up with parallel sniffing tracks given that each task schedules the following one. We need to
// make sure that onFailure takes scheduling over while at the same time ordinary rounds don't go on.
assertFalse(task.hasStarted());
assertTrue(task.isSkipped());
assertTrue(future.isCancelled());
assertTrue(future.isDone());
} else {
// if a future is cancelled when its execution has already started, future#get throws CancellationException before
// completion. The execution continues though so we use a latch to try and wait for the task to be completed.
// Here we want to make sure that whenever skip returns false, the task will be completed, otherwise we may be
// missing to schedule the following round, which means no sniffing will ever happen again besides on failure sniffing.
assertTrue(wrapper.await());
// the future may or may not be cancelled but the task has for sure started and completed
assertTrue(task.toString(), task.hasStarted());
assertFalse(task.isSkipped());
assertTrue(future.isDone());
}
// subsequent cancel calls return false for sure
int cancelCalls = randomIntBetween(1, 10);
for (int j = 0; j < cancelCalls; j++) {
assertFalse(scheduledTask.skip());
}
}
} finally {
executor.shutdown();
executor.awaitTermination(1000, TimeUnit.MILLISECONDS);
}
}
use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.
the class OpenSearchNodesSnifferTests method testSniffNodes.
public void testSniffNodes() throws IOException {
HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort());
try (RestClient restClient = RestClient.builder(httpHost).build()) {
OpenSearchNodesSniffer sniffer = new OpenSearchNodesSniffer(restClient, sniffRequestTimeout, scheme);
try {
List<Node> sniffedNodes = sniffer.sniff();
if (sniffResponse.isFailure) {
fail("sniffNodes should have failed");
}
assertEquals(sniffResponse.result, sniffedNodes);
} catch (ResponseException e) {
Response response = e.getResponse();
if (sniffResponse.isFailure) {
final String errorPrefix = "method [GET], host [" + httpHost + "], URI [/_nodes/http?timeout=" + sniffRequestTimeout + "ms], status line [HTTP/1.1";
assertThat(e.getMessage(), startsWith(errorPrefix));
assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode)));
assertThat(response.getHost(), equalTo(httpHost));
assertThat(response.getStatusLine().getStatusCode(), equalTo(sniffResponse.nodesInfoResponseCode));
assertThat(response.getRequestLine().toString(), equalTo("GET /_nodes/http?timeout=" + sniffRequestTimeout + "ms HTTP/1.1"));
} else {
fail("sniffNodes should have succeeded: " + response.getStatusLine());
}
}
}
}
use of org.opensearch.client.RestClient in project OpenSearch by opensearch-project.
the class SnifferBuilderTests method testBuild.
public void testBuild() throws Exception {
int numNodes = RandomNumbers.randomIntBetween(getRandom(), 1, 5);
HttpHost[] hosts = new HttpHost[numNodes];
for (int i = 0; i < numNodes; i++) {
hosts[i] = new HttpHost("localhost", 9200 + i);
}
try (RestClient client = RestClient.builder(hosts).build()) {
try {
Sniffer.builder(null).build();
fail("should have failed");
} catch (NullPointerException e) {
assertEquals("restClient cannot be null", e.getMessage());
}
try {
Sniffer.builder(client).setSniffIntervalMillis(RandomNumbers.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0));
fail("should have failed");
} catch (IllegalArgumentException e) {
assertEquals("sniffIntervalMillis must be greater than 0", e.getMessage());
}
try {
Sniffer.builder(client).setSniffAfterFailureDelayMillis(RandomNumbers.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0));
fail("should have failed");
} catch (IllegalArgumentException e) {
assertEquals("sniffAfterFailureDelayMillis must be greater than 0", e.getMessage());
}
try {
Sniffer.builder(client).setNodesSniffer(null);
fail("should have failed");
} catch (NullPointerException e) {
assertEquals("nodesSniffer cannot be null", e.getMessage());
}
try (Sniffer sniffer = Sniffer.builder(client).build()) {
assertNotNull(sniffer);
}
SnifferBuilder builder = Sniffer.builder(client);
if (getRandom().nextBoolean()) {
builder.setSniffIntervalMillis(RandomNumbers.randomIntBetween(getRandom(), 1, Integer.MAX_VALUE));
}
if (getRandom().nextBoolean()) {
builder.setSniffAfterFailureDelayMillis(RandomNumbers.randomIntBetween(getRandom(), 1, Integer.MAX_VALUE));
}
if (getRandom().nextBoolean()) {
builder.setNodesSniffer(new MockNodesSniffer());
}
try (Sniffer sniffer = builder.build()) {
assertNotNull(sniffer);
}
}
}
use of org.opensearch.client.RestClient in project data-prepper by opensearch-project.
the class OpenSearchIntegrationHelper method wipeAllTemplates.
/**
* Copied from OpenSearch test framework
* TODO: Consolidate in OpenSearch
*/
static void wipeAllTemplates() throws IOException {
final RestClient openSearchClient = createOpenSearchClient();
openSearchClient.performRequest(new Request("DELETE", "_template/*"));
try {
openSearchClient.performRequest(new Request("DELETE", "_index_template/*"));
openSearchClient.performRequest(new Request("DELETE", "_component_template/*"));
} catch (ResponseException e) {
// We hit a version of ES that doesn't support index templates v2 yet, so it's safe to ignore
}
}
Aggregations