|
33 | 33 | import org.elasticsearch.index.shard.IndexShardState;
|
34 | 34 | import org.elasticsearch.index.shard.IndexShardTestCase;
|
35 | 35 | import org.elasticsearch.index.shard.ShardId;
|
| 36 | +import org.elasticsearch.index.snapshots.IndexShardSnapshotFailedException; |
36 | 37 | import org.elasticsearch.index.store.Store;
|
37 | 38 | import org.elasticsearch.index.store.StoreFileMetaData;
|
38 | 39 | import org.elasticsearch.repositories.IndexId;
|
|
48 | 49 | import java.util.List;
|
49 | 50 |
|
50 | 51 | import static org.elasticsearch.cluster.routing.RecoverySource.StoreRecoverySource.EXISTING_STORE_INSTANCE;
|
| 52 | +import static org.hamcrest.Matchers.containsString; |
51 | 53 |
|
52 | 54 | /**
|
53 | 55 | * This class tests the behavior of {@link BlobStoreRepository} when it
|
@@ -126,6 +128,43 @@ public void testRestoreSnapshotWithExistingFiles() throws IOException {
|
126 | 128 | }
|
127 | 129 | }
|
128 | 130 |
|
| 131 | + public void testSnapshotWithConflictingName() throws IOException { |
| 132 | + final IndexId indexId = new IndexId(randomAlphaOfLength(10), UUIDs.randomBase64UUID()); |
| 133 | + final ShardId shardId = new ShardId(indexId.getName(), indexId.getId(), 0); |
| 134 | + |
| 135 | + IndexShard shard = newShard(shardId, true); |
| 136 | + try { |
| 137 | + // index documents in the shards |
| 138 | + final int numDocs = scaledRandomIntBetween(1, 500); |
| 139 | + recoverShardFromStore(shard); |
| 140 | + for (int i = 0; i < numDocs; i++) { |
| 141 | + indexDoc(shard, "doc", Integer.toString(i)); |
| 142 | + if (rarely()) { |
| 143 | + flushShard(shard, false); |
| 144 | + } |
| 145 | + } |
| 146 | + assertDocCount(shard, numDocs); |
| 147 | + |
| 148 | + // snapshot the shard |
| 149 | + final Repository repository = createRepository(); |
| 150 | + final Snapshot snapshot = new Snapshot(repository.getMetadata().name(), new SnapshotId(randomAlphaOfLength(10), "_uuid")); |
| 151 | + snapshotShard(shard, snapshot, repository); |
| 152 | + final Snapshot snapshotWithSameName = new Snapshot(repository.getMetadata().name(), new SnapshotId( |
| 153 | + snapshot.getSnapshotId().getName(), "_uuid2")); |
| 154 | + IndexShardSnapshotFailedException isfe = expectThrows(IndexShardSnapshotFailedException.class, |
| 155 | + () -> snapshotShard(shard, snapshotWithSameName, repository)); |
| 156 | + assertThat(isfe.getMessage(), containsString("Duplicate snapshot name")); |
| 157 | + } finally { |
| 158 | + if (shard != null && shard.state() != IndexShardState.CLOSED) { |
| 159 | + try { |
| 160 | + shard.close("test", false); |
| 161 | + } finally { |
| 162 | + IOUtils.close(shard.store()); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
129 | 168 | /** Create a {@link Repository} with a random name **/
|
130 | 169 | private Repository createRepository() throws IOException {
|
131 | 170 | Settings settings = Settings.builder().put("location", randomAlphaOfLength(10)).build();
|
|
0 commit comments