-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Avoid stack overflow in IndicesClusterStateService applyClusterState #132536
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
Merged
+218
−3
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
054f5ee
sometimes fork the thread
albertzaharovits 73e3304
Update docs/changelog/132536.yaml
albertzaharovits fa49097
Merge branch 'main' into fix-3855
albertzaharovits 3a00599
Avoid chaining when no shard has been closed
albertzaharovits e18cbd5
Merge branch 'main' into fix-3855
albertzaharovits e07e36d
ooops
albertzaharovits c6f1d0b
Test skeleton
albertzaharovits 447e140
Test WIP
albertzaharovits 40bfdf7
WIP no threadpool
albertzaharovits 027380e
Merge branch 'main' into fix-3855
albertzaharovits 13eecf9
test done
albertzaharovits b6d6742
nit
albertzaharovits fa44494
[CI] Auto commit changes from spotless
2d5d9ca
Merge branch 'main' into fix-3855
albertzaharovits 5f8bd19
Merge branch 'main' into fix-3855
elasticmachine 8ae0256
Merge branch 'main' into fix-3855
elasticmachine 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
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,5 @@ | ||
| pr: 132536 | ||
| summary: Avoid stack overflow in `IndicesClusterStateService` `applyClusterState` | ||
| area: Cluster Coordination | ||
| type: bug | ||
| issues: [] |
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
175 changes: 175 additions & 0 deletions
175
...g/elasticsearch/indices/cluster/IndicesClusterStateServiceShardsClosedListenersTests.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,175 @@ | ||
| /* | ||
| * 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.indices.cluster; | ||
|
|
||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.action.support.SubscribableListener; | ||
| import org.elasticsearch.client.internal.node.NodeClient; | ||
| import org.elasticsearch.cluster.ClusterChangedEvent; | ||
| import org.elasticsearch.cluster.action.shard.ShardStateAction; | ||
| import org.elasticsearch.cluster.service.ClusterService; | ||
| import org.elasticsearch.common.settings.ClusterSettings; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.common.util.concurrent.DeterministicTaskQueue; | ||
| import org.elasticsearch.index.seqno.RetentionLeaseSyncer; | ||
| import org.elasticsearch.index.shard.PrimaryReplicaSyncer; | ||
| import org.elasticsearch.indices.IndicesService; | ||
| import org.elasticsearch.indices.recovery.PeerRecoverySourceService; | ||
| import org.elasticsearch.indices.recovery.PeerRecoveryTargetService; | ||
| import org.elasticsearch.repositories.RepositoriesService; | ||
| import org.elasticsearch.search.SearchService; | ||
| import org.elasticsearch.snapshots.SnapshotShardsService; | ||
| import org.elasticsearch.test.transport.MockTransport; | ||
| import org.elasticsearch.test.transport.MockTransportService; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.function.BiConsumer; | ||
|
|
||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| public class IndicesClusterStateServiceShardsClosedListenersTests extends AbstractIndicesClusterStateServiceTestCase { | ||
|
|
||
| public void testRunnablesAreExecutedOnlyAfterAllPreviousListenersComplete() { | ||
| AtomicInteger clusterStateAppliedRound = new AtomicInteger(); | ||
| int totalClusterStateAppliedRounds = randomIntBetween(10, 100); | ||
| Map<Integer, List<Runnable>> runnablesOnShardsClosedForRoundMap = new ConcurrentHashMap<>(); | ||
| Map<Integer, List<ActionListener<Void>>> shardsClosedListenersForRoundMap = new ConcurrentHashMap<>(); | ||
| List<ActionListener<Void>> allShardsClosedListeners = Collections.synchronizedList(new ArrayList<>()); | ||
| DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue(); | ||
| try ( | ||
| TestIndicesClusterStateService testIndicesClusterStateService = new TestIndicesClusterStateService( | ||
| deterministicTaskQueue.getThreadPool(), | ||
| // the apply cluster state hook | ||
| (indicesClusterStateService, clusterChangedEvent) -> { | ||
| final int round = clusterStateAppliedRound.get(); | ||
| // maybe register runnable for when all the shards in the currently applied cluster states are closed | ||
| if (randomBoolean()) { | ||
| Runnable mockRunnable = mock(Runnable.class); | ||
| indicesClusterStateService.onClusterStateShardsClosed(mockRunnable); | ||
| runnablesOnShardsClosedForRoundMap.get(round).add(mockRunnable); | ||
| } | ||
| // maybe get some listeners as if asynchronously closing some shards | ||
| int listenersCount = randomIntBetween(0, 2); | ||
| for (int i = 0; i < listenersCount; i++) { | ||
| var shardsClosedListener = new SubscribableListener<Void>(); | ||
| shardsClosedListener.addListener(indicesClusterStateService.getShardsClosedListener()); | ||
| shardsClosedListenersForRoundMap.get(round).add(shardsClosedListener); | ||
| allShardsClosedListeners.add(shardsClosedListener); | ||
| shardsClosedListener.andThen(l -> { | ||
| // the listeners auto-removes itself form the map, for testing purposes | ||
| shardsClosedListenersForRoundMap.get(round).remove(shardsClosedListener); | ||
| allShardsClosedListeners.remove(shardsClosedListener); | ||
| }); | ||
| } | ||
| // maybe register runnable for when all the shards in the currently applied cluster states are closed | ||
| if (randomBoolean()) { | ||
| Runnable mockRunnable = mock(Runnable.class); | ||
| indicesClusterStateService.onClusterStateShardsClosed(mockRunnable); | ||
| runnablesOnShardsClosedForRoundMap.get(round).add(mockRunnable); | ||
| } | ||
| } | ||
| ) | ||
| ) { | ||
| int round = clusterStateAppliedRound.get(); | ||
| int runnablesDoneUpToRound = 0; | ||
| while (round < totalClusterStateAppliedRounds || allShardsClosedListeners.isEmpty() == false) { | ||
| if (round < totalClusterStateAppliedRounds) { | ||
| runnablesOnShardsClosedForRoundMap.put(round, Collections.synchronizedList(new ArrayList<>())); | ||
| shardsClosedListenersForRoundMap.put(round, Collections.synchronizedList(new ArrayList<>())); | ||
|
|
||
| // apply cluster state this round | ||
| testIndicesClusterStateService.applyClusterState(mock(ClusterChangedEvent.class)); | ||
|
|
||
| // maybe register runnable for when all the shards in the previously applied cluster states are closed | ||
| runnablesOnShardsClosedForRoundMap.get(round).addAll(randomList(0, 2, () -> { | ||
| Runnable mockRunnable = mock(Runnable.class); | ||
| testIndicesClusterStateService.onClusterStateShardsClosed(mockRunnable); | ||
| return mockRunnable; | ||
| })); | ||
| } | ||
|
|
||
| // complete one random listener | ||
| if ((round >= totalClusterStateAppliedRounds || randomBoolean()) && allShardsClosedListeners.isEmpty() == false) { | ||
| randomFrom(allShardsClosedListeners).onResponse(null); | ||
| deterministicTaskQueue.runAllTasksInTimeOrder(); | ||
| } | ||
|
|
||
| // find the "oldest" applied cluster state that still has unfinished listeners | ||
| for (int i = runnablesDoneUpToRound; i < totalClusterStateAppliedRounds; i++) { | ||
| if (shardsClosedListenersForRoundMap.get(i) != null && shardsClosedListenersForRoundMap.get(i).isEmpty()) { | ||
| runnablesDoneUpToRound = i + 1; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| // assert older runnables executed | ||
| for (int i = 0; i < runnablesDoneUpToRound; i++) { | ||
| for (var runnable : runnablesOnShardsClosedForRoundMap.get(i)) { | ||
| verify(runnable, times(1)).run(); | ||
| } | ||
| } | ||
| // assert any newer runnables not yet executed | ||
| for (int i = runnablesDoneUpToRound; i < totalClusterStateAppliedRounds; i++) { | ||
| if (runnablesOnShardsClosedForRoundMap.get(i) != null) { | ||
| for (var runable : runnablesOnShardsClosedForRoundMap.get(i)) { | ||
| verify(runable, times(0)).run(); | ||
| } | ||
| } | ||
| } | ||
| round = clusterStateAppliedRound.incrementAndGet(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class TestIndicesClusterStateService extends IndicesClusterStateService { | ||
| BiConsumer<IndicesClusterStateService, ClusterChangedEvent> doApplyClusterStateHook; | ||
|
|
||
| TestIndicesClusterStateService( | ||
| ThreadPool threadPool, | ||
| BiConsumer<IndicesClusterStateService, ClusterChangedEvent> doApplyClusterStateHook | ||
| ) { | ||
| super( | ||
| Settings.EMPTY, | ||
| new MockIndicesService(), | ||
| new ClusterService(Settings.EMPTY, ClusterSettings.createBuiltInClusterSettings(), threadPool, null), | ||
| threadPool, | ||
| mock(PeerRecoveryTargetService.class), | ||
| mock(ShardStateAction.class), | ||
| mock(RepositoriesService.class), | ||
| mock(SearchService.class), | ||
| mock(PeerRecoverySourceService.class), | ||
| new SnapshotShardsService( | ||
| Settings.EMPTY, | ||
| new ClusterService(Settings.EMPTY, ClusterSettings.createBuiltInClusterSettings(), threadPool, null), | ||
| mock(RepositoriesService.class), | ||
| MockTransportService.createMockTransportService(new MockTransport(), threadPool), | ||
| mock(IndicesService.class) | ||
| ), | ||
| mock(PrimaryReplicaSyncer.class), | ||
| RetentionLeaseSyncer.EMPTY, | ||
| mock(NodeClient.class) | ||
| ); | ||
| this.doApplyClusterStateHook = doApplyClusterStateHook; | ||
| } | ||
|
|
||
| @Override | ||
| protected void doApplyClusterState(final ClusterChangedEvent event) { | ||
| doApplyClusterStateHook.accept(this, event); | ||
| } | ||
| } | ||
| } |
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.
Hmm are you sure we should move all this listener stuff below
doApplyClusterState()?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.
I can't think of any impact to execution.
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.
But I've put it back at the original place.