Skip to content
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

[ML] Removing legacy code from ML/transform auditor #79434

Merged
merged 3 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
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 @@ -9,10 +9,8 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.template.put.PutComposableIndexTemplateAction;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
Expand All @@ -24,7 +22,6 @@
import org.elasticsearch.xcontent.NamedXContentRegistry;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xcontent.json.JsonXContent;
import org.elasticsearch.xpack.core.ml.utils.MlIndexAndAlias;
import org.elasticsearch.xpack.core.template.IndexTemplateConfig;
Expand Down Expand Up @@ -52,8 +49,6 @@ public abstract class AbstractAuditor<T extends AbstractAuditMessage> {
private final String nodeName;
private final String auditIndex;
private final String templateName;
private final Version versionComposableTemplateExpected;
private final Supplier<PutIndexTemplateRequest> legacyTemplateSupplier;
private final Supplier<PutComposableIndexTemplateAction.Request> templateSupplier;
private final AbstractAuditMessageFactory<T> messageFactory;
private final AtomicBoolean hasLatestTemplate;
Expand All @@ -64,16 +59,12 @@ public abstract class AbstractAuditor<T extends AbstractAuditMessage> {

protected AbstractAuditor(OriginSettingClient client,
String auditIndex,
Version versionComposableTemplateExpected,
IndexTemplateConfig legacyTemplateConfig,
IndexTemplateConfig templateConfig,
String nodeName,
AbstractAuditMessageFactory<T> messageFactory,
ClusterService clusterService) {

this(client, auditIndex, templateConfig.getTemplateName(), versionComposableTemplateExpected,
() -> new PutIndexTemplateRequest(legacyTemplateConfig.getTemplateName())
.source(legacyTemplateConfig.loadBytes(), XContentType.JSON).masterNodeTimeout(MASTER_TIMEOUT),
this(client, auditIndex, templateConfig.getTemplateName(),
() -> {
try {
return new PutComposableIndexTemplateAction.Request(templateConfig.getTemplateName())
Expand All @@ -90,17 +81,13 @@ protected AbstractAuditor(OriginSettingClient client,
protected AbstractAuditor(OriginSettingClient client,
String auditIndex,
String templateName,
Version versionComposableTemplateExpected,
Supplier<PutIndexTemplateRequest> legacyTemplateSupplier,
Supplier<PutComposableIndexTemplateAction.Request> templateSupplier,
String nodeName,
AbstractAuditMessageFactory<T> messageFactory,
ClusterService clusterService) {
this.client = Objects.requireNonNull(client);
this.auditIndex = Objects.requireNonNull(auditIndex);
this.templateName = Objects.requireNonNull(templateName);
this.versionComposableTemplateExpected = versionComposableTemplateExpected;
this.legacyTemplateSupplier = Objects.requireNonNull(legacyTemplateSupplier);
this.templateSupplier = Objects.requireNonNull(templateSupplier);
this.messageFactory = Objects.requireNonNull(messageFactory);
this.clusterService = Objects.requireNonNull(clusterService);
Expand Down Expand Up @@ -136,7 +123,7 @@ protected void indexDoc(ToXContent toXContent) {
return;
}

if (MlIndexAndAlias.hasIndexTemplate(clusterService.state(), templateName, templateName, versionComposableTemplateExpected)) {
if (MlIndexAndAlias.hasIndexTemplate(clusterService.state(), templateName)) {
synchronized (this) {
// synchronized so nothing can be added to backlog while this value changes
hasLatestTemplate.set(true);
Expand Down Expand Up @@ -177,8 +164,8 @@ protected void indexDoc(ToXContent toXContent) {

// stop multiple invocations
if (putTemplateInProgress.compareAndSet(false, true)) {
MlIndexAndAlias.installIndexTemplateIfRequired(clusterService.state(), client, versionComposableTemplateExpected,
legacyTemplateSupplier.get(), templateSupplier.get(), putTemplateListener);
MlIndexAndAlias.installIndexTemplateIfRequired(clusterService.state(), client,
templateSupplier.get(), putTemplateListener);
}
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.ResourceAlreadyExistsException;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
Expand All @@ -22,7 +21,6 @@
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.template.put.PutComposableIndexTemplateAction;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.Client;
Expand All @@ -35,7 +33,6 @@
import org.elasticsearch.index.Index;
import org.elasticsearch.xcontent.DeprecationHandler;
import org.elasticsearch.xcontent.NamedXContentRegistry;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xcontent.json.JsonXContent;
import org.elasticsearch.indices.SystemIndexDescriptor;
import org.elasticsearch.xpack.core.template.IndexTemplateConfig;
Expand Down Expand Up @@ -311,24 +308,18 @@ private static void updateWriteAlias(Client client,
public static void installIndexTemplateIfRequired(
ClusterState clusterState,
Client client,
Version versionComposableTemplateExpected,
IndexTemplateConfig legacyTemplateConfig,
IndexTemplateConfig templateConfig,
TimeValue masterTimeout,
ActionListener<Boolean> listener
) {
String legacyTemplateName = legacyTemplateConfig.getTemplateName();
String templateName = templateConfig.getTemplateName();

// The check for existence of the template is against the cluster state, so very cheap
if (hasIndexTemplate(clusterState, legacyTemplateName, templateName, versionComposableTemplateExpected)) {
if (hasIndexTemplate(clusterState, templateName)) {
listener.onResponse(true);
return;
}

PutIndexTemplateRequest legacyRequest = new PutIndexTemplateRequest(legacyTemplateName)
.source(legacyTemplateConfig.loadBytes(), XContentType.JSON).masterNodeTimeout(masterTimeout);

PutComposableIndexTemplateAction.Request request;
try {
request = new PutComposableIndexTemplateAction.Request(templateConfig.getTemplateName())
Expand All @@ -339,12 +330,11 @@ public static void installIndexTemplateIfRequired(
throw new ElasticsearchParseException("unable to parse composable template " + templateConfig.getTemplateName(), e);
}

installIndexTemplateIfRequired(clusterState, client, versionComposableTemplateExpected, legacyRequest, request, listener);
installIndexTemplateIfRequired(clusterState, client, request, listener);
}

/**
* See {@link #installIndexTemplateIfRequired(ClusterState, Client, Version, IndexTemplateConfig, IndexTemplateConfig, TimeValue,
* ActionListener)}.
* See {@link #installIndexTemplateIfRequired(ClusterState, Client, IndexTemplateConfig, TimeValue, ActionListener)}.
*
* Overload takes a {@code PutIndexTemplateRequest} instead of {@code IndexTemplateConfig}
*
Expand All @@ -356,51 +346,29 @@ public static void installIndexTemplateIfRequired(
public static void installIndexTemplateIfRequired(
ClusterState clusterState,
Client client,
Version versionComposableTemplateExpected,
PutIndexTemplateRequest legacyTemplateRequest,
PutComposableIndexTemplateAction.Request templateRequest,
ActionListener<Boolean> listener
) {
// The check for existence of the template is against the cluster state, so very cheap
if (hasIndexTemplate(clusterState, legacyTemplateRequest.name(), templateRequest.name(), versionComposableTemplateExpected)) {
if (hasIndexTemplate(clusterState, templateRequest.name())) {
listener.onResponse(true);
return;
}

if (versionComposableTemplateExpected != null &&
clusterState.nodes().getMinNodeVersion().onOrAfter(versionComposableTemplateExpected)) {
ActionListener<AcknowledgedResponse> innerListener = ActionListener.wrap(
response -> {
if (response.isAcknowledged() == false) {
logger.warn("error adding template [{}], request was not acknowledged", templateRequest.name());
}
listener.onResponse(response.isAcknowledged());
},
listener::onFailure);

executeAsyncWithOrigin(client, ML_ORIGIN, PutComposableIndexTemplateAction.INSTANCE, templateRequest, innerListener);
} else {
ActionListener<AcknowledgedResponse> innerListener = ActionListener.wrap(
response -> {
if (response.isAcknowledged() == false) {
logger.warn("error adding legacy template [{}], request was not acknowledged", legacyTemplateRequest.name());
}
listener.onResponse(response.isAcknowledged());
},
listener::onFailure);
ActionListener<AcknowledgedResponse> innerListener = ActionListener.wrap(
response -> {
if (response.isAcknowledged() == false) {
logger.warn("error adding template [{}], request was not acknowledged", templateRequest.name());
}
listener.onResponse(response.isAcknowledged());
},
listener::onFailure);

executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, legacyTemplateRequest, innerListener,
client.admin().indices()::putTemplate);
}
executeAsyncWithOrigin(client, ML_ORIGIN, PutComposableIndexTemplateAction.INSTANCE, templateRequest, innerListener);
}

public static boolean hasIndexTemplate(ClusterState state, String legacyTemplateName,
String templateName, Version versionComposableTemplateExpected) {
if (versionComposableTemplateExpected != null && state.nodes().getMinNodeVersion().onOrAfter(versionComposableTemplateExpected)) {
return state.getMetadata().templatesV2().containsKey(templateName);
} else {
return state.getMetadata().getTemplates().containsKey(legacyTemplateName);
}
public static boolean hasIndexTemplate(ClusterState state, String templateName) {
return state.getMetadata().templatesV2().containsKey(templateName);
}

public static boolean hasIndex(ClusterState state, String index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,26 +121,14 @@ public static String replaceVariable(String input, String variable, String value
* Checks if a versioned template exists, and if it exists checks if the version is greater than or equal to the current version.
* @param templateName Name of the index template
* @param state Cluster state
* @param versionComposableTemplateExpected In which version of Elasticsearch did this template switch to being a composable template?
* <code>null</code> means the template hasn't been switched yet.
*/
public static boolean checkTemplateExistsAndVersionIsGTECurrentVersion(String templateName, ClusterState state,
Version versionComposableTemplateExpected) {
if (versionComposableTemplateExpected != null && state.nodes().getMinNodeVersion().onOrAfter(versionComposableTemplateExpected)) {
ComposableIndexTemplate templateMetadata = state.metadata().templatesV2().get(templateName);
if (templateMetadata == null) {
return false;
}

return templateMetadata.version() != null && templateMetadata.version() >= Version.CURRENT.id;
} else {
IndexTemplateMetadata templateMetadata = state.metadata().templates().get(templateName);
if (templateMetadata == null) {
return false;
}

return templateMetadata.version() != null && templateMetadata.version() >= Version.CURRENT.id;
public static boolean checkTemplateExistsAndVersionIsGTECurrentVersion(String templateName, ClusterState state) {
ComposableIndexTemplate templateMetadata = state.metadata().templatesV2().get(templateName);
if (templateMetadata == null) {
return false;
}

return templateMetadata.version() != null && templateMetadata.version() >= Version.CURRENT.id;
}

/**
Expand Down

This file was deleted.

Loading