-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Delete shard store files before restoring a snapshot #27476
Merged
tlrx
merged 2 commits into
elastic:master
from
tlrx:file-already-exist-in-snapshot-recovery
Nov 24, 2017
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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
143 changes: 143 additions & 0 deletions
143
...c/test/java/org/elasticsearch/repositories/blobstore/BlobStoreRepositoryRestoreTests.java
This file contains 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,143 @@ | ||
/* | ||
* 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.repositories.blobstore; | ||
|
||
import org.apache.lucene.store.Directory; | ||
import org.apache.lucene.util.IOUtils; | ||
import org.apache.lucene.util.TestUtil; | ||
import org.elasticsearch.cluster.metadata.RepositoryMetaData; | ||
import org.elasticsearch.cluster.routing.ShardRouting; | ||
import org.elasticsearch.cluster.routing.ShardRoutingHelper; | ||
import org.elasticsearch.common.UUIDs; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.env.Environment; | ||
import org.elasticsearch.env.TestEnvironment; | ||
import org.elasticsearch.index.shard.IndexShard; | ||
import org.elasticsearch.index.shard.IndexShardState; | ||
import org.elasticsearch.index.shard.IndexShardTestCase; | ||
import org.elasticsearch.index.shard.ShardId; | ||
import org.elasticsearch.index.store.Store; | ||
import org.elasticsearch.index.store.StoreFileMetaData; | ||
import org.elasticsearch.repositories.IndexId; | ||
import org.elasticsearch.repositories.Repository; | ||
import org.elasticsearch.repositories.fs.FsRepository; | ||
import org.elasticsearch.snapshots.Snapshot; | ||
import org.elasticsearch.snapshots.SnapshotId; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.elasticsearch.cluster.routing.RecoverySource.StoreRecoverySource.EXISTING_STORE_INSTANCE; | ||
|
||
/** | ||
* This class tests the behavior of {@link BlobStoreRepository} when it | ||
* restores a shard from a snapshot but some files with same names already | ||
* exist on disc. | ||
*/ | ||
public class BlobStoreRepositoryRestoreTests extends IndexShardTestCase { | ||
|
||
/** | ||
* Restoring a snapshot that contains multiple files must succeed even when | ||
* some files already exist in the shard's store. | ||
*/ | ||
public void testRestoreSnapshotWithExistingFiles() throws IOException { | ||
final IndexId indexId = new IndexId(randomAlphaOfLength(10), UUIDs.randomBase64UUID()); | ||
final ShardId shardId = new ShardId(indexId.getName(), indexId.getId(), 0); | ||
|
||
IndexShard shard = newShard(shardId, true); | ||
try { | ||
// index documents in the shards | ||
final int numDocs = scaledRandomIntBetween(1, 500); | ||
recoverShardFromStore(shard); | ||
for (int i = 0; i < numDocs; i++) { | ||
indexDoc(shard, "doc", Integer.toString(i)); | ||
if (rarely()) { | ||
flushShard(shard, false); | ||
} | ||
} | ||
assertDocCount(shard, numDocs); | ||
|
||
// snapshot the shard | ||
final Repository repository = createRepository(); | ||
final Snapshot snapshot = new Snapshot(repository.getMetadata().name(), new SnapshotId(randomAlphaOfLength(10), "_uuid")); | ||
snapshotShard(shard, snapshot, repository); | ||
|
||
// capture current store files | ||
final Store.MetadataSnapshot storeFiles = shard.snapshotStoreMetadata(); | ||
assertFalse(storeFiles.asMap().isEmpty()); | ||
|
||
// close the shard | ||
closeShards(shard); | ||
|
||
// delete some random files in the store | ||
List<String> deletedFiles = randomSubsetOf(randomIntBetween(1, storeFiles.size() - 1), storeFiles.asMap().keySet()); | ||
for (String deletedFile : deletedFiles) { | ||
Files.delete(shard.shardPath().resolveIndex().resolve(deletedFile)); | ||
} | ||
|
||
// build a new shard using the same store directory as the closed shard | ||
ShardRouting shardRouting = ShardRoutingHelper.initWithSameId(shard.routingEntry(), EXISTING_STORE_INSTANCE); | ||
shard = newShard(shardRouting, shard.shardPath(), shard.indexSettings().getIndexMetaData(), null, null, () -> {}); | ||
|
||
// restore the shard | ||
recoverShardFromSnapshot(shard, snapshot, repository); | ||
|
||
// check that the shard is not corrupted | ||
TestUtil.checkIndex(shard.store().directory()); | ||
|
||
// check that all files have been restored | ||
final Directory directory = shard.store().directory(); | ||
final List<String> directoryFiles = Arrays.asList(directory.listAll()); | ||
|
||
for (StoreFileMetaData storeFile : storeFiles) { | ||
String fileName = storeFile.name(); | ||
assertTrue("File [" + fileName + "] does not exist in store directory", directoryFiles.contains(fileName)); | ||
assertEquals(storeFile.length(), shard.store().directory().fileLength(fileName)); | ||
} | ||
} finally { | ||
if (shard != null && shard.state() != IndexShardState.CLOSED) { | ||
try { | ||
shard.close("test", false); | ||
} finally { | ||
IOUtils.close(shard.store()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** Create a {@link Repository} with a random name **/ | ||
private Repository createRepository() throws IOException { | ||
Settings settings = Settings.builder().put("location", randomAlphaOfLength(10)).build(); | ||
RepositoryMetaData repositoryMetaData = new RepositoryMetaData(randomAlphaOfLength(10), FsRepository.TYPE, settings); | ||
return new FsRepository(repositoryMetaData, createEnvironment(), xContentRegistry()); | ||
} | ||
|
||
/** Create a {@link Environment} with random path.home and path.repo **/ | ||
private Environment createEnvironment() { | ||
Path home = createTempDir(); | ||
return TestEnvironment.newEnvironment(Settings.builder() | ||
.put(Environment.PATH_HOME_SETTING.getKey(), home.toAbsolutePath()) | ||
.put(Environment.PATH_REPO_SETTING.getKey(), home.resolve("repo").toAbsolutePath()) | ||
.build()); | ||
} | ||
} |
This file contains 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
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 see that you fixed the check here (before it read
recoveryTargetMetadata == null
which did not make any sense as that one was always non-null). I wonder what impact this bug fix has. What change in behavior do we expect by this (it's a bit unclear to me what this check does)?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.
Sadly there's no test for this and as you noticed the previous check did not make any sense. I saw it as a bug and I think that we can't restore a snapshot without segments file so I don't expect any impact for this. Maybe @imotov has more knowledge?
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 cannot really think of a scenario where snapshot would have no segments. So, removing check for recoveryTargetMetadata and replacing it with check for no snapshots shouldn't have any change in behavior expect in some pathological cases that would fail anyway. Now they will at least fail with a reasonable error message.