Skip to content

License checks for archive tier #83894

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 2 commits into from
Feb 15, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package org.elasticsearch.plugins;

import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.env.Environment;
Expand All @@ -17,6 +18,7 @@

import java.util.Collections;
import java.util.Map;
import java.util.function.Consumer;

/**
* An extension point for {@link Plugin} implementations to add custom snapshot repositories.
Expand Down Expand Up @@ -59,4 +61,13 @@ default Map<String, Repository.Factory> getInternalRepositories(
return Collections.emptyMap();
}

/**
* Returns a check that is run on restore. This allows plugins to prevent certain restores from happening.
*
* returns null if no check is provided
*/
default Consumer<IndexMetadata> addPreRestoreCheck() {
Copy link
Member

Choose a reason for hiding this comment

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

Just curious why you prefer returning a consumer instead of consuming IndexMetadata?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This follows the model of not calling directly into plugins, but plugins providing extensions points (at startup time) that are then being called at runtime.

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package org.elasticsearch.repositories;

import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.BigArrays;
Expand All @@ -18,10 +19,12 @@
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.xcontent.NamedXContentRegistry;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
* Sets up classes for Snapshot/Restore.
Expand Down Expand Up @@ -80,6 +83,14 @@ public RepositoriesModule(
}
}

List<Consumer<IndexMetadata>> preRestoreChecks = new ArrayList<>();
for (RepositoryPlugin repoPlugin : repoPlugins) {
Consumer<IndexMetadata> preRestoreCheck = repoPlugin.addPreRestoreCheck();
if (preRestoreCheck != null) {
preRestoreChecks.add(preRestoreCheck);
}
}

Settings settings = env.settings();
Map<String, Repository.Factory> repositoryTypes = Collections.unmodifiableMap(factories);
Map<String, Repository.Factory> internalRepositoryTypes = Collections.unmodifiableMap(internalFactories);
Expand All @@ -89,7 +100,8 @@ public RepositoriesModule(
transportService,
repositoryTypes,
internalRepositoryTypes,
transportService.getThreadPool()
transportService.getThreadPool(),
preRestoreChecks
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -97,13 +98,16 @@ public class RepositoriesService extends AbstractLifecycleComponent implements C
private volatile Map<String, Repository> repositories = Collections.emptyMap();
private final RepositoriesStatsArchive repositoriesStatsArchive;

private final List<Consumer<IndexMetadata>> preRestoreChecks;

public RepositoriesService(
Settings settings,
ClusterService clusterService,
TransportService transportService,
Map<String, Repository.Factory> typesRegistry,
Map<String, Repository.Factory> internalTypesRegistry,
ThreadPool threadPool
ThreadPool threadPool,
List<Consumer<IndexMetadata>> preRestoreChecks
) {
this.typesRegistry = typesRegistry;
this.internalTypesRegistry = internalTypesRegistry;
Expand All @@ -122,6 +126,7 @@ public RepositoriesService(
REPOSITORIES_STATS_ARCHIVE_MAX_ARCHIVED_STATS.get(settings),
threadPool::relativeTimeInMillis
);
this.preRestoreChecks = preRestoreChecks;
}

/**
Expand Down Expand Up @@ -776,6 +781,10 @@ private static RepositoryConflictException newRepositoryConflictException(String
);
}

public List<Consumer<IndexMetadata>> getPreRestoreChecks() {
return preRestoreChecks;
}

@Override
protected void doStart() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1289,9 +1289,11 @@ public ClusterState execute(ClusterState currentState) {
final String localNodeId = clusterService.state().nodes().getLocalNodeId();
for (Map.Entry<String, IndexId> indexEntry : indicesToRestore.entrySet()) {
final IndexId index = indexEntry.getValue();
final IndexMetadata originalIndexMetadata = metadata.index(index.getName());
repositoriesService.getPreRestoreChecks().forEach(check -> check.accept(originalIndexMetadata));
IndexMetadata snapshotIndexMetadata = updateIndexSettings(
snapshot,
metadata.index(index.getName()),
originalIndexMetadata,
request.indexSettings(),
request.ignoreIndexSettings()
);
Expand Down Expand Up @@ -1591,39 +1593,40 @@ private IndexMetadata convertLegacyIndex(IndexMetadata snapshotIndexMetadata, Cl
if (snapshotIndexMetadata.getCreationVersion().before(Version.fromString("5.0.0"))) {
throw new IllegalArgumentException("can't restore an index created before version 5.0.0");
}
IndexMetadata.Builder convertedIndexMetadata = IndexMetadata.builder(snapshotIndexMetadata);
MappingMetadata mappingMetadata = snapshotIndexMetadata.mapping();
Map<String, Object> loadedMappingSource = mappingMetadata.rawSourceAsMap();

// store old mapping under _meta/legacy_mappings
Map<String, Object> legacyMapping = new LinkedHashMap<>();
boolean sourceOnlySnapshot = snapshotIndexMetadata.getSettings().getAsBoolean("index.source_only", false);
if (sourceOnlySnapshot) {
// actual mapping is under "_meta" (but strip type first)
Object sourceOnlyMeta = mappingMetadata.sourceAsMap().get("_meta");
if (sourceOnlyMeta instanceof Map<?, ?> sourceOnlyMetaMap) {
legacyMapping.put("legacy_mappings", sourceOnlyMetaMap);
if (mappingMetadata != null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the newly added test uncovered a bug as mappings can be null sometimes (even if not very useful, it's still a thing). This method was just refactored to put the existing logic under an if != null

Map<String, Object> loadedMappingSource = mappingMetadata.rawSourceAsMap();

// store old mapping under _meta/legacy_mappings
Map<String, Object> legacyMapping = new LinkedHashMap<>();
boolean sourceOnlySnapshot = snapshotIndexMetadata.getSettings().getAsBoolean("index.source_only", false);
if (sourceOnlySnapshot) {
// actual mapping is under "_meta" (but strip type first)
Object sourceOnlyMeta = mappingMetadata.sourceAsMap().get("_meta");
if (sourceOnlyMeta instanceof Map<?, ?> sourceOnlyMetaMap) {
legacyMapping.put("legacy_mappings", sourceOnlyMetaMap);
}
} else {
legacyMapping.put("legacy_mappings", loadedMappingSource);
}
} else {
legacyMapping.put("legacy_mappings", loadedMappingSource);
}

Map<String, Object> newMappingSource = new LinkedHashMap<>();
newMappingSource.put("_meta", legacyMapping);
Map<String, Object> newMappingSource = new LinkedHashMap<>();
newMappingSource.put("_meta", legacyMapping);

Map<String, Object> newMapping = new LinkedHashMap<>();
newMapping.put(mappingMetadata.type(), newMappingSource);
Map<String, Object> newMapping = new LinkedHashMap<>();
newMapping.put(mappingMetadata.type(), newMappingSource);

convertedIndexMetadata.putMapping(new MappingMetadata(mappingMetadata.type(), newMapping));
}

convertedIndexMetadata.settings(
Settings.builder()
.put(snapshotIndexMetadata.getSettings())
.put(IndexMetadata.SETTING_INDEX_VERSION_COMPATIBILITY.getKey(), clusterState.getNodes().getSmallestNonClientNodeVersion())
);
// TODO: _routing? Perhaps we don't need to obey any routing here as stuff is read-only anyway and get API will be disabled
return IndexMetadata.builder(snapshotIndexMetadata)
.putMapping(new MappingMetadata(mappingMetadata.type(), newMapping))
.settings(
Settings.builder()
.put(snapshotIndexMetadata.getSettings())
.put(
IndexMetadata.SETTING_INDEX_VERSION_COMPATIBILITY.getKey(),
clusterState.getNodes().getSmallestNonClientNodeVersion()
)
)
.build();
return convertedIndexMetadata.build();
}

private static IndexMetadata.Builder restoreToCreateNewIndex(IndexMetadata snapshotIndexMetadata, String renamedIndexName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,8 @@ private IndicesClusterStateService createIndicesClusterStateService(
transportService,
Collections.emptyMap(),
Collections.emptyMap(),
threadPool
threadPool,
List.of()
);
final PeerRecoveryTargetService recoveryTargetService = new PeerRecoveryTargetService(
threadPool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public void setUp() throws Exception {
transportService,
typesRegistry,
typesRegistry,
threadPool
threadPool,
List.of()
);
repositoriesService.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1715,7 +1715,8 @@ protected void assertSnapshotOrGenericThread() {
}
),
emptyMap(),
threadPool
threadPool,
List.of()
);
final ActionFilters actionFilters = new ActionFilters(emptySet());
snapshotsService = new SnapshotsService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.coordination.ElectionStrategy;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.IndexTemplateMetadata;
import org.elasticsearch.cluster.metadata.SingleNodeShutdownMetadata;
Expand Down Expand Up @@ -104,6 +105,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.LongSupplier;
import java.util.function.Predicate;
Expand Down Expand Up @@ -568,6 +570,15 @@ public Map<String, Repository.Factory> getInternalRepositories(
return internalRepositories;
}

@Override
public Consumer<IndexMetadata> addPreRestoreCheck() {
List<Consumer<IndexMetadata>> checks = filterPlugins(RepositoryPlugin.class).stream()
.map(RepositoryPlugin::addPreRestoreCheck)
.filter(Objects::nonNull)
.collect(Collectors.toList());
return checks.isEmpty() ? null : imd -> checks.forEach(c -> c.accept(imd));
}

@Override
public void close() throws IOException {
IOUtils.close(plugins);
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugin/old-lucene-versions/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
apply plugin: 'elasticsearch.internal-cluster-test'
apply plugin: 'elasticsearch.internal-es-plugin'
apply plugin: 'elasticsearch.internal-test-artifact'

Expand All @@ -11,6 +12,7 @@ archivesBaseName = 'x-pack-old-lucene-versions'

dependencies {
compileOnly project(path: xpackModule('core'))
internalClusterTestImplementation(testArtifact(project(xpackModule('core'))))
}

addQaCheckDependencies()
Loading