use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.
the class ClusterRerouteResponseTests method testToXContent.
public void testToXContent() throws IOException {
DiscoveryNode node0 = new DiscoveryNode("node0", new TransportAddress(TransportAddress.META_ADDRESS, 9000), Version.CURRENT);
DiscoveryNodes nodes = new DiscoveryNodes.Builder().add(node0).masterNodeId(node0.getId()).build();
IndexMetadata indexMetadata = IndexMetadata.builder("index").settings(Settings.builder().put(IndexSettings.INDEX_CHECK_ON_STARTUP.getKey(), true).put(IndexSettings.MAX_SCRIPT_FIELDS_SETTING.getKey(), 10).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build()).build();
ImmutableOpenMap.Builder<String, IndexMetadata> openMapBuilder = ImmutableOpenMap.builder();
openMapBuilder.put("index", indexMetadata);
Metadata metadata = Metadata.builder().indices(openMapBuilder.build()).build();
ClusterState clusterState = ClusterState.builder(new ClusterName("test")).nodes(nodes).metadata(metadata).build();
RoutingExplanations routingExplanations = new RoutingExplanations();
routingExplanations.add(new RerouteExplanation(new AllocateReplicaAllocationCommand("index", 0, "node0"), Decision.YES));
ClusterRerouteResponse clusterRerouteResponse = new ClusterRerouteResponse(true, clusterState, routingExplanations);
{
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
clusterRerouteResponse.toXContent(builder, ToXContent.EMPTY_PARAMS);
assertEquals("{\n" + " \"acknowledged\" : true,\n" + " \"state\" : {\n" + " \"cluster_uuid\" : \"_na_\",\n" + " \"version\" : 0,\n" + " \"state_uuid\" : \"" + clusterState.stateUUID() + "\",\n" + " \"master_node\" : \"node0\",\n" + " \"cluster_manager_node\" : \"node0\",\n" + " \"blocks\" : { },\n" + " \"nodes\" : {\n" + " \"node0\" : {\n" + " \"name\" : \"\",\n" + " \"ephemeral_id\" : \"" + node0.getEphemeralId() + "\",\n" + " \"transport_address\" : \"0.0.0.0:9000\",\n" + " \"attributes\" : { }\n" + " }\n" + " },\n" + " \"metadata\" : {\n" + " \"cluster_uuid\" : \"_na_\",\n" + " \"cluster_uuid_committed\" : false,\n" + " \"cluster_coordination\" : {\n" + " \"term\" : 0,\n" + " \"last_committed_config\" : [ ],\n" + " \"last_accepted_config\" : [ ],\n" + " \"voting_config_exclusions\" : [ ]\n" + " },\n" + " \"templates\" : { },\n" + " \"indices\" : {\n" + " \"index\" : {\n" + " \"version\" : 1,\n" + " \"mapping_version\" : 1,\n" + " \"settings_version\" : 1,\n" + " \"aliases_version\" : 1,\n" + " \"routing_num_shards\" : 1,\n" + " \"state\" : \"open\",\n" + " \"settings\" : {\n" + " \"index\" : {\n" + " \"shard\" : {\n" + " \"check_on_startup\" : \"true\"\n" + " },\n" + " \"number_of_shards\" : \"1\",\n" + " \"number_of_replicas\" : \"0\",\n" + " \"version\" : {\n" + " \"created\" : \"" + Version.CURRENT.id + "\"\n" + " },\n" + " \"max_script_fields\" : \"10\"\n" + " }\n" + " },\n" + " \"mappings\" : { },\n" + " \"aliases\" : [ ],\n" + " \"primary_terms\" : {\n" + " \"0\" : 0\n" + " },\n" + " \"in_sync_allocations\" : {\n" + " \"0\" : [ ]\n" + " },\n" + " \"rollover_info\" : { },\n" + " \"system\" : false\n" + " }\n" + " },\n" + " \"index-graveyard\" : {\n" + " \"tombstones\" : [ ]\n" + " }\n" + " },\n" + " \"routing_table\" : {\n" + " \"indices\" : { }\n" + " },\n" + " \"routing_nodes\" : {\n" + " \"unassigned\" : [ ],\n" + " \"nodes\" : {\n" + " \"node0\" : [ ]\n" + " }\n" + " }\n" + " }\n" + "}", Strings.toString(builder));
}
{
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
Map<String, String> params = new HashMap<>();
params.put("explain", "true");
params.put("metric", "version,cluster_manager_node");
clusterRerouteResponse.toXContent(builder, new ToXContent.MapParams(params));
assertEquals("{\n" + " \"acknowledged\" : true,\n" + " \"state\" : {\n" + " \"cluster_uuid\" : \"_na_\",\n" + " \"version\" : 0,\n" + " \"state_uuid\" : \"" + clusterState.stateUUID() + "\",\n" + " \"cluster_manager_node\" : \"node0\"\n" + " },\n" + " \"explanations\" : [\n" + " {\n" + " \"command\" : \"allocate_replica\",\n" + " \"parameters\" : {\n" + " \"index\" : \"index\",\n" + " \"shard\" : 0,\n" + " \"node\" : \"node0\"\n" + " },\n" + " \"decisions\" : [\n" + " {\n" + " \"decider\" : null,\n" + " \"decision\" : \"YES\",\n" + " \"explanation\" : \"none\"\n" + " }\n" + " ]\n" + " }\n" + " ]\n" + "}", Strings.toString(builder));
}
{
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
Map<String, String> params = new HashMap<>();
params.put("metric", "metadata");
params.put("settings_filter", "index.number*,index.version.created");
clusterRerouteResponse.toXContent(builder, new ToXContent.MapParams(params));
assertEquals("{\n" + " \"acknowledged\" : true,\n" + " \"state\" : {\n" + " \"cluster_uuid\" : \"_na_\",\n" + " \"metadata\" : {\n" + " \"cluster_uuid\" : \"_na_\",\n" + " \"cluster_uuid_committed\" : false,\n" + " \"cluster_coordination\" : {\n" + " \"term\" : 0,\n" + " \"last_committed_config\" : [ ],\n" + " \"last_accepted_config\" : [ ],\n" + " \"voting_config_exclusions\" : [ ]\n" + " },\n" + " \"templates\" : { },\n" + " \"indices\" : {\n" + " \"index\" : {\n" + " \"version\" : 1,\n" + " \"mapping_version\" : 1,\n" + " \"settings_version\" : 1,\n" + " \"aliases_version\" : 1,\n" + " \"routing_num_shards\" : 1,\n" + " \"state\" : \"open\",\n" + " \"settings\" : {\n" + " \"index\" : {\n" + " \"max_script_fields\" : \"10\",\n" + " \"shard\" : {\n" + " \"check_on_startup\" : \"true\"\n" + " }\n" + " }\n" + " },\n" + " \"mappings\" : { },\n" + " \"aliases\" : [ ],\n" + " \"primary_terms\" : {\n" + " \"0\" : 0\n" + " },\n" + " \"in_sync_allocations\" : {\n" + " \"0\" : [ ]\n" + " },\n" + " \"rollover_info\" : { },\n" + " \"system\" : false\n" + " }\n" + " },\n" + " \"index-graveyard\" : {\n" + " \"tombstones\" : [ ]\n" + " }\n" + " }\n" + " }\n" + "}", Strings.toString(builder));
}
}
use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.
the class ExceptionSerializationTests method testConnectTransportException.
public void testConnectTransportException() throws IOException {
TransportAddress transportAddress = buildNewFakeTransportAddress();
DiscoveryNode node = new DiscoveryNode("thenode", transportAddress, emptyMap(), emptySet(), Version.CURRENT);
ConnectTransportException ex = serialize(new ConnectTransportException(node, "msg", "action", null));
assertEquals("[][" + transportAddress.toString() + "][action] msg", ex.getMessage());
assertEquals(node, ex.node());
assertEquals("action", ex.action());
assertNull(ex.getCause());
ex = serialize(new ConnectTransportException(node, "msg", "action", new NullPointerException()));
assertEquals("[][" + transportAddress + "][action] msg", ex.getMessage());
assertEquals(node, ex.node());
assertEquals("action", ex.action());
assertTrue(ex.getCause() instanceof NullPointerException);
}
use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.
the class NioHttpServerTransportTests method testReadTimeout.
public void testReadTimeout() throws Exception {
final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {
@Override
public void dispatchRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) {
logger.error("--> Unexpected successful request [{}]", FakeRestRequest.requestToString(request));
throw new AssertionError("Should not have received a dispatched request");
}
@Override
public void dispatchBadRequest(final RestChannel channel, final ThreadContext threadContext, final Throwable cause) {
logger.error(new ParameterizedMessage("--> Unexpected bad request [{}]", FakeRestRequest.requestToString(channel.request())), cause);
throw new AssertionError("Should not have received a dispatched request");
}
};
Settings settings = createBuilderWithPort().put(HttpTransportSettings.SETTING_HTTP_READ_TIMEOUT.getKey(), new TimeValue(randomIntBetween(100, 300))).build();
try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler, threadPool, xContentRegistry(), dispatcher, new NioGroupFactory(settings, logger), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS))) {
transport.start();
final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
try (NioHttpClient client = new NioHttpClient()) {
NioSocketChannel channel = null;
try {
CountDownLatch channelClosedLatch = new CountDownLatch(1);
channel = client.connect(remoteAddress.address());
channel.addCloseListener((r, t) -> channelClosedLatch.countDown());
assertTrue("Channel should be closed due to read timeout", channelClosedLatch.await(1, TimeUnit.MINUTES));
} finally {
if (channel != null) {
channel.close();
}
}
}
}
}
use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.
the class NioHttpServerTransportTests method runExpectHeaderTest.
private void runExpectHeaderTest(final Settings settings, final String expectation, final int contentLength, final HttpResponseStatus expectedStatus) throws InterruptedException {
final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {
@Override
public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) {
channel.sendResponse(new BytesRestResponse(OK, BytesRestResponse.TEXT_CONTENT_TYPE, new BytesArray("done")));
}
@Override
public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, Throwable cause) {
logger.error(new ParameterizedMessage("--> Unexpected bad request [{}]", FakeRestRequest.requestToString(channel.request())), cause);
throw new AssertionError();
}
};
try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler, threadPool, xContentRegistry(), dispatcher, new NioGroupFactory(settings, logger), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS))) {
transport.start();
final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
try (NioHttpClient client = new NioHttpClient()) {
final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
request.headers().set(HttpHeaderNames.EXPECT, expectation);
HttpUtil.setContentLength(request, contentLength);
final FullHttpResponse response = client.send(remoteAddress.address(), request);
try {
assertThat(response.status(), equalTo(expectedStatus));
if (expectedStatus.equals(HttpResponseStatus.CONTINUE)) {
final FullHttpRequest continuationRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Unpooled.EMPTY_BUFFER);
final FullHttpResponse continuationResponse = client.send(remoteAddress.address(), continuationRequest);
try {
assertThat(continuationResponse.status(), is(HttpResponseStatus.OK));
assertThat(new String(ByteBufUtil.getBytes(continuationResponse.content()), StandardCharsets.UTF_8), is("done"));
} finally {
continuationResponse.release();
}
}
} finally {
response.release();
}
}
}
}
use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.
the class NioHttpServerTransportTests method testBadRequest.
public void testBadRequest() throws InterruptedException {
final AtomicReference<Throwable> causeReference = new AtomicReference<>();
final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {
@Override
public void dispatchRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) {
logger.error("--> Unexpected successful request [{}]", FakeRestRequest.requestToString(request));
throw new AssertionError();
}
@Override
public void dispatchBadRequest(final RestChannel channel, final ThreadContext threadContext, final Throwable cause) {
causeReference.set(cause);
try {
final OpenSearchException e = new OpenSearchException("you sent a bad request and you should feel bad");
channel.sendResponse(new BytesRestResponse(channel, BAD_REQUEST, e));
} catch (final IOException e) {
throw new AssertionError(e);
}
}
};
final Settings settings;
final int maxInitialLineLength;
final Setting<ByteSizeValue> httpMaxInitialLineLengthSetting = HttpTransportSettings.SETTING_HTTP_MAX_INITIAL_LINE_LENGTH;
if (randomBoolean()) {
maxInitialLineLength = httpMaxInitialLineLengthSetting.getDefault(Settings.EMPTY).bytesAsInt();
settings = createSettings();
} else {
maxInitialLineLength = randomIntBetween(1, 8192);
settings = createBuilderWithPort().put(httpMaxInitialLineLengthSetting.getKey(), maxInitialLineLength + "b").build();
}
try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler, threadPool, xContentRegistry(), dispatcher, new NioGroupFactory(settings, logger), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS))) {
transport.start();
final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
try (NioHttpClient client = new NioHttpClient()) {
final String url = "/" + new String(new byte[maxInitialLineLength], Charset.forName("UTF-8"));
final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, url);
final FullHttpResponse response = client.send(remoteAddress.address(), request);
try {
assertThat(response.status(), equalTo(HttpResponseStatus.BAD_REQUEST));
assertThat(new String(response.content().array(), Charset.forName("UTF-8")), containsString("you sent a bad request and you should feel bad"));
} finally {
response.release();
}
}
}
assertNotNull(causeReference.get());
assertThat(causeReference.get(), instanceOf(TooLongFrameException.class));
}
Aggregations