Search in sources :

Example 1 with BulkCreateIndicesRequest

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

the class BulkShardProcessor method createPendingIndices.

private void createPendingIndices() {
    final List<PendingRequest> pendings = new ArrayList<>();
    final Set<String> indices;
    synchronized (requestsForNewIndices) {
        indices = ImmutableSet.copyOf(Iterables.filter(Sets.difference(requestsForNewIndices.keySet(), indicesCreated), shouldAutocreateIndexPredicate));
        for (Map.Entry<String, List<PendingRequest>> entry : requestsForNewIndices.entrySet()) {
            pendings.addAll(entry.getValue());
        }
        requestsForNewIndices.clear();
        pendingNewIndexRequests.set(0);
    }
    if (pendings.size() > 0 || indices.size() > 0) {
        LOGGER.debug("create {} pending indices in bulk...", indices.size());
        BulkCreateIndicesRequest bulkCreateIndicesRequest = new BulkCreateIndicesRequest(indices, jobId);
        final FutureCallback<Void> indicesCreatedCallback = new FutureCallback<Void>() {

            @Override
            public void onSuccess(@Nullable Void result) {
                if (failure.get() != null) {
                    return;
                }
                trace("applying pending requests for created indices...");
                Iterator<PendingRequest> it = pendings.iterator();
                while (it.hasNext()) {
                    PendingRequest pendingRequest = it.next();
                    // add pending requests for created indices
                    ShardId shardId = shardId(pendingRequest.indexName, pendingRequest.item.id(), pendingRequest.routing);
                    if (shardId == null) {
                        // seems like index is deleted meanwhile, mark item as failed and remove pending
                        indicesDeleted.add(pendingRequest.indexName);
                        it.remove();
                        synchronized (responsesLock) {
                            responses.set(globalCounter.getAndIncrement(), false);
                        }
                        setResultIfDone(1);
                        continue;
                    }
                    partitionRequestByShard(shardId, pendingRequest.item, pendingRequest.routing);
                }
                trace("added %d pending requests, lets see if we can execute them", pendings.size());
                executeRequestsIfNeeded();
            }

            @Override
            public void onFailure(Throwable t) {
                setFailure(t);
            }
        };
        if (indices.isEmpty()) {
            indicesCreatedCallback.onSuccess(null);
        } else {
            transportBulkCreateIndicesAction.execute(bulkCreateIndicesRequest, new ActionListener<BulkCreateIndicesResponse>() {

                @Override
                public void onResponse(BulkCreateIndicesResponse response) {
                    indicesCreated.addAll(indices);
                    indicesCreatedCallback.onSuccess(null);
                }

                @Override
                public void onFailure(Throwable t) {
                    indicesCreatedCallback.onFailure(t);
                }
            });
        }
    }
}
Also used : BulkCreateIndicesResponse(org.elasticsearch.action.admin.indices.create.BulkCreateIndicesResponse) ShardId(org.elasticsearch.index.shard.ShardId) BulkCreateIndicesRequest(org.elasticsearch.action.admin.indices.create.BulkCreateIndicesRequest) FutureCallback(com.google.common.util.concurrent.FutureCallback) Nullable(javax.annotation.Nullable)

Aggregations

FutureCallback (com.google.common.util.concurrent.FutureCallback)1 Nullable (javax.annotation.Nullable)1 BulkCreateIndicesRequest (org.elasticsearch.action.admin.indices.create.BulkCreateIndicesRequest)1 BulkCreateIndicesResponse (org.elasticsearch.action.admin.indices.create.BulkCreateIndicesResponse)1 ShardId (org.elasticsearch.index.shard.ShardId)1