Search in sources :

Example 56 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger 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 57 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project elasticsearch by elastic.

the class MetaDataCreateIndexService method validateShrinkIndex.

/**
     * Validates the settings and mappings for shrinking an index.
     * @return the list of nodes at least one instance of the source index shards are allocated
     */
static List<String> validateShrinkIndex(ClusterState state, String sourceIndex, Set<String> targetIndexMappingsTypes, String targetIndexName, Settings targetIndexSettings) {
    if (state.metaData().hasIndex(targetIndexName)) {
        throw new ResourceAlreadyExistsException(state.metaData().index(targetIndexName).getIndex());
    }
    final IndexMetaData sourceMetaData = state.metaData().index(sourceIndex);
    if (sourceMetaData == null) {
        throw new IndexNotFoundException(sourceIndex);
    }
    // ensure index is read-only
    if (state.blocks().indexBlocked(ClusterBlockLevel.WRITE, sourceIndex) == false) {
        throw new IllegalStateException("index " + sourceIndex + " must be read-only to shrink index. use \"index.blocks.write=true\"");
    }
    if (sourceMetaData.getNumberOfShards() == 1) {
        throw new IllegalArgumentException("can't shrink an index with only one shard");
    }
    if ((targetIndexMappingsTypes.size() > 1 || (targetIndexMappingsTypes.isEmpty() || targetIndexMappingsTypes.contains(MapperService.DEFAULT_MAPPING)) == false)) {
        throw new IllegalArgumentException("mappings are not allowed when shrinking indices" + ", all mappings are copied from the source index");
    }
    if (IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.exists(targetIndexSettings)) {
        // this method applies all necessary checks ie. if the target shards are less than the source shards
        // of if the source shards are divisible by the number of target shards
        IndexMetaData.getRoutingFactor(sourceMetaData, IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.get(targetIndexSettings));
    }
    // now check that index is all on one node
    final IndexRoutingTable table = state.routingTable().index(sourceIndex);
    Map<String, AtomicInteger> nodesToNumRouting = new HashMap<>();
    int numShards = sourceMetaData.getNumberOfShards();
    for (ShardRouting routing : table.shardsWithState(ShardRoutingState.STARTED)) {
        nodesToNumRouting.computeIfAbsent(routing.currentNodeId(), (s) -> new AtomicInteger(0)).incrementAndGet();
    }
    List<String> nodesToAllocateOn = new ArrayList<>();
    for (Map.Entry<String, AtomicInteger> entries : nodesToNumRouting.entrySet()) {
        int numAllocations = entries.getValue().get();
        assert numAllocations <= numShards : "wait what? " + numAllocations + " is > than num shards " + numShards;
        if (numAllocations == numShards) {
            nodesToAllocateOn.add(entries.getKey());
        }
    }
    if (nodesToAllocateOn.isEmpty()) {
        throw new IllegalStateException("index " + sourceIndex + " must have all shards allocated on the same node to shrink index");
    }
    return nodesToAllocateOn;
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) SETTING_INDEX_UUID(org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_INDEX_UUID) DateTimeZone(org.joda.time.DateTimeZone) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Alias(org.elasticsearch.action.admin.indices.alias.Alias) Environment(org.elasticsearch.env.Environment) BiFunction(java.util.function.BiFunction) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) CreateIndexClusterStateUpdateResponse(org.elasticsearch.cluster.ack.CreateIndexClusterStateUpdateResponse) ClusterBlock(org.elasticsearch.cluster.block.ClusterBlock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndexCreationException(org.elasticsearch.indices.IndexCreationException) Locale(java.util.Locale) Map(java.util.Map) ValidationException(org.elasticsearch.common.ValidationException) ThreadPool(org.elasticsearch.threadpool.ThreadPool) State(org.elasticsearch.cluster.metadata.IndexMetaData.State) Path(java.nio.file.Path) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) CreateIndexClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest) Priority(org.elasticsearch.common.Priority) Predicate(java.util.function.Predicate) UUIDs(org.elasticsearch.common.UUIDs) Set(java.util.Set) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) ContextPreservingActionListener(org.elasticsearch.action.support.ContextPreservingActionListener) MapperService(org.elasticsearch.index.mapper.MapperService) List(java.util.List) Version(org.elasticsearch.Version) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) Supplier(org.apache.logging.log4j.util.Supplier) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse) InvalidIndexNameException(org.elasticsearch.indices.InvalidIndexNameException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) AckedClusterStateUpdateTask(org.elasticsearch.cluster.AckedClusterStateUpdateTask) ClusterService(org.elasticsearch.cluster.service.ClusterService) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException) ActiveShardsObserver(org.elasticsearch.action.support.ActiveShardsObserver) Strings(org.elasticsearch.common.Strings) Inject(org.elasticsearch.common.inject.Inject) ArrayList(java.util.ArrayList) SETTING_NUMBER_OF_REPLICAS(org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS) XContentHelper(org.elasticsearch.common.xcontent.XContentHelper) IndexRemovalReason(org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason) Custom(org.elasticsearch.cluster.metadata.IndexMetaData.Custom) Regex(org.elasticsearch.common.regex.Regex) SETTING_VERSION_CREATED(org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_VERSION_CREATED) IndicesService(org.elasticsearch.indices.IndicesService) SETTING_AUTO_EXPAND_REPLICAS(org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS) ClusterBlockLevel(org.elasticsearch.cluster.block.ClusterBlockLevel) SETTING_CREATION_DATE(org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_CREATION_DATE) PathUtils(org.elasticsearch.common.io.PathUtils) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) AbstractComponent(org.elasticsearch.common.component.AbstractComponent) SETTING_NUMBER_OF_SHARDS(org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS) IndexService(org.elasticsearch.index.IndexService) DateTime(org.joda.time.DateTime) IOException(java.io.IOException) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) CollectionUtil(org.apache.lucene.util.CollectionUtil) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MergeReason(org.elasticsearch.index.mapper.MapperService.MergeReason) Comparator(java.util.Comparator) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Map(java.util.Map) HashMap(java.util.HashMap)

Example 58 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project elasticsearch by elastic.

the class ActionListenerTests method testOnResponse.

public void testOnResponse() {
    final int numListeners = randomIntBetween(1, 20);
    List<AtomicReference<Boolean>> refList = new ArrayList<>();
    List<AtomicReference<Exception>> excList = new ArrayList<>();
    List<ActionListener<Boolean>> listeners = new ArrayList<>();
    List<Boolean> failOnTrue = new ArrayList<>();
    AtomicInteger exceptionCounter = new AtomicInteger(0);
    for (int i = 0; i < numListeners; i++) {
        boolean doFailOnTrue = rarely();
        failOnTrue.add(doFailOnTrue);
        AtomicReference<Boolean> reference = new AtomicReference<>();
        AtomicReference<Exception> exReference = new AtomicReference<>();
        refList.add(reference);
        excList.add(exReference);
        CheckedConsumer<Boolean, ? extends Exception> handler = (o) -> {
            if (Boolean.FALSE.equals(o)) {
                throw new IllegalArgumentException("must not be false " + exceptionCounter.getAndIncrement());
            }
            if (doFailOnTrue) {
                throw new IllegalStateException("must not be true");
            }
            reference.set(o);
        };
        listeners.add(ActionListener.wrap(handler, exReference::set));
    }
    ActionListener.onResponse(listeners, Boolean.TRUE);
    for (int i = 0; i < numListeners; i++) {
        if (failOnTrue.get(i) == false) {
            assertTrue("listener index " + i, refList.get(i).get());
            refList.get(i).set(null);
        } else {
            assertNull("listener index " + i, refList.get(i).get());
        }
    }
    for (int i = 0; i < numListeners; i++) {
        if (failOnTrue.get(i) == false) {
            assertNull("listener index " + i, excList.get(i).get());
        } else {
            assertEquals("listener index " + i, "must not be true", excList.get(i).get().getMessage());
        }
    }
    ActionListener.onResponse(listeners, Boolean.FALSE);
    for (int i = 0; i < numListeners; i++) {
        assertNull("listener index " + i, refList.get(i).get());
    }
    assertEquals(numListeners, exceptionCounter.get());
    for (int i = 0; i < numListeners; i++) {
        assertNotNull(excList.get(i).get());
        assertEquals("listener index " + i, "must not be false " + i, excList.get(i).get().getMessage());
    }
}
Also used : List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CheckedConsumer(org.elasticsearch.common.CheckedConsumer) ESTestCase(org.elasticsearch.test.ESTestCase) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 59 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project elasticsearch by elastic.

the class BackoffPolicyTests method testWrapBackoffPolicy.

public void testWrapBackoffPolicy() {
    TimeValue timeValue = timeValueMillis(between(0, Integer.MAX_VALUE));
    int maxNumberOfRetries = between(1, 1000);
    BackoffPolicy policy = BackoffPolicy.constantBackoff(timeValue, maxNumberOfRetries);
    AtomicInteger retries = new AtomicInteger();
    policy = BackoffPolicy.wrap(policy, retries::getAndIncrement);
    int expectedRetries = 0;
    {
        // Fetching the iterator doesn't call the callback
        Iterator<TimeValue> itr = policy.iterator();
        assertEquals(expectedRetries, retries.get());
        while (itr.hasNext()) {
            // hasNext doesn't trigger the callback
            assertEquals(expectedRetries, retries.get());
            // next does
            itr.next();
            expectedRetries += 1;
            assertEquals(expectedRetries, retries.get());
        }
        // next doesn't call the callback when there isn't a backoff available
        expectThrows(NoSuchElementException.class, () -> itr.next());
        assertEquals(expectedRetries, retries.get());
    }
    {
        // The second iterator also calls the callback
        Iterator<TimeValue> itr = policy.iterator();
        itr.next();
        expectedRetries += 1;
        assertEquals(expectedRetries, retries.get());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(java.util.Iterator) TimeValue(org.elasticsearch.common.unit.TimeValue) NoSuchElementException(java.util.NoSuchElementException)

Example 60 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project elasticsearch by elastic.

the class TransportClientNodesServiceTests method testListenerFailures.

public void testListenerFailures() throws InterruptedException {
    int iters = iterations(10, 100);
    for (int i = 0; i < iters; i++) {
        try (TestIteration iteration = new TestIteration()) {
            // stop transport from responding early
            iteration.transport.endConnectMode();
            final CountDownLatch latch = new CountDownLatch(1);
            final AtomicInteger finalFailures = new AtomicInteger();
            final AtomicReference<Throwable> finalFailure = new AtomicReference<>();
            final AtomicReference<TestResponse> response = new AtomicReference<>();
            ActionListener<TestResponse> actionListener = new ActionListener<TestResponse>() {

                @Override
                public void onResponse(TestResponse testResponse) {
                    response.set(testResponse);
                    latch.countDown();
                }

                @Override
                public void onFailure(Exception e) {
                    finalFailures.incrementAndGet();
                    finalFailure.set(e);
                    latch.countDown();
                }
            };
            final AtomicInteger preSendFailures = new AtomicInteger();
            iteration.transportClientNodesService.execute((node, retryListener) -> {
                if (rarely()) {
                    preSendFailures.incrementAndGet();
                    //throw whatever exception that is not a subclass of ConnectTransportException
                    throw new IllegalArgumentException();
                }
                iteration.transportService.sendRequest(node, "action", new TestRequest(), TransportRequestOptions.EMPTY, new TransportResponseHandler<TestResponse>() {

                    @Override
                    public TestResponse newInstance() {
                        return new TestResponse();
                    }

                    @Override
                    public void handleResponse(TestResponse response1) {
                        retryListener.onResponse(response1);
                    }

                    @Override
                    public void handleException(TransportException exp) {
                        retryListener.onFailure(exp);
                    }

                    @Override
                    public String executor() {
                        return randomBoolean() ? ThreadPool.Names.SAME : ThreadPool.Names.GENERIC;
                    }
                });
            }, actionListener);
            assertThat(latch.await(1, TimeUnit.SECONDS), equalTo(true));
            //there can be only either one failure that causes the request to fail straightaway or success
            assertThat(preSendFailures.get() + iteration.transport.failures() + iteration.transport.successes(), lessThanOrEqualTo(1));
            if (iteration.transport.successes() == 1) {
                assertThat(finalFailures.get(), equalTo(0));
                assertThat(finalFailure.get(), nullValue());
                assertThat(response.get(), notNullValue());
            } else {
                assertThat(finalFailures.get(), equalTo(1));
                assertThat(finalFailure.get(), notNullValue());
                assertThat(response.get(), nullValue());
                if (preSendFailures.get() == 0 && iteration.transport.failures() == 0) {
                    assertThat(finalFailure.get(), instanceOf(NoNodeAvailableException.class));
                }
            }
            assertThat(iteration.transport.triedNodes().size(), lessThanOrEqualTo(iteration.listNodesCount));
            assertThat(iteration.transport.triedNodes().size(), equalTo(iteration.transport.connectTransportExceptions() + iteration.transport.failures() + iteration.transport.successes()));
        }
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TransportException(org.elasticsearch.transport.TransportException) TransportException(org.elasticsearch.transport.TransportException) ActionListener(org.elasticsearch.action.ActionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7986 Test (org.junit.Test)3775 CountDownLatch (java.util.concurrent.CountDownLatch)1072 ArrayList (java.util.ArrayList)1018 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)849 List (java.util.List)740 IOException (java.io.IOException)719 AtomicReference (java.util.concurrent.atomic.AtomicReference)574 HashMap (java.util.HashMap)499 Map (java.util.Map)460 Test (org.testng.annotations.Test)419 File (java.io.File)337 ExecutorService (java.util.concurrent.ExecutorService)337 Test (org.junit.jupiter.api.Test)334 AtomicLong (java.util.concurrent.atomic.AtomicLong)329 TimeUnit (java.util.concurrent.TimeUnit)323 HashSet (java.util.HashSet)315 Arrays (java.util.Arrays)308 Set (java.util.Set)284 Collections (java.util.Collections)266