Skip to content

Commit adb7aad

Browse files
author
Ali Beyad
committed
[TEST] adds randomness between atomic and non-atomic move
operations in MockRepository
1 parent 2d3a52c commit adb7aad

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2675,8 +2675,12 @@ public void testSnapshotCanceledOnRemovedShard() throws Exception {
26752675
public void testSnapshotSucceedsAfterSnapshotFailure() throws Exception {
26762676
logger.info("--> creating repository");
26772677
final Path repoPath = randomRepoPath();
2678-
assertAcked(client().admin().cluster().preparePutRepository("test-repo").setType("mock").setVerify(false).setSettings(
2679-
Settings.builder().put("location", repoPath).put("random_control_io_exception_rate", randomIntBetween(5, 20) / 100f)));
2678+
final Client client = client();
2679+
assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("mock").setVerify(false).setSettings(
2680+
Settings.builder()
2681+
.put("location", repoPath)
2682+
.put("random_control_io_exception_rate", randomIntBetween(5, 20) / 100f)
2683+
.put("random", randomAsciiOfLength(10))));
26802684

26812685
logger.info("--> indexing some data");
26822686
createIndex("test-idx");
@@ -2686,12 +2690,12 @@ public void testSnapshotSucceedsAfterSnapshotFailure() throws Exception {
26862690
index("test-idx", "doc", Integer.toString(i), "foo", "bar" + i);
26872691
}
26882692
refresh();
2689-
assertThat(client().prepareSearch("test-idx").setSize(0).get().getHits().totalHits(), equalTo((long) numDocs));
2693+
assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().totalHits(), equalTo((long) numDocs));
26902694

26912695
logger.info("--> snapshot with potential I/O failures");
26922696
try {
26932697
CreateSnapshotResponse createSnapshotResponse =
2694-
client().admin().cluster().prepareCreateSnapshot("test-repo", "test-snap")
2698+
client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap")
26952699
.setWaitForCompletion(true)
26962700
.setIndices("test-idx")
26972701
.get();
@@ -2702,21 +2706,21 @@ public void testSnapshotSucceedsAfterSnapshotFailure() throws Exception {
27022706
assertThat(shardFailure.reason(), containsString("Random IOException"));
27032707
}
27042708
}
2705-
} catch (Exception ex) {
2709+
} catch (SnapshotCreationException | RepositoryException ex) {
27062710
// sometimes, the snapshot will fail with a top level I/O exception
27072711
assertThat(ExceptionsHelper.stackTrace(ex), containsString("Random IOException"));
27082712
}
27092713

27102714
logger.info("--> snapshot with no I/O failures");
2711-
assertAcked(client().admin().cluster().preparePutRepository("test-repo-2").setType("mock").setVerify(false).setSettings(
2715+
assertAcked(client.admin().cluster().preparePutRepository("test-repo-2").setType("mock").setSettings(
27122716
Settings.builder().put("location", repoPath)));
27132717
CreateSnapshotResponse createSnapshotResponse =
2714-
client().admin().cluster().prepareCreateSnapshot("test-repo-2", "test-snap-2")
2718+
client.admin().cluster().prepareCreateSnapshot("test-repo-2", "test-snap-2")
27152719
.setWaitForCompletion(true)
27162720
.setIndices("test-idx")
27172721
.get();
27182722
assertEquals(0, createSnapshotResponse.getSnapshotInfo().failedShards());
2719-
GetSnapshotsResponse getSnapshotsResponse = client().admin().cluster().prepareGetSnapshots("test-repo-2")
2723+
GetSnapshotsResponse getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots("test-repo-2")
27202724
.addSnapshots("test-snap-2").get();
27212725
assertEquals(SnapshotState.SUCCESS, getSnapshotsResponse.getSnapshots().get(0).state());
27222726
}

core/src/test/java/org/elasticsearch/snapshots/mockstore/MockRepository.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.concurrent.ConcurrentMap;
3434
import java.util.concurrent.atomic.AtomicLong;
3535

36+
import com.carrotsearch.randomizedtesting.RandomizedContext;
3637
import org.apache.lucene.index.CorruptIndexException;
3738
import org.elasticsearch.ElasticsearchException;
3839
import org.elasticsearch.cluster.metadata.MetaData;
@@ -321,20 +322,28 @@ public Map<String, BlobMetaData> listBlobsByPrefix(String blobNamePrefix) throws
321322

322323
@Override
323324
public void move(String sourceBlob, String targetBlob) throws IOException {
324-
// simulate a non-atomic move, since many blob container implementations
325-
// will not have an atomic move, and we should be able to handle that
326-
maybeIOExceptionOrBlock(targetBlob);
327-
super.writeBlob(targetBlob, super.readBlob(sourceBlob), 0L);
328-
super.deleteBlob(sourceBlob);
325+
if (RandomizedContext.current().getRandom().nextBoolean()) {
326+
// simulate a non-atomic move, since many blob container implementations
327+
// will not have an atomic move, and we should be able to handle that
328+
maybeIOExceptionOrBlock(targetBlob);
329+
super.writeBlob(targetBlob, super.readBlob(sourceBlob), 0L);
330+
super.deleteBlob(sourceBlob);
331+
} else {
332+
// atomic move since this inherits from FsBlobContainer which provides atomic moves
333+
maybeIOExceptionOrBlock(targetBlob);
334+
super.move(sourceBlob, targetBlob);
335+
}
329336
}
330337

331338
@Override
332339
public void writeBlob(String blobName, InputStream inputStream, long blobSize) throws IOException {
333340
maybeIOExceptionOrBlock(blobName);
334341
super.writeBlob(blobName, inputStream, blobSize);
335-
// for network based repositories, the blob may have been written but we may still
336-
// get an error with the client connection, so an IOException here simulates this
337-
maybeIOExceptionOrBlock(blobName);
342+
if (RandomizedContext.current().getRandom().nextBoolean()) {
343+
// for network based repositories, the blob may have been written but we may still
344+
// get an error with the client connection, so an IOException here simulates this
345+
maybeIOExceptionOrBlock(blobName);
346+
}
338347
}
339348
}
340349
}

0 commit comments

Comments
 (0)