Skip to content

[8.17] Cheaper snapshot-related toString() impls (#121283) #121307

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
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions docs/changelog/121283.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 121283
summary: Cheaper snapshot-related `toString()` impls
area: Snapshot/Restore
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import org.elasticsearch.cluster.SnapshotDeletionsInProgress;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.cluster.service.MasterService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.snapshots.mockstore.MockRepository;
import org.elasticsearch.test.ClusterServiceUtils;
import org.elasticsearch.test.MockLog;
import org.elasticsearch.test.junit.annotations.TestLogging;

import java.util.List;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -223,4 +225,30 @@ public void testRerouteWhenShardSnapshotsCompleted() throws Exception {
safeAwait(shardMovedListener);
ensureGreen(indexName);
}

@TestLogging(reason = "testing task description, logged at DEBUG", value = "org.elasticsearch.cluster.service.MasterService:DEBUG")
public void testCreateSnapshotTaskDescription() {
createIndexWithRandomDocs(randomIdentifier(), randomIntBetween(1, 5));
final var repositoryName = randomIdentifier();
createRepository(repositoryName, "mock");

final var snapshotName = randomIdentifier();
MockLog.assertThatLogger(
() -> createFullSnapshot(repositoryName, snapshotName),
MasterService.class,
new MockLog.SeenEventExpectation(
"executing cluster state update debug message",
MasterService.class.getCanonicalName(),
Level.DEBUG,
"executing cluster state update for [create_snapshot ["
+ snapshotName
+ "][CreateSnapshotTask{repository="
+ repositoryName
+ ", snapshot=*"
+ snapshotName
+ "*}]]"
)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,11 @@ public int hashCode() {
return Objects.hash(snapshotIds, snapshotsDetails, indices, indexSnapshots, shardGenerations, indexMetaDataGenerations);
}

@Override
public String toString() {
return Strings.format("RepositoryData[uuid=%s,gen=%s]", uuid, genId);
}

/**
* Resolve the index name to the index id specific to the repository,
* throwing an exception if the index could not be resolved.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3894,6 +3894,11 @@ public void onFailure(Exception e) {
logSnapshotFailure("create", snapshot, e);
listener.onFailure(e);
}

@Override
public String toString() {
return "CreateSnapshotTask{repository=" + repository.getMetadata().name() + ", snapshot=" + snapshot + '}';
}
}

private static void logSnapshotFailure(String operation, Snapshot snapshot, Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@

import static org.elasticsearch.repositories.RepositoryData.EMPTY_REPO_GEN;
import static org.elasticsearch.repositories.RepositoryData.MISSING_UUID;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.not;

/**
* Tests for the {@link RepositoryData} class.
Expand Down Expand Up @@ -430,6 +433,19 @@ public void testFailsIfMinVersionNotSatisfied() throws IOException {
}
}

public void testToString() {
final var repositoryData = generateRandomRepoData();
assertThat(
repositoryData.toString(),
allOf(
containsString("RepositoryData"),
containsString(repositoryData.getUuid()),
containsString(Long.toString(repositoryData.getGenId())),
not(containsString("@")) // not the default Object#toString which does a very expensive hashcode computation
)
);
}

public static RepositoryData generateRandomRepoData() {
final int numIndices = randomIntBetween(1, 30);
final List<IndexId> indices = new ArrayList<>(numIndices);
Expand Down