Skip to content

Commit 02741fb

Browse files
committed
Fixing elastic#69799 and cleaning up test
1 parent e2ca6d3 commit 02741fb

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/v2/RollupActionSingleNodeTests.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public class RollupActionSingleNodeTests extends ESSingleNodeTestCase {
9494
private static final int MAX_DIM_VALUES = 5;
9595
private static final long MAX_NUM_BUCKETS = 10;
9696

97-
private String sourceIndex, sourceIndexClone, rollupIndex;
97+
private String sourceIndex, rollupIndex;
9898
private long startTime;
9999
private int docCount, numOfShards, numOfReplicas;
100100
private List<String> dimensionValues;
@@ -112,13 +112,12 @@ protected Collection<Class<? extends Plugin>> getPlugins() {
112112

113113
@Before
114114
public void setup() {
115-
sourceIndex = randomAlphaOfLength(5).toLowerCase(Locale.ROOT);
116-
sourceIndexClone = sourceIndex + "-clone";
117-
rollupIndex = randomAlphaOfLength(6).toLowerCase(Locale.ROOT);
115+
sourceIndex = randomAlphaOfLength(12).toLowerCase(Locale.ROOT);
116+
rollupIndex = "rollup-" + sourceIndex + "-" + randomAlphaOfLength(4).toLowerCase(Locale.ROOT);
118117
startTime = randomLongBetween(946769284000L, 1607470084000L); // random date between 2000-2020
119118
docCount = randomIntBetween(10, 9000);
120119
numOfShards = randomIntBetween(1, 4);
121-
numOfReplicas = randomIntBetween(0, 3);
120+
numOfReplicas = 0; // Since this is a single node, we cannot have replicas
122121

123122
// Values for keyword dimensions
124123
dimensionValues = new ArrayList<>(MAX_DIM_VALUES);
@@ -169,6 +168,8 @@ public void testRollupIndex() throws IOException {
169168
};
170169
bulkIndex(sourceSupplier);
171170
prepareSourceIndex(sourceIndex);
171+
// Clone the source index before rollup deletes it
172+
String sourceIndexClone = cloneSourceIndex(sourceIndex);
172173
rollup(sourceIndex, rollupIndex, config);
173174
assertRollupIndex(config, sourceIndex, sourceIndexClone, rollupIndex);
174175
}
@@ -217,6 +218,8 @@ public void testRollupSparseMetrics() throws IOException {
217218
};
218219
bulkIndex(sourceSupplier);
219220
prepareSourceIndex(sourceIndex);
221+
// Clone the source index before rollup deletes it
222+
String sourceIndexClone = cloneSourceIndex(sourceIndex);
220223
rollup(sourceIndex, rollupIndex, config);
221224
assertRollupIndex(config, sourceIndex, sourceIndexClone, rollupIndex);
222225
}
@@ -238,14 +241,15 @@ public void testRollupEmptyIndex() {
238241
RollupActionConfig config = new RollupActionConfig(randomInterval());
239242
// Source index has been created in the setup() method
240243
prepareSourceIndex(sourceIndex);
244+
// Clone the source index before rollup deletes it
245+
String sourceIndexClone = cloneSourceIndex(sourceIndex);
241246
rollup(sourceIndex, rollupIndex, config);
242247
assertRollupIndex(config, sourceIndex, sourceIndexClone, rollupIndex);
243248
}
244249

245250
public void testCannotRollupIndexWithNoMetrics() {
246251
// Create a source index that contains no metric fields in its mapping
247-
sourceIndex = randomAlphaOfLength(5).toLowerCase(Locale.ROOT);
248-
sourceIndexClone = sourceIndex + "-clone";
252+
String sourceIndex = "no-metrics-idx-" + randomAlphaOfLength(5).toLowerCase(Locale.ROOT);
249253
client().admin()
250254
.indices()
251255
.prepareCreate(sourceIndex)
@@ -270,7 +274,6 @@ public void testCannotRollupIndexWithNoMetrics() {
270274
.get();
271275

272276
RollupActionConfig config = new RollupActionConfig(randomInterval());
273-
// Source index has been created in the setup() method
274277
prepareSourceIndex(sourceIndex);
275278
Exception exception = expectThrows(RollupActionRequestValidationException.class, () -> rollup(sourceIndex, rollupIndex, config));
276279
assertThat(exception.getMessage(), containsString("does not contain any metric fields"));
@@ -325,10 +328,11 @@ public void testRollupDatastream() throws Exception {
325328
};
326329
bulkIndex(dataStreamName, sourceSupplier);
327330

328-
this.sourceIndex = rollover(dataStreamName).getOldIndex();
329-
this.sourceIndexClone = sourceIndex + "-clone";
330-
this.rollupIndex = ".rollup-" + sourceIndex;
331+
String sourceIndex = rollover(dataStreamName).getOldIndex();
331332
prepareSourceIndex(sourceIndex);
333+
// Clone the source index before rollup deletes it
334+
String sourceIndexClone = cloneSourceIndex(sourceIndex);
335+
String rollupIndex = "rollup-" + sourceIndex + "-" + randomAlphaOfLength(4).toLowerCase(Locale.ROOT);
332336
rollup(sourceIndex, rollupIndex, config);
333337
assertRollupIndex(config, sourceIndex, sourceIndexClone, rollupIndex);
334338

@@ -354,7 +358,15 @@ private String randomDateForRange(long start, long end) {
354358
return DATE_FORMATTER.formatMillis(randomLongBetween(start, end));
355359
}
356360

357-
private void cloneSourceIndex(String sourceIndex, String sourceIndexClone) {
361+
/**
362+
* The source index is deleted at the end of the rollup process.
363+
* We clone the source index, so that we validate rollup results against the
364+
* source index clone.
365+
*
366+
* @return the name of the source index clone
367+
*/
368+
private String cloneSourceIndex(String sourceIndex) {
369+
String sourceIndexClone = "clone-" + sourceIndex;
358370
ResizeResponse r = client().admin()
359371
.indices()
360372
.prepareResizeIndex(sourceIndex, sourceIndexClone)
@@ -364,6 +376,7 @@ private void cloneSourceIndex(String sourceIndex, String sourceIndexClone) {
364376
)
365377
.get();
366378
assertTrue(r.isAcknowledged());
379+
return sourceIndexClone;
367380
}
368381

369382
private void bulkIndex(SourceSupplier sourceSupplier) throws IOException {
@@ -386,15 +399,15 @@ private void bulkIndex(String indexName, SourceSupplier sourceSupplier) throws I
386399
if (response.getFailure().getCause() instanceof VersionConflictEngineException) {
387400
// A duplicate event was created by random generator. We should not fail for this
388401
// reason.
389-
logger.info("We tried to insert a duplicate: " + response.getFailureMessage());
402+
logger.debug("We tried to insert a duplicate: [{}]", response.getFailureMessage());
390403
duplicates++;
391404
} else {
392405
fail("Failed to index data: " + bulkResponse.buildFailureMessage());
393406
}
394407
}
395408
}
396409
int docsIndexed = docCount - duplicates;
397-
logger.info("Indexed [" + docsIndexed + "] documents");
410+
logger.info("Indexed [{}] documents. Dropped [{}] duplicates.", docsIndexed, duplicates);
398411
assertHitCount(client().prepareSearch(indexName).setSize(0).get(), docsIndexed);
399412
}
400413

@@ -406,11 +419,6 @@ private void prepareSourceIndex(String sourceIndex) {
406419
.setSettings(Settings.builder().put(IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.getKey(), true).build())
407420
.get();
408421
assertTrue(r.isAcknowledged());
409-
410-
// The source index is deleted at the end of the rollup process.
411-
// We clone the source index, so that we validate rollup results against the
412-
// source index clone.
413-
cloneSourceIndex(sourceIndex, sourceIndexClone);
414422
}
415423

416424
private void rollup(String sourceIndex, String rollupIndex, RollupActionConfig config) {

0 commit comments

Comments
 (0)