Search in sources :

Example 11 with RestClient

use of org.elasticsearch.client.RestClient in project elasticsearch by elastic.

the class IndexingIT method testIndexVersionPropagation.

public void testIndexVersionPropagation() 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";
    final int minUpdates = 5;
    final int maxUpdates = 10;
    createIndex(index, settings.build());
    try (RestClient newNodeClient = buildClient(restClientSettings(), nodes.getNewNodes().stream().map(Node::getPublishAddress).toArray(HttpHost[]::new))) {
        int nUpdates = randomIntBetween(minUpdates, maxUpdates);
        logger.info("indexing docs with [{}] concurrent updates initially", nUpdates);
        final int finalVersionForDoc1 = indexDocWithConcurrentUpdates(index, 1, nUpdates);
        logger.info("allowing shards on all nodes");
        updateIndexSetting(index, Settings.builder().putNull("index.routing.allocation.include._name"));
        ensureGreen();
        assertOK(client().performRequest("POST", index + "/_refresh"));
        List<Shard> shards = buildShards(nodes, newNodeClient);
        for (Shard shard : shards) {
            assertVersion(index, 1, "_only_nodes:" + shard.getNode().getNodeName(), finalVersionForDoc1);
            assertCount(index, "_only_nodes:" + shard.getNode().getNodeName(), 1);
        }
        nUpdates = randomIntBetween(minUpdates, maxUpdates);
        logger.info("indexing docs with [{}] concurrent updates after allowing shards on all nodes", nUpdates);
        final int finalVersionForDoc2 = indexDocWithConcurrentUpdates(index, 2, nUpdates);
        assertOK(client().performRequest("POST", index + "/_refresh"));
        shards = buildShards(nodes, newNodeClient);
        for (Shard shard : shards) {
            assertVersion(index, 2, "_only_nodes:" + shard.getNode().getNodeName(), finalVersionForDoc2);
            assertCount(index, "_only_nodes:" + shard.getNode().getNodeName(), 2);
        }
        Shard primary = buildShards(nodes, newNodeClient).stream().filter(Shard::isPrimary).findFirst().get();
        logger.info("moving primary to new node by excluding {}", primary.getNode().getNodeName());
        updateIndexSetting(index, Settings.builder().put("index.routing.allocation.exclude._name", primary.getNode().getNodeName()));
        ensureGreen();
        nUpdates = randomIntBetween(minUpdates, maxUpdates);
        logger.info("indexing docs with [{}] concurrent updates after moving primary", nUpdates);
        final int finalVersionForDoc3 = indexDocWithConcurrentUpdates(index, 3, nUpdates);
        assertOK(client().performRequest("POST", index + "/_refresh"));
        shards = buildShards(nodes, newNodeClient);
        for (Shard shard : shards) {
            assertVersion(index, 3, "_only_nodes:" + shard.getNode().getNodeName(), finalVersionForDoc3);
            assertCount(index, "_only_nodes:" + shard.getNode().getNodeName(), 3);
        }
        logger.info("setting number of replicas to 0");
        updateIndexSetting(index, Settings.builder().put("index.number_of_replicas", 0));
        ensureGreen();
        nUpdates = randomIntBetween(minUpdates, maxUpdates);
        logger.info("indexing doc with [{}] concurrent updates after setting number of replicas to 0", nUpdates);
        final int finalVersionForDoc4 = indexDocWithConcurrentUpdates(index, 4, nUpdates);
        assertOK(client().performRequest("POST", index + "/_refresh"));
        shards = buildShards(nodes, newNodeClient);
        for (Shard shard : shards) {
            assertVersion(index, 4, "_only_nodes:" + shard.getNode().getNodeName(), finalVersionForDoc4);
            assertCount(index, "_only_nodes:" + shard.getNode().getNodeName(), 4);
        }
        logger.info("setting number of replicas to 1");
        updateIndexSetting(index, Settings.builder().put("index.number_of_replicas", 1));
        ensureGreen();
        nUpdates = randomIntBetween(minUpdates, maxUpdates);
        logger.info("indexing doc with [{}] concurrent updates after setting number of replicas to 1", nUpdates);
        final int finalVersionForDoc5 = indexDocWithConcurrentUpdates(index, 5, nUpdates);
        assertOK(client().performRequest("POST", index + "/_refresh"));
        shards = buildShards(nodes, newNodeClient);
        for (Shard shard : shards) {
            assertVersion(index, 5, "_only_nodes:" + shard.getNode().getNodeName(), finalVersionForDoc5);
            assertCount(index, "_only_nodes:" + shard.getNode().getNodeName(), 5);
        }
        // the number of documents on the primary and on the recovered replica should match the number of indexed documents
        assertCount(index, "_primary", 5);
        assertCount(index, "_replica", 5);
    }
}
Also used : HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 12 with RestClient

use of org.elasticsearch.client.RestClient in project elasticsearch by elastic.

the class HttpCompressionIT method testCompressesResponseIfRequested.

public void testCompressesResponseIfRequested() throws IOException {
    RestClient client = client();
    Response response = client.performRequest("GET", "/", new BasicHeader(HttpHeaders.ACCEPT_ENCODING, GZIP_ENCODING));
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals(GZIP_ENCODING, response.getHeader(HttpHeaders.CONTENT_ENCODING));
}
Also used : Response(org.elasticsearch.client.Response) RestClient(org.elasticsearch.client.RestClient) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

RestClient (org.elasticsearch.client.RestClient)12 HttpHost (org.apache.http.HttpHost)9 Response (org.elasticsearch.client.Response)3 Settings (org.elasticsearch.common.settings.Settings)3 IOException (java.io.IOException)2 BasicHeader (org.apache.http.message.BasicHeader)2 Version (org.elasticsearch.Version)2 BackoffPolicy (org.elasticsearch.action.bulk.BackoffPolicy)2 ResponseException (org.elasticsearch.client.ResponseException)2 BytesArray (org.elasticsearch.common.bytes.BytesArray)2 InputStreamReader (java.io.InputStreamReader)1 UncheckedIOException (java.io.UncheckedIOException)1 URL (java.net.URL)1 StandardCharsets (java.nio.charset.StandardCharsets)1 FileSystem (java.nio.file.FileSystem)1 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.synchronizedList (java.util.Collections.synchronizedList)1 List (java.util.List)1 Map (java.util.Map)1