Skip to content

Commit cf9a015

Browse files
authored
Adapt testIndexCanChangeCustomDataPath for replicated closed indices (#38327)
Relates to #33888 and #38024
1 parent b9becdd commit cf9a015

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

server/src/test/java/org/elasticsearch/index/shard/IndexShardIT.java

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
import java.io.UncheckedIOException;
8888
import java.nio.file.Files;
8989
import java.nio.file.Path;
90-
import java.nio.file.StandardCopyOption;
9190
import java.util.ArrayList;
9291
import java.util.Arrays;
9392
import java.util.Collection;
@@ -103,15 +102,16 @@
103102
import java.util.concurrent.atomic.AtomicLong;
104103
import java.util.concurrent.atomic.AtomicReference;
105104
import java.util.function.Predicate;
105+
import java.util.stream.Stream;
106106

107+
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomAsciiLettersOfLength;
107108
import static java.util.Collections.emptyMap;
108109
import static java.util.Collections.emptySet;
109110
import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE;
110111
import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.NONE;
111112
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
112113
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
113114
import static org.elasticsearch.cluster.routing.TestShardRouting.newShardRouting;
114-
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
115115
import static org.elasticsearch.index.seqno.SequenceNumbers.NO_OPS_PERFORMED;
116116
import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO;
117117
import static org.elasticsearch.index.shard.IndexShardTestCase.getTranslog;
@@ -277,76 +277,64 @@ public void testExpectedShardSizeIsPresent() throws InterruptedException {
277277
assertTrue(test > 0);
278278
}
279279

280-
// NORELEASE This test need to be adapted for replicated closed indices
281-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/33888")
282280
public void testIndexCanChangeCustomDataPath() throws Exception {
283-
Environment env = getInstanceFromNode(Environment.class);
284-
Path idxPath = env.sharedDataFile().resolve(randomAlphaOfLength(10));
285-
final String INDEX = "idx";
286-
Path startDir = idxPath.resolve("start-" + randomAlphaOfLength(10));
287-
Path endDir = idxPath.resolve("end-" + randomAlphaOfLength(10));
288-
logger.info("--> start dir: [{}]", startDir.toAbsolutePath().toString());
289-
logger.info("--> end dir: [{}]", endDir.toAbsolutePath().toString());
290-
// temp dirs are automatically created, but the end dir is what
291-
// startDir is going to be renamed as, so it needs to be deleted
292-
// otherwise we get all sorts of errors about the directory
293-
// already existing
294-
IOUtils.rm(endDir);
295-
296-
Settings sb = Settings.builder()
297-
.put(IndexMetaData.SETTING_DATA_PATH, startDir.toAbsolutePath().toString())
298-
.build();
299-
Settings sb2 = Settings.builder()
300-
.put(IndexMetaData.SETTING_DATA_PATH, endDir.toAbsolutePath().toString())
301-
.build();
281+
final String index = "test-custom-data-path";
282+
final Path sharedDataPath = getInstanceFromNode(Environment.class).sharedDataFile().resolve(randomAsciiLettersOfLength(10));
283+
final Path indexDataPath = sharedDataPath.resolve("start-" + randomAsciiLettersOfLength(10));
302284

303-
logger.info("--> creating an index with data_path [{}]", startDir.toAbsolutePath().toString());
304-
createIndex(INDEX, sb);
305-
ensureGreen(INDEX);
306-
client().prepareIndex(INDEX, "bar", "1").setSource("{}", XContentType.JSON).setRefreshPolicy(IMMEDIATE).get();
285+
logger.info("--> creating index [{}] with data_path [{}]", index, indexDataPath);
286+
createIndex(index, Settings.builder().put(IndexMetaData.SETTING_DATA_PATH, indexDataPath.toAbsolutePath().toString()).build());
287+
client().prepareIndex(index, "bar", "1").setSource("foo", "bar").setRefreshPolicy(IMMEDIATE).get();
288+
ensureGreen(index);
307289

308-
SearchResponse resp = client().prepareSearch(INDEX).setQuery(matchAllQuery()).get();
309-
assertThat("found the hit", resp.getHits().getTotalHits().value, equalTo(1L));
290+
assertHitCount(client().prepareSearch(index).setSize(0).get(), 1L);
310291

311-
logger.info("--> closing the index [{}]", INDEX);
312-
client().admin().indices().prepareClose(INDEX).get();
292+
logger.info("--> closing the index [{}]", index);
293+
assertAcked(client().admin().indices().prepareClose(index));
313294
logger.info("--> index closed, re-opening...");
314-
client().admin().indices().prepareOpen(INDEX).get();
295+
assertAcked(client().admin().indices().prepareOpen(index));
315296
logger.info("--> index re-opened");
316-
ensureGreen(INDEX);
297+
ensureGreen(index);
317298

318-
resp = client().prepareSearch(INDEX).setQuery(matchAllQuery()).get();
319-
assertThat("found the hit", resp.getHits().getTotalHits().value, equalTo(1L));
299+
assertHitCount(client().prepareSearch(index).setSize(0).get(), 1L);
320300

321301
// Now, try closing and changing the settings
302+
logger.info("--> closing the index [{}] before updating data_path", index);
303+
assertAcked(client().admin().indices().prepareClose(index));
322304

323-
logger.info("--> closing the index [{}]", INDEX);
324-
client().admin().indices().prepareClose(INDEX).get();
325-
326-
logger.info("--> moving data on disk [{}] to [{}]", startDir.getFileName(), endDir.getFileName());
327-
assert Files.exists(endDir) == false : "end directory should not exist!";
328-
Files.move(startDir, endDir, StandardCopyOption.REPLACE_EXISTING);
305+
final Path newIndexDataPath = sharedDataPath.resolve("end-" + randomAlphaOfLength(10));
306+
IOUtils.rm(newIndexDataPath);
329307

330-
logger.info("--> updating settings...");
331-
client().admin().indices().prepareUpdateSettings(INDEX)
332-
.setSettings(sb2)
333-
.setIndicesOptions(IndicesOptions.fromOptions(true, false, true, true))
334-
.get();
308+
logger.info("--> copying data on disk from [{}] to [{}]", indexDataPath, newIndexDataPath);
309+
assert Files.exists(newIndexDataPath) == false : "new index data path directory should not exist!";
310+
try (Stream<Path> stream = Files.walk(indexDataPath)) {
311+
stream.forEach(path -> {
312+
try {
313+
if (path.endsWith(".lock") == false) {
314+
Files.copy(path, newIndexDataPath.resolve(indexDataPath.relativize(path)));
315+
}
316+
} catch (final Exception e) {
317+
logger.error("Failed to copy data path directory", e);
318+
fail();
319+
}
320+
});
321+
}
335322

336-
assert Files.exists(startDir) == false : "start dir shouldn't exist";
323+
logger.info("--> updating data_path to [{}] for index [{}]", newIndexDataPath, index);
324+
assertAcked(client().admin().indices().prepareUpdateSettings(index)
325+
.setSettings(Settings.builder().put(IndexMetaData.SETTING_DATA_PATH, newIndexDataPath.toAbsolutePath().toString()).build())
326+
.setIndicesOptions(IndicesOptions.fromOptions(true, false, true, true)));
337327

338328
logger.info("--> settings updated and files moved, re-opening index");
339-
client().admin().indices().prepareOpen(INDEX).get();
329+
assertAcked(client().admin().indices().prepareOpen(index));
340330
logger.info("--> index re-opened");
341-
ensureGreen(INDEX);
331+
ensureGreen(index);
342332

343-
resp = client().prepareSearch(INDEX).setQuery(matchAllQuery()).get();
344-
assertThat("found the hit", resp.getHits().getTotalHits().value, equalTo(1L));
333+
assertHitCount(client().prepareSearch(index).setSize(0).get(), 1L);
345334

346-
assertAcked(client().admin().indices().prepareDelete(INDEX));
335+
assertAcked(client().admin().indices().prepareDelete(index));
347336
assertAllIndicesRemovedAndDeletionCompleted(Collections.singleton(getInstanceFromNode(IndicesService.class)));
348-
assertPathHasBeenCleared(startDir.toAbsolutePath());
349-
assertPathHasBeenCleared(endDir.toAbsolutePath());
337+
assertPathHasBeenCleared(newIndexDataPath.toAbsolutePath());
350338
}
351339

352340
public void testMaybeFlush() throws Exception {

0 commit comments

Comments
 (0)