Search in sources :

Example 6 with CreateIndexRequest

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequest in project elasticsearch by elastic.

the class TransportBulkAction method doExecute.

@Override
protected void doExecute(Task task, BulkRequest bulkRequest, ActionListener<BulkResponse> listener) {
    if (bulkRequest.hasIndexRequestsWithPipelines()) {
        if (clusterService.localNode().isIngestNode()) {
            processBulkIndexIngestRequest(task, bulkRequest, listener);
        } else {
            ingestForwarder.forwardIngestRequest(BulkAction.INSTANCE, bulkRequest, listener);
        }
        return;
    }
    final long startTime = relativeTime();
    final AtomicArray<BulkItemResponse> responses = new AtomicArray<>(bulkRequest.requests.size());
    if (needToCheck()) {
        // Keep track of all unique indices and all unique types per index for the create index requests:
        final Set<String> autoCreateIndices = bulkRequest.requests.stream().map(DocWriteRequest::index).collect(Collectors.toSet());
        final AtomicInteger counter = new AtomicInteger(autoCreateIndices.size());
        ClusterState state = clusterService.state();
        for (String index : autoCreateIndices) {
            if (shouldAutoCreate(index, state)) {
                CreateIndexRequest createIndexRequest = new CreateIndexRequest();
                createIndexRequest.index(index);
                createIndexRequest.cause("auto(bulk api)");
                createIndexRequest.masterNodeTimeout(bulkRequest.timeout());
                createIndexAction.execute(createIndexRequest, new ActionListener<CreateIndexResponse>() {

                    @Override
                    public void onResponse(CreateIndexResponse result) {
                        if (counter.decrementAndGet() == 0) {
                            executeBulk(task, bulkRequest, startTime, listener, responses);
                        }
                    }

                    @Override
                    public void onFailure(Exception e) {
                        if (!(ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException)) {
                            // fail all requests involving this index, if create didnt work
                            for (int i = 0; i < bulkRequest.requests.size(); i++) {
                                DocWriteRequest request = bulkRequest.requests.get(i);
                                if (request != null && setResponseFailureIfIndexMatches(responses, i, request, index, e)) {
                                    bulkRequest.requests.set(i, null);
                                }
                            }
                        }
                        if (counter.decrementAndGet() == 0) {
                            executeBulk(task, bulkRequest, startTime, ActionListener.wrap(listener::onResponse, inner -> {
                                inner.addSuppressed(e);
                                listener.onFailure(inner);
                            }), responses);
                        }
                    }
                });
            } else {
                if (counter.decrementAndGet() == 0) {
                    executeBulk(task, bulkRequest, startTime, listener, responses);
                }
            }
        }
    } else {
        executeBulk(task, bulkRequest, startTime, listener, responses);
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException) IndexClosedException(org.elasticsearch.indices.IndexClosedException) NodeClosedException(org.elasticsearch.node.NodeClosedException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) DocWriteRequest(org.elasticsearch.action.DocWriteRequest)

Example 7 with CreateIndexRequest

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequest in project elasticsearch-skywalker by jprante.

the class AbstractNodeTest method createIndices.

@BeforeMethod
public void createIndices() throws Exception {
    startNode("1");
    NodesInfoRequest nodesInfoRequest = new NodesInfoRequest().transport(true);
    NodesInfoResponse response = client("1").admin().cluster().nodesInfo(nodesInfoRequest).actionGet();
    InetSocketTransportAddress address = (InetSocketTransportAddress) response.iterator().next().getTransport().getAddress().publishAddress();
    addresses.put("1", address);
    client("1").admin().indices().create(new CreateIndexRequest(INDEX)).actionGet();
}
Also used : NodesInfoResponse(org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse) NodesInfoRequest(org.elasticsearch.action.admin.cluster.node.info.NodesInfoRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 8 with CreateIndexRequest

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequest 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 9 with CreateIndexRequest

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequest in project graylog2-server by Graylog2.

the class Indices method create.

public boolean create(String indexName, IndexSet indexSet, Settings customSettings) {
    final Settings settings = Settings.builder().put("number_of_shards", indexSet.getConfig().shards()).put("number_of_replicas", indexSet.getConfig().replicas()).put(customSettings).build();
    // Make sure our index template exists before creating an index!
    ensureIndexTemplate(indexSet);
    final CreateIndexRequest cir = c.admin().indices().prepareCreate(indexName).setSettings(settings).request();
    final boolean acknowledged = c.admin().indices().create(cir).actionGet().isAcknowledged();
    if (acknowledged) {
        auditEventSender.success(AuditActor.system(nodeId), ES_INDEX_CREATE, ImmutableMap.of("indexName", indexName));
    } else {
        auditEventSender.failure(AuditActor.system(nodeId), ES_INDEX_CREATE, ImmutableMap.of("indexName", indexName));
    }
    return acknowledged;
}
Also used : CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) Settings(org.elasticsearch.common.settings.Settings)

Example 10 with CreateIndexRequest

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequest in project crate by crate.

the class BlobAdminClient method createBlobTable.

public CompletableFuture<Void> createBlobTable(String tableName, Settings indexSettings) {
    Settings.Builder builder = Settings.builder();
    builder.put(indexSettings);
    builder.put(SETTING_INDEX_BLOBS_ENABLED, true);
    final CompletableFuture<Void> result = new CompletableFuture<>();
    createIndexAction.execute(new CreateIndexRequest(fullIndexName(tableName), builder.build()), new ActionListener<CreateIndexResponse>() {

        @Override
        public void onResponse(CreateIndexResponse createIndexResponse) {
            assert createIndexResponse.isAcknowledged() : "createIndexResponse must be acknowledged";
            result.complete(null);
        }

        @Override
        public void onFailure(Throwable e) {
            result.completeExceptionally(e);
        }
    });
    return result;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)13 Settings (org.elasticsearch.common.settings.Settings)5 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ResourceAlreadyExistsException (org.elasticsearch.ResourceAlreadyExistsException)2 CreateIndexClusterStateUpdateRequest (org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest)2 ClusterState (org.elasticsearch.cluster.ClusterState)2 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)2 InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 UnknownHostException (java.net.UnknownHostException)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Properties (java.util.Properties)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1