|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.action; |
| 9 | + |
| 10 | +import org.elasticsearch.ElasticsearchException; |
| 11 | +import org.elasticsearch.ExceptionsHelper; |
| 12 | +import org.elasticsearch.test.junit.annotations.TestLogging; |
| 13 | +import org.elasticsearch.test.transport.MockTransportService; |
| 14 | +import org.elasticsearch.transport.TransportChannel; |
| 15 | +import org.elasticsearch.transport.TransportResponse; |
| 16 | +import org.elasticsearch.transport.TransportService; |
| 17 | +import org.elasticsearch.xpack.esql.VerificationException; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import static org.elasticsearch.xpack.esql.EsqlTestUtils.getValuesList; |
| 23 | +import static org.hamcrest.Matchers.containsString; |
| 24 | +import static org.hamcrest.Matchers.empty; |
| 25 | +import static org.hamcrest.Matchers.equalTo; |
| 26 | +import static org.hamcrest.Matchers.hasItems; |
| 27 | +import static org.hamcrest.Matchers.hasSize; |
| 28 | +import static org.hamcrest.Matchers.not; |
| 29 | + |
| 30 | +@TestLogging(value = "org.elasticsearch.xpack.esql.session:DEBUG", reason = "to better understand planning") |
| 31 | +public class CrossClusterLookupJoinFailuresIT extends AbstractCrossClusterTestCase { |
| 32 | + protected boolean reuseClusters() { |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + public void testLookupFail() throws IOException { |
| 37 | + setupClusters(3); |
| 38 | + populateLookupIndex(LOCAL_CLUSTER, "values_lookup", 10); |
| 39 | + populateLookupIndex(REMOTE_CLUSTER_1, "values_lookup", 25); |
| 40 | + populateLookupIndex(REMOTE_CLUSTER_2, "values_lookup", 25); |
| 41 | + |
| 42 | + setSkipUnavailable(REMOTE_CLUSTER_1, true); |
| 43 | + Exception simulatedFailure = randomFailure(); |
| 44 | + // fail when trying to do the lookup on remote cluster |
| 45 | + for (TransportService transportService : cluster(REMOTE_CLUSTER_1).getInstances(TransportService.class)) { |
| 46 | + MockTransportService ts = asInstanceOf(MockTransportService.class, transportService); |
| 47 | + ts.addRequestHandlingBehavior( |
| 48 | + EsqlResolveFieldsAction.RESOLVE_REMOTE_TYPE.name(), |
| 49 | + (handler, request, channel, task) -> handler.messageReceived(request, new TransportChannel() { |
| 50 | + @Override |
| 51 | + public String getProfileName() { |
| 52 | + return channel.getProfileName(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void sendResponse(TransportResponse response) { |
| 57 | + sendResponse(simulatedFailure); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void sendResponse(Exception exception) { |
| 62 | + channel.sendResponse(exception); |
| 63 | + } |
| 64 | + }, task) |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + try ( |
| 70 | + EsqlQueryResponse resp = runQuery( |
| 71 | + "FROM logs-*,c*:logs-* | EVAL lookup_key = v | LOOKUP JOIN values_lookup ON lookup_key", |
| 72 | + randomBoolean() |
| 73 | + ) |
| 74 | + ) { |
| 75 | + var columns = resp.columns().stream().map(ColumnInfoImpl::name).toList(); |
| 76 | + assertThat(columns, hasItems("lookup_key", "lookup_name", "lookup_tag", "v", "tag")); |
| 77 | + |
| 78 | + List<List<Object>> values = getValuesList(resp); |
| 79 | + assertThat(values, hasSize(10)); |
| 80 | + EsqlExecutionInfo executionInfo = resp.getExecutionInfo(); |
| 81 | + |
| 82 | + var localCluster = executionInfo.getCluster(LOCAL_CLUSTER); |
| 83 | + assertThat(localCluster.getStatus(), equalTo(EsqlExecutionInfo.Cluster.Status.SUCCESSFUL)); |
| 84 | + var remoteCluster = executionInfo.getCluster(REMOTE_CLUSTER_1); |
| 85 | + assertThat(remoteCluster.getStatus(), equalTo(EsqlExecutionInfo.Cluster.Status.SKIPPED)); |
| 86 | + assertThat(remoteCluster.getFailures(), not(empty())); |
| 87 | + var failure = remoteCluster.getFailures().get(0); |
| 88 | + // FIXME: this produces a wrong message currently |
| 89 | + // assertThat(failure.reason(), containsString(simulatedFailure.getMessage())); |
| 90 | + assertThat(failure.reason(), containsString("lookup index [values_lookup] is not available in remote cluster [cluster-a]")); |
| 91 | + } |
| 92 | + |
| 93 | + try ( |
| 94 | + EsqlQueryResponse resp = runQuery( |
| 95 | + "FROM logs-*,*:logs-* | EVAL lookup_key = v | LOOKUP JOIN values_lookup ON lookup_key", |
| 96 | + randomBoolean() |
| 97 | + ) |
| 98 | + ) { |
| 99 | + var columns = resp.columns().stream().map(ColumnInfoImpl::name).toList(); |
| 100 | + assertThat(columns, hasItems("lookup_key", "lookup_name", "lookup_tag", "v", "tag")); |
| 101 | + |
| 102 | + List<List<Object>> values = getValuesList(resp); |
| 103 | + assertThat(values, hasSize(20)); |
| 104 | + EsqlExecutionInfo executionInfo = resp.getExecutionInfo(); |
| 105 | + |
| 106 | + var localCluster = executionInfo.getCluster(LOCAL_CLUSTER); |
| 107 | + assertThat(localCluster.getStatus(), equalTo(EsqlExecutionInfo.Cluster.Status.SUCCESSFUL)); |
| 108 | + var remoteCluster = executionInfo.getCluster(REMOTE_CLUSTER_1); |
| 109 | + assertThat(remoteCluster.getStatus(), equalTo(EsqlExecutionInfo.Cluster.Status.SKIPPED)); |
| 110 | + var remoteCluster2 = executionInfo.getCluster(REMOTE_CLUSTER_2); |
| 111 | + assertThat(remoteCluster2.getStatus(), equalTo(EsqlExecutionInfo.Cluster.Status.SUCCESSFUL)); |
| 112 | + assertThat(remoteCluster.getFailures(), not(empty())); |
| 113 | + var failure = remoteCluster.getFailures().get(0); |
| 114 | + // FIXME: this produces a wrong message currently |
| 115 | + // assertThat(failure.reason(), containsString(simulatedFailure.getMessage())); |
| 116 | + assertThat(failure.reason(), containsString("lookup index [values_lookup] is not available in remote cluster [cluster-a]")); |
| 117 | + } |
| 118 | + |
| 119 | + // FIXME: this should work but fails |
| 120 | + /* |
| 121 | + try ( |
| 122 | + EsqlQueryResponse resp = runQuery( |
| 123 | + "FROM c*:logs-* | EVAL lookup_key = v | LOOKUP JOIN values_lookup ON lookup_key", |
| 124 | + randomBoolean() |
| 125 | + ) |
| 126 | + ) { |
| 127 | + var columns = resp.columns().stream().map(ColumnInfoImpl::name).toList(); |
| 128 | + assertThat(columns, hasItems("lookup_key", "lookup_name", "lookup_tag", "v", "tag")); |
| 129 | +
|
| 130 | + List<List<Object>> values = getValuesList(resp); |
| 131 | + assertThat(values, hasSize(0)); |
| 132 | +
|
| 133 | + EsqlExecutionInfo executionInfo = resp.getExecutionInfo(); |
| 134 | +
|
| 135 | + var remoteCluster = executionInfo.getCluster(REMOTE_CLUSTER_1); |
| 136 | + assertThat(remoteCluster.getStatus(), equalTo(EsqlExecutionInfo.Cluster.Status.SKIPPED)); |
| 137 | + assertThat(remoteCluster.getFailures(), not(empty())); |
| 138 | + var failure = remoteCluster.getFailures().get(0); |
| 139 | + assertThat(failure.reason(), containsString(simulatedFailure.getMessage())); |
| 140 | + } */ |
| 141 | + |
| 142 | + // now fail |
| 143 | + setSkipUnavailable(REMOTE_CLUSTER_1, false); |
| 144 | + Exception ex = expectThrows( |
| 145 | + VerificationException.class, |
| 146 | + () -> runQuery("FROM logs-*,c*:logs-* | EVAL lookup_key = v | LOOKUP JOIN values_lookup ON lookup_key", randomBoolean()) |
| 147 | + ); |
| 148 | + assertThat(ex.getMessage(), containsString("lookup index [values_lookup] is not available in remote cluster [cluster-a]")); |
| 149 | + |
| 150 | + ex = expectThrows( |
| 151 | + Exception.class, |
| 152 | + () -> runQuery("FROM c*:logs-* | EVAL lookup_key = v | LOOKUP JOIN values_lookup ON lookup_key", randomBoolean()) |
| 153 | + ); |
| 154 | + assertThat(ex.getMessage(), containsString(simulatedFailure.getMessage())); |
| 155 | + } finally { |
| 156 | + for (TransportService transportService : cluster(REMOTE_CLUSTER_1).getInstances(TransportService.class)) { |
| 157 | + MockTransportService ts = asInstanceOf(MockTransportService.class, transportService); |
| 158 | + ts.clearAllRules(); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + public void testLookupDisconnect() throws IOException { |
| 164 | + setupClusters(2); |
| 165 | + populateLookupIndex(LOCAL_CLUSTER, "values_lookup", 10); |
| 166 | + populateLookupIndex(REMOTE_CLUSTER_1, "values_lookup", 10); |
| 167 | + |
| 168 | + setSkipUnavailable(REMOTE_CLUSTER_1, true); |
| 169 | + try { |
| 170 | + // close the remote cluster so that it is unavailable |
| 171 | + cluster(REMOTE_CLUSTER_1).close(); |
| 172 | + |
| 173 | + try ( |
| 174 | + EsqlQueryResponse resp = runQuery( |
| 175 | + "FROM c*:logs-* | EVAL lookup_key = v | LOOKUP JOIN values_lookup ON lookup_key", |
| 176 | + randomBoolean() |
| 177 | + ) |
| 178 | + ) { |
| 179 | + var columns = resp.columns().stream().map(ColumnInfoImpl::name).toList(); |
| 180 | + // this is a bit weird but that's how it works |
| 181 | + assertThat(columns, hasSize(1)); |
| 182 | + assertThat(columns, hasItems("<no-fields>")); |
| 183 | + List<List<Object>> values = getValuesList(resp); |
| 184 | + assertThat(values, hasSize(0)); |
| 185 | + EsqlExecutionInfo executionInfo = resp.getExecutionInfo(); |
| 186 | + |
| 187 | + var remoteCluster = executionInfo.getCluster(REMOTE_CLUSTER_1); |
| 188 | + assertThat(remoteCluster.getStatus(), equalTo(EsqlExecutionInfo.Cluster.Status.SKIPPED)); |
| 189 | + assertThat(remoteCluster.getFailures(), not(empty())); |
| 190 | + var failure = remoteCluster.getFailures().get(0); |
| 191 | + assertThat(failure.reason(), containsString("unable to connect to remote cluster")); |
| 192 | + } |
| 193 | + |
| 194 | + try ( |
| 195 | + EsqlQueryResponse resp = runQuery( |
| 196 | + "FROM logs-*,c*:logs-* | EVAL lookup_key = v | LOOKUP JOIN values_lookup ON lookup_key", |
| 197 | + randomBoolean() |
| 198 | + ) |
| 199 | + ) { |
| 200 | + var columns = resp.columns().stream().map(ColumnInfoImpl::name).toList(); |
| 201 | + assertThat(columns, hasItems("lookup_key", "lookup_name", "lookup_tag", "v", "tag")); |
| 202 | + |
| 203 | + List<List<Object>> values = getValuesList(resp); |
| 204 | + assertThat(values, hasSize(10)); |
| 205 | + EsqlExecutionInfo executionInfo = resp.getExecutionInfo(); |
| 206 | + |
| 207 | + var localCluster = executionInfo.getCluster(LOCAL_CLUSTER); |
| 208 | + assertThat(localCluster.getStatus(), equalTo(EsqlExecutionInfo.Cluster.Status.SUCCESSFUL)); |
| 209 | + var remoteCluster = executionInfo.getCluster(REMOTE_CLUSTER_1); |
| 210 | + assertThat(remoteCluster.getStatus(), equalTo(EsqlExecutionInfo.Cluster.Status.SKIPPED)); |
| 211 | + assertThat(remoteCluster.getFailures(), not(empty())); |
| 212 | + var failure = remoteCluster.getFailures().get(0); |
| 213 | + assertThat( |
| 214 | + failure.reason(), |
| 215 | + containsString("Remote cluster [cluster-a] (with setting skip_unavailable=true) is not available") |
| 216 | + ); |
| 217 | + } |
| 218 | + |
| 219 | + setSkipUnavailable(REMOTE_CLUSTER_1, false); |
| 220 | + Exception ex = expectThrows( |
| 221 | + ElasticsearchException.class, |
| 222 | + () -> runQuery("FROM c*:logs-* | EVAL lookup_key = v | LOOKUP JOIN values_lookup ON lookup_key", randomBoolean()) |
| 223 | + ); |
| 224 | + assertTrue(ExceptionsHelper.isRemoteUnavailableException(ex)); |
| 225 | + |
| 226 | + ex = expectThrows( |
| 227 | + ElasticsearchException.class, |
| 228 | + () -> runQuery("FROM logs-*,c*:logs-* | EVAL lookup_key = v | LOOKUP JOIN values_lookup ON lookup_key", randomBoolean()) |
| 229 | + ); |
| 230 | + assertTrue(ExceptionsHelper.isRemoteUnavailableException(ex)); |
| 231 | + } finally { |
| 232 | + clearSkipUnavailable(2); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | +} |
0 commit comments