Skip to content

Commit d0a8866

Browse files
Create a utility class for static SnapshotsService methods
This moves the static methods out of the SnapshotsService to make the stateful code more visible. This is one step towards refactoring the SnapshotsService.
1 parent 5c753a8 commit d0a8866

File tree

17 files changed

+1222
-1105
lines changed

17 files changed

+1222
-1105
lines changed

modules/repository-s3/src/main/java/org/elasticsearch/repositories/s3/S3Repository.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.elasticsearch.repositories.blobstore.MeteredBlobStoreRepository;
4242
import org.elasticsearch.snapshots.SnapshotId;
4343
import org.elasticsearch.snapshots.SnapshotsService;
44+
import org.elasticsearch.snapshots.SnapshotsServiceUtils;
4445
import org.elasticsearch.threadpool.Scheduler;
4546
import org.elasticsearch.threadpool.ThreadPool;
4647
import org.elasticsearch.xcontent.NamedXContentRegistry;
@@ -385,7 +386,7 @@ private static ByteSizeValue objectSizeLimit(ByteSizeValue chunkSize, ByteSizeVa
385386
@Override
386387
public void finalizeSnapshot(final FinalizeSnapshotContext finalizeSnapshotContext) {
387388
final FinalizeSnapshotContext wrappedFinalizeContext;
388-
if (SnapshotsService.useShardGenerations(finalizeSnapshotContext.repositoryMetaVersion()) == false) {
389+
if (SnapshotsServiceUtils.useShardGenerations(finalizeSnapshotContext.repositoryMetaVersion()) == false) {
389390
final ListenableFuture<Void> metadataDone = new ListenableFuture<>();
390391
wrappedFinalizeContext = new FinalizeSnapshotContext(
391392
finalizeSnapshotContext.updatedShardGenerations(),

qa/repository-multi-version/src/test/java/org/elasticsearch/upgrades/MultiVersionRepositoryAccessIT.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.elasticsearch.common.settings.Settings;
1919
import org.elasticsearch.index.IndexVersion;
2020
import org.elasticsearch.index.IndexVersions;
21-
import org.elasticsearch.snapshots.SnapshotsService;
21+
import org.elasticsearch.snapshots.SnapshotsServiceUtils;
2222
import org.elasticsearch.test.rest.ESRestTestCase;
2323
import org.elasticsearch.test.rest.ObjectPath;
2424
import org.elasticsearch.xcontent.XContentParser;
@@ -183,7 +183,7 @@ public void testUpgradeMovesRepoToNewMetaVersion() throws IOException {
183183
// incompatibility in the downgrade test step. We verify that it is impossible here and then create the repo using verify=false
184184
// to check behavior on other operations below.
185185
final boolean verify = TEST_STEP != TestStep.STEP3_OLD_CLUSTER
186-
|| SnapshotsService.includesUUIDs(minNodeVersion)
186+
|| SnapshotsServiceUtils.includesUUIDs(minNodeVersion)
187187
|| minNodeVersion.before(IndexVersions.V_7_12_0);
188188
if (verify == false) {
189189
expectThrowsAnyOf(EXPECTED_BWC_EXCEPTIONS, () -> createRepository(repoName, false, true));
@@ -208,7 +208,7 @@ public void testUpgradeMovesRepoToNewMetaVersion() throws IOException {
208208
ensureSnapshotRestoreWorks(repoName, "snapshot-2", shards, index);
209209
}
210210
} else {
211-
if (SnapshotsService.includesUUIDs(minNodeVersion) == false) {
211+
if (SnapshotsServiceUtils.includesUUIDs(minNodeVersion) == false) {
212212
assertThat(TEST_STEP, is(TestStep.STEP3_OLD_CLUSTER));
213213
expectThrowsAnyOf(EXPECTED_BWC_EXCEPTIONS, () -> listSnapshots(repoName));
214214
expectThrowsAnyOf(EXPECTED_BWC_EXCEPTIONS, () -> deleteSnapshot(repoName, "snapshot-1"));

server/src/internalClusterTest/java/org/elasticsearch/snapshots/CorruptedBlobStoreRepositoryIT.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public void testHandlingMissingRootLevelSnapshotMetadata() throws Exception {
327327

328328
logger.info("--> verify that repo is assumed in old metadata format");
329329
assertThat(
330-
SnapshotsService.minCompatibleVersion(IndexVersion.current(), getRepositoryData(repoName), null),
330+
SnapshotsServiceUtils.minCompatibleVersion(IndexVersion.current(), getRepositoryData(repoName), null),
331331
is(SnapshotsService.OLD_SNAPSHOT_FORMAT)
332332
);
333333

@@ -336,7 +336,7 @@ public void testHandlingMissingRootLevelSnapshotMetadata() throws Exception {
336336

337337
logger.info("--> verify that repository is assumed in new metadata format after removing corrupted snapshot");
338338
assertThat(
339-
SnapshotsService.minCompatibleVersion(IndexVersion.current(), getRepositoryData(repoName), null),
339+
SnapshotsServiceUtils.minCompatibleVersion(IndexVersion.current(), getRepositoryData(repoName), null),
340340
is(IndexVersion.current())
341341
);
342342
final RepositoryData finalRepositoryData = getRepositoryData(repoName);

server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/cleanup/TransportCleanupRepositoryAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.elasticsearch.repositories.RepositoryCleanupResult;
3535
import org.elasticsearch.repositories.RepositoryData;
3636
import org.elasticsearch.repositories.blobstore.BlobStoreRepository;
37-
import org.elasticsearch.snapshots.SnapshotsService;
37+
import org.elasticsearch.snapshots.SnapshotsServiceUtils;
3838
import org.elasticsearch.tasks.Task;
3939
import org.elasticsearch.threadpool.ThreadPool;
4040
import org.elasticsearch.transport.TransportService;
@@ -170,7 +170,7 @@ private void cleanupRepo(String repositoryName, ActionListener<RepositoryCleanup
170170

171171
@Override
172172
public ClusterState execute(ClusterState currentState) {
173-
SnapshotsService.ensureRepositoryExists(repositoryName, currentState);
173+
SnapshotsServiceUtils.ensureRepositoryExists(repositoryName, currentState);
174174
final RepositoryCleanupInProgress repositoryCleanupInProgress = RepositoryCleanupInProgress.get(currentState);
175175
if (repositoryCleanupInProgress.hasCleanupInProgress()) {
176176
throw new IllegalStateException(

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/TransportSnapshotsStatusAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import org.elasticsearch.snapshots.SnapshotShardFailure;
4343
import org.elasticsearch.snapshots.SnapshotShardsService;
4444
import org.elasticsearch.snapshots.SnapshotState;
45-
import org.elasticsearch.snapshots.SnapshotsService;
45+
import org.elasticsearch.snapshots.SnapshotsServiceUtils;
4646
import org.elasticsearch.tasks.CancellableTask;
4747
import org.elasticsearch.tasks.Task;
4848
import org.elasticsearch.threadpool.ThreadPool;
@@ -113,7 +113,7 @@ protected void masterOperation(
113113
final CancellableTask cancellableTask = (CancellableTask) task;
114114

115115
final SnapshotsInProgress snapshotsInProgress = SnapshotsInProgress.get(state);
116-
List<SnapshotsInProgress.Entry> currentSnapshots = SnapshotsService.currentSnapshots(
116+
List<SnapshotsInProgress.Entry> currentSnapshots = SnapshotsServiceUtils.currentSnapshots(
117117
snapshotsInProgress,
118118
request.repository(),
119119
Arrays.asList(request.snapshots())

server/src/main/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
import org.elasticsearch.indices.SystemIndices;
5151
import org.elasticsearch.injection.guice.Inject;
5252
import org.elasticsearch.snapshots.SnapshotInProgressException;
53-
import org.elasticsearch.snapshots.SnapshotsService;
53+
import org.elasticsearch.snapshots.SnapshotsServiceUtils;
5454
import org.elasticsearch.telemetry.TelemetryProvider;
5555
import org.elasticsearch.telemetry.metric.MeterRegistry;
5656
import org.elasticsearch.threadpool.ThreadPool;
@@ -306,7 +306,7 @@ private RolloverResult rolloverDataStream(
306306
boolean isFailureStoreRollover
307307
) throws Exception {
308308
final ProjectMetadata metadata = projectState.metadata();
309-
Set<String> snapshottingDataStreams = SnapshotsService.snapshottingDataStreams(
309+
Set<String> snapshottingDataStreams = SnapshotsServiceUtils.snapshottingDataStreams(
310310
projectState,
311311
Collections.singleton(dataStream.getName())
312312
);

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataDataStreamsService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import org.elasticsearch.logging.LogManager;
3636
import org.elasticsearch.logging.Logger;
3737
import org.elasticsearch.snapshots.SnapshotInProgressException;
38-
import org.elasticsearch.snapshots.SnapshotsService;
38+
import org.elasticsearch.snapshots.SnapshotsServiceUtils;
3939

4040
import java.io.IOException;
4141
import java.util.HashSet;
@@ -464,7 +464,7 @@ public static ClusterState deleteDataStreams(ProjectState projectState, Set<Data
464464
}
465465

466466
Set<String> dataStreamNames = dataStreams.stream().map(DataStream::getName).collect(Collectors.toSet());
467-
Set<String> snapshottingDataStreams = SnapshotsService.snapshottingDataStreams(projectState, dataStreamNames);
467+
Set<String> snapshottingDataStreams = SnapshotsServiceUtils.snapshottingDataStreams(projectState, dataStreamNames);
468468
if (snapshottingDataStreams.isEmpty() == false) {
469469
throw new SnapshotInProgressException(
470470
"Cannot delete data streams that are being snapshotted: ["

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataDeleteIndexService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import org.elasticsearch.injection.guice.Inject;
3636
import org.elasticsearch.snapshots.RestoreService;
3737
import org.elasticsearch.snapshots.SnapshotInProgressException;
38-
import org.elasticsearch.snapshots.SnapshotsService;
38+
import org.elasticsearch.snapshots.SnapshotsServiceUtils;
3939

4040
import java.util.HashMap;
4141
import java.util.HashSet;
@@ -186,7 +186,7 @@ public static ClusterState deleteIndices(ProjectState projectState, Set<Index> i
186186
}
187187

188188
// Check if index deletion conflicts with any running snapshots
189-
Set<Index> snapshottingIndices = SnapshotsService.snapshottingIndices(projectState, indicesToDelete);
189+
Set<Index> snapshottingIndices = SnapshotsServiceUtils.snapshottingIndices(projectState, indicesToDelete);
190190
if (snapshottingIndices.isEmpty() == false) {
191191
throw new SnapshotInProgressException(
192192
"Cannot delete indices that are being snapshotted: "

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexStateService.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
import org.elasticsearch.rest.RestStatus;
7575
import org.elasticsearch.snapshots.RestoreService;
7676
import org.elasticsearch.snapshots.SnapshotInProgressException;
77-
import org.elasticsearch.snapshots.SnapshotsService;
77+
import org.elasticsearch.snapshots.SnapshotsServiceUtils;
7878
import org.elasticsearch.tasks.TaskId;
7979
import org.elasticsearch.threadpool.ThreadPool;
8080

@@ -343,7 +343,7 @@ static ClusterState addIndexClosedBlocks(
343343
}
344344

345345
// Check if index closing conflicts with any running snapshots
346-
Set<Index> snapshottingIndices = SnapshotsService.snapshottingIndices(currentProjectState, indicesToClose);
346+
Set<Index> snapshottingIndices = SnapshotsServiceUtils.snapshottingIndices(currentProjectState, indicesToClose);
347347
if (snapshottingIndices.isEmpty() == false) {
348348
throw new SnapshotInProgressException(
349349
"Cannot close indices that are being snapshotted: "
@@ -925,7 +925,7 @@ static Tuple<ClusterState, List<IndexResult>> closeRoutingTable(
925925
}
926926

927927
// Check if index closing conflicts with any running snapshots
928-
Set<Index> snapshottingIndices = SnapshotsService.snapshottingIndices(currentProjectState, Set.of(index));
928+
Set<Index> snapshottingIndices = SnapshotsServiceUtils.snapshottingIndices(currentProjectState, Set.of(index));
929929
if (snapshottingIndices.isEmpty() == false) {
930930
closingResults.put(
931931
result.getKey(),

server/src/main/java/org/elasticsearch/repositories/FinalizeSnapshotContext.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.elasticsearch.cluster.metadata.Metadata;
1818
import org.elasticsearch.index.IndexVersion;
1919
import org.elasticsearch.snapshots.SnapshotInfo;
20-
import org.elasticsearch.snapshots.SnapshotsService;
20+
import org.elasticsearch.snapshots.SnapshotsServiceUtils;
2121

2222
import java.util.Map;
2323
import java.util.Set;
@@ -103,7 +103,11 @@ public Map<RepositoryShardId, Set<ShardGeneration>> obsoleteShardGenerations() {
103103
* Returns a new {@link ClusterState}, based on the given {@code state} with the create-snapshot entry removed.
104104
*/
105105
public ClusterState updatedClusterState(ClusterState state) {
106-
final ClusterState updatedState = SnapshotsService.stateWithoutSnapshot(state, snapshotInfo.snapshot(), updatedShardGenerations);
106+
final ClusterState updatedState = SnapshotsServiceUtils.stateWithoutSnapshot(
107+
state,
108+
snapshotInfo.snapshot(),
109+
updatedShardGenerations
110+
);
107111
// Now that the updated cluster state may have changed in-progress shard snapshots' shard generations to the latest shard
108112
// generation, let's mark any now unreferenced shard generations as obsolete and ready to be deleted.
109113
obsoleteGenerations.set(

server/src/main/java/org/elasticsearch/repositories/RepositoryData.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.elasticsearch.snapshots.SnapshotInfo;
3030
import org.elasticsearch.snapshots.SnapshotState;
3131
import org.elasticsearch.snapshots.SnapshotsService;
32+
import org.elasticsearch.snapshots.SnapshotsServiceUtils;
3233
import org.elasticsearch.threadpool.ThreadPool;
3334
import org.elasticsearch.xcontent.XContentBuilder;
3435
import org.elasticsearch.xcontent.XContentParser;
@@ -700,9 +701,9 @@ public XContentBuilder snapshotsToXContent(final XContentBuilder builder, final
700701
public XContentBuilder snapshotsToXContent(final XContentBuilder builder, final IndexVersion repoMetaVersion, boolean permitMissingUuid)
701702
throws IOException {
702703

703-
final boolean shouldWriteUUIDS = SnapshotsService.includesUUIDs(repoMetaVersion);
704-
final boolean shouldWriteIndexGens = SnapshotsService.useIndexGenerations(repoMetaVersion);
705-
final boolean shouldWriteShardGens = SnapshotsService.useShardGenerations(repoMetaVersion);
704+
final boolean shouldWriteUUIDS = SnapshotsServiceUtils.includesUUIDs(repoMetaVersion);
705+
final boolean shouldWriteIndexGens = SnapshotsServiceUtils.useIndexGenerations(repoMetaVersion);
706+
final boolean shouldWriteShardGens = SnapshotsServiceUtils.useShardGenerations(repoMetaVersion);
706707

707708
assert Boolean.compare(shouldWriteUUIDS, shouldWriteIndexGens) <= 0;
708709
assert Boolean.compare(shouldWriteIndexGens, shouldWriteShardGens) <= 0;
@@ -909,7 +910,7 @@ public static RepositoryData snapshotsFromXContent(XContentParser parser, long g
909910
this snapshot repository format requires Elasticsearch version [%s] or later""", versionString));
910911
};
911912

912-
assert SnapshotsService.useShardGenerations(version);
913+
assert SnapshotsServiceUtils.useShardGenerations(version);
913914
}
914915
case UUID -> {
915916
XContentParserUtils.ensureExpectedToken(XContentParser.Token.VALUE_STRING, parser.nextToken(), parser);

server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
import org.elasticsearch.snapshots.SnapshotInfo;
129129
import org.elasticsearch.snapshots.SnapshotMissingException;
130130
import org.elasticsearch.snapshots.SnapshotsService;
131+
import org.elasticsearch.snapshots.SnapshotsServiceUtils;
131132
import org.elasticsearch.tasks.TaskCancelledException;
132133
import org.elasticsearch.threadpool.ThreadPool;
133134
import org.elasticsearch.transport.LeakTracker;
@@ -974,7 +975,7 @@ private void createSnapshotsDeletion(
974975
return new SnapshotsDeletion(
975976
snapshotIds,
976977
repositoryDataGeneration,
977-
SnapshotsService.minCompatibleVersion(minimumNodeVersion, originalRepositoryData, snapshotIds),
978+
SnapshotsServiceUtils.minCompatibleVersion(minimumNodeVersion, originalRepositoryData, snapshotIds),
978979
originalRootBlobs,
979980
blobStore().blobContainer(indicesPath()).children(OperationPurpose.SNAPSHOT_DATA),
980981
originalRepositoryData
@@ -1080,7 +1081,7 @@ class SnapshotsDeletion {
10801081
this.snapshotIds = snapshotIds;
10811082
this.originalRepositoryDataGeneration = originalRepositoryDataGeneration;
10821083
this.repositoryFormatIndexVersion = repositoryFormatIndexVersion;
1083-
this.useShardGenerations = SnapshotsService.useShardGenerations(repositoryFormatIndexVersion);
1084+
this.useShardGenerations = SnapshotsServiceUtils.useShardGenerations(repositoryFormatIndexVersion);
10841085
this.originalRootBlobs = originalRootBlobs;
10851086
this.originalIndexContainers = originalIndexContainers;
10861087
this.originalRepositoryData = originalRepositoryData;
@@ -1750,11 +1751,11 @@ public void finalizeSnapshot(final FinalizeSnapshotContext finalizeSnapshotConte
17501751
// If there are older version nodes in the cluster, we don't need to run this cleanup as it will have already happened
17511752
// when writing the index-${N} to each shard directory.
17521753
final IndexVersion repositoryMetaVersion = finalizeSnapshotContext.repositoryMetaVersion();
1753-
final boolean writeShardGens = SnapshotsService.useShardGenerations(repositoryMetaVersion);
1754+
final boolean writeShardGens = SnapshotsServiceUtils.useShardGenerations(repositoryMetaVersion);
17541755

17551756
final Executor executor = threadPool.executor(ThreadPool.Names.SNAPSHOT);
17561757

1757-
final boolean writeIndexGens = SnapshotsService.useIndexGenerations(repositoryMetaVersion);
1758+
final boolean writeIndexGens = SnapshotsServiceUtils.useIndexGenerations(repositoryMetaVersion);
17581759

17591760
record MetadataWriteResult(
17601761
RepositoryData existingRepositoryData,
@@ -2522,7 +2523,7 @@ private void cacheRepositoryData(RepositoryData repositoryData, IndexVersion ver
25222523
return;
25232524
}
25242525
final RepositoryData toCache;
2525-
if (SnapshotsService.useShardGenerations(version)) {
2526+
if (SnapshotsServiceUtils.useShardGenerations(version)) {
25262527
toCache = repositoryData;
25272528
} else {
25282529
// don't cache shard generations here as they may be unreliable
@@ -2871,7 +2872,7 @@ public void onFailure(Exception e) {
28712872
}, true);
28722873
maybeWriteIndexLatest(newGen);
28732874

2874-
if (filteredRepositoryData.getUuid().equals(RepositoryData.MISSING_UUID) && SnapshotsService.includesUUIDs(version)) {
2875+
if (filteredRepositoryData.getUuid().equals(RepositoryData.MISSING_UUID) && SnapshotsServiceUtils.includesUUIDs(version)) {
28752876
assert newRepositoryData.getUuid().equals(RepositoryData.MISSING_UUID) == false;
28762877
logger.info(
28772878
Strings.format(
@@ -2954,7 +2955,7 @@ public String toString() {
29542955
}
29552956

29562957
private RepositoryData updateRepositoryData(RepositoryData repositoryData, IndexVersion repositoryMetaversion, long newGen) {
2957-
if (SnapshotsService.includesUUIDs(repositoryMetaversion)) {
2958+
if (SnapshotsServiceUtils.includesUUIDs(repositoryMetaversion)) {
29582959
final String clusterUUID = clusterService.state().metadata().clusterUUID();
29592960
if (repositoryData.getClusterUUID().equals(clusterUUID) == false) {
29602961
repositoryData = repositoryData.withClusterUuid(clusterUUID);
@@ -3089,7 +3090,7 @@ private ClusterState updateRepositoryGenerationsIfNecessary(ClusterState state,
30893090
}
30903091
}
30913092
updatedDeletionsInProgress = changedDeletions ? SnapshotDeletionsInProgress.of(deletionEntries) : null;
3092-
return SnapshotsService.updateWithSnapshots(state, updatedSnapshotsInProgress, updatedDeletionsInProgress);
3093+
return SnapshotsServiceUtils.updateWithSnapshots(state, updatedSnapshotsInProgress, updatedDeletionsInProgress);
30933094
}
30943095

30953096
private RepositoryMetadata getRepoMetadata(ClusterState state) {
@@ -3328,8 +3329,8 @@ private void doSnapshotShard(SnapshotShardContext context) {
33283329
);
33293330

33303331
final ShardGeneration indexGeneration;
3331-
final boolean writeShardGens = SnapshotsService.useShardGenerations(context.getRepositoryMetaVersion());
3332-
final boolean writeFileInfoWriterUUID = SnapshotsService.includeFileInfoWriterUUID(context.getRepositoryMetaVersion());
3332+
final boolean writeShardGens = SnapshotsServiceUtils.useShardGenerations(context.getRepositoryMetaVersion());
3333+
final boolean writeFileInfoWriterUUID = SnapshotsServiceUtils.includeFileInfoWriterUUID(context.getRepositoryMetaVersion());
33333334
// build a new BlobStoreIndexShardSnapshot, that includes this one and all the saved ones
33343335
final BlobStoreIndexShardSnapshots updatedBlobStoreIndexShardSnapshots = snapshots.withAddedSnapshot(
33353336
new SnapshotFiles(snapshotId.getName(), indexCommitPointFiles, context.stateIdentifier())

server/src/main/java/org/elasticsearch/snapshots/SnapshotShardsService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ private void startNewShardSnapshots(String localNodeId, SnapshotsInProgress.Entr
398398
newSnapshotShards.put(shardId, snapshotStatus);
399399
final IndexId indexId = entry.indices().get(shardId.getIndexName());
400400
assert indexId != null;
401-
assert SnapshotsService.useShardGenerations(entry.version())
401+
assert SnapshotsServiceUtils.useShardGenerations(entry.version())
402402
|| ShardGenerations.fixShardGeneration(snapshotStatus.generation()) == null
403403
: "Found non-null, non-numeric shard generation ["
404404
+ snapshotStatus.generation()

0 commit comments

Comments
 (0)