Skip to content

Make TransportGetDatabaseConfigurationAction project aware #130065

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -13,11 +13,15 @@
import org.elasticsearch.action.FailedNodeException;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.nodes.TransportNodesAction;
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.cluster.metadata.ProjectMetadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.project.ProjectResolver;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.ingest.geoip.DatabaseNodeService;
import org.elasticsearch.ingest.geoip.GeoIpDownloaderTaskExecutor;
import org.elasticsearch.ingest.geoip.GeoIpTaskState;
import org.elasticsearch.ingest.geoip.IngestGeoIpMetadata;
import org.elasticsearch.injection.guice.Inject;
Expand Down Expand Up @@ -48,14 +52,16 @@ public class TransportGetDatabaseConfigurationAction extends TransportNodesActio
List<DatabaseConfigurationMetadata>> {

private final DatabaseNodeService databaseNodeService;
private final ProjectResolver projectResolver;

@Inject
public TransportGetDatabaseConfigurationAction(
TransportService transportService,
ClusterService clusterService,
ThreadPool threadPool,
ActionFilters actionFilters,
DatabaseNodeService databaseNodeService
DatabaseNodeService databaseNodeService,
ProjectResolver projectResolver
) {
super(
GetDatabaseConfigurationAction.NAME,
Expand All @@ -66,6 +72,7 @@ public TransportGetDatabaseConfigurationAction(
threadPool.executor(ThreadPool.Names.MANAGEMENT)
);
this.databaseNodeService = databaseNodeService;
this.projectResolver = projectResolver;
}

protected List<DatabaseConfigurationMetadata> createActionContext(Task task, GetDatabaseConfigurationAction.Request request) {
Expand All @@ -82,25 +89,31 @@ protected List<DatabaseConfigurationMetadata> createActionContext(Task task, Get
"wildcard only supports a single value, please use comma-separated values or a single wildcard value"
);
}

ProjectId projectId = projectResolver.getProjectId();
List<DatabaseConfigurationMetadata> results = new ArrayList<>();
PersistentTasksCustomMetadata tasksMetadata = PersistentTasksCustomMetadata.getPersistentTasksCustomMetadata(
clusterService.state()
PersistentTasksCustomMetadata tasksMetadata = PersistentTasksCustomMetadata.get(
clusterService.state().metadata().getProject(projectId)
);
String geoIpTaskId = GeoIpDownloaderTaskExecutor.getTaskId(projectId, projectResolver.supportsMultipleProjects());
ProjectMetadata projectMetadata = projectResolver.getProjectMetadata(clusterService.state());
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use this projectMetadata for retrieving the PersistentTasksCustomMetadata and its ID for GeoIpDownloaderTaskExecutor.getTaskId()? That way we only need to resolve the project ID once.

for (String id : ids) {
results.addAll(getWebDatabases(tasksMetadata, id));
results.addAll(getMaxmindDatabases(clusterService, id));
results.addAll(getWebDatabases(geoIpTaskId, tasksMetadata, id));
results.addAll(getMaxmindDatabases(projectMetadata, id));
}
return results;
}

/*
* This returns read-only database information about the databases managed by the standard downloader
*/
private static Collection<DatabaseConfigurationMetadata> getWebDatabases(PersistentTasksCustomMetadata tasksMetadata, String id) {
private static Collection<DatabaseConfigurationMetadata> getWebDatabases(
String geoIpTaskId,
PersistentTasksCustomMetadata tasksMetadata,
String id
) {
List<DatabaseConfigurationMetadata> webDatabases = new ArrayList<>();
if (tasksMetadata != null) {
PersistentTasksCustomMetadata.PersistentTask<?> maybeGeoIpTask = tasksMetadata.getTask("geoip-downloader");
PersistentTasksCustomMetadata.PersistentTask<?> maybeGeoIpTask = tasksMetadata.getTask(geoIpTaskId);
if (maybeGeoIpTask != null) {
GeoIpTaskState geoIpTaskState = (GeoIpTaskState) maybeGeoIpTask.getState();
if (geoIpTaskState != null) {
Expand Down Expand Up @@ -137,12 +150,9 @@ private static String getDatabaseNameForFileName(String databaseFileName) {
/*
* This returns information about databases that are downloaded from maxmind.
*/
private static Collection<DatabaseConfigurationMetadata> getMaxmindDatabases(ClusterService clusterService, String id) {
private static Collection<DatabaseConfigurationMetadata> getMaxmindDatabases(ProjectMetadata projectMetadata, String id) {
List<DatabaseConfigurationMetadata> maxmindDatabases = new ArrayList<>();
final IngestGeoIpMetadata geoIpMeta = clusterService.state()
.metadata()
.getProject()
.custom(IngestGeoIpMetadata.TYPE, IngestGeoIpMetadata.EMPTY);
final IngestGeoIpMetadata geoIpMeta = projectMetadata.custom(IngestGeoIpMetadata.TYPE, IngestGeoIpMetadata.EMPTY);
if (Regex.isSimpleMatchPattern(id)) {
for (Map.Entry<String, DatabaseConfigurationMetadata> entry : geoIpMeta.getDatabases().entrySet()) {
if (Regex.simpleMatch(id, entry.getKey())) {
Expand Down