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;
}
}
}
}
}
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));
}
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
}
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);
}
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();
}
Aggregations