Search in sources :

Example 6 with ClusterHealthRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest in project YCSB by brianfrankcooper.

the class ElasticsearchClient method init.

/**
   * Initialize any state for this DB. Called once per DB instance; there is one
   * DB instance per client thread.
   */
@Override
public void init() throws DBException {
    final Properties props = getProperties();
    // Check if transport client needs to be used (To connect to multiple
    // elasticsearch nodes)
    remoteMode = Boolean.parseBoolean(props.getProperty("es.remote", "false"));
    final String pathHome = props.getProperty("path.home");
    // when running in embedded mode, require path.home
    if (!remoteMode && (pathHome == null || pathHome.isEmpty())) {
        throw new IllegalArgumentException("path.home must be specified when running in embedded mode");
    }
    this.indexKey = props.getProperty("es.index.key", DEFAULT_INDEX_KEY);
    int numberOfShards = parseIntegerProperty(props, "es.number_of_shards", NUMBER_OF_SHARDS);
    int numberOfReplicas = parseIntegerProperty(props, "es.number_of_replicas", NUMBER_OF_REPLICAS);
    Boolean newdb = Boolean.parseBoolean(props.getProperty("es.newdb", "false"));
    Builder settings = Settings.settingsBuilder().put("cluster.name", DEFAULT_CLUSTER_NAME).put("node.local", Boolean.toString(!remoteMode)).put("path.home", pathHome);
    // if properties file contains elasticsearch user defined properties
    // add it to the settings file (will overwrite the defaults).
    settings.put(props);
    final String clusterName = settings.get("cluster.name");
    System.err.println("Elasticsearch starting node = " + clusterName);
    System.err.println("Elasticsearch node path.home = " + settings.get("path.home"));
    System.err.println("Elasticsearch Remote Mode = " + remoteMode);
    // Remote mode support for connecting to remote elasticsearch cluster
    if (remoteMode) {
        settings.put("client.transport.sniff", true).put("client.transport.ignore_cluster_name", false).put("client.transport.ping_timeout", "30s").put("client.transport.nodes_sampler_interval", "30s");
        // Default it to localhost:9300
        String[] nodeList = props.getProperty("es.hosts.list", DEFAULT_REMOTE_HOST).split(",");
        System.out.println("Elasticsearch Remote Hosts = " + props.getProperty("es.hosts.list", DEFAULT_REMOTE_HOST));
        TransportClient tClient = TransportClient.builder().settings(settings).build();
        for (String h : nodeList) {
            String[] nodes = h.split(":");
            try {
                tClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(nodes[0]), Integer.parseInt(nodes[1])));
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("Unable to parse port number.", e);
            } catch (UnknownHostException e) {
                throw new IllegalArgumentException("Unable to Identify host.", e);
            }
        }
        client = tClient;
    } else {
        // Start node only if transport client mode is disabled
        node = nodeBuilder().clusterName(clusterName).settings(settings).node();
        node.start();
        client = node.client();
    }
    final boolean exists = client.admin().indices().exists(Requests.indicesExistsRequest(indexKey)).actionGet().isExists();
    if (exists && newdb) {
        client.admin().indices().prepareDelete(indexKey).execute().actionGet();
    }
    if (!exists || newdb) {
        client.admin().indices().create(new CreateIndexRequest(indexKey).settings(Settings.builder().put("index.number_of_shards", numberOfShards).put("index.number_of_replicas", numberOfReplicas).put("index.mapping._id.indexed", true))).actionGet();
    }
    client.admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet();
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) UnknownHostException(java.net.UnknownHostException) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) NodeBuilder.nodeBuilder(org.elasticsearch.node.NodeBuilder.nodeBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) Builder(org.elasticsearch.common.settings.Settings.Builder) Properties(java.util.Properties) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Example 7 with ClusterHealthRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest in project unipop by unipop-graph.

the class LocalNode method checkHealth.

public void checkHealth() {
    final ClusterHealthRequest clusterHealthRequest = new ClusterHealthRequest().timeout(TimeValue.timeValueSeconds(10)).waitForYellowStatus();
    final ClusterHealthResponse clusterHealth = client.admin().cluster().health(clusterHealthRequest).actionGet();
    if (clusterHealth.isTimedOut()) {
        System.out.println(clusterHealth.getStatus() + " status returned from cluster '" + client.admin().cluster().toString());
    }
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest)

Example 8 with ClusterHealthRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest in project logging-log4j2 by apache.

the class LogstashIT method createClient.

