forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DRAFT] Introduce PluginMetadataClient
Reference RFC: opensearch-project#13274
- Loading branch information
Showing
16 changed files
with
1,063 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
server/src/main/java/org/opensearch/plugins/SdkAwarePlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugins; | ||
|
||
import org.opensearch.plugins.sdk.PluginMetadataClient; | ||
|
||
/** | ||
* SdkAwarePlugin is an additional extension point for {@link Plugin}'s for receiving | ||
* OpenSearch abstractions. | ||
*/ | ||
public interface SdkAwarePlugin { | ||
|
||
void setupSdkPlugin(Dependencies dependencies); | ||
|
||
/** | ||
* Dependencies is a simple container class holding plugin sdk dependencies. | ||
*/ | ||
public static interface Dependencies { | ||
PluginMetadataClient getPluginMetadataClient(); | ||
} | ||
} |
149 changes: 149 additions & 0 deletions
149
server/src/main/java/org/opensearch/plugins/sdk/DefaultPluginMetadataClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugins.sdk; | ||
|
||
import org.opensearch.cluster.ClusterState; | ||
import org.opensearch.cluster.metadata.IndexMetadata; | ||
import org.opensearch.cluster.metadata.IndexNameExpressionResolver; | ||
import org.opensearch.cluster.metadata.MappingMetadata; | ||
import org.opensearch.cluster.metadata.Metadata; | ||
import org.opensearch.cluster.service.ClusterService; | ||
import org.opensearch.common.CheckedSupplier; | ||
import org.opensearch.common.annotation.ExperimentalApi; | ||
import org.opensearch.common.settings.ClusterSettings; | ||
import org.opensearch.common.settings.IndexScopedSettings; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.core.index.Index; | ||
import org.opensearch.plugins.sdk.actions.ConcreteIndicesRequest; | ||
import org.opensearch.plugins.sdk.actions.ConcreteIndicesResponse; | ||
import org.opensearch.plugins.sdk.actions.GetIndexMappingMetadataRequest; | ||
import org.opensearch.plugins.sdk.actions.GetIndexMappingMetadataResponse; | ||
import org.opensearch.plugins.sdk.actions.GetIndexSettingsRequest; | ||
import org.opensearch.plugins.sdk.actions.GetIndexSettingsResponse; | ||
import org.opensearch.plugins.sdk.actions.GetSystemSettingsRequest; | ||
import org.opensearch.plugins.sdk.actions.GetSystemSettingsResponse; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
/** | ||
* DefaultPluginMetadataClient is the de-facto implementation of the | ||
* PluginMetadataClient. | ||
* | ||
* @see PluginMetadataClient | ||
*/ | ||
@ExperimentalApi | ||
public class DefaultPluginMetadataClient implements PluginMetadataClient { | ||
|
||
private final ClusterService clusterService; | ||
private final IndexNameExpressionResolver indexNameExpressionResolver; | ||
private final IndexScopedSettings indexScopedSettings; | ||
|
||
public DefaultPluginMetadataClient(ClusterService clusterService, | ||
IndexNameExpressionResolver indexNameExpressionResolver, | ||
IndexScopedSettings indexScopedSettings) { | ||
|
||
this.clusterService = clusterService; | ||
this.indexNameExpressionResolver = indexNameExpressionResolver; | ||
this.indexScopedSettings = indexScopedSettings; | ||
} | ||
|
||
@Override | ||
public CompletionStage<ConcreteIndicesResponse> concreteIndices( | ||
ConcreteIndicesRequest request | ||
) { | ||
return synchronously(() -> { | ||
ClusterState state = clusterService.state(); | ||
|
||
Index[] result = indexNameExpressionResolver.concreteIndices( | ||
state, | ||
request.indicesOptions(), | ||
request.indexExpressions()); | ||
|
||
return ConcreteIndicesResponse.builder() | ||
.indices(result) | ||
.build(); | ||
}); | ||
} | ||
|
||
@Override | ||
public CompletionStage<GetSystemSettingsResponse> getSystemSettings( | ||
GetSystemSettingsRequest request | ||
) { | ||
return synchronously(() -> { | ||
ClusterSettings clusterSettings = clusterService.getClusterSettings(); | ||
Settings settings = clusterSettings.toSettings(); | ||
Set<String> settingKeys = Set.of(request.settings()); | ||
settings = settings.filter(settingKeys::contains); | ||
return GetSystemSettingsResponse.builder() | ||
.settings(settings) | ||
.build(); | ||
}); | ||
} | ||
|
||
@Override | ||
public CompletionStage<GetIndexMappingMetadataResponse> getIndexMappingMetadata( | ||
GetIndexMappingMetadataRequest request | ||
) { | ||
return synchronously(() -> { | ||
ClusterState state = clusterService.state(); | ||
Metadata metadata = state.metadata(); | ||
Map<Index, MappingMetadata> result = new HashMap<>(); | ||
for (Index index : request.indices()) { | ||
MappingMetadata mappingMetadata = metadata.getIndexSafe(index).mapping(); | ||
result.put(index, mappingMetadata); | ||
} | ||
return GetIndexMappingMetadataResponse.builder() | ||
.mappingMetadata(result) | ||
.build(); | ||
}); | ||
} | ||
|
||
@Override | ||
public CompletionStage<GetIndexSettingsResponse> getIndexSettings( | ||
GetIndexSettingsRequest request | ||
) { | ||
return synchronously(() -> { | ||
ClusterState state = clusterService.state(); | ||
Settings currentDefaultIndexScopedSettings = indexScopedSettings.toSettings(); | ||
Set<String> settingsKeys = Set.of(request.settings()); | ||
Map<Index, Settings> result = new HashMap<>(); | ||
|
||
for (Index index : request.indices()) { | ||
IndexMetadata indexMetadata = state.metadata().getIndexSafe(index); | ||
|
||
Settings indexSettings = indexMetadata.getSettings(); | ||
|
||
Settings resultSettings = Settings.builder() | ||
.put(currentDefaultIndexScopedSettings) | ||
.put(indexSettings) | ||
.build() | ||
.filter(settingsKeys::contains); | ||
|
||
result.put(index, resultSettings); | ||
} | ||
|
||
return GetIndexSettingsResponse.builder() | ||
.settings(result) | ||
.build(); | ||
}); | ||
} | ||
|
||
private static <T> CompletionStage<T> synchronously(CheckedSupplier<T, Exception> supplier) { | ||
try { | ||
T result = supplier.get(); | ||
return CompletableFuture.completedStage(result); | ||
} catch (Exception e) { | ||
return CompletableFuture.failedStage(e); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
server/src/main/java/org/opensearch/plugins/sdk/PluginMetadataClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.plugins.sdk; | ||
|
||
import org.opensearch.action.support.IndicesOptions; | ||
import org.opensearch.cluster.ClusterState; | ||
import org.opensearch.common.annotation.ExperimentalApi; | ||
import org.opensearch.common.settings.Setting; | ||
import org.opensearch.plugins.sdk.actions.ConcreteIndicesRequest; | ||
import org.opensearch.plugins.sdk.actions.ConcreteIndicesResponse; | ||
import org.opensearch.plugins.sdk.actions.GetIndexMappingMetadataRequest; | ||
import org.opensearch.plugins.sdk.actions.GetIndexMappingMetadataResponse; | ||
import org.opensearch.plugins.sdk.actions.GetIndexSettingsRequest; | ||
import org.opensearch.plugins.sdk.actions.GetIndexSettingsResponse; | ||
import org.opensearch.plugins.sdk.actions.GetSystemSettingsRequest; | ||
import org.opensearch.plugins.sdk.actions.GetSystemSettingsResponse; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
/** | ||
* PluginMetadataClient provides an abstract interface for plugins to access OpenSearch core cluster metadata. | ||
*/ | ||
@ExperimentalApi | ||
public interface PluginMetadataClient { | ||
|
||
/** | ||
* Resolves the concrete indices for a given set of index name expressions. | ||
* | ||
* This is an abstraction over IndexNameExpressionResolver. | ||
* | ||
* @see org.opensearch.cluster.metadata.IndexNameExpressionResolver#concreteIndexNames(ClusterState, IndicesOptions, String...) | ||
* | ||
* @param request The index names to resolve and associated resolution options | ||
* @return The resolved concrete index names | ||
*/ | ||
CompletionStage<ConcreteIndicesResponse> concreteIndices(ConcreteIndicesRequest request); | ||
|
||
/** | ||
* Gets the system settings for a particular set of setting. | ||
* | ||
* This is an abstraction over ClusterSetting access. | ||
* | ||
* @see org.opensearch.common.settings.ClusterSettings#get(Setting) | ||
* | ||
* @param request The settings to retrieve. | ||
* @return The system settings. | ||
*/ | ||
CompletionStage<GetSystemSettingsResponse> getSystemSettings(GetSystemSettingsRequest request); | ||
|
||
/** | ||
* Gets the mapping metadata of the provided indices. | ||
* | ||
* This is an abstraction over ClusterState Metadata access. | ||
* | ||
* @see org.opensearch.cluster.metadata.Metadata#indices() | ||
* | ||
* @param request The indices to retrieve mapping metadata for. | ||
* @return The mapping metadata for the requested indices. | ||
*/ | ||
CompletionStage<GetIndexMappingMetadataResponse> getIndexMappingMetadata(GetIndexMappingMetadataRequest request); | ||
|
||
/** | ||
* Gets the index settings for a particular set of indices. | ||
* | ||
* This is an abstraction over ClusterState Metadata access. | ||
* | ||
* @see org.opensearch.cluster.metadata.IndexMetadata#getSettings() | ||
* | ||
* @param request The indices to retrieve mapping metadata for. | ||
* @return The mapping metadata for the requested indices. | ||
*/ | ||
CompletionStage<GetIndexSettingsResponse> getIndexSettings(GetIndexSettingsRequest request); | ||
} |
Oops, something went wrong.