|
20 | 20 | package org.elasticsearch;
|
21 | 21 |
|
22 | 22 | import org.apache.commons.codec.DecoderException;
|
| 23 | +import org.elasticsearch.action.OriginalIndices; |
| 24 | +import org.elasticsearch.action.ShardOperationFailedException; |
| 25 | +import org.elasticsearch.action.search.ShardSearchFailure; |
| 26 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 27 | +import org.elasticsearch.common.ParsingException; |
23 | 28 | import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException;
|
| 29 | +import org.elasticsearch.index.Index; |
| 30 | +import org.elasticsearch.index.query.QueryShardException; |
| 31 | +import org.elasticsearch.index.shard.ShardId; |
24 | 32 | import org.elasticsearch.rest.RestStatus;
|
| 33 | +import org.elasticsearch.search.SearchShardTarget; |
25 | 34 | import org.elasticsearch.test.ESTestCase;
|
| 35 | +import org.elasticsearch.transport.RemoteClusterAware; |
26 | 36 |
|
27 | 37 | import java.util.Optional;
|
28 | 38 |
|
29 | 39 | import static org.elasticsearch.ExceptionsHelper.MAX_ITERATIONS;
|
30 | 40 | import static org.elasticsearch.ExceptionsHelper.maybeError;
|
31 | 41 | import static org.hamcrest.CoreMatchers.equalTo;
|
| 42 | +import static org.hamcrest.CoreMatchers.instanceOf; |
| 43 | +import static org.hamcrest.CoreMatchers.nullValue; |
32 | 44 |
|
33 | 45 | public class ExceptionsHelperTests extends ESTestCase {
|
34 | 46 |
|
@@ -91,4 +103,107 @@ public void testStatus() {
|
91 | 103 | assertThat(ExceptionsHelper.status(new EsRejectedExecutionException("rejected")), equalTo(RestStatus.TOO_MANY_REQUESTS));
|
92 | 104 | }
|
93 | 105 |
|
| 106 | + public void testGroupBy() { |
| 107 | + ShardOperationFailedException[] failures = new ShardOperationFailedException[]{ |
| 108 | + createShardFailureParsingException("error", "node0", "index", 0, null), |
| 109 | + createShardFailureParsingException("error", "node1", "index", 1, null), |
| 110 | + createShardFailureParsingException("error", "node2", "index2", 2, null), |
| 111 | + createShardFailureParsingException("error", "node0", "index", 0, "cluster1"), |
| 112 | + createShardFailureParsingException("error", "node1", "index", 1, "cluster1"), |
| 113 | + createShardFailureParsingException("error", "node2", "index", 2, "cluster1"), |
| 114 | + createShardFailureParsingException("error", "node0", "index", 0, "cluster2"), |
| 115 | + createShardFailureParsingException("error", "node1", "index", 1, "cluster2"), |
| 116 | + createShardFailureParsingException("error", "node2", "index", 2, "cluster2"), |
| 117 | + createShardFailureParsingException("another error", "node2", "index", 2, "cluster2") |
| 118 | + }; |
| 119 | + |
| 120 | + ShardOperationFailedException[] groupBy = ExceptionsHelper.groupBy(failures); |
| 121 | + assertThat(groupBy.length, equalTo(5)); |
| 122 | + String[] expectedIndices = new String[]{"index", "index2", "cluster1:index", "cluster2:index", "cluster2:index"}; |
| 123 | + String[] expectedErrors = new String[]{"error", "error", "error", "error", "another error"}; |
| 124 | + int i = 0; |
| 125 | + for (ShardOperationFailedException shardOperationFailedException : groupBy) { |
| 126 | + assertThat(shardOperationFailedException.getCause().getMessage(), equalTo(expectedErrors[i])); |
| 127 | + assertThat(shardOperationFailedException.index(), equalTo(expectedIndices[i++])); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private static ShardSearchFailure createShardFailureParsingException(String error, String nodeId, |
| 132 | + String index, int shardId, String clusterAlias) { |
| 133 | + ParsingException ex = new ParsingException(0, 0, error, new IllegalArgumentException("some bad argument")); |
| 134 | + ex.setIndex(index); |
| 135 | + return new ShardSearchFailure(ex, createSearchShardTarget(nodeId, shardId, index, clusterAlias)); |
| 136 | + } |
| 137 | + |
| 138 | + private static SearchShardTarget createSearchShardTarget(String nodeId, int shardId, String index, String clusterAlias) { |
| 139 | + return new SearchShardTarget(nodeId, |
| 140 | + new ShardId(new Index(index, IndexMetaData.INDEX_UUID_NA_VALUE), shardId), clusterAlias, OriginalIndices.NONE); |
| 141 | + } |
| 142 | + |
| 143 | + public void testGroupByNullTarget() { |
| 144 | + ShardOperationFailedException[] failures = new ShardOperationFailedException[] { |
| 145 | + createShardFailureQueryShardException("error", "index", null), |
| 146 | + createShardFailureQueryShardException("error", "index", null), |
| 147 | + createShardFailureQueryShardException("error", "index", null), |
| 148 | + createShardFailureQueryShardException("error", "index", "cluster1"), |
| 149 | + createShardFailureQueryShardException("error", "index", "cluster1"), |
| 150 | + createShardFailureQueryShardException("error", "index", "cluster1"), |
| 151 | + createShardFailureQueryShardException("error", "index", "cluster2"), |
| 152 | + createShardFailureQueryShardException("error", "index", "cluster2"), |
| 153 | + createShardFailureQueryShardException("error", "index2", null), |
| 154 | + createShardFailureQueryShardException("another error", "index2", null), |
| 155 | + }; |
| 156 | + |
| 157 | + ShardOperationFailedException[] groupBy = ExceptionsHelper.groupBy(failures); |
| 158 | + assertThat(groupBy.length, equalTo(5)); |
| 159 | + String[] expectedIndices = new String[]{"index", "cluster1:index", "cluster2:index", "index2", "index2"}; |
| 160 | + String[] expectedErrors = new String[]{"error", "error", "error", "error", "another error"}; |
| 161 | + int i = 0; |
| 162 | + for (ShardOperationFailedException shardOperationFailedException : groupBy) { |
| 163 | + assertThat(shardOperationFailedException.index(), nullValue()); |
| 164 | + assertThat(shardOperationFailedException.getCause(), instanceOf(ElasticsearchException.class)); |
| 165 | + ElasticsearchException elasticsearchException = (ElasticsearchException) shardOperationFailedException.getCause(); |
| 166 | + assertThat(elasticsearchException.getMessage(), equalTo(expectedErrors[i])); |
| 167 | + assertThat(elasticsearchException.getIndex().getName(), equalTo(expectedIndices[i++])); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private static ShardSearchFailure createShardFailureQueryShardException(String error, String indexName, String clusterAlias) { |
| 172 | + Index index = new Index(RemoteClusterAware.buildRemoteIndexName(clusterAlias, indexName), "uuid"); |
| 173 | + QueryShardException queryShardException = new QueryShardException(index, error, new IllegalArgumentException("parse error")); |
| 174 | + return new ShardSearchFailure(queryShardException, null); |
| 175 | + } |
| 176 | + |
| 177 | + public void testGroupByNullCause() { |
| 178 | + ShardOperationFailedException[] failures = new ShardOperationFailedException[] { |
| 179 | + new ShardSearchFailure("error", createSearchShardTarget("node0", 0, "index", null)), |
| 180 | + new ShardSearchFailure("error", createSearchShardTarget("node1", 1, "index", null)), |
| 181 | + new ShardSearchFailure("error", createSearchShardTarget("node1", 1, "index2", null)), |
| 182 | + new ShardSearchFailure("error", createSearchShardTarget("node2", 2, "index", "cluster1")), |
| 183 | + new ShardSearchFailure("error", createSearchShardTarget("node1", 1, "index", "cluster1")), |
| 184 | + new ShardSearchFailure("a different error", createSearchShardTarget("node3", 3, "index", "cluster1")) |
| 185 | + }; |
| 186 | + |
| 187 | + ShardOperationFailedException[] groupBy = ExceptionsHelper.groupBy(failures); |
| 188 | + assertThat(groupBy.length, equalTo(4)); |
| 189 | + String[] expectedIndices = new String[]{"index", "index2", "cluster1:index", "cluster1:index"}; |
| 190 | + String[] expectedErrors = new String[]{"error", "error", "error", "a different error"}; |
| 191 | + |
| 192 | + int i = 0; |
| 193 | + for (ShardOperationFailedException shardOperationFailedException : groupBy) { |
| 194 | + assertThat(shardOperationFailedException.reason(), equalTo(expectedErrors[i])); |
| 195 | + assertThat(shardOperationFailedException.index(), equalTo(expectedIndices[i++])); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + public void testGroupByNullIndex() { |
| 200 | + ShardOperationFailedException[] failures = new ShardOperationFailedException[] { |
| 201 | + new ShardSearchFailure("error", null), |
| 202 | + new ShardSearchFailure(new IllegalArgumentException("error")), |
| 203 | + new ShardSearchFailure(new ParsingException(0, 0, "error", null)), |
| 204 | + }; |
| 205 | + |
| 206 | + ShardOperationFailedException[] groupBy = ExceptionsHelper.groupBy(failures); |
| 207 | + assertThat(groupBy.length, equalTo(3)); |
| 208 | + } |
94 | 209 | }
|
0 commit comments