Skip to content

Make SnapshotsInProgress project compatible #125470

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

Merged
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.upgrades;

import com.carrotsearch.randomizedtesting.annotations.Name;

import org.elasticsearch.client.Request;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.rest.ObjectPath;

import java.io.IOException;
import java.util.Collection;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;

public class RunningSnapshotIT extends AbstractRollingUpgradeTestCase {

public RunningSnapshotIT(@Name("upgradedNodes") int upgradedNodes) {
super(upgradedNodes);
}

public void testRunningSnapshotCompleteAfterUpgrade() throws Exception {
final String indexName = "index";
final String repositoryName = "repo";
final String snapshotName = "snapshot";
final var nodeIds = getNodesInfo(client()).keySet();

if (isOldCluster()) {
// create an index to have one shard per node
createIndex(indexName, indexSettings(3, 0).put("index.routing.allocation.total_shards_per_node", 1).build());
ensureGreen(indexName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of completeness maybe (randomly) index some data?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure see e211f26

flush(indexName, true);
// Signal shutdown to prevent snapshot from being completed
putShutdownMetadata(nodeIds);
registerRepository(repositoryName, "fs", randomBoolean(), Settings.builder().put("location", "backup").build());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe doesn't matter, but seems more readable to register the repo a bit earlier?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved as suggested

createSnapshot(repositoryName, snapshotName, false);
assertRunningSnapshot(repositoryName, snapshotName);
} else {
if (isUpgradedCluster()) {
deleteShutdownMetadata(nodeIds);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe assert that no shutdown metadata exists after deleting it. It might make possible test failures more concrete.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have SnapshotShutdownProgressTracker reporting quite detailed information about shard snapshot pausing and resuming due to shutdown. That said, it does not hurt to double check as well. So I added it in e211f26

ensureGreen(indexName);
assertBusy(() -> assertCompletedSnapshot(repositoryName, snapshotName));
} else {
assertRunningSnapshot(repositoryName, snapshotName);
}
}
}

private void putShutdownMetadata(Collection<String> nodeIds) throws IOException {
for (String nodeId : nodeIds) {
final Request putShutdownRequest = new Request("PUT", "/_nodes/" + nodeId + "/shutdown");
putShutdownRequest.setJsonEntity("""
{
"type": "remove",
"reason": "test"
}""");
client().performRequest(putShutdownRequest);
}
}

private void deleteShutdownMetadata(Collection<String> nodeIds) throws IOException {
for (String nodeId : nodeIds) {
final Request request = new Request("DELETE", "/_nodes/" + nodeId + "/shutdown");
client().performRequest(request);
}
}

private void assertRunningSnapshot(String repositoryName, String snapshotName) throws IOException {
final Request request = new Request("GET", "/_snapshot/" + repositoryName + "/_current");
final ObjectPath responsePath = assertOKAndCreateObjectPath(client().performRequest(request));
assertThat(responsePath.evaluate("total"), equalTo(1));
assertThat(responsePath.evaluate("snapshots.0.snapshot"), equalTo(snapshotName));
}

private void assertCompletedSnapshot(String repositoryName, String snapshotName) throws IOException {
{
final Request request = new Request("GET", "/_snapshot/" + repositoryName + "/_current");
final ObjectPath responsePath = assertOKAndCreateObjectPath(client().performRequest(request));
assertThat(responsePath.evaluate("total"), equalTo(0));
}

{
final Request request = new Request("GET", "/_snapshot/" + repositoryName + "/" + snapshotName);
final ObjectPath responsePath = assertOKAndCreateObjectPath(client().performRequest(request));
assertThat(responsePath.evaluate("total"), equalTo(1));
assertThat(responsePath.evaluate("snapshots.0.snapshot"), equalTo(snapshotName));
assertThat(responsePath.evaluate("snapshots.0.state"), not(equalTo("IN_PROGRESS")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ static TransportVersion def(int id) {
public static final TransportVersion INDEXING_STATS_INCLUDES_RECENT_WRITE_LOAD = def(9_034_0_00);
public static final TransportVersion ESQL_AGGREGATE_METRIC_DOUBLE_LITERAL = def(9_035_0_00);
public static final TransportVersion INDEX_METADATA_INCLUDES_RECENT_WRITE_LOAD = def(9_036_0_00);
public static final TransportVersion PROJECT_ID_IN_SNAPSHOT = def(9_037_0_00);

/*
* STOP! READ THIS FIRST! No, really,
Expand Down
23 changes: 23 additions & 0 deletions server/src/main/java/org/elasticsearch/cluster/DiffableUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,29 @@ public static <K, T, M extends Map<K, T>> boolean hasKey(MapDiff<K, T, M> diff,
return false;
}

/**
* Create a new JDK map backed MapDiff by transforming the keys with the provided keyFunction.
* @param diff Original MapDiff to transform
* @param keyFunction Function to transform the key
* @param keySerializer Serializer for the new key
*/
public static <K1, K2, T extends Diffable<T>, M1 extends Map<K1, T>> MapDiff<K2, T, Map<K2, T>> jdkMapDiffWithUpdatedKeys(
MapDiff<K1, T, M1> diff,
Function<K1, K2> keyFunction,
KeySerializer<K2> keySerializer
) {
final List<K2> deletes = diff.getDeletes().stream().map(keyFunction).toList();
final List<Map.Entry<K2, Diff<T>>> diffs = diff.getDiffs()
.stream()
.map(entry -> Map.entry(keyFunction.apply(entry.getKey()), entry.getValue()))
.toList();
final List<Map.Entry<K2, T>> upserts = diff.getUpserts()
.stream()
.map(entry -> Map.entry(keyFunction.apply(entry.getKey()), entry.getValue()))
.toList();
return new MapDiff<>(keySerializer, DiffableValueSerializer.getWriteOnlyInstance(), deletes, diffs, upserts, JdkMapBuilder::new);
}

/**
* Creates a MapDiff that applies a single entry diff to a map
*/
Expand Down
Loading