Search in sources :

Example 61 with Client

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

the class ESIntegTestCase method ensureClusterStateConsistency.

/**
     * Verifies that all nodes that have the same version of the cluster state as master have same cluster state
     */
protected void ensureClusterStateConsistency() throws IOException {
    if (cluster() != null && cluster().size() > 0) {
        final NamedWriteableRegistry namedWriteableRegistry;
        if (isInternalCluster()) {
            // If it's internal cluster - using existing registry in case plugin registered custom data
            namedWriteableRegistry = internalCluster().getInstance(NamedWriteableRegistry.class);
        } else {
            // If it's external cluster - fall back to the standard set
            namedWriteableRegistry = new NamedWriteableRegistry(ClusterModule.getNamedWriteables());
        }
        ClusterState masterClusterState = client().admin().cluster().prepareState().all().get().getState();
        byte[] masterClusterStateBytes = ClusterState.Builder.toBytes(masterClusterState);
        // remove local node reference
        masterClusterState = ClusterState.Builder.fromBytes(masterClusterStateBytes, null, namedWriteableRegistry);
        Map<String, Object> masterStateMap = convertToMap(masterClusterState);
        int masterClusterStateSize = ClusterState.Builder.toBytes(masterClusterState).length;
        String masterId = masterClusterState.nodes().getMasterNodeId();
        for (Client client : cluster().getClients()) {
            ClusterState localClusterState = client.admin().cluster().prepareState().all().setLocal(true).get().getState();
            byte[] localClusterStateBytes = ClusterState.Builder.toBytes(localClusterState);
            // remove local node reference
            localClusterState = ClusterState.Builder.fromBytes(localClusterStateBytes, null, namedWriteableRegistry);
            final Map<String, Object> localStateMap = convertToMap(localClusterState);
            final int localClusterStateSize = ClusterState.Builder.toBytes(localClusterState).length;
            // that the master node matches the master (otherwise there is no requirement for the cluster state to match)
            if (masterClusterState.version() == localClusterState.version() && masterId.equals(localClusterState.nodes().getMasterNodeId())) {
                try {
                    assertEquals("clusterstate UUID does not match", masterClusterState.stateUUID(), localClusterState.stateUUID());
                    // We cannot compare serialization bytes since serialization order of maps is not guaranteed
                    // but we can compare serialization sizes - they should be the same
                    assertEquals("clusterstate size does not match", masterClusterStateSize, localClusterStateSize);
                    // Compare JSON serialization
                    assertNull("clusterstate JSON serialization does not match", differenceBetweenMapsIgnoringArrayOrder(masterStateMap, localStateMap));
                } catch (AssertionError error) {
                    logger.error("Cluster state from master:\n{}\nLocal cluster state:\n{}", masterClusterState.toString(), localClusterState.toString());
                    throw error;
                }
            }
        }
    }
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) ClusterState(org.elasticsearch.cluster.ClusterState) AdminClient(org.elasticsearch.client.AdminClient) RandomizingClient(org.elasticsearch.test.client.RandomizingClient) Client(org.elasticsearch.client.Client) RestClient(org.elasticsearch.client.RestClient)

Example 62 with Client

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

the class SmokeTestClientIT method testSimpleClient.

/**
     * Check that we are connected to a cluster named "elasticsearch".
     */
public void testSimpleClient() {
    // TODO: remove when Netty 4.1.5 is upgraded to Netty 4.1.6 including https://github.com/netty/netty/pull/5778
    assumeFalse("JDK is JDK 9", Constants.JRE_IS_MINIMUM_JAVA9);
    Client client = getClient();
    // START SNIPPET: java-doc-admin-cluster-health
    ClusterHealthResponse health = client.admin().cluster().prepareHealth().setWaitForYellowStatus().get();
    String clusterName = health.getClusterName();
    int numberOfNodes = health.getNumberOfNodes();
    // END SNIPPET: java-doc-admin-cluster-health
    assertThat("cluster [" + clusterName + "] should have at least 1 node", numberOfNodes, greaterThan(0));
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) Client(org.elasticsearch.client.Client)

Example 63 with Client

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

the class SmokeTestClientIT method testPutDocument.

/**
     * Create an index and index some docs
     */
public void testPutDocument() {
    // TODO: remove when Netty 4.1.5 is upgraded to Netty 4.1.6 including https://github.com/netty/netty/pull/5778
    assumeFalse("JDK is JDK 9", Constants.JRE_IS_MINIMUM_JAVA9);
    Client client = getClient();
    // START SNIPPET: java-doc-index-doc-simple
    // Index, Type, Id
    client.prepareIndex(index, "doc", "1").setSource("foo", // Simple document: { "foo" : "bar" }
    "bar").get();
    // END SNIPPET: java-doc-index-doc-simple
    // START SNIPPET: java-doc-admin-indices-refresh
    // Prepare a refresh action on a given index, execute and wait for the result
    client.admin().indices().prepareRefresh(index).get();
    // END SNIPPET: java-doc-admin-indices-refresh
    // START SNIPPET: java-doc-search-simple
    SearchResponse searchResponse = client.prepareSearch(index).get();
    assertThat(searchResponse.getHits().getTotalHits(), is(1L));
// END SNIPPET: java-doc-search-simple
}
Also used : Client(org.elasticsearch.client.Client) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 64 with Client

use of org.elasticsearch.client.Client in project elasticsearch-river-kafka by endgameinc.

the class JsonMessageHandlerTest method testIt.

public void testIt() throws Exception {
    Client client = createMock(Client.class);
    IndexRequestBuilder irb = createMock(IndexRequestBuilder.class);
    JsonMessageHandler h = new JsonMessageHandler(client);
    byte[] json = toJson(rec).getBytes();
    expect(client.prepareIndex(anyObject(String.class), anyObject(String.class), anyObject(String.class))).andReturn(irb);
    replay(client);
    Message message = createMock(Message.class);
    expect(message.payload()).andReturn(ByteBuffer.wrap(json));
    replay(message);
    BulkRequestBuilder bulkRequestBuilder = createMock(BulkRequestBuilder.class);
    expect(bulkRequestBuilder.add(anyObject(IndexRequestBuilder.class))).andReturn(null);
    replay(bulkRequestBuilder);
    try {
        h.handle(bulkRequestBuilder, message);
    } catch (Exception e) {
        fail("This should not fail");
    }
    verify(client);
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Message(kafka.message.Message) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) Client(org.elasticsearch.client.Client)

Example 65 with Client

use of org.elasticsearch.client.Client in project sonarqube by SonarSource.

the class IndexerStartupTask method setIndexSetting.

private void setIndexSetting(String index, String name, boolean value) {
    Client nativeClient = esClient.nativeClient();
    Builder setting = org.elasticsearch.common.settings.Settings.builder().put(name, value);
    nativeClient.admin().indices().prepareUpdateSettings(index).setSettings(setting).get();
}
Also used : Builder(org.elasticsearch.common.settings.Settings.Builder) Client(org.elasticsearch.client.Client)

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