private static RestHighLevelClient createClient() throws IOException {
    // Instantiate the client.
    LOGGER.info("instantiating the ES client");
    final HttpHost httpHost = new HttpHost(HOST_NAME, MavenHardcodedConstants.ES_PORT);
    final RestClientBuilder clientBuilder = RestClient.builder(httpHost);
    final RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
    // Verify the connection.
    LOGGER.info("verifying the ES connection");
    final ClusterHealthResponse healthResponse = client.cluster().health(new ClusterHealthRequest(), RequestOptions.DEFAULT);
    Assertions.assertThat(healthResponse.getStatus()).isNotEqualTo(ClusterHealthStatus.RED);
    // Delete the index.
    LOGGER.info("deleting the ES index");
    final DeleteIndexRequest deleteRequest = new DeleteIndexRequest(MavenHardcodedConstants.ES_INDEX_NAME);
    try {
        final AcknowledgedResponse deleteResponse = client.indices().delete(deleteRequest, RequestOptions.DEFAULT);
        Assertions.assertThat(deleteResponse.isAcknowledged()).isTrue();
    } catch (ElasticsearchStatusException error) {
        Assertions.assertThat(error).satisfies(ignored -> Assertions.assertThat(error.status()).isEqualTo(RestStatus.NOT_FOUND));
    }
    return client;
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Arrays(java.util.Arrays) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) Level(org.apache.logging.log4j.Level) LogEvent(org.apache.logging.log4j.core.LogEvent) Duration(java.time.Duration) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) RequestOptions(org.elasticsearch.client.RequestOptions) Assertions(org.assertj.core.api.Assertions) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SearchHit(org.elasticsearch.search.SearchHit) ExecutionMode(org.junit.jupiter.api.parallel.ExecutionMode) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ThreadLocalRecyclerFactory(org.apache.logging.log4j.layout.template.json.util.ThreadLocalRecyclerFactory) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) GelfLayout(org.apache.logging.log4j.core.layout.GelfLayout) RestStatus(org.elasticsearch.rest.RestStatus) Layout(org.apache.logging.log4j.core.Layout) Awaitility(org.awaitility.Awaitility) RestClient(org.elasticsearch.client.RestClient) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Appender(org.apache.logging.log4j.core.Appender) SearchRequest(org.elasticsearch.action.search.SearchRequest) Function(java.util.function.Function) EventTemplateAdditionalField(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout.EventTemplateAdditionalField) NetUtils(org.apache.logging.log4j.core.util.NetUtils) Charset(java.nio.charset.Charset) SocketAppender(org.apache.logging.log4j.core.appender.SocketAppender) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) PrintStream(java.io.PrintStream) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) EcsLayout(co.elastic.logging.log4j2.EcsLayout) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) IOException(java.io.IOException) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) StatusLogger(org.apache.logging.log4j.status.StatusLogger) ClusterHealthStatus(org.elasticsearch.cluster.health.ClusterHealthStatus) Execution(org.junit.jupiter.api.parallel.Execution) HttpHost(org.apache.http.HttpHost) Collections(java.util.Collections) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) HttpHost(org.apache.http.HttpHost) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException)

Example 9 with ClusterHealthRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest in project YCSB by brianfrankcooper.

the class ElasticsearchClient method init.

/**
 * Initialize any state for this DB. Called once per DB instance; there is one
 * DB instance per client thread.
 */
@Override
public void init() throws DBException {
    final Properties props = getProperties();
    // Check if transport client needs to be used (To connect to multiple
    // elasticsearch nodes)
    remoteMode = Boolean.parseBoolean(props.getProperty("es.remote", "false"));
    final String pathHome = props.getProperty("path.home");
    // when running in embedded mode, require path.home
    if (!remoteMode && (pathHome == null || pathHome.isEmpty())) {
        throw new IllegalArgumentException("path.home must be specified when running in embedded mode");
    }
    this.indexKey = props.getProperty("es.index.key", DEFAULT_INDEX_KEY);
    int numberOfShards = parseIntegerProperty(props, "es.number_of_shards", NUMBER_OF_SHARDS);
    int numberOfReplicas = parseIntegerProperty(props, "es.number_of_replicas", NUMBER_OF_REPLICAS);
    Boolean newdb = Boolean.parseBoolean(props.getProperty("es.newdb", "false"));
    Builder settings = Settings.settingsBuilder().put("cluster.name", DEFAULT_CLUSTER_NAME).put("node.local", Boolean.toString(!remoteMode)).put("path.home", pathHome);
    // if properties file contains elasticsearch user defined properties
    // add it to the settings file (will overwrite the defaults).
    settings.put(props);
    final String clusterName = settings.get("cluster.name");
    System.err.println("Elasticsearch starting node = " + clusterName);
    System.err.println("Elasticsearch node path.home = " + settings.get("path.home"));
    System.err.println("Elasticsearch Remote Mode = " + remoteMode);
    // Remote mode support for connecting to remote elasticsearch cluster
    if (remoteMode) {
        settings.put("client.transport.sniff", true).put("client.transport.ignore_cluster_name", false).put("client.transport.ping_timeout", "30s").put("client.transport.nodes_sampler_interval", "30s");
        // Default it to localhost:9300
        String[] nodeList = props.getProperty("es.hosts.list", DEFAULT_REMOTE_HOST).split(",");
        System.out.println("Elasticsearch Remote Hosts = " + props.getProperty("es.hosts.list", DEFAULT_REMOTE_HOST));
        TransportClient tClient = TransportClient.builder().settings(settings).build();
        for (String h : nodeList) {
            String[] nodes = h.split(":");
            try {
                tClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(nodes[0]), Integer.parseInt(nodes[1])));
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("Unable to parse port number.", e);
            } catch (UnknownHostException e) {
                throw new IllegalArgumentException("Unable to Identify host.", e);
            }
        }
        client = tClient;
    } else {
        // Start node only if transport client mode is disabled
        node = nodeBuilder().clusterName(clusterName).settings(settings).node();
        node.start();
        client = node.client();
    }
    final boolean exists = client.admin().indices().exists(Requests.indicesExistsRequest(indexKey)).actionGet().isExists();
    if (exists && newdb) {
        client.admin().indices().prepareDelete(indexKey).execute().actionGet();
    }
    if (!exists || newdb) {
        client.admin().indices().create(new CreateIndexRequest(indexKey).settings(Settings.builder().put("index.number_of_shards", numberOfShards).put("index.number_of_replicas", numberOfReplicas).put("index.mapping._id.indexed", true))).actionGet();
    }
    client.admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet();
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) UnknownHostException(java.net.UnknownHostException) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) NodeBuilder.nodeBuilder(org.elasticsearch.node.NodeBuilder.nodeBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) Builder(org.elasticsearch.common.settings.Settings.Builder) Properties(java.util.Properties) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Example 10 with ClusterHealthRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest in project YCSB by brianfrankcooper.

the class ElasticsearchClient method init.

/**
 * Initialize any state for this DB. Called once per DB instance; there is one
 * DB instance per client thread.
 */
@Override
public void init() throws DBException {
    final Properties props = getProperties();
    this.indexKey = props.getProperty("es.index.key", DEFAULT_INDEX_KEY);
    final int numberOfShards = parseIntegerProperty(props, "es.number_of_shards", NUMBER_OF_SHARDS);
    final int numberOfReplicas = parseIntegerProperty(props, "es.number_of_replicas", NUMBER_OF_REPLICAS);
    final Boolean newIndex = Boolean.parseBoolean(props.getProperty("es.new_index", "false"));
    final Builder settings = Settings.builder().put("cluster.name", DEFAULT_CLUSTER_NAME);
    // add it to the settings file (will overwrite the defaults).
    for (final Entry<Object, Object> e : props.entrySet()) {
        if (e.getKey() instanceof String) {
            final String key = (String) e.getKey();
            if (key.startsWith("es.setting.")) {
                settings.put(key.substring("es.setting.".length()), e.getValue());
            }
        }
    }
    settings.put("client.transport.sniff", true).put("client.transport.ignore_cluster_name", false).put("client.transport.ping_timeout", "30s").put("client.transport.nodes_sampler_interval", "30s");
    // Default it to localhost:9300
    final String[] nodeList = props.getProperty("es.hosts.list", DEFAULT_REMOTE_HOST).split(",");
    client = new PreBuiltTransportClient(settings.build());
    for (String h : nodeList) {
        String[] nodes = h.split(":");
        final InetAddress address;
        try {
            address = InetAddress.getByName(nodes[0]);
        } catch (UnknownHostException e) {
            throw new IllegalArgumentException("unable to identity host [" + nodes[0] + "]", e);
        }
        final int port;
        try {
            port = Integer.parseInt(nodes[1]);
        } catch (final NumberFormatException e) {
            throw new IllegalArgumentException("unable to parse port [" + nodes[1] + "]", e);
        }
        client.addTransportAddress(new InetSocketTransportAddress(address, port));
    }
    final boolean exists = client.admin().indices().exists(Requests.indicesExistsRequest(indexKey)).actionGet().isExists();
    if (exists && newIndex) {
        client.admin().indices().prepareDelete(indexKey).get();
    }
    if (!exists || newIndex) {
        client.admin().indices().create(new CreateIndexRequest(indexKey).settings(Settings.builder().put("index.number_of_shards", numberOfShards).put("index.number_of_replicas", numberOfReplicas))).actionGet();
    }
    client.admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet();
}
Also used : UnknownHostException(java.net.UnknownHostException) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) Builder(org.elasticsearch.common.settings.Settings.Builder) Properties(java.util.Properties) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) InetAddress(java.net.InetAddress)

Aggregations

ClusterHealthRequest (org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest)22 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)15 IOException (java.io.IOException)6 Set (java.util.Set)4 ClusterHealthStatus (org.elasticsearch.cluster.health.ClusterHealthStatus)4 UnknownHostException (java.net.UnknownHostException)3 Collections (java.util.Collections)3 Locale (java.util.Locale)3 Properties (java.util.Properties)3 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)3 Settings (org.elasticsearch.common.settings.Settings)3 Builder (org.elasticsearch.common.settings.Settings.Builder)3 InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)3 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 XContentFactory.jsonBuilder (org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder)3 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Arrays (java.util.Arrays)2 HashSet (java.util.HashSet)2 List (java.util.List)2