Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ protected void masterOperation(

}

private void buildResponse(
// Package access for testing.
void buildResponse(
SnapshotsInProgress snapshotsInProgress,
SnapshotsStatusRequest request,
List<SnapshotsInProgress.Entry> currentSnapshotEntries,
Expand All @@ -190,6 +191,9 @@ private void buildResponse(
for (Map.Entry<RepositoryShardId, SnapshotsInProgress.ShardSnapshotStatus> shardEntry : entry
.shardSnapshotStatusByRepoShardId()
.entrySet()) {
if (task.notifyIfCancelled(listener)) {
return;
}
SnapshotsInProgress.ShardSnapshotStatus status = shardEntry.getValue();
if (status.nodeId() != null) {
// We should have information about this shard from the shard:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* 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.action.admin.cluster.snapshots.status;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.cluster.SnapshotsInProgress;
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.repositories.IndexId;
import org.elasticsearch.repositories.RepositoriesService;
import org.elasticsearch.repositories.ShardGeneration;
import org.elasticsearch.snapshots.Snapshot;
import org.elasticsearch.snapshots.SnapshotId;
import org.elasticsearch.tasks.CancellableTask;
import org.elasticsearch.tasks.TaskCancelHelper;
import org.elasticsearch.tasks.TaskCancelledException;
import org.elasticsearch.test.ClusterServiceUtils;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.transport.CapturingTransport;
import org.elasticsearch.threadpool.TestThreadPool;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
import org.junit.After;
import org.junit.Before;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

public class TransportSnapshotsStatusActionTests extends ESTestCase {

private ThreadPool threadPool;
private ClusterService clusterService;
private TransportService transportService;
private RepositoriesService repositoriesService;
private TransportSnapshotsStatusAction action;

@Before
public void initializeComponents() throws Exception {
threadPool = new TestThreadPool(TransportSnapshotsStatusActionTests.class.getName());
clusterService = ClusterServiceUtils.createClusterService(threadPool);
transportService = new CapturingTransport().createTransportService(
clusterService.getSettings(),
threadPool,
TransportService.NOOP_TRANSPORT_INTERCEPTOR,
address -> clusterService.localNode(),
clusterService.getClusterSettings(),
Set.of()
);
final var nodeClient = new NodeClient(clusterService.getSettings(), threadPool);
repositoriesService = new RepositoriesService(
clusterService.getSettings(),
clusterService,
Map.of(),
Map.of(),
threadPool,
nodeClient,
List.of()
);
action = new TransportSnapshotsStatusAction(
transportService,
clusterService,
threadPool,
repositoriesService,
nodeClient,
new ActionFilters(Set.of())
);
}

@After
public void shutdownComponents() throws Exception {
threadPool.shutdown();
repositoriesService.close();
transportService.close();
clusterService.close();
}

public void testBuildResponseDetectsTaskIsCancelledWhileProcessingCurrentSnapshotEntries() throws Exception {
runBasicBuildResponseTest(true);
}

public void testBuildResponseInvokesListenerWithResponseWhenTaskIsNotCancelled() throws Exception {
runBasicBuildResponseTest(false);
}

private void runBasicBuildResponseTest(boolean shouldCancelTask) {
final var expectedSnapshot = new Snapshot(ProjectId.DEFAULT, "test-repo", new SnapshotId("snapshot", "uuid"));
final var expectedState = SnapshotsInProgress.State.STARTED;
final var indexName = "test-index-name";
final var indexUuid = "test-index-uuid";
final var currentSnapshotEntries = List.of(
SnapshotsInProgress.Entry.snapshot(
expectedSnapshot,
randomBoolean(),
randomBoolean(),
SnapshotsInProgress.State.STARTED,
Map.of(indexName, new IndexId(indexName, indexUuid)),
List.of(),
List.of(),
randomNonNegativeLong(),
randomNonNegativeLong(),
Map.of(
new ShardId(indexName, indexUuid, 0),
new SnapshotsInProgress.ShardSnapshotStatus("node", new ShardGeneration("gen"))
),
null,
Map.of(),
IndexVersion.current()
)
);
final var nodeSnapshotStatuses = new TransportNodesSnapshotsStatus.NodesSnapshotStatus(
clusterService.getClusterName(),
List.of(),
List.of()
);

// Run some sanity checks for when the task is not cancelled and we get back a response object.
// Note that thorough verification of the SnapshotsStatusResponse is done in the higher level SnapshotStatus API integration tests.
final Consumer<SnapshotsStatusResponse> verifyResponse = rsp -> {
assertNotNull(rsp);
final var snapshotStatuses = rsp.getSnapshots();
assertNotNull(snapshotStatuses);
assertEquals("expected a single snapshot status instead of " + snapshotStatuses, 1, snapshotStatuses.size());
final var snapshotStatus = snapshotStatuses.getFirst();
assertNotNull(snapshotStatus.getSnapshot());
assertEquals(expectedSnapshot, snapshotStatus.getSnapshot());
assertEquals(expectedState, snapshotStatus.getState());
final var snapshotStatusShards = snapshotStatus.getShards();
assertNotNull(snapshotStatusShards);
assertEquals(1, snapshotStatusShards.size());
Copy link
Contributor

Choose a reason for hiding this comment

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

Debug string pls.

final var snapshotStatusIndices = snapshotStatus.getIndices();
assertNotNull(snapshotStatusIndices);
assertEquals(
"expected a single entry in snapshotStatusIndices instead of " + snapshotStatusIndices,
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I think I see a wrinkle I didn't anticipate. IIUC, SnapshotIndexStatus doesn't have a toString method defined, so it'll print out a class name + hash code? Not ideal.

SnapshotStatus has a toString method that takes advantage of the chunking interface, variations of which SnapshotIndexStatus and SnapshotIndexShardStatus also implement. So it might be easy to write something similar for them, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added toString() methods for the relevant classes. The Strings.toString() that takes a ChunkedToXContent is deprecated. This form is the one that SnapshotStatus is currently calling. In SnapshotStatusResponse I used Strings.toString(ChunkedToXContent.wrapAsToXContent(this), true, true) directly since the assumption is we are calling toString() in scenarios where it would be reasonable to fully serialize to a String. I also set the human parameter to true in all the new toString() methods since toString() is meant to be human readable.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!

1,
snapshotStatusIndices.size()
);
assertTrue(
"no entry for indexName [" + indexName + "] found in snapshotStatusIndices keyset " + snapshotStatusIndices.keySet(),
snapshotStatusIndices.containsKey(indexName)
);
assertNotNull(snapshotStatus.getShardsStats());
};

final var listener = new ActionListener<SnapshotsStatusResponse>() {
@Override
public void onResponse(SnapshotsStatusResponse rsp) {
if (shouldCancelTask) {
fail(
"expected detection of task cancellation and onFailure() instead of onResponse("
+ (rsp == null ? "null" : rsp.getSnapshots())
+ ")"
);
} else {
verifyResponse.accept(rsp);
}
}

@Override
public void onFailure(Exception e) {
if (shouldCancelTask) {
assertTrue(e instanceof TaskCancelledException);
} else {
fail("expected onResponse() instead of onFailure(" + e + ")");
}
}
};

final var listenerInvoked = new AtomicBoolean(false);
final var cancellableTask = new CancellableTask(randomLong(), "type", "action", "desc", null, Map.of());

if (shouldCancelTask) {
TaskCancelHelper.cancel(cancellableTask, "simulated cancellation");
}

action.buildResponse(
SnapshotsInProgress.EMPTY,
new SnapshotsStatusRequest(TEST_REQUEST_TIMEOUT),
currentSnapshotEntries,
nodeSnapshotStatuses,
cancellableTask,
ActionListener.runAfter(listener, () -> listenerInvoked.set(true))
);
assertTrue("Expected listener to be invoked", listenerInvoked.get());
}
}