Class MlIndexAndAlias

java.lang.Object
org.elasticsearch.xpack.core.ml.utils.MlIndexAndAlias

public final class MlIndexAndAlias extends Object
Utils to create an ML index with alias ready for rollover with a 6-digit suffix
  • Field Details

    • BWC_MAPPINGS_VERSION

      public static final String 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

      public static final String FIRST_INDEX_SIX_DIGIT_SUFFIX
      See Also:
    • ROLLOVER_ALIAS_SUFFIX

      public static final String 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 given indexPatternPrefix followed by "-000001", if the index is missing. Adds an alias to 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 a boolean that 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 ResourceAlreadyExistsException during 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 by templateConfig if it is not in already installed in clusterState.

      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 state
      client - For putting the template
      templateConfig - The config
      listener - Async listener
    • installIndexTemplateIfRequired

      public static void installIndexTemplateIfRequired(ClusterState clusterState, Client client, int templateVersion, TransportPutComposableIndexTemplateAction.Request templateRequest, ActionListener<Boolean> listener)
      See installIndexTemplateIfRequired(ClusterState, Client, IndexTemplateConfig, TimeValue, ActionListener). Overload takes a PutIndexTemplateRequest instead of IndexTemplateConfig
      Parameters:
      clusterState - The cluster state
      client - For putting the template
      templateRequest - The Put template request
      listener - Async listener
    • ensureValidResultsIndexName

      public static String ensureValidResultsIndexName(String indexName)
    • has6DigitSuffix

      public static boolean has6DigitSuffix(String indexName)
      Checks if an index name ends with a 6-digit suffix (e.g., "-000001").
      Parameters:
      indexName - The name of the index to check.
      Returns:
      true if the index name has a 6-digit suffix, false otherwise.
    • isAnomaliesSharedIndex

      public static boolean isAnomaliesSharedIndex(String indexName)
      Checks if an index name matches the pattern for the default ML anomalies indices (e.g., ".ml-anomalies-shared-000001").
      Parameters:
      indexName - The name of the index to check.
      Returns:
      true if the index is a shared anomalies index, false otherwise.
    • latestIndex

      public static String latestIndex(String[] concreteIndices)
      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

      public static boolean indexIsReadWriteCompatibleInV9(IndexVersion version)
      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 check
      expressionResolver - The expression resolver
      latestState - 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 handles ResourceAlreadyExistsException gracefully 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)
    • createIndicesAliasesRequestBuilder

      public static IndicesAliasesRequestBuilder createIndicesAliasesRequestBuilder(Client client)
    • 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 prepared IndicesAliasesRequestBuilder and notifies the listener of the result.
      Parameters:
      request - The prepared request builder containing alias actions.
      listener - A listener that will be notified with true on 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

      public static boolean isAnomaliesWriteAlias(String aliasName)
      Determines if an alias name is an ML anomalies write alias.
      Parameters:
      aliasName - The alias name to check.
      Returns:
      true if the name matches the write alias pattern, false otherwise.
    • isAnomaliesReadAlias

      public static boolean isAnomaliesReadAlias(String aliasName)
      Determines if an alias name is an ML anomalies read alias.
      Parameters:
      aliasName - The alias name to check.
      Returns:
      true if the name matches the read alias pattern, false otherwise.