Search in sources :

Example 56 with Client

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

the class AzureSnapshotRestoreTests method testMultipleSnapshots.

/**
     * For issue #51: https://github.com/elastic/elasticsearch-cloud-azure/issues/51
     */
public void testMultipleSnapshots() throws URISyntaxException, StorageException {
    final String indexName = "test-idx-1";
    final String typeName = "doc";
    final String repositoryName = "test-repo";
    final String snapshot1Name = "test-snap-1";
    final String snapshot2Name = "test-snap-2";
    Client client = client();
    logger.info("creating index [{}]", indexName);
    createIndex(indexName);
    ensureGreen();
    logger.info("indexing first document");
    index(indexName, typeName, Integer.toString(1), "foo", "bar " + Integer.toString(1));
    refresh();
    assertThat(client.prepareSearch(indexName).setSize(0).get().getHits().getTotalHits(), equalTo(1L));
    logger.info("creating Azure repository with path [{}]", getRepositoryPath());
    PutRepositoryResponse putRepositoryResponse = client.admin().cluster().preparePutRepository(repositoryName).setType("azure").setSettings(Settings.builder().put(Repository.CONTAINER_SETTING.getKey(), getContainerName()).put(Repository.BASE_PATH_SETTING.getKey(), getRepositoryPath()).put(Repository.BASE_PATH_SETTING.getKey(), randomIntBetween(1000, 10000), ByteSizeUnit.BYTES)).get();
    assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true));
    logger.info("creating snapshot [{}]", snapshot1Name);
    CreateSnapshotResponse createSnapshotResponse1 = client.admin().cluster().prepareCreateSnapshot(repositoryName, snapshot1Name).setWaitForCompletion(true).setIndices(indexName).get();
    assertThat(createSnapshotResponse1.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse1.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse1.getSnapshotInfo().totalShards()));
    assertThat(client.admin().cluster().prepareGetSnapshots(repositoryName).setSnapshots(snapshot1Name).get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    logger.info("indexing second document");
    index(indexName, typeName, Integer.toString(2), "foo", "bar " + Integer.toString(2));
    refresh();
    assertThat(client.prepareSearch(indexName).setSize(0).get().getHits().getTotalHits(), equalTo(2L));
    logger.info("creating snapshot [{}]", snapshot2Name);
    CreateSnapshotResponse createSnapshotResponse2 = client.admin().cluster().prepareCreateSnapshot(repositoryName, snapshot2Name).setWaitForCompletion(true).setIndices(indexName).get();
    assertThat(createSnapshotResponse2.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse2.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse2.getSnapshotInfo().totalShards()));
    assertThat(client.admin().cluster().prepareGetSnapshots(repositoryName).setSnapshots(snapshot2Name).get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    logger.info("closing index [{}]", indexName);
    client.admin().indices().prepareClose(indexName).get();
    logger.info("attempting restore from snapshot [{}]", snapshot1Name);
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot(repositoryName, snapshot1Name).setWaitForCompletion(true).get();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    ensureGreen();
    assertThat(client.prepareSearch(indexName).setSize(0).get().getHits().getTotalHits(), equalTo(1L));
}
Also used : CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) PutRepositoryResponse(org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse) Client(org.elasticsearch.client.Client) ClusterAdminClient(org.elasticsearch.client.ClusterAdminClient) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 57 with Client

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

the class RecoverAfterNodesIT method testRecoverAfterMasterNodes.

public void testRecoverAfterMasterNodes() throws Exception {
    logger.info("--> start master_node (1)");
    Client master1 = startNode(Settings.builder().put("gateway.recover_after_master_nodes", 2).put(Node.NODE_DATA_SETTING.getKey(), false).put(Node.NODE_MASTER_SETTING.getKey(), true), 1);
    assertThat(master1.admin().cluster().prepareState().setLocal(true).execute().actionGet().getState().blocks().global(ClusterBlockLevel.METADATA_WRITE), hasItem(GatewayService.STATE_NOT_RECOVERED_BLOCK));
    logger.info("--> start data_node (1)");
    Client data1 = startNode(Settings.builder().put("gateway.recover_after_master_nodes", 2).put(Node.NODE_DATA_SETTING.getKey(), true).put(Node.NODE_MASTER_SETTING.getKey(), false), 1);
    assertThat(master1.admin().cluster().prepareState().setLocal(true).execute().actionGet().getState().blocks().global(ClusterBlockLevel.METADATA_WRITE), hasItem(GatewayService.STATE_NOT_RECOVERED_BLOCK));
    assertThat(data1.admin().cluster().prepareState().setLocal(true).execute().actionGet().getState().blocks().global(ClusterBlockLevel.METADATA_WRITE), hasItem(GatewayService.STATE_NOT_RECOVERED_BLOCK));
    logger.info("--> start data_node (2)");
    Client data2 = startNode(Settings.builder().put("gateway.recover_after_master_nodes", 2).put(Node.NODE_DATA_SETTING.getKey(), true).put(Node.NODE_MASTER_SETTING.getKey(), false), 1);
    assertThat(master1.admin().cluster().prepareState().setLocal(true).execute().actionGet().getState().blocks().global(ClusterBlockLevel.METADATA_WRITE), hasItem(GatewayService.STATE_NOT_RECOVERED_BLOCK));
    assertThat(data1.admin().cluster().prepareState().setLocal(true).execute().actionGet().getState().blocks().global(ClusterBlockLevel.METADATA_WRITE), hasItem(GatewayService.STATE_NOT_RECOVERED_BLOCK));
    assertThat(data2.admin().cluster().prepareState().setLocal(true).execute().actionGet().getState().blocks().global(ClusterBlockLevel.METADATA_WRITE), hasItem(GatewayService.STATE_NOT_RECOVERED_BLOCK));
    logger.info("--> start master_node (2)");
    Client master2 = startNode(Settings.builder().put("gateway.recover_after_master_nodes", 2).put(Node.NODE_DATA_SETTING.getKey(), false).put(Node.NODE_MASTER_SETTING.getKey(), true), 1);
    assertThat(waitForNoBlocksOnNode(BLOCK_WAIT_TIMEOUT, master1).isEmpty(), equalTo(true));
    assertThat(waitForNoBlocksOnNode(BLOCK_WAIT_TIMEOUT, master2).isEmpty(), equalTo(true));
    assertThat(waitForNoBlocksOnNode(BLOCK_WAIT_TIMEOUT, data1).isEmpty(), equalTo(true));
    assertThat(waitForNoBlocksOnNode(BLOCK_WAIT_TIMEOUT, data2).isEmpty(), equalTo(true));
}
Also used : Client(org.elasticsearch.client.Client)

Example 58 with Client

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

the class UpdateMappingOnClusterIT method compareMappingOnNodes.

private void compareMappingOnNodes(GetMappingsResponse previousMapping) {
    // make sure all nodes have same cluster state
    for (Client client : cluster().getClients()) {
        GetMappingsResponse currentMapping = client.admin().indices().prepareGetMappings(INDEX).addTypes(TYPE).setLocal(true).get();
        assertThat(previousMapping.getMappings().get(INDEX).get(TYPE).source(), equalTo(currentMapping.getMappings().get(INDEX).get(TYPE).source()));
    }
}
Also used : Client(org.elasticsearch.client.Client) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 59 with Client

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

the class ExceptionRetryIT method testRetryDueToExceptionOnNetworkLayer.

/**
     * Tests retry mechanism when indexing. If an exception occurs when indexing then the indexing request is tried again before finally
     * failing. If auto generated ids are used this must not lead to duplicate ids
     * see https://github.com/elastic/elasticsearch/issues/8788
     */
public void testRetryDueToExceptionOnNetworkLayer() throws ExecutionException, InterruptedException, IOException {
    final AtomicBoolean exceptionThrown = new AtomicBoolean(false);
    int numDocs = scaledRandomIntBetween(100, 1000);
    Client client = internalCluster().coordOnlyNodeClient();
    NodesStatsResponse nodeStats = client().admin().cluster().prepareNodesStats().get();
    NodeStats unluckyNode = randomFrom(nodeStats.getNodes().stream().filter((s) -> s.getNode().isDataNode()).collect(Collectors.toList()));
    assertAcked(client().admin().indices().prepareCreate("index").setSettings(Settings.builder().put("index.number_of_replicas", 1).put("index.number_of_shards", 5)));
    ensureGreen("index");
    logger.info("unlucky node: {}", unluckyNode.getNode());
    //create a transport service that throws a ConnectTransportException for one bulk request and therefore triggers a retry.
    for (NodeStats dataNode : nodeStats.getNodes()) {
        MockTransportService mockTransportService = ((MockTransportService) internalCluster().getInstance(TransportService.class, dataNode.getNode().getName()));
        mockTransportService.addDelegate(internalCluster().getInstance(TransportService.class, unluckyNode.getNode().getName()), new MockTransportService.DelegateTransport(mockTransportService.original()) {

            @Override
            protected void sendRequest(Connection connection, long requestId, String action, TransportRequest request, TransportRequestOptions options) throws IOException {
                super.sendRequest(connection, requestId, action, request, options);
                if (action.equals(TransportShardBulkAction.ACTION_NAME) && exceptionThrown.compareAndSet(false, true)) {
                    logger.debug("Throw ConnectTransportException");
                    throw new ConnectTransportException(connection.getNode(), action);
                }
            }
        });
    }
    BulkRequestBuilder bulkBuilder = client.prepareBulk();
    for (int i = 0; i < numDocs; i++) {
        XContentBuilder doc = null;
        doc = jsonBuilder().startObject().field("foo", "bar").endObject();
        bulkBuilder.add(client.prepareIndex("index", "type").setSource(doc));
    }
    BulkResponse response = bulkBuilder.get();
    if (response.hasFailures()) {
        for (BulkItemResponse singleIndexRespons : response.getItems()) {
            if (singleIndexRespons.isFailed()) {
                fail("None of the bulk items should fail but got " + singleIndexRespons.getFailureMessage());
            }
        }
    }
    refresh();
    SearchResponse searchResponse = client().prepareSearch("index").setSize(numDocs * 2).addStoredField("_id").get();
    Set<String> uniqueIds = new HashSet();
    long dupCounter = 0;
    boolean found_duplicate_already = false;
    for (int i = 0; i < searchResponse.getHits().getHits().length; i++) {
        if (!uniqueIds.add(searchResponse.getHits().getHits()[i].getId())) {
            if (!found_duplicate_already) {
                SearchResponse dupIdResponse = client().prepareSearch("index").setQuery(termQuery("_id", searchResponse.getHits().getHits()[i].getId())).setExplain(true).get();
                assertThat(dupIdResponse.getHits().getTotalHits(), greaterThan(1L));
                logger.info("found a duplicate id:");
                for (SearchHit hit : dupIdResponse.getHits()) {
                    logger.info("Doc {} was found on shard {}", hit.getId(), hit.getShard().getShardId());
                }
                logger.info("will not print anymore in case more duplicates are found.");
                found_duplicate_already = true;
            }
            dupCounter++;
        }
    }
    assertSearchResponse(searchResponse);
    assertThat(dupCounter, equalTo(0L));
    assertHitCount(searchResponse, numDocs);
    IndicesStatsResponse index = client().admin().indices().prepareStats("index").clear().setSegments(true).get();
    IndexStats indexStats = index.getIndex("index");
    long maxUnsafeAutoIdTimestamp = Long.MIN_VALUE;
    for (IndexShardStats indexShardStats : indexStats) {
        for (ShardStats shardStats : indexShardStats) {
            SegmentsStats segments = shardStats.getStats().getSegments();
            maxUnsafeAutoIdTimestamp = Math.max(maxUnsafeAutoIdTimestamp, segments.getMaxUnsafeAutoIdTimestamp());
        }
    }
    assertTrue("exception must have been thrown otherwise setup is broken", exceptionThrown.get());
    assertTrue("maxUnsafeAutoIdTimestamp must be > than 0 we have at least one retry", maxUnsafeAutoIdTimestamp > -1);
}
Also used : MockTransportService(org.elasticsearch.test.transport.MockTransportService) SearchHit(org.elasticsearch.search.SearchHit) IndexShardStats(org.elasticsearch.action.admin.indices.stats.IndexShardStats) SegmentsStats(org.elasticsearch.index.engine.SegmentsStats) NodesStatsResponse(org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse) NodeStats(org.elasticsearch.action.admin.cluster.node.stats.NodeStats) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) Client(org.elasticsearch.client.Client) HashSet(java.util.HashSet) IndexShardStats(org.elasticsearch.action.admin.indices.stats.IndexShardStats) ShardStats(org.elasticsearch.action.admin.indices.stats.ShardStats) IndicesStatsResponse(org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse) TransportRequest(org.elasticsearch.transport.TransportRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IOException(java.io.IOException) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) IndexStats(org.elasticsearch.action.admin.indices.stats.IndexStats) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 60 with Client

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

the class InternalTestCluster method getMasterName.

/**
     * Returns the name of the current master node in the cluster and executes the request via the node specified
     * in the viaNode parameter. If viaNode isn't specified a random node will be picked to the send the request to.
     */
public String getMasterName(@Nullable String viaNode) {
    try {
        Client client = viaNode != null ? client(viaNode) : client();
        ClusterState state = client.admin().cluster().prepareState().execute().actionGet().getState();
        return state.nodes().getMasterNode().getName();
    } catch (Exception e) {
        logger.warn("Can't fetch cluster state", e);
        throw new RuntimeException("Can't get master node " + e.getMessage(), e);
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) TransportClient(org.elasticsearch.client.transport.TransportClient) Client(org.elasticsearch.client.Client) MockTransportClient(org.elasticsearch.transport.MockTransportClient) NodeValidationException(org.elasticsearch.node.NodeValidationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException)

Aggregations

Client (org.elasticsearch.client.Client)164 CreateSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)42 Settings (org.elasticsearch.common.settings.Settings)38 Path (java.nio.file.Path)30 RestoreSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)28 PutRepositoryResponse (org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse)24 ArrayList (java.util.ArrayList)23 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)23 IOException (java.io.IOException)20 Matchers.containsString (org.hamcrest.Matchers.containsString)20 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)18 ClusterState (org.elasticsearch.cluster.ClusterState)17 ExecutionException (java.util.concurrent.ExecutionException)16 SearchResponse (org.elasticsearch.action.search.SearchResponse)14 ClusterAdminClient (org.elasticsearch.client.ClusterAdminClient)13 ClusterService (org.elasticsearch.cluster.service.ClusterService)10 List (java.util.List)9 ActionRequestValidationException (org.elasticsearch.action.ActionRequestValidationException)9 CloseIndexResponse (org.elasticsearch.action.admin.indices.close.CloseIndexResponse)9 OpenIndexResponse (org.elasticsearch.action.admin.indices.open.OpenIndexResponse)9