From c1b0bfd74aff0691b3dc5ed74049fd1f48d38a7b Mon Sep 17 00:00:00 2001 From: Dimitris Athanasiou Date: Tue, 8 Oct 2019 16:02:47 +0300 Subject: [PATCH] [7.x][ML] Unwrap exception causes before calling instanceof (#47676) (#47724) When exceptions could be returned from another node, the exception might be wrapped in a `RemoteTransportException`. In places where we handled specific exceptions using `instanceof` we ought to unwrap the cause first. This commit attempts to fix this issue after searching code in the ML plugin. Backport of #47676 --- .../xpack/core/ml/annotations/AnnotationIndex.java | 3 ++- .../core/ml/job/persistence/AnomalyDetectorsIndex.java | 3 ++- .../xpack/core/ml/utils/ExceptionsHelper.java | 4 ++++ .../org/elasticsearch/xpack/ml/MlConfigMigrator.java | 3 ++- .../xpack/ml/action/TransportCloseJobAction.java | 2 +- .../xpack/ml/action/TransportDeleteDatafeedAction.java | 2 +- .../xpack/ml/action/TransportDeleteJobAction.java | 8 ++++---- .../xpack/ml/action/TransportOpenJobAction.java | 2 +- .../xpack/ml/action/TransportPutCalendarAction.java | 2 +- .../xpack/ml/action/TransportPutDatafeedAction.java | 2 +- .../xpack/ml/action/TransportPutFilterAction.java | 2 +- .../xpack/ml/action/TransportSetUpgradeModeAction.java | 3 ++- .../action/TransportStartDataFrameAnalyticsAction.java | 4 ++-- .../xpack/ml/action/TransportStartDatafeedAction.java | 2 +- .../ml/action/TransportStopDataFrameAnalyticsAction.java | 2 +- .../xpack/ml/action/TransportStopDatafeedAction.java | 5 +++-- .../xpack/ml/action/TransportUpdateFilterAction.java | 2 +- .../xpack/ml/datafeed/DatafeedJobBuilder.java | 2 +- .../elasticsearch/xpack/ml/datafeed/DatafeedManager.java | 3 ++- .../ml/datafeed/extractor/DataExtractorFactory.java | 6 ++++-- .../extractor/scroll/ScrollDataExtractorFactory.java | 5 +++-- .../ml/datafeed/persistence/DatafeedConfigProvider.java | 2 +- .../xpack/ml/dataframe/DataFrameAnalyticsManager.java | 7 ++++--- .../xpack/ml/dataframe/DataFrameAnalyticsTask.java | 4 ++-- .../extractor/DataFrameDataExtractorFactory.java | 3 ++- .../persistence/DataFrameAnalyticsConfigProvider.java | 2 +- .../ml/inference/persistence/TrainedModelProvider.java | 3 ++- .../java/org/elasticsearch/xpack/ml/job/JobManager.java | 4 ++-- .../xpack/ml/job/UpdateJobProcessNotifier.java | 9 +++++---- .../xpack/ml/job/persistence/JobConfigProvider.java | 4 ++-- .../xpack/ml/job/persistence/JobResultsProvider.java | 6 +++--- 31 files changed, 64 insertions(+), 47 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/annotations/AnnotationIndex.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/annotations/AnnotationIndex.java index e9da7238fad..ad72c82c8c3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/annotations/AnnotationIndex.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/annotations/AnnotationIndex.java @@ -22,6 +22,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ml.MachineLearningField; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.job.persistence.ElasticsearchMappings; +import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import java.io.IOException; import java.util.SortedMap; @@ -84,7 +85,7 @@ public class AnnotationIndex { e -> { // Possible that the index was created while the request was executing, // so we need to handle that possibility - if (e instanceof ResourceAlreadyExistsException) { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) { // Create the alias createAliasListener.onResponse(true); } else { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/AnomalyDetectorsIndex.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/AnomalyDetectorsIndex.java index 7e61d42705a..7d4e2367cce 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/AnomalyDetectorsIndex.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/AnomalyDetectorsIndex.java @@ -16,6 +16,7 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; +import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import java.util.Arrays; import java.util.Collections; @@ -133,7 +134,7 @@ public final class AnomalyDetectorsIndex { // If it was created between our last check, and this request being handled, we should add the alias // Adding an alias that already exists is idempotent. So, no need to double check if the alias exists // as well. - if (createIndexFailure instanceof ResourceAlreadyExistsException) { + if (ExceptionsHelper.unwrapCause(createIndexFailure) instanceof ResourceAlreadyExistsException) { createAliasListener.onResponse(AnomalyDetectorsIndexFields.STATE_INDEX_PREFIX); } else { finalListener.onFailure(createIndexFailure); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ExceptionsHelper.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ExceptionsHelper.java index 320eace9835..517e600ab44 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ExceptionsHelper.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/utils/ExceptionsHelper.java @@ -99,4 +99,8 @@ public class ExceptionsHelper { public static T requireNonNull(T obj, ParseField paramName) { return requireNonNull(obj, paramName.getPreferredName()); } + + public static Throwable unwrapCause(Throwable t) { + return org.elasticsearch.ExceptionsHelper.unwrapCause(t); + } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlConfigMigrator.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlConfigMigrator.java index b9177613881..bfa426f47b6 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlConfigMigrator.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlConfigMigrator.java @@ -44,6 +44,7 @@ import org.elasticsearch.xpack.core.ml.job.config.AnalysisLimits; import org.elasticsearch.xpack.core.ml.job.config.Job; import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex; import org.elasticsearch.xpack.core.ml.job.persistence.ElasticsearchMappings; +import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.ml.utils.ToXContentParams; import org.elasticsearch.xpack.ml.datafeed.persistence.DatafeedConfigProvider; import org.elasticsearch.xpack.ml.job.persistence.JobConfigProvider; @@ -468,7 +469,7 @@ public class MlConfigMigrator { listener.onResponse(indexResponse.getResult() == DocWriteResponse.Result.CREATED); }, e -> { - if (e instanceof VersionConflictEngineException) { + if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { // the snapshot already exists listener.onResponse(Boolean.TRUE); } else { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportCloseJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportCloseJobAction.java index 8816807948a..9ed0210aa9a 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportCloseJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportCloseJobAction.java @@ -341,7 +341,7 @@ public class TransportCloseJobAction extends TransportTasksAction finishedHandler.onResponse(true), e -> { // It's not a problem for us if the index wasn't found - it's equivalent to document not found - if (e instanceof IndexNotFoundException) { + if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { finishedHandler.onResponse(true); } else { finishedHandler.onFailure(e); @@ -467,7 +467,7 @@ public class TransportDeleteJobAction extends TransportMasterNodeAction { // It's not a problem for us if the index wasn't found - it's equivalent to document not found - if (e instanceof IndexNotFoundException) { + if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { finishedHandler.onResponse(true); } else { finishedHandler.onFailure(e); @@ -537,7 +537,7 @@ public class TransportDeleteJobAction extends TransportMasterNodeAction killJobListener = ActionListener.wrap( response -> removePersistentTask(request.getJobId(), state, removeTaskListener), e -> { - if (e instanceof ElasticsearchStatusException) { + if (ExceptionsHelper.unwrapCause(e) instanceof ElasticsearchStatusException) { // Killing the process marks the task as completed so it // may have disappeared when we get here removePersistentTask(request.getJobId(), state, removeTaskListener); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportOpenJobAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportOpenJobAction.java index 02c62cccd25..a3dcb167c7f 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportOpenJobAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportOpenJobAction.java @@ -232,7 +232,7 @@ public class TransportOpenJobAction extends TransportMasterNodeAction { - if (e instanceof IndexNotFoundException) { + if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { indicesPrivilegesBuilder.privileges(SearchAction.NAME); privRequest.indexPrivileges(indicesPrivilegesBuilder.build()); client.execute(HasPrivilegesAction.INSTANCE, privRequest, privResponseListener); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutFilterAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutFilterAction.java index b9c8afa9340..8565a86e4c6 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutFilterAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportPutFilterAction.java @@ -67,7 +67,7 @@ public class TransportPutFilterAction extends HandledTransportAction ex instanceof ResourceNotFoundException == false); + ex -> ExceptionsHelper.unwrapCause(ex) instanceof ResourceNotFoundException == false); for (PersistentTask task : datafeedAndJobTasks) { chainTaskExecutor.add( diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDataFrameAnalyticsAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDataFrameAnalyticsAction.java index 562c1e6fc59..490987882b0 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDataFrameAnalyticsAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDataFrameAnalyticsAction.java @@ -153,7 +153,7 @@ public class TransportStartDataFrameAnalyticsAction @Override public void onFailure(Exception e) { - if (e instanceof ResourceAlreadyExistsException) { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) { e = new ElasticsearchStatusException("Cannot start data frame analytics [" + request.getId() + "] because it has already been started", RestStatus.CONFLICT, e); } @@ -341,7 +341,7 @@ public class TransportStartDataFrameAnalyticsAction } }, e -> { - if (e instanceof IndexNotFoundException) { + if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { listener.onResponse(startContext); } else { listener.onFailure(e); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDatafeedAction.java index 1289f70cd27..6761d616464 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStartDatafeedAction.java @@ -174,7 +174,7 @@ public class TransportStartDatafeedAction extends TransportMasterNodeAction { - if (e instanceof ResourceNotFoundException) { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { // the task has disappeared so must have stopped listener.onResponse(new StopDataFrameAnalyticsAction.Response(true)); } else { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopDatafeedAction.java index cbd55bb60d8..2d8c62223f2 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportStopDatafeedAction.java @@ -29,6 +29,7 @@ import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.ml.MlTasks; import org.elasticsearch.xpack.core.ml.action.StopDatafeedAction; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedState; +import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.ml.MachineLearning; import org.elasticsearch.xpack.ml.datafeed.persistence.DatafeedConfigProvider; @@ -195,7 +196,7 @@ public class TransportStopDatafeedAction extends TransportTasksAction { - if (e instanceof ResourceNotFoundException) { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { // the task has disappeared so must have stopped listener.onResponse(new StopDatafeedAction.Response(true)); } else { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateFilterAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateFilterAction.java index 49d83886691..3cb716dc504 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateFilterAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportUpdateFilterAction.java @@ -125,7 +125,7 @@ public class TransportUpdateFilterAction extends HandledTransportAction { - if (e instanceof ResourceNotFoundException) { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { QueryPage empty = new QueryPage<>(Collections.emptyList(), 0, Bucket.RESULT_TYPE_FIELD); bucketsHandler.accept(empty); } else { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedManager.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedManager.java index 457701740e0..30a3948fcc2 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedManager.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedManager.java @@ -30,6 +30,7 @@ import org.elasticsearch.xpack.core.ml.datafeed.DatafeedState; import org.elasticsearch.xpack.core.ml.job.config.JobState; import org.elasticsearch.xpack.core.ml.job.config.JobTaskState; import org.elasticsearch.xpack.core.ml.job.messages.Messages; +import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.ml.MachineLearning; import org.elasticsearch.xpack.ml.action.TransportStartDatafeedAction; import org.elasticsearch.xpack.ml.job.process.autodetect.AutodetectProcessManager; @@ -96,7 +97,7 @@ public class DatafeedManager { @Override public void onFailure(Exception e) { - if (e instanceof ResourceNotFoundException) { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { // The task was stopped in the meantime, no need to do anything logger.info("[{}] Aborting as datafeed has been stopped", datafeedId); } else { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/DataExtractorFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/DataExtractorFactory.java index 40e819affa0..d43ede48d05 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/DataExtractorFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/DataExtractorFactory.java @@ -15,6 +15,7 @@ import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; import org.elasticsearch.xpack.core.ml.datafeed.extractor.DataExtractor; import org.elasticsearch.xpack.core.ml.job.config.Job; +import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.core.rollup.action.GetRollupIndexCapsAction; import org.elasticsearch.xpack.ml.datafeed.DatafeedTimingStatsReporter; import org.elasticsearch.xpack.ml.datafeed.extractor.aggregation.AggregationDataExtractorFactory; @@ -59,9 +60,10 @@ public interface DataExtractorFactory { } }, e -> { - if (e instanceof IndexNotFoundException) { + Throwable cause = ExceptionsHelper.unwrapCause(e); + if (cause instanceof IndexNotFoundException) { listener.onFailure(new ResourceNotFoundException("datafeed [" + datafeed.getId() - + "] cannot retrieve data because index " + ((IndexNotFoundException)e).getIndex() + " does not exist")); + + "] cannot retrieve data because index " + ((IndexNotFoundException) cause).getIndex() + " does not exist")); } else { listener.onFailure(e); } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorFactory.java index 20ed7f664f9..e58689736cd 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorFactory.java @@ -74,9 +74,10 @@ public class ScrollDataExtractorFactory implements DataExtractorFactory { new ScrollDataExtractorFactory(client, datafeed, job, extractedFields, xContentRegistry, timingStatsReporter)); }, e -> { - if (e instanceof IndexNotFoundException) { + Throwable cause = ExceptionsHelper.unwrapCause(e); + if (cause instanceof IndexNotFoundException) { listener.onFailure(new ResourceNotFoundException("datafeed [" + datafeed.getId() - + "] cannot retrieve data because index " + ((IndexNotFoundException) e).getIndex() + " does not exist")); + + "] cannot retrieve data because index " + ((IndexNotFoundException) cause).getIndex() + " does not exist")); } else if (e instanceof IllegalArgumentException) { listener.onFailure(ExceptionsHelper.badRequestException("[" + datafeed.getId() + "] " + e.getMessage())); } else { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/persistence/DatafeedConfigProvider.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/persistence/DatafeedConfigProvider.java index 1c0d6dea598..bb8d7667374 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/persistence/DatafeedConfigProvider.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/datafeed/persistence/DatafeedConfigProvider.java @@ -133,7 +133,7 @@ public class DatafeedConfigProvider { executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, ActionListener.wrap( listener::onResponse, e -> { - if (e instanceof VersionConflictEngineException) { + if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { // the dafafeed already exists listener.onFailure(ExceptionsHelper.datafeedAlreadyExists(datafeedId)); } else { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsManager.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsManager.java index a3a0d7cce17..fea9753314d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsManager.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsManager.java @@ -34,6 +34,7 @@ import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsState; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsTaskState; import org.elasticsearch.xpack.core.ml.job.messages.Messages; import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex; +import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.ml.dataframe.extractor.DataFrameDataExtractorFactory; import org.elasticsearch.xpack.ml.dataframe.persistence.DataFrameAnalyticsConfigProvider; import org.elasticsearch.xpack.ml.dataframe.process.AnalyticsProcessManager; @@ -142,7 +143,7 @@ public class DataFrameAnalyticsManager { ActionListener.wrap( r-> reindexDataframeAndStartAnalysis(task, config), e -> { - if (e instanceof IndexNotFoundException) { + if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { reindexDataframeAndStartAnalysis(task, config); } else { task.updateState(DataFrameAnalyticsState.FAILED, e.getMessage()); @@ -224,7 +225,7 @@ public class DataFrameAnalyticsManager { )); }, e -> { - if (org.elasticsearch.ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { + if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { auditor.info( config.getId(), Messages.getMessage(Messages.DATA_FRAME_ANALYTICS_AUDIT_CREATING_DEST_INDEX, config.getDest().getIndex())); @@ -260,7 +261,7 @@ public class DataFrameAnalyticsManager { } }), error -> { - if (error instanceof ResourceNotFoundException) { + if (ExceptionsHelper.unwrapCause(error) instanceof ResourceNotFoundException) { // Task has stopped } else { task.updateState(DataFrameAnalyticsState.FAILED, error.getMessage()); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTask.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTask.java index c8fbd3d6da0..d6be817804b 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTask.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/DataFrameAnalyticsTask.java @@ -162,7 +162,7 @@ public class DataFrameAnalyticsTask extends AllocatedPersistentTask implements S } // There is a chance that the task is finished by the time we cancel it in which case we'll get // a ResourceNotFoundException which we can ignore. - if (firstError != null && firstError instanceof ResourceNotFoundException == false) { + if (firstError != null && ExceptionsHelper.unwrapCause(firstError) instanceof ResourceNotFoundException == false) { throw ExceptionsHelper.serverError("[" + taskParams.getId() + "] Error cancelling reindex task", firstError); } else { LOGGER.debug("[{}] Reindex task was successfully cancelled", taskParams.getId()); @@ -215,7 +215,7 @@ public class DataFrameAnalyticsTask extends AllocatedPersistentTask implements S listener.onResponse(progress); }, error -> { - if (error instanceof ResourceNotFoundException) { + if (ExceptionsHelper.unwrapCause(error) instanceof ResourceNotFoundException) { // The task is not present which means either it has not started yet or it finished. // We keep track of whether the task has finished so we can use that to tell whether the progress 100. listener.onResponse(isReindexingFinished ? 100 : 0); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorFactory.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorFactory.java index d24d157d4f5..a93efe67319 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorFactory.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorFactory.java @@ -24,6 +24,7 @@ import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.xpack.core.ClientHelper; import org.elasticsearch.xpack.core.ml.dataframe.DataFrameAnalyticsConfig; +import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import org.elasticsearch.xpack.ml.datafeed.extractor.fields.ExtractedField; import org.elasticsearch.xpack.ml.datafeed.extractor.fields.ExtractedFields; @@ -219,7 +220,7 @@ public class DataFrameDataExtractorFactory { docValueFieldsLimitListener.onResponse(minDocValueFieldsLimit); }, e -> { - if (e instanceof IndexNotFoundException) { + if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { docValueFieldsLimitListener.onFailure(new ResourceNotFoundException("cannot retrieve data because index " + ((IndexNotFoundException) e).getIndex() + " does not exist")); } else { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/persistence/DataFrameAnalyticsConfigProvider.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/persistence/DataFrameAnalyticsConfigProvider.java index d8d0cd775dd..d13ed2c6a4d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/persistence/DataFrameAnalyticsConfigProvider.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/persistence/DataFrameAnalyticsConfigProvider.java @@ -99,7 +99,7 @@ public class DataFrameAnalyticsConfigProvider { executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, ActionListener.wrap( listener::onResponse, e -> { - if (e instanceof VersionConflictEngineException) { + if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { listener.onFailure(ExceptionsHelper.dataFrameAnalyticsAlreadyExists(id)); } else { listener.onFailure(e); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelProvider.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelProvider.java index e569edc07fd..2028dfe9edf 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelProvider.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelProvider.java @@ -36,6 +36,7 @@ import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; import org.elasticsearch.xpack.core.ml.inference.persistence.InferenceIndexConstants; import org.elasticsearch.xpack.core.ml.job.messages.Messages; +import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; import java.io.IOException; import java.io.InputStream; @@ -72,7 +73,7 @@ public class TrainedModelProvider { trainedModelConfig.getModelId(), trainedModelConfig.getModelVersion()), e); - if (e instanceof VersionConflictEngineException) { + if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { listener.onFailure(new ResourceAlreadyExistsException( Messages.getMessage(Messages.INFERENCE_TRAINED_MODEL_EXISTS, trainedModelConfig.getModelId(), trainedModelConfig.getModelVersion()))); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/JobManager.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/JobManager.java index aa7771ac21f..b134cf59c19 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/JobManager.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/JobManager.java @@ -142,7 +142,7 @@ public class JobManager { jobConfigProvider.getJob(jobId, ActionListener.wrap( r -> jobListener.onResponse(r.build()), // TODO JIndex we shouldn't be building the job here e -> { - if (e instanceof ResourceNotFoundException) { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { // Try to get the job from the cluster state getJobFromClusterState(jobId, jobListener); } else { @@ -272,7 +272,7 @@ public class JobManager { @Override public void onFailure(Exception e) { - if (e instanceof IllegalArgumentException) { + if (ExceptionsHelper.unwrapCause(e) instanceof IllegalArgumentException) { // the underlying error differs depending on which way around the clashing fields are seen Matcher matcher = Pattern.compile("(?:mapper|Can't merge a non object mapping) \\[(.*)\\] (?:of different type, " + "current_type \\[.*\\], merged_type|with an object mapping) \\[.*\\]").matcher(e.getMessage()); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/UpdateJobProcessNotifier.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/UpdateJobProcessNotifier.java index 29e98d01ca9..a1bc5aa357c 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/UpdateJobProcessNotifier.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/UpdateJobProcessNotifier.java @@ -135,13 +135,14 @@ public class UpdateJobProcessNotifier { @Override public void onFailure(Exception e) { - if (e instanceof ResourceNotFoundException) { + Throwable cause = ExceptionsHelper.unwrapCause(e); + if (cause instanceof ResourceNotFoundException) { logger.debug("Remote job [{}] not updated as it has been deleted", update.getJobId()); - } else if (e.getMessage().contains("because job [" + update.getJobId() + "] is not open") - && e instanceof ElasticsearchStatusException) { + } else if (cause.getMessage().contains("because job [" + update.getJobId() + "] is not open") + && cause instanceof ElasticsearchStatusException) { logger.debug("Remote job [{}] not updated as it is no longer open", update.getJobId()); } else { - logger.error("Failed to update remote job [" + update.getJobId() + "]", e); + logger.error("Failed to update remote job [" + update.getJobId() + "]", cause); } updateHolder.listener.onFailure(e); executeProcessUpdates(updatesIterator); diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobConfigProvider.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobConfigProvider.java index 549945e44fb..91fdfdbc26d 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobConfigProvider.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobConfigProvider.java @@ -130,7 +130,7 @@ public class JobConfigProvider { executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, ActionListener.wrap( listener::onResponse, e -> { - if (e instanceof VersionConflictEngineException) { + if (ExceptionsHelper.unwrapCause(e) instanceof VersionConflictEngineException) { // the job already exists listener.onFailure(ExceptionsHelper.jobAlreadyExists(job.getId())); } else { @@ -415,7 +415,7 @@ public class JobConfigProvider { * For the list of job Ids find all that match existing jobs Ids. * The repsonse is all the job Ids in {@code ids} that match an existing * job Id. - * @param ids Job Ids to find + * @param ids Job Ids to find * @param listener The matched Ids listener */ public void jobIdMatches(List ids, ActionListener> listener) { diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsProvider.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsProvider.java index 1c2b7af7937..9cbee001d77 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsProvider.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/persistence/JobResultsProvider.java @@ -304,7 +304,7 @@ public class JobResultsProvider { e -> { // Possible that the index was created while the request was executing, // so we need to handle that possibility - if (e instanceof ResourceAlreadyExistsException) { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) { LOGGER.info("Index already exists"); // Add the term field mappings and alias. The complication is that the state at the // beginning of the operation doesn't have any knowledge of the index, as it's only @@ -1189,7 +1189,7 @@ public class JobResultsProvider { .sortDescending(true).from(BUCKETS_FOR_ESTABLISHED_MEMORY_SIZE - 1).size(1) .includeInterim(false); bucketsViaInternalClient(jobId, bucketQuery, bucketHandler, e -> { - if (e instanceof ResourceNotFoundException) { + if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) { handler.accept(0L); } else { errorHandler.accept(e); @@ -1437,7 +1437,7 @@ public class JobResultsProvider { @Override public void onFailure(Exception e) { - if (e instanceof IndexNotFoundException) { + if (ExceptionsHelper.unwrapCause(e) instanceof IndexNotFoundException) { listener.onFailure(new ResourceNotFoundException("No calendar with id [" + calendarId + "]")); } else { listener.onFailure(e);