java.lang.Object
org.elasticsearch.xpack.core.ml.utils.MlIndexAndAlias
Utils to create an ML index with alias ready for rollover with a 6-digit suffix
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic IndicesAliasesRequestBuilderaddIndexAliasesRequests(IndicesAliasesRequestBuilder aliasRequestBuilder, String oldIndex, String newIndex, ClusterState clusterState) Adds alias actions to a request builder to move ML job aliases from an old index to a new one after a rollover.static voidcreateAliasForRollover(Client client, String indexName, String aliasName, ActionListener<IndicesAliasesResponse> listener) Creates a hidden alias for an index, typically used as a rollover target.static voidcreateIndexAndAliasIfNecessary(Client client, ClusterState clusterState, IndexNameExpressionResolver resolver, String indexPatternPrefix, String indexNumber, String alias, org.elasticsearch.core.TimeValue masterNodeTimeout, ActiveShardCount waitForShardCount, ActionListener<Boolean> finalListener) Same as createIndexAndAliasIfNecessary but with the first concrete index number specified.static voidcreateIndexAndAliasIfNecessary(Client client, ClusterState clusterState, IndexNameExpressionResolver resolver, String indexPatternPrefix, String alias, org.elasticsearch.core.TimeValue masterNodeTimeout, ActiveShardCount waitForShardCount, ActionListener<Boolean> finalListener) Creates the first index with a name of the givenindexPatternPrefixfollowed by "-000001", if the index is missing.static IndicesAliasesRequestBuilderstatic voidcreateSystemIndexIfNecessary(Client client, ClusterState clusterState, SystemIndexDescriptor descriptor, org.elasticsearch.core.TimeValue masterNodeTimeout, ActionListener<Boolean> finalListener) Creates a system index based on the provided descriptor if it does not already exist.static StringensureValidResultsIndexName(String indexName) static booleanhas6DigitSuffix(String indexName) Checks if an index name ends with a 6-digit suffix (e.g., "-000001").static booleanTrue if the version is read *and* write compatible not just read only compatiblestatic voidinstallIndexTemplateIfRequired(ClusterState clusterState, Client client, int templateVersion, TransportPutComposableIndexTemplateAction.Request templateRequest, ActionListener<Boolean> listener) static voidinstallIndexTemplateIfRequired(ClusterState clusterState, Client client, IndexTemplateConfig templateConfig, org.elasticsearch.core.TimeValue masterTimeout, ActionListener<Boolean> listener) Installs the index template specified bytemplateConfigif it is not in already installed inclusterState.static booleanisAnomaliesReadAlias(String aliasName) Determines if an alias name is an ML anomalies read alias.static booleanisAnomaliesSharedIndex(String indexName) Checks if an index name matches the pattern for the default ML anomalies indices (e.g., ".ml-anomalies-shared-000001").static booleanisAnomaliesWriteAlias(String aliasName) Determines if an alias name is an ML anomalies write alias.static StringlatestIndex(String[] concreteIndices) Returns the latest index.static StringlatestIndexMatchingBaseName(String index, IndexNameExpressionResolver expressionResolver, ClusterState latestState) Strip any suffix from the index name and find any other indices that match the base name.static voidrollover(Client client, RolloverRequest rolloverRequest, ActionListener<String> listener) Executes a rollover request.static voidupdateAliases(IndicesAliasesRequestBuilder request, ActionListener<Boolean> listener) Executes a preparedIndicesAliasesRequestBuilderand notifies the listener of the result.static voidupdateWriteAlias(Client client, String alias, String currentIndex, String newIndex, org.elasticsearch.core.TimeValue masterNodeTimeout, ActionListener<Boolean> listener) Creates or moves a write alias from one index to another.
-
Field Details
-
BWC_MAPPINGS_VERSION
ML managed index mappings used to be updated based on the product version. They are now updated based on per-index mappings versions. However, older nodes will still look for a product version in the mappings metadata, so we have to put something in that field that will allow the older node to realise that the mappings are ahead of what it knows about. The easiest solution is to hardcode 8.11.0 in this field, because any node from 8.10.0 onwards should be using per-index mappings versions to determine whether mappings are up-to-date.- See Also:
-
FIRST_INDEX_SIX_DIGIT_SUFFIX
- See Also:
-
ROLLOVER_ALIAS_SUFFIX
- See Also:
-
-
Method Details
-
createIndexAndAliasIfNecessary
public static void createIndexAndAliasIfNecessary(Client client, ClusterState clusterState, IndexNameExpressionResolver resolver, String indexPatternPrefix, String alias, org.elasticsearch.core.TimeValue masterNodeTimeout, ActiveShardCount waitForShardCount, ActionListener<Boolean> finalListener) Creates the first index with a name of the givenindexPatternPrefixfollowed by "-000001", if the index is missing. Adds analiasto that index if it was created, or to the index with the highest suffix if the index did not have to be created. The listener is notified with abooleanthat informs whether the index or the alias were created. If the index is created, the listener is not called until the index is ready to use via the supplied alias, so that a method that receives a success response from this method can safely use the index immediately. -
createIndexAndAliasIfNecessary
public static void createIndexAndAliasIfNecessary(Client client, ClusterState clusterState, IndexNameExpressionResolver resolver, String indexPatternPrefix, String indexNumber, String alias, org.elasticsearch.core.TimeValue masterNodeTimeout, ActiveShardCount waitForShardCount, ActionListener<Boolean> finalListener) Same as createIndexAndAliasIfNecessary but with the first concrete index number specified. -
createSystemIndexIfNecessary
public static void createSystemIndexIfNecessary(Client client, ClusterState clusterState, SystemIndexDescriptor descriptor, org.elasticsearch.core.TimeValue masterNodeTimeout, ActionListener<Boolean> finalListener) Creates a system index based on the provided descriptor if it does not already exist.The check for existence is simple and will return the listener on the calling thread if successful. If the index needs to be created an async call will be made and this method will wait for the index to reach at least a yellow health status before notifying the listener, ensuring it is ready for use upon a successful response. A
ResourceAlreadyExistsExceptionduring creation is handled gracefully and treated as a success.- Parameters:
client- The client to use for the create index request.clusterState- The current cluster state, used for the initial existence check.descriptor- The descriptor containing the index name, settings, and mappings.masterNodeTimeout- The timeout for waiting on the master node.finalListener- Async listener
-
updateWriteAlias
public static void updateWriteAlias(Client client, String alias, @Nullable String currentIndex, String newIndex, org.elasticsearch.core.TimeValue masterNodeTimeout, ActionListener<Boolean> listener) Creates or moves a write alias from one index to another.- Parameters:
client- The client to use for the add alias request.alias- The alias to update.currentIndex- The index the alias is currently pointing to.newIndex- The new index the alias should point to.masterNodeTimeout- The timeout for waiting on the master node.listener- Async listener
-
installIndexTemplateIfRequired
public static void installIndexTemplateIfRequired(ClusterState clusterState, Client client, IndexTemplateConfig templateConfig, org.elasticsearch.core.TimeValue masterTimeout, ActionListener<Boolean> listener) Installs the index template specified bytemplateConfigif it is not in already installed inclusterState.The check for presence is simple and will return the listener on the calling thread if successful. If the template has to be installed an async call will be made.
- Parameters:
clusterState- The cluster stateclient- For putting the templatetemplateConfig- The configlistener- Async listener
-
installIndexTemplateIfRequired
public static void installIndexTemplateIfRequired(ClusterState clusterState, Client client, int templateVersion, TransportPutComposableIndexTemplateAction.Request templateRequest, ActionListener<Boolean> listener) SeeinstallIndexTemplateIfRequired(ClusterState, Client, IndexTemplateConfig, TimeValue, ActionListener). Overload takes aPutIndexTemplateRequestinstead ofIndexTemplateConfig- Parameters:
clusterState- The cluster stateclient- For putting the templatetemplateRequest- The Put template requestlistener- Async listener
-
ensureValidResultsIndexName
-
has6DigitSuffix
Checks if an index name ends with a 6-digit suffix (e.g., "-000001").- Parameters:
indexName- The name of the index to check.- Returns:
trueif the index name has a 6-digit suffix,falseotherwise.
-
latestIndex
Returns the latest index. Latest is the index with the highest 6 digit suffix.- Parameters:
concreteIndices- List of index names- Returns:
- The latest index by index name version suffix
-
indexIsReadWriteCompatibleInV9
True if the version is read *and* write compatible not just read only compatible -
latestIndexMatchingBaseName
public static String latestIndexMatchingBaseName(String index, IndexNameExpressionResolver expressionResolver, ClusterState latestState) Strip any suffix from the index name and find any other indices that match the base name. Then return the latest index from the matching ones.- Parameters:
index- The index to checkexpressionResolver- The expression resolverlatestState- The latest cluster state- Returns:
- The latest index that matches the base name of the given index
-
rollover
public static void rollover(Client client, RolloverRequest rolloverRequest, ActionListener<String> listener) Executes a rollover request. It handlesResourceAlreadyExistsExceptiongracefully by treating it as a success and returning the name of the existing index.- Parameters:
client- The client to use for the rollover request.rolloverRequest- The rollover request to execute.listener- A listener that will be notified with the name of the new (or pre-existing) index on success, or an exception on failure.
-
createRolloverAliasAndNewIndexName
-
createIndicesAliasesRequestBuilder
-
createAliasForRollover
public static void createAliasForRollover(Client client, String indexName, String aliasName, ActionListener<IndicesAliasesResponse> listener) Creates a hidden alias for an index, typically used as a rollover target.- Parameters:
client- The client to use for the alias request.indexName- The name of the index to which the alias will be added.aliasName- The name of the alias to create.listener- A listener that will be notified with the response.
-
updateAliases
public static void updateAliases(IndicesAliasesRequestBuilder request, ActionListener<Boolean> listener) Executes a preparedIndicesAliasesRequestBuilderand notifies the listener of the result.- Parameters:
request- The prepared request builder containing alias actions.listener- A listener that will be notified withtrueon success.
-
addIndexAliasesRequests
public static IndicesAliasesRequestBuilder addIndexAliasesRequests(IndicesAliasesRequestBuilder aliasRequestBuilder, String oldIndex, String newIndex, ClusterState clusterState) Adds alias actions to a request builder to move ML job aliases from an old index to a new one after a rollover. This includes moving the write alias and re-creating the filtered read aliases on the new index.- Parameters:
aliasRequestBuilder- The request builder to add actions to.oldIndex- The index from which aliases are being moved.newIndex- The new index to which aliases will be moved.clusterState- The current cluster state, used to inspect existing aliases on the old index.- Returns:
- The modified
IndicesAliasesRequestBuilder.
-
isAnomaliesWriteAlias
Determines if an alias name is an ML anomalies write alias.- Parameters:
aliasName- The alias name to check.- Returns:
trueif the name matches the write alias pattern,falseotherwise.
-
isAnomaliesReadAlias
Determines if an alias name is an ML anomalies read alias.- Parameters:
aliasName- The alias name to check.- Returns:
trueif the name matches the read alias pattern,falseotherwise.
-