|
| 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.client.internal.Client; |
| 11 | +import org.elasticsearch.common.Strings; |
| 12 | +import org.elasticsearch.common.settings.Settings; |
| 13 | +import org.elasticsearch.test.junit.annotations.TestLogging; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 20 | +import static org.elasticsearch.xpack.esql.EsqlTestUtils.getValuesList; |
| 21 | +import static org.hamcrest.Matchers.equalTo; |
| 22 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 23 | +import static org.hamcrest.Matchers.hasItems; |
| 24 | +import static org.hamcrest.Matchers.hasSize; |
| 25 | + |
| 26 | +@TestLogging(value = "org.elasticsearch.xpack.esql.session:DEBUG", reason = "to better understand planning") |
| 27 | +public class CrossClusterLookupJoinIT extends AbstractCrossClusterTestCase { |
| 28 | + |
| 29 | + public void testLookupJoinAcrossClusters() throws IOException { |
| 30 | + setupClustersAndLookups(); |
| 31 | + |
| 32 | + try ( |
| 33 | + EsqlQueryResponse resp = runQuery( |
| 34 | + "FROM logs-*,c*:logs-* | EVAL lookup_key = v | LOOKUP JOIN values_lookup ON lookup_key", |
| 35 | + randomBoolean() |
| 36 | + ) |
| 37 | + ) { |
| 38 | + var columns = resp.columns().stream().map(ColumnInfoImpl::name).toList(); |
| 39 | + assertThat(columns, hasItems("lookup_key", "lookup_name", "lookup_tag", "v", "tag")); |
| 40 | + int vIndex = columns.indexOf("v"); |
| 41 | + int lookupNameIndex = columns.indexOf("lookup_name"); |
| 42 | + int tagIndex = columns.indexOf("tag"); |
| 43 | + int lookupTagIndex = columns.indexOf("lookup_tag"); |
| 44 | + |
| 45 | + List<List<Object>> values = getValuesList(resp); |
| 46 | + assertThat(values, hasSize(20)); |
| 47 | + for (var row : values) { |
| 48 | + assertThat(row, hasSize(7)); |
| 49 | + Long v = (Long) row.get(vIndex); |
| 50 | + assertThat(v, greaterThanOrEqualTo(0L)); |
| 51 | + if (v < 25) { |
| 52 | + assertThat((String) row.get(lookupNameIndex), equalTo("lookup_" + v)); |
| 53 | + String tag = (String) row.get(tagIndex); |
| 54 | + if (tag.equals("local")) { |
| 55 | + assertThat(row.get(lookupTagIndex), equalTo("local")); |
| 56 | + } else { |
| 57 | + assertThat(row.get(lookupTagIndex), equalTo(REMOTE_CLUSTER_1)); |
| 58 | + } |
| 59 | + } else { |
| 60 | + assertNull(row.get(lookupNameIndex)); |
| 61 | + assertNull(row.get(lookupTagIndex)); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + EsqlExecutionInfo executionInfo = resp.getExecutionInfo(); |
| 66 | + assertCCSExecutionInfoDetails(executionInfo); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + protected Map<String, Object> setupClustersAndLookups() throws IOException { |
| 71 | + var setupData = setupClusters(2); |
| 72 | + populateLookupIndex(LOCAL_CLUSTER, "values_lookup", 10); |
| 73 | + populateLookupIndex(REMOTE_CLUSTER_1, "values_lookup", 25); |
| 74 | + return setupData; |
| 75 | + } |
| 76 | + |
| 77 | + protected void populateLookupIndex(String clusterAlias, String indexName, int numDocs) { |
| 78 | + Client client = client(clusterAlias); |
| 79 | + assertAcked( |
| 80 | + client.admin() |
| 81 | + .indices() |
| 82 | + .prepareCreate(indexName) |
| 83 | + .setSettings(Settings.builder().put("index.mode", "lookup")) |
| 84 | + .setMapping("lookup_key", "type=long", "lookup_name", "type=keyword", "lookup_tag", "type=keyword") |
| 85 | + ); |
| 86 | + String tag = Strings.isEmpty(clusterAlias) ? "local" : clusterAlias; |
| 87 | + for (int i = 0; i < numDocs; i++) { |
| 88 | + client.prepareIndex(indexName).setSource("lookup_key", i, "lookup_name", "lookup_" + i, "lookup_tag", tag).get(); |
| 89 | + } |
| 90 | + client.admin().indices().prepareRefresh(indexName).get(); |
| 91 | + } |
| 92 | + |
| 93 | + private static void assertCCSExecutionInfoDetails(EsqlExecutionInfo executionInfo) { |
| 94 | + assertNotNull(executionInfo); |
| 95 | + assertThat(executionInfo.overallTook().millis(), greaterThanOrEqualTo(0L)); |
| 96 | + assertTrue(executionInfo.isCrossClusterSearch()); |
| 97 | + List<EsqlExecutionInfo.Cluster> clusters = executionInfo.clusterAliases().stream().map(executionInfo::getCluster).toList(); |
| 98 | + |
| 99 | + for (EsqlExecutionInfo.Cluster cluster : clusters) { |
| 100 | + assertThat(cluster.getTook().millis(), greaterThanOrEqualTo(0L)); |
| 101 | + assertThat(cluster.getStatus(), equalTo(EsqlExecutionInfo.Cluster.Status.SUCCESSFUL)); |
| 102 | + assertThat(cluster.getSkippedShards(), equalTo(0)); |
| 103 | + assertThat(cluster.getFailedShards(), equalTo(0)); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments