-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Fix snapshot getting stuck in INIT state #27214
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
imotov
merged 3 commits into
elastic:master
from
imotov:issue-27180-fix-snapshot-stuck-in-init-state
Nov 3, 2017
Merged
Changes from all commits
Commits
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
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
173 changes: 173 additions & 0 deletions
173
core/src/test/java/org/elasticsearch/discovery/SnapshotDisruptionIT.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,173 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.elasticsearch.discovery; | ||
|
|
||
| import org.elasticsearch.action.ActionFuture; | ||
| import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; | ||
| import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse; | ||
| import org.elasticsearch.action.index.IndexRequestBuilder; | ||
| import org.elasticsearch.cluster.ClusterChangedEvent; | ||
| import org.elasticsearch.cluster.ClusterStateListener; | ||
| import org.elasticsearch.cluster.SnapshotsInProgress; | ||
| import org.elasticsearch.cluster.service.ClusterService; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.common.unit.ByteSizeUnit; | ||
| import org.elasticsearch.snapshots.SnapshotInfo; | ||
| import org.elasticsearch.snapshots.SnapshotMissingException; | ||
| import org.elasticsearch.snapshots.SnapshotState; | ||
| import org.elasticsearch.test.ESIntegTestCase; | ||
| import org.elasticsearch.test.disruption.NetworkDisruption; | ||
| import org.elasticsearch.test.junit.annotations.TestLogging; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; | ||
| import static org.hamcrest.Matchers.instanceOf; | ||
|
|
||
| /** | ||
| * Tests snapshot operations during disruptions. | ||
| */ | ||
| @ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, transportClientRatio = 0, autoMinMasterNodes = false) | ||
| @TestLogging("org.elasticsearch.snapshot:TRACE") | ||
| public class SnapshotDisruptionIT extends AbstractDisruptionTestCase { | ||
|
|
||
| public void testDisruptionOnSnapshotInitialization() throws Exception { | ||
| final Settings settings = Settings.builder() | ||
| .put(DEFAULT_SETTINGS) | ||
| .put(DiscoverySettings.COMMIT_TIMEOUT_SETTING.getKey(), "30s") // wait till cluster state is committed | ||
| .build(); | ||
| final String idxName = "test"; | ||
| configureCluster(settings, 4, null, 2); | ||
| final List<String> allMasterEligibleNodes = internalCluster().startMasterOnlyNodes(3); | ||
| final String dataNode = internalCluster().startDataOnlyNode(); | ||
| ensureStableCluster(4); | ||
|
|
||
| createRandomIndex(idxName); | ||
|
|
||
| logger.info("--> creating repository"); | ||
| assertAcked(client().admin().cluster().preparePutRepository("test-repo") | ||
| .setType("fs").setSettings(Settings.builder() | ||
| .put("location", randomRepoPath()) | ||
| .put("compress", randomBoolean()) | ||
| .put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES))); | ||
|
|
||
| // Writing incompatible snapshot can cause this test to fail due to a race condition in repo initialization | ||
| // by the current master and the former master. It is not causing any issues in real life scenario, but | ||
| // might make this test to fail. We are going to complete initialization of the snapshot to prevent this failures. | ||
| logger.info("--> initializing the repository"); | ||
| assertEquals(SnapshotState.SUCCESS, client().admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-1") | ||
| .setWaitForCompletion(true).setIncludeGlobalState(true).setIndices().get().getSnapshotInfo().state()); | ||
|
|
||
| final String masterNode1 = internalCluster().getMasterName(); | ||
| Set<String> otherNodes = new HashSet<>(); | ||
| otherNodes.addAll(allMasterEligibleNodes); | ||
| otherNodes.remove(masterNode1); | ||
| otherNodes.add(dataNode); | ||
|
|
||
| NetworkDisruption networkDisruption = | ||
| new NetworkDisruption(new NetworkDisruption.TwoPartitions(Collections.singleton(masterNode1), otherNodes), | ||
| new NetworkDisruption.NetworkUnresponsive()); | ||
| internalCluster().setDisruptionScheme(networkDisruption); | ||
|
|
||
| ClusterService clusterService = internalCluster().clusterService(masterNode1); | ||
| CountDownLatch disruptionStarted = new CountDownLatch(1); | ||
| clusterService.addListener(new ClusterStateListener() { | ||
| @Override | ||
| public void clusterChanged(ClusterChangedEvent event) { | ||
| SnapshotsInProgress snapshots = event.state().custom(SnapshotsInProgress.TYPE); | ||
| if (snapshots != null && snapshots.entries().size() > 0) { | ||
| if (snapshots.entries().get(0).state() == SnapshotsInProgress.State.INIT) { | ||
| // The snapshot started, we can start disruption so the INIT state will arrive to another master node | ||
| logger.info("--> starting disruption"); | ||
| networkDisruption.startDisrupting(); | ||
| clusterService.removeListener(this); | ||
| disruptionStarted.countDown(); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| logger.info("--> starting snapshot"); | ||
| ActionFuture<CreateSnapshotResponse> future = client(masterNode1).admin().cluster() | ||
| .prepareCreateSnapshot("test-repo", "test-snap-2").setWaitForCompletion(false).setIndices(idxName).execute(); | ||
|
|
||
| logger.info("--> waiting for disruption to start"); | ||
| assertTrue(disruptionStarted.await(1, TimeUnit.MINUTES)); | ||
|
|
||
| logger.info("--> wait until the snapshot is done"); | ||
| assertBusy(() -> { | ||
| SnapshotsInProgress snapshots = dataNodeClient().admin().cluster().prepareState().setLocal(true).get().getState() | ||
| .custom(SnapshotsInProgress.TYPE); | ||
| if (snapshots != null && snapshots.entries().size() > 0) { | ||
| logger.info("Current snapshot state [{}]", snapshots.entries().get(0).state()); | ||
| fail("Snapshot is still running"); | ||
| } else { | ||
| logger.info("Snapshot is no longer in the cluster state"); | ||
| } | ||
| }, 1, TimeUnit.MINUTES); | ||
|
|
||
| logger.info("--> verify that snapshot was successful or no longer exist"); | ||
| assertBusy(() -> { | ||
| try { | ||
| GetSnapshotsResponse snapshotsStatusResponse = dataNodeClient().admin().cluster().prepareGetSnapshots("test-repo") | ||
| .setSnapshots("test-snap-2").get(); | ||
| SnapshotInfo snapshotInfo = snapshotsStatusResponse.getSnapshots().get(0); | ||
| assertEquals(SnapshotState.SUCCESS, snapshotInfo.state()); | ||
| assertEquals(snapshotInfo.totalShards(), snapshotInfo.successfulShards()); | ||
| assertEquals(0, snapshotInfo.failedShards()); | ||
| logger.info("--> done verifying"); | ||
| } catch (SnapshotMissingException exception) { | ||
| logger.info("--> snapshot doesn't exist"); | ||
| } | ||
| }, 1, TimeUnit.MINUTES); | ||
|
|
||
| logger.info("--> stopping disrupting"); | ||
| networkDisruption.stopDisrupting(); | ||
| ensureStableCluster(4, masterNode1); | ||
| logger.info("--> done"); | ||
|
|
||
| try { | ||
| future.get(); | ||
| } catch (Exception ex) { | ||
| logger.info("--> got exception from hanged master", ex); | ||
| Throwable cause = ex.getCause(); | ||
| assertThat(cause, instanceOf(MasterNotDiscoveredException.class)); | ||
| cause = cause.getCause(); | ||
| assertThat(cause, instanceOf(Discovery.FailedToCommitClusterStateException.class)); | ||
| } | ||
| } | ||
|
|
||
| private void createRandomIndex(String idxName) throws ExecutionException, InterruptedException { | ||
| assertAcked(prepareCreate(idxName, 0, Settings.builder().put("number_of_shards", between(1, 20)) | ||
| .put("number_of_replicas", 0))); | ||
| logger.info("--> indexing some data"); | ||
| final int numdocs = randomIntBetween(10, 100); | ||
| IndexRequestBuilder[] builders = new IndexRequestBuilder[numdocs]; | ||
| for (int i = 0; i < builders.length; i++) { | ||
| builders[i] = client().prepareIndex(idxName, "type1", Integer.toString(i)).setSource("field1", "bar " + i); | ||
| } | ||
| indexRandom(true, builders); | ||
| } | ||
| } | ||
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.
I don't understand this comment. What's the issue with repo initialization here? When the disruption triggers, then there is no more repo writing done by the old master node AFAICS?
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.
There is a race condition in writing a list of incompatible snapshot in getRepositoryData method that occurs on empty repositories. This method is called in the START phase on the former master and during clean up on the new master if this file doesn't exist in the repo, which happens in the repo. It shouldn't cause any issues in the real life, but it makes test to fail occasionally due to asserts. We will definitely need to address it at some point of time, but I don't think we should do it as part of this PR.