From 97db0c2182e20c499017e8d5fe0fc27f78a0360e Mon Sep 17 00:00:00 2001 From: David Turner Date: Wed, 25 Sep 2024 08:17:51 +0100 Subject: [PATCH 1/2] Remove `{Indices,}ClusterStateUpdateRequest` (#113483) These abstract classes are now unused so this commit removes them. --- server/src/main/java/module-info.java | 1 - .../ack/ClusterStateUpdateRequest.java | 55 ------------------- .../ack/IndicesClusterStateUpdateRequest.java | 35 ------------ 3 files changed, 91 deletions(-) delete mode 100644 server/src/main/java/org/elasticsearch/cluster/ack/ClusterStateUpdateRequest.java delete mode 100644 server/src/main/java/org/elasticsearch/cluster/ack/IndicesClusterStateUpdateRequest.java diff --git a/server/src/main/java/module-info.java b/server/src/main/java/module-info.java index f60a595d94c11..507fef10a5f44 100644 --- a/server/src/main/java/module-info.java +++ b/server/src/main/java/module-info.java @@ -159,7 +159,6 @@ exports org.elasticsearch.client.internal.support; exports org.elasticsearch.client.internal.transport; exports org.elasticsearch.cluster; - exports org.elasticsearch.cluster.ack; exports org.elasticsearch.cluster.action.index; exports org.elasticsearch.cluster.action.shard; exports org.elasticsearch.cluster.block; diff --git a/server/src/main/java/org/elasticsearch/cluster/ack/ClusterStateUpdateRequest.java b/server/src/main/java/org/elasticsearch/cluster/ack/ClusterStateUpdateRequest.java deleted file mode 100644 index 8841b315b0138..0000000000000 --- a/server/src/main/java/org/elasticsearch/cluster/ack/ClusterStateUpdateRequest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the "Elastic License - * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side - * Public License v 1"; you may not use this file except in compliance with, at - * your election, the "Elastic License 2.0", the "GNU Affero General Public - * License v3.0 only", or the "Server Side Public License, v 1". - */ - -package org.elasticsearch.cluster.ack; - -import org.elasticsearch.core.TimeValue; - -/** - * Base class to be used when needing to update the cluster state - * Contains the basic fields that are always needed - */ -public abstract class ClusterStateUpdateRequest> { - - private TimeValue ackTimeout; - private TimeValue masterNodeTimeout; - - /** - * Returns the maximum time interval to wait for acknowledgements - */ - public TimeValue ackTimeout() { - return ackTimeout; - } - - /** - * Sets the acknowledgement timeout - */ - @SuppressWarnings("unchecked") - public T ackTimeout(TimeValue ackTimeout) { - this.ackTimeout = ackTimeout; - return (T) this; - } - - /** - * Returns the maximum time interval to wait for the request to - * be completed on the master node - */ - public TimeValue masterNodeTimeout() { - return masterNodeTimeout; - } - - /** - * Sets the master node timeout - */ - @SuppressWarnings("unchecked") - public T masterNodeTimeout(TimeValue masterNodeTimeout) { - this.masterNodeTimeout = masterNodeTimeout; - return (T) this; - } -} diff --git a/server/src/main/java/org/elasticsearch/cluster/ack/IndicesClusterStateUpdateRequest.java b/server/src/main/java/org/elasticsearch/cluster/ack/IndicesClusterStateUpdateRequest.java deleted file mode 100644 index b1a52d50fd544..0000000000000 --- a/server/src/main/java/org/elasticsearch/cluster/ack/IndicesClusterStateUpdateRequest.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the "Elastic License - * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side - * Public License v 1"; you may not use this file except in compliance with, at - * your election, the "Elastic License 2.0", the "GNU Affero General Public - * License v3.0 only", or the "Server Side Public License, v 1". - */ -package org.elasticsearch.cluster.ack; - -import org.elasticsearch.index.Index; - -/** - * Base cluster state update request that allows to execute update against multiple indices - */ -public abstract class IndicesClusterStateUpdateRequest> extends ClusterStateUpdateRequest { - - private Index[] indices; - - /** - * Returns the indices the operation needs to be executed on - */ - public Index[] indices() { - return indices; - } - - /** - * Sets the indices the operation needs to be executed on - */ - @SuppressWarnings("unchecked") - public T indices(Index[] indices) { - this.indices = indices; - return (T) this; - } -} From 8b31a775d6ad66bd00d2c504c96c1e638762a951 Mon Sep 17 00:00:00 2001 From: Nick Tindall Date: Wed, 25 Sep 2024 17:23:16 +1000 Subject: [PATCH 2/2] Refactor/move backoff policy to common (#113500) --- .../AbstractAsyncBulkByScrollAction.java | 4 +- .../org/elasticsearch/reindex/Reindexer.java | 2 +- .../remote/RemoteScrollableHitSource.java | 2 +- .../reindex/AsyncBulkByScrollActionTests.java | 4 +- .../ClientScrollableHitSourceTests.java | 2 +- .../org/elasticsearch/reindex/RetryTests.java | 2 +- .../RemoteScrollableHitSourceTests.java | 2 +- .../action/bulk/BulkProcessorRetryIT.java | 1 + .../action/bulk/BulkProcessor.java | 3 +- .../action/bulk/BulkRequestHandler.java | 1 + .../org/elasticsearch/action/bulk/Retry.java | 1 + .../bulk => common}/BackoffPolicy.java | 6 +- .../reindex/ClientScrollableHitSource.java | 2 +- .../index/reindex/RetryListener.java | 2 +- .../index/reindex/ScrollableHitSource.java | 2 +- .../tasks/TaskResultsService.java | 2 +- .../action/bulk/BackoffPolicyTests.java | 55 --------- .../action/bulk/BulkProcessorTests.java | 1 + .../elasticsearch/action/bulk/RetryTests.java | 1 + .../common/BackoffPolicyTests.java | 105 ++++++++++++++++++ .../InitialNodeSecurityAutoConfiguration.java | 2 +- .../xpack/security/authc/TokenService.java | 2 +- .../security/profile/ProfileService.java | 2 +- ...InternalEnrollmentTokenGeneratorTests.java | 2 +- 24 files changed, 132 insertions(+), 76 deletions(-) rename server/src/main/java/org/elasticsearch/{action/bulk => common}/BackoffPolicy.java (96%) delete mode 100644 server/src/test/java/org/elasticsearch/action/bulk/BackoffPolicyTests.java create mode 100644 server/src/test/java/org/elasticsearch/common/BackoffPolicyTests.java diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollAction.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollAction.java index 6a5cf8a95bf97..fe591387e9b35 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollAction.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/AbstractAsyncBulkByScrollAction.java @@ -14,7 +14,6 @@ import org.elasticsearch.action.DocWriteRequest; import org.elasticsearch.action.DocWriteResponse; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; -import org.elasticsearch.action.bulk.BackoffPolicy; import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.bulk.BulkItemResponse.Failure; import org.elasticsearch.action.bulk.BulkRequest; @@ -26,6 +25,7 @@ import org.elasticsearch.action.support.TransportAction; import org.elasticsearch.action.support.broadcast.BroadcastResponse; import org.elasticsearch.client.internal.ParentTaskAssigningClient; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; @@ -64,7 +64,7 @@ import static java.lang.Math.min; import static java.util.Collections.emptyList; import static java.util.Collections.unmodifiableList; -import static org.elasticsearch.action.bulk.BackoffPolicy.exponentialBackoff; +import static org.elasticsearch.common.BackoffPolicy.exponentialBackoff; import static org.elasticsearch.core.TimeValue.timeValueNanos; import static org.elasticsearch.index.reindex.AbstractBulkByScrollRequest.MAX_DOCS_ALL_MATCHES; import static org.elasticsearch.rest.RestStatus.CONFLICT; diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/Reindexer.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/Reindexer.java index 371d3488c3099..91ce987ff78c5 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/Reindexer.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/Reindexer.java @@ -21,7 +21,6 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.DocWriteRequest; -import org.elasticsearch.action.bulk.BackoffPolicy; import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.search.SearchRequest; @@ -33,6 +32,7 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.MetadataIndexTemplateService; import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.lucene.uid.Versions; diff --git a/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSource.java b/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSource.java index 29204b5bb0163..5c3db5aaa6cda 100644 --- a/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSource.java +++ b/modules/reindex/src/main/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSource.java @@ -18,12 +18,12 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.Version; -import org.elasticsearch.action.bulk.BackoffPolicy; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.client.Request; import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseListener; import org.elasticsearch.client.RestClient; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.Strings; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/AsyncBulkByScrollActionTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/AsyncBulkByScrollActionTests.java index e047b2cd0675b..83e4695829373 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/AsyncBulkByScrollActionTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/AsyncBulkByScrollActionTests.java @@ -20,7 +20,6 @@ import org.elasticsearch.action.DocWriteResponse; import org.elasticsearch.action.DocWriteResponse.Result; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; -import org.elasticsearch.action.bulk.BackoffPolicy; import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.bulk.BulkItemResponse.Failure; import org.elasticsearch.action.bulk.BulkRequest; @@ -47,6 +46,7 @@ import org.elasticsearch.client.internal.ParentTaskAssigningClient; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.AbstractRunnable; @@ -105,7 +105,7 @@ import static java.util.Collections.singletonList; import static java.util.Collections.synchronizedSet; import static org.apache.lucene.tests.util.TestUtil.randomSimpleString; -import static org.elasticsearch.action.bulk.BackoffPolicy.constantBackoff; +import static org.elasticsearch.common.BackoffPolicy.constantBackoff; import static org.elasticsearch.core.TimeValue.timeValueMillis; import static org.elasticsearch.core.TimeValue.timeValueSeconds; import static org.hamcrest.Matchers.contains; diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/ClientScrollableHitSourceTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/ClientScrollableHitSourceTests.java index ee65b980c5fc8..1c104cbd08197 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/ClientScrollableHitSourceTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/ClientScrollableHitSourceTests.java @@ -14,7 +14,6 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionType; -import org.elasticsearch.action.bulk.BackoffPolicy; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchScrollRequest; @@ -22,6 +21,7 @@ import org.elasticsearch.action.search.TransportSearchScrollAction; import org.elasticsearch.client.internal.ParentTaskAssigningClient; import org.elasticsearch.client.internal.support.AbstractClient; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/RetryTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/RetryTests.java index 9c30ba07069f9..fa0e1d22f4556 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/RetryTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/RetryTests.java @@ -12,11 +12,11 @@ import org.elasticsearch.action.ActionFuture; import org.elasticsearch.action.admin.cluster.node.info.NodeInfo; import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksResponse; -import org.elasticsearch.action.bulk.BackoffPolicy; import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.bulk.Retry; import org.elasticsearch.client.internal.Client; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSourceTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSourceTests.java index 3224a8429792d..c91b2e448bf7d 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSourceTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteScrollableHitSourceTests.java @@ -29,10 +29,10 @@ import org.apache.http.nio.protocol.HttpAsyncResponseConsumer; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.Version; -import org.elasticsearch.action.bulk.BackoffPolicy; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.client.HeapBufferedAsyncResponseConsumer; import org.elasticsearch.client.RestClient; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.io.FileSystemUtils; diff --git a/server/src/internalClusterTest/java/org/elasticsearch/action/bulk/BulkProcessorRetryIT.java b/server/src/internalClusterTest/java/org/elasticsearch/action/bulk/BulkProcessorRetryIT.java index d4ffad33d2314..37904e9f639ac 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/action/bulk/BulkProcessorRetryIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/action/bulk/BulkProcessorRetryIT.java @@ -10,6 +10,7 @@ import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.core.TimeValue; diff --git a/server/src/main/java/org/elasticsearch/action/bulk/BulkProcessor.java b/server/src/main/java/org/elasticsearch/action/bulk/BulkProcessor.java index 967ec1529ae26..8d39644bbf5b2 100644 --- a/server/src/main/java/org/elasticsearch/action/bulk/BulkProcessor.java +++ b/server/src/main/java/org/elasticsearch/action/bulk/BulkProcessor.java @@ -13,6 +13,7 @@ import org.elasticsearch.action.DocWriteRequest; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; @@ -165,7 +166,7 @@ public Builder setGlobalPipeline(String globalPipeline) { * * The default is to back off exponentially. * - * @see org.elasticsearch.action.bulk.BackoffPolicy#exponentialBackoff() + * @see BackoffPolicy#exponentialBackoff() */ public Builder setBackoffPolicy(BackoffPolicy backoffPolicy) { if (backoffPolicy == null) { diff --git a/server/src/main/java/org/elasticsearch/action/bulk/BulkRequestHandler.java b/server/src/main/java/org/elasticsearch/action/bulk/BulkRequestHandler.java index 9da9cac0712d1..c005799ac99c0 100644 --- a/server/src/main/java/org/elasticsearch/action/bulk/BulkRequestHandler.java +++ b/server/src/main/java/org/elasticsearch/action/bulk/BulkRequestHandler.java @@ -11,6 +11,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.threadpool.Scheduler; import java.util.concurrent.CountDownLatch; diff --git a/server/src/main/java/org/elasticsearch/action/bulk/Retry.java b/server/src/main/java/org/elasticsearch/action/bulk/Retry.java index 574799881369e..4ebb5ecae8516 100644 --- a/server/src/main/java/org/elasticsearch/action/bulk/Retry.java +++ b/server/src/main/java/org/elasticsearch/action/bulk/Retry.java @@ -16,6 +16,7 @@ import org.elasticsearch.action.DocWriteRequest; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.support.PlainActionFuture; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.core.Predicates; import org.elasticsearch.core.TimeValue; diff --git a/server/src/main/java/org/elasticsearch/action/bulk/BackoffPolicy.java b/server/src/main/java/org/elasticsearch/common/BackoffPolicy.java similarity index 96% rename from server/src/main/java/org/elasticsearch/action/bulk/BackoffPolicy.java rename to server/src/main/java/org/elasticsearch/common/BackoffPolicy.java index b9f89d4a65a01..27d98f9ade203 100644 --- a/server/src/main/java/org/elasticsearch/action/bulk/BackoffPolicy.java +++ b/server/src/main/java/org/elasticsearch/common/BackoffPolicy.java @@ -6,7 +6,7 @@ * your election, the "Elastic License 2.0", the "GNU Affero General Public * License v3.0 only", or the "Server Side Public License, v 1". */ -package org.elasticsearch.action.bulk; +package org.elasticsearch.common; import org.elasticsearch.core.TimeValue; @@ -15,8 +15,8 @@ import java.util.NoSuchElementException; /** - * Provides a backoff policy for bulk requests. Whenever a bulk request is rejected due to resource constraints (i.e. the client's internal - * thread pool is full), the backoff policy decides how long the bulk processor will wait before the operation is retried internally. + * Provides a set of generic backoff policies. Backoff policies are used to calculate the number of times an action will be retried + * and the intervals between those retries. * * Notes for implementing custom subclasses: * diff --git a/server/src/main/java/org/elasticsearch/index/reindex/ClientScrollableHitSource.java b/server/src/main/java/org/elasticsearch/index/reindex/ClientScrollableHitSource.java index fdacb4563ab87..528f0bd6dae08 100644 --- a/server/src/main/java/org/elasticsearch/index/reindex/ClientScrollableHitSource.java +++ b/server/src/main/java/org/elasticsearch/index/reindex/ClientScrollableHitSource.java @@ -12,7 +12,6 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.action.bulk.BackoffPolicy; import org.elasticsearch.action.search.ClearScrollRequest; import org.elasticsearch.action.search.ClearScrollResponse; import org.elasticsearch.action.search.SearchRequest; @@ -21,6 +20,7 @@ import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.client.internal.Client; import org.elasticsearch.client.internal.ParentTaskAssigningClient; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.document.DocumentField; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; diff --git a/server/src/main/java/org/elasticsearch/index/reindex/RetryListener.java b/server/src/main/java/org/elasticsearch/index/reindex/RetryListener.java index 7b5961c07ae04..e3ecc435b5a36 100644 --- a/server/src/main/java/org/elasticsearch/index/reindex/RetryListener.java +++ b/server/src/main/java/org/elasticsearch/index/reindex/RetryListener.java @@ -12,7 +12,7 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.DelegatingActionListener; -import org.elasticsearch.action.bulk.BackoffPolicy; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.core.TimeValue; import org.elasticsearch.threadpool.ThreadPool; diff --git a/server/src/main/java/org/elasticsearch/index/reindex/ScrollableHitSource.java b/server/src/main/java/org/elasticsearch/index/reindex/ScrollableHitSource.java index cb5b8800916f6..b57f14f749b96 100644 --- a/server/src/main/java/org/elasticsearch/index/reindex/ScrollableHitSource.java +++ b/server/src/main/java/org/elasticsearch/index/reindex/ScrollableHitSource.java @@ -13,9 +13,9 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.action.bulk.BackoffPolicy; import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.search.ShardSearchFailure; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; diff --git a/server/src/main/java/org/elasticsearch/tasks/TaskResultsService.java b/server/src/main/java/org/elasticsearch/tasks/TaskResultsService.java index ebf9f2e27118d..0c9833fad7640 100644 --- a/server/src/main/java/org/elasticsearch/tasks/TaskResultsService.java +++ b/server/src/main/java/org/elasticsearch/tasks/TaskResultsService.java @@ -14,12 +14,12 @@ import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.DocWriteResponse; -import org.elasticsearch.action.bulk.BackoffPolicy; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.client.internal.Client; import org.elasticsearch.client.internal.OriginSettingClient; import org.elasticsearch.client.internal.Requests; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; diff --git a/server/src/test/java/org/elasticsearch/action/bulk/BackoffPolicyTests.java b/server/src/test/java/org/elasticsearch/action/bulk/BackoffPolicyTests.java deleted file mode 100644 index bda9bb337096d..0000000000000 --- a/server/src/test/java/org/elasticsearch/action/bulk/BackoffPolicyTests.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the "Elastic License - * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side - * Public License v 1"; you may not use this file except in compliance with, at - * your election, the "Elastic License 2.0", the "GNU Affero General Public - * License v3.0 only", or the "Server Side Public License, v 1". - */ - -package org.elasticsearch.action.bulk; - -import org.elasticsearch.core.TimeValue; -import org.elasticsearch.test.ESTestCase; - -import java.util.Iterator; -import java.util.NoSuchElementException; -import java.util.concurrent.atomic.AtomicInteger; - -import static org.elasticsearch.core.TimeValue.timeValueMillis; - -public class BackoffPolicyTests extends ESTestCase { - 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 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 itr = policy.iterator(); - itr.next(); - expectedRetries += 1; - assertEquals(expectedRetries, retries.get()); - } - } -} diff --git a/server/src/test/java/org/elasticsearch/action/bulk/BulkProcessorTests.java b/server/src/test/java/org/elasticsearch/action/bulk/BulkProcessorTests.java index 2ab51816043c0..24cc3265e0429 100644 --- a/server/src/test/java/org/elasticsearch/action/bulk/BulkProcessorTests.java +++ b/server/src/test/java/org/elasticsearch/action/bulk/BulkProcessorTests.java @@ -16,6 +16,7 @@ import org.elasticsearch.action.DocWriteResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; diff --git a/server/src/test/java/org/elasticsearch/action/bulk/RetryTests.java b/server/src/test/java/org/elasticsearch/action/bulk/RetryTests.java index 461d8634fb56e..e6181a4ff9cb9 100644 --- a/server/src/test/java/org/elasticsearch/action/bulk/RetryTests.java +++ b/server/src/test/java/org/elasticsearch/action/bulk/RetryTests.java @@ -14,6 +14,7 @@ import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.action.update.UpdateRequest; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.shard.ShardId; diff --git a/server/src/test/java/org/elasticsearch/common/BackoffPolicyTests.java b/server/src/test/java/org/elasticsearch/common/BackoffPolicyTests.java new file mode 100644 index 0000000000000..0cbbcdc0f1674 --- /dev/null +++ b/server/src/test/java/org/elasticsearch/common/BackoffPolicyTests.java @@ -0,0 +1,105 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +package org.elasticsearch.common; + +import org.elasticsearch.core.TimeValue; +import org.elasticsearch.test.ESTestCase; + +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.elasticsearch.core.TimeValue.timeValueMillis; + +public class BackoffPolicyTests extends ESTestCase { + 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 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 itr = policy.iterator(); + itr.next(); + expectedRetries += 1; + assertEquals(expectedRetries, retries.get()); + } + } + + public void testExponentialBackOff() { + long initialDelayMillis = randomLongBetween(0, 100); + int maxNumberOfRetries = randomIntBetween(0, 10); + BackoffPolicy exponentialBackoff = BackoffPolicy.exponentialBackoff(timeValueMillis(initialDelayMillis), maxNumberOfRetries); + int numberOfBackoffsToPerform = randomIntBetween(1, 3); + for (int i = 0; i < numberOfBackoffsToPerform; i++) { + Iterator iterator = exponentialBackoff.iterator(); + TimeValue lastTimeValue = null; + int counter = 0; + while (iterator.hasNext()) { + TimeValue timeValue = iterator.next(); + if (lastTimeValue == null) { + assertEquals(timeValueMillis(initialDelayMillis), timeValue); + } else { + // intervals should be always increasing + assertTrue(timeValue.compareTo(lastTimeValue) > 0); + } + lastTimeValue = timeValue; + counter++; + } + assertEquals(maxNumberOfRetries, counter); + } + } + + public void testNoBackoff() { + BackoffPolicy noBackoff = BackoffPolicy.noBackoff(); + int numberOfBackoffsToPerform = randomIntBetween(1, 3); + for (int i = 0; i < numberOfBackoffsToPerform; i++) { + Iterator iterator = noBackoff.iterator(); + assertFalse(iterator.hasNext()); + } + } + + public void testConstantBackoff() { + long delayMillis = randomLongBetween(0, 100); + int maxNumberOfRetries = randomIntBetween(0, 10); + BackoffPolicy exponentialBackoff = BackoffPolicy.constantBackoff(timeValueMillis(delayMillis), maxNumberOfRetries); + int numberOfBackoffsToPerform = randomIntBetween(1, 3); + for (int i = 0; i < numberOfBackoffsToPerform; i++) { + final Iterator iterator = exponentialBackoff.iterator(); + int counter = 0; + while (iterator.hasNext()) { + TimeValue timeValue = iterator.next(); + assertEquals(timeValueMillis(delayMillis), timeValue); + counter++; + } + assertEquals(maxNumberOfRetries, counter); + } + } +} diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/InitialNodeSecurityAutoConfiguration.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/InitialNodeSecurityAutoConfiguration.java index e84f6f3efeadb..192a5a1b8bb15 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/InitialNodeSecurityAutoConfiguration.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/InitialNodeSecurityAutoConfiguration.java @@ -11,11 +11,11 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.Version; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.action.bulk.BackoffPolicy; import org.elasticsearch.action.support.GroupedActionListener; import org.elasticsearch.bootstrap.BootstrapInfo; import org.elasticsearch.bootstrap.ConsoleLoader; import org.elasticsearch.client.internal.Client; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.Strings; import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.core.TimeValue; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/TokenService.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/TokenService.java index 7c09adc276c3e..4f7ba7808b823 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/TokenService.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/TokenService.java @@ -21,7 +21,6 @@ import org.elasticsearch.action.DocWriteRequest.OpType; import org.elasticsearch.action.DocWriteResponse; import org.elasticsearch.action.DocWriteResponse.Result; -import org.elasticsearch.action.bulk.BackoffPolicy; import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.bulk.BulkResponse; @@ -41,6 +40,7 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateUpdateTask; import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.Priority; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/profile/ProfileService.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/profile/ProfileService.java index dd2377ec773c4..b347ceb833f64 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/profile/ProfileService.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/profile/ProfileService.java @@ -16,7 +16,6 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.DocWriteRequest; import org.elasticsearch.action.DocWriteResponse; -import org.elasticsearch.action.bulk.BackoffPolicy; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.TransportBulkAction; import org.elasticsearch.action.get.GetRequest; @@ -37,6 +36,7 @@ import org.elasticsearch.client.internal.Client; import org.elasticsearch.client.internal.OriginSettingClient; import org.elasticsearch.cluster.service.ClusterService; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.lucene.Lucene; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/enrollment/InternalEnrollmentTokenGeneratorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/enrollment/InternalEnrollmentTokenGeneratorTests.java index 0a1f5f801143d..dd6c41b0a10eb 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/enrollment/InternalEnrollmentTokenGeneratorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/enrollment/InternalEnrollmentTokenGeneratorTests.java @@ -13,11 +13,11 @@ import org.elasticsearch.action.admin.cluster.node.info.NodeInfo; import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse; import org.elasticsearch.action.admin.cluster.node.info.TransportNodesInfoAction; -import org.elasticsearch.action.bulk.BackoffPolicy; import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.client.internal.Client; import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.node.DiscoveryNodeUtils; +import org.elasticsearch.common.BackoffPolicy; import org.elasticsearch.common.settings.MockSecureSettings; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings;