-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add circuit breaking logic for GET _mappings to estimate the size of the mappings before executing a request #19857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
cwperks
wants to merge
11
commits into
opensearch-project:main
Choose a base branch
from
cwperks:estimate-get-mappings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
beb3f7f
WIP for estimating size in bytes for GET _mapping API for circuit bre…
cwperks eff861a
Fix tests
cwperks c14a50a
Attempt to calculate size for circuit breaker
cwperks a980a9a
Revert "Attempt to calculate size for circuit breaker"
cwperks 62336f1
Revert "Fix tests"
cwperks 64cbe24
Revert "WIP for estimating size in bytes for GET _mapping API for cir…
cwperks f04c2f1
Move circuit breaker logic inside transport action
cwperks fc36b7d
Add unit tests
cwperks 117c434
Add integration test
cwperks c7443c6
Fix test
cwperks 6b861b2
Add test with concurrent requests
cwperks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
160 changes: 160 additions & 0 deletions
160
...java/org/opensearch/action/admin/indices/mapping/get/GetMappingsBreakerConcurrencyIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
| package org.opensearch.action.admin.indices.mapping.get; | ||
|
|
||
| import org.opensearch.action.admin.indices.create.CreateIndexRequestBuilder; | ||
| import org.opensearch.cluster.metadata.IndexMetadata; | ||
| import org.opensearch.common.settings.Settings; | ||
| import org.opensearch.common.xcontent.XContentFactory; | ||
| import org.opensearch.core.common.breaker.CircuitBreakingException; | ||
| import org.opensearch.core.xcontent.XContentBuilder; | ||
| import org.opensearch.test.OpenSearchIntegTestCase; | ||
| import org.opensearch.transport.client.Client; | ||
|
|
||
| import java.security.SecureRandom; | ||
| import java.util.ArrayList; | ||
| import java.util.Base64; | ||
| import java.util.List; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; | ||
| import static org.hamcrest.Matchers.containsString; | ||
|
|
||
| @OpenSearchIntegTestCase.ClusterScope(numDataNodes = 1, scope = OpenSearchIntegTestCase.Scope.TEST) | ||
| public class GetMappingsBreakerConcurrencyIT extends OpenSearchIntegTestCase { | ||
|
|
||
| // For this suite: allow a single request, but keep the parent breaker tight so many concurrent in-flight requests trip it. | ||
| @Override | ||
| protected Settings nodeSettings(int nodeOrdinal) { | ||
| return Settings.builder() | ||
| .put(super.nodeSettings(nodeOrdinal)) | ||
| // Keep request breaker above a single mapping response size | ||
| .put("indices.breaker.request.limit", "512kb") | ||
| // Tight parent breaker so concurrent requests can exceed it | ||
| .put("indices.breaker.total.limit", "768kb") | ||
| .build(); | ||
| } | ||
|
|
||
| public void testSingleGetMappingsDoesNotTrip() throws Exception { | ||
| final String index = "big-mappings-ok"; | ||
|
|
||
| // ~256KB of low-compressibility pad under _meta to inflate the response materially, | ||
| // but still below the 512kb request breaker. | ||
| final String pad = randomBase64(256 * 1024); | ||
|
|
||
| final XContentBuilder mapping = XContentFactory.jsonBuilder() | ||
| .startObject() | ||
| .startObject("_meta") | ||
| .field("pad", pad) | ||
| .endObject() | ||
| .startObject("properties") | ||
| .startObject("title") | ||
| .field("type", "text") | ||
| .endObject() | ||
| .startObject("year") | ||
| .field("type", "integer") | ||
| .endObject() | ||
| .endObject() | ||
| .endObject(); | ||
|
|
||
| final Settings indexSettings = Settings.builder() | ||
| .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) | ||
| .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) | ||
| .build(); | ||
|
|
||
| final Client client = client(); | ||
|
|
||
| CreateIndexRequestBuilder create = client.admin().indices().prepareCreate(index).setSettings(indexSettings).setMapping(mapping); | ||
| assertAcked(create.get()); | ||
| ensureGreen(index); | ||
|
|
||
| // Should not trip. | ||
| client.admin().indices().getMappings(new GetMappingsRequest().indices(index)).actionGet(); | ||
| } | ||
|
|
||
| public void testManyConcurrentGetMappingsTripParentBreaker() throws Exception { | ||
| final String index = "big-mappings-concurrent"; | ||
|
|
||
| // Each request fits under request breaker, but a burst of them should exceed the 768kb parent limit. | ||
| final String pad = randomBase64(256 * 1024); | ||
|
|
||
| final XContentBuilder mapping = XContentFactory.jsonBuilder() | ||
| .startObject() | ||
| .startObject("_meta") | ||
| .field("pad", pad) | ||
| .endObject() | ||
| .startObject("properties") | ||
| .startObject("title") | ||
| .field("type", "text") | ||
| .endObject() | ||
| .startObject("year") | ||
| .field("type", "integer") | ||
| .endObject() | ||
| .endObject() | ||
| .endObject(); | ||
|
|
||
| final Settings indexSettings = Settings.builder() | ||
| .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) | ||
| .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) | ||
| .build(); | ||
|
|
||
| final Client client = client(); | ||
|
|
||
| CreateIndexRequestBuilder create = client.admin().indices().prepareCreate(index).setSettings(indexSettings).setMapping(mapping); | ||
| assertAcked(create.get()); | ||
| ensureGreen(index); | ||
|
|
||
| final int threads = 8; // small node, but enough to blow past ~768kb with in-flight results | ||
| final CountDownLatch ready = new CountDownLatch(threads); | ||
| final CountDownLatch startGun = new CountDownLatch(1); | ||
| final List<Thread> workers = new ArrayList<>(threads); | ||
| final AtomicReference<CircuitBreakingException> anyCBE = new AtomicReference<>(); | ||
|
|
||
| for (int i = 0; i < threads; i++) { | ||
| final Thread t = new Thread(() -> { | ||
| try { | ||
| ready.countDown(); | ||
| startGun.await(); | ||
| // Fire several requests per thread in quick succession to increase overlap | ||
| for (int j = 0; j < 3; j++) { | ||
| try { | ||
| client.admin().indices().getMappings(new GetMappingsRequest().indices(index)).actionGet(); | ||
| } catch (CircuitBreakingException cbe) { | ||
| anyCBE.compareAndSet(null, cbe); | ||
| // Stop this worker early after a breaker trip to reduce noise | ||
| break; | ||
| } | ||
| } | ||
| } catch (InterruptedException ignored) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| }); | ||
| workers.add(t); | ||
| t.start(); | ||
| } | ||
|
|
||
| // synchronize the start so requests overlap | ||
| ready.await(); | ||
| startGun.countDown(); | ||
|
|
||
| for (Thread t : workers) { | ||
| t.join(); | ||
| } | ||
|
|
||
| final CircuitBreakingException cbe = anyCBE.get(); | ||
| assertNotNull("Expected at least one GET _mappings to trip the parent breaker under concurrent load", cbe); | ||
| assertThat(cbe.getMessage(), containsString("Data too large")); | ||
| } | ||
|
|
||
| private static String randomBase64(int approxBytes) { | ||
| byte[] buf = new byte[approxBytes]; | ||
| new SecureRandom().nextBytes(buf); | ||
| return Base64.getEncoder().encodeToString(buf); | ||
| } | ||
| } |
91 changes: 91 additions & 0 deletions
91
...lusterTest/java/org/opensearch/action/admin/indices/mapping/get/GetMappingsBreakerIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
| package org.opensearch.action.admin.indices.mapping.get; | ||
|
|
||
| import org.opensearch.action.admin.indices.create.CreateIndexRequestBuilder; | ||
| import org.opensearch.cluster.metadata.IndexMetadata; | ||
| import org.opensearch.common.settings.Settings; | ||
| import org.opensearch.common.xcontent.XContentFactory; | ||
| import org.opensearch.core.common.breaker.CircuitBreakingException; | ||
| import org.opensearch.core.xcontent.XContentBuilder; | ||
| import org.opensearch.test.OpenSearchIntegTestCase; | ||
| import org.opensearch.transport.client.Client; | ||
|
|
||
| import java.security.SecureRandom; | ||
| import java.util.Base64; | ||
|
|
||
| import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; | ||
| import static org.hamcrest.Matchers.containsString; | ||
|
|
||
| @OpenSearchIntegTestCase.ClusterScope(numDataNodes = 1, scope = OpenSearchIntegTestCase.Scope.TEST) | ||
| public class GetMappingsBreakerIT extends OpenSearchIntegTestCase { | ||
|
|
||
| // Keep the limit very small so the mapping easily exceeds it. | ||
| @Override | ||
| protected Settings nodeSettings(int nodeOrdinal) { | ||
| return Settings.builder() | ||
| .put(super.nodeSettings(nodeOrdinal)) | ||
| // Request breaker (used by TransportGetMappingsAction) | ||
| .put("indices.breaker.request.limit", "128kb") | ||
| // keep parent breaker high so it doesn't interfere | ||
| .put("indices.breaker.total.limit", "2gb") | ||
| .build(); | ||
| } | ||
|
|
||
| public void testGetMappingsTripsRequestBreaker() throws Exception { | ||
| final String index = "big-mappings"; | ||
|
|
||
| // Build a mapping with low-compressibility padding under _meta. | ||
| // Using ~256KB of random base64. | ||
| final String pad = randomBase64(256 * 1024); | ||
|
|
||
| final XContentBuilder mapping = XContentFactory.jsonBuilder() | ||
| .startObject() | ||
| .startObject("_meta") | ||
| .field("pad", pad) | ||
| .endObject() | ||
| .startObject("properties") | ||
| .startObject("title") | ||
| .field("type", "text") | ||
| .endObject() | ||
| .startObject("year") | ||
| .field("type", "integer") | ||
| .endObject() | ||
| .endObject() | ||
| .endObject(); | ||
|
|
||
| final Settings indexSettings = Settings.builder() | ||
| .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) | ||
| .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) | ||
| .build(); | ||
|
|
||
| final Client client = client(); | ||
|
|
||
| // Create index with the mapping containing the big _meta pad | ||
| CreateIndexRequestBuilder create = client.admin().indices().prepareCreate(index).setSettings(indexSettings).setMapping(mapping); | ||
| assertAcked(create.get()); | ||
|
|
||
| ensureGreen(index); | ||
|
|
||
| // Now call GET _mappings and expect the REQUEST breaker to trip. | ||
| final GetMappingsRequest req = new GetMappingsRequest().indices(index); | ||
|
|
||
| CircuitBreakingException cbe = expectThrows( | ||
| CircuitBreakingException.class, | ||
| () -> client.admin().indices().getMappings(req).actionGet() | ||
| ); | ||
|
|
||
| assertThat(cbe.getMessage(), containsString("Data too large")); | ||
| } | ||
|
|
||
| private static String randomBase64(int approxBytes) { | ||
| byte[] buf = new byte[approxBytes]; | ||
| new SecureRandom().nextBytes(buf); | ||
| return Base64.getEncoder().encodeToString(buf); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we write a test firing multiple GET mappings call to ensure that operation execution is parallel and triggering the CB
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a new IT suite that runs requests concurrently. Each individual request is below the CB limit, but in aggregate it shows that it trips the parent CB.