Skip to content

Commit e012784

Browse files
committed
Add data stream template validation
to snapshot restore
1 parent a91a9a5 commit e012784

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.elasticsearch.common.Strings;
5353
import org.elasticsearch.common.UUIDs;
5454
import org.elasticsearch.common.collect.ImmutableOpenMap;
55+
import org.elasticsearch.common.logging.HeaderWarning;
5556
import org.elasticsearch.common.lucene.Lucene;
5657
import org.elasticsearch.common.regex.Regex;
5758
import org.elasticsearch.common.settings.ClusterSettings;
@@ -398,6 +399,8 @@ private void startRestore(
398399
Map<String, DataStream> dataStreamsToRestore = result.v1();
399400
Map<String, DataStreamAlias> dataStreamAliasesToRestore = result.v2();
400401

402+
validateDatastreamTemplatesExistAndWarnIfMissing(dataStreamsToRestore, snapshotInfo);
403+
401404
// Remove the data streams from the list of requested indices
402405
requestIndices.removeAll(dataStreamsToRestore.keySet());
403406

@@ -510,6 +513,30 @@ private void startRestore(
510513
);
511514
}
512515

516+
private void validateDatastreamTemplatesExistAndWarnIfMissing(Map<String, DataStream> dataStreamsToRestore, SnapshotInfo snapshotInfo) {
517+
var templatePatterns = clusterService.state()
518+
.metadata()
519+
.templatesV2()
520+
.values()
521+
.stream()
522+
.filter(cit -> cit.getDataStreamTemplate() != null)
523+
.flatMap(cit -> cit.indexPatterns().stream())
524+
.collect(Collectors.toSet());
525+
526+
for (String name : dataStreamsToRestore.keySet()) {
527+
if (templatePatterns.stream().noneMatch(pattern -> Regex.simpleMatch(pattern, name))) {
528+
String warningMessage = format(
529+
"Snapshot [%s] contains data stream [%s] but custer does not have a matching index template. This will cause"
530+
+ " rollover to fail until a matching index template is created",
531+
snapshotInfo.snapshotId(),
532+
name
533+
);
534+
logger.warn(() -> warningMessage);
535+
HeaderWarning.addWarning(warningMessage);
536+
}
537+
}
538+
}
539+
513540
@SuppressForbidden(reason = "legacy usage of unbatched task") // TODO add support for batching here
514541
private void submitUnbatchedTask(@SuppressWarnings("SameParameterValue") String source, ClusterStateUpdateTask task) {
515542
clusterService.submitUnbatchedStateUpdateTask(source, task);

0 commit comments

Comments
 (0)