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 IndicesAliasesRequestBuilderaddResultsIndexRolloverAliasActions(IndicesAliasesRequestBuilder aliasRequestBuilder, String newIndex, ClusterState clusterState, List<String> currentJobResultsIndices) Adds alias actions to a request builder to move ML job aliases from an old index to a new one after a rollover.static IndicesAliasesRequestBuilderaddStateIndexRolloverAliasActions(IndicesAliasesRequestBuilder aliasRequestBuilder, String newIndex, ClusterState clusterState, List<String> allStateIndices) Adds alias actions to a request builder to move the ML state write alias from an old index to a new one after a rollover.static StringbaseIndexName(String index) Returns the given index name without its 6 digit suffix.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 IndicesAliasesRequestBuilderCreates a pre-configuredIndicesAliasesRequestBuilderwith default timeouts.Generates a temporary rollover alias and a potential new index name based on a source index name.static 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) Ensures a given index name is valid for ML results by appending the 6-digit suffix if it is missing.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 String[]indicesMatchingBasename(String baseIndexName, IndexNameExpressionResolver expressionResolver, ClusterState latestState) Returns an array of indices that match the given base index name.static 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 booleanisAnomaliesStateIndex(String indexName) Checks if an index name matches the pattern for the ML anomalies state indices (e.g., ".ml-state-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 voidsortIndices(List<String> indices) Sorts the given list of indices based on their 6 digit suffix.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
Ensures a given index name is valid for ML results by appending the 6-digit suffix if it is missing.- Parameters:
indexName- The index name to validate.- Returns:
- The validated index name, with the suffix added if it was missing.
-
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.
-
isAnomaliesStateIndex
Checks if an index name matches the pattern for the ML anomalies state indices (e.g., ".ml-state-000001").- Parameters:
indexName- The name of the index to check.- Returns:
trueif the index is an anomalies state index,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
-
sortIndices
Sorts the given list of indices based on their 6 digit suffix.- Parameters:
indices- List of index names
-
indexIsReadWriteCompatibleInV9
True if the version is read *and* write compatible not just read only compatible -
baseIndexName
Returns the given index name without its 6 digit suffix.- Parameters:
index- The index name to check- Returns:
- The base index name, without the 6 digit suffix.
-
indicesMatchingBasename
public static String[] indicesMatchingBasename(String baseIndexName, IndexNameExpressionResolver expressionResolver, ClusterState latestState) Returns an array of indices that match the given base index name.- Parameters:
baseIndexName- The base part of an index name, without the 6 digit suffix.expressionResolver- The expression resolverlatestState- The latest cluster state- Returns:
- An array of matching indices.
-
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
public static org.elasticsearch.core.Tuple<String,String> createRolloverAliasAndNewIndexName(String index) Generates a temporary rollover alias and a potential new index name based on a source index name. This is a preparatory step for a rollover action. If the source index already has a 6-digit suffix, the new index name will be null, allowing the rollover API to auto-increment the suffix.- Parameters:
index- The name of the index that is a candidate for rollover.- Returns:
- A
Tuplewherev1is the generated rollover alias andv2is the new index name (ornullif rollover can auto-determine it).
-
createIndicesAliasesRequestBuilder
Creates a pre-configuredIndicesAliasesRequestBuilderwith default timeouts.- Parameters:
client- The client to use for the request.- Returns:
- A new
IndicesAliasesRequestBuilder.
-
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.
-
addStateIndexRolloverAliasActions
public static IndicesAliasesRequestBuilder addStateIndexRolloverAliasActions(IndicesAliasesRequestBuilder aliasRequestBuilder, String newIndex, ClusterState clusterState, List<String> allStateIndices) Adds alias actions to a request builder to move the ML state write alias from an old index to a new one after a rollover. This method is robust and will move the correct alias regardless of the current alias state on the old index.- Parameters:
aliasRequestBuilder- The request builder to add actions to.newIndex- The new index to which the alias will be moved.clusterState- The current cluster state, used to inspect existing aliases on the old index.allStateIndices- A list of all current .ml-state indices- Returns:
- The modified
IndicesAliasesRequestBuilder.
-
addResultsIndexRolloverAliasActions
public static IndicesAliasesRequestBuilder addResultsIndexRolloverAliasActions(IndicesAliasesRequestBuilder aliasRequestBuilder, String newIndex, ClusterState clusterState, List<String> currentJobResultsIndices) 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.newIndex- The new index to which aliases will be moved.clusterState- The current cluster state, used to inspect existing aliases on the old index.currentJobResultsIndices- A list of all current .ml-anomalies indices- 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.
-