|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.calcite.remote; |
| 7 | + |
| 8 | +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATE_FORMATS; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.concurrent.CompletableFuture; |
| 13 | +import java.util.concurrent.ExecutionException; |
| 14 | +import java.util.concurrent.Executors; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | +import java.util.concurrent.atomic.AtomicInteger; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.opensearch.sql.ppl.PPLIntegTestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Integration tests for {@code UnifiedQueryPlanner} state isolation under concurrent load. |
| 22 | + * |
| 23 | + * <p>The planner's post-analysis pipeline (extensions registered via {@code |
| 24 | + * LanguageSpec.postAnalysisRules}) uses Calcite {@code RelShuttle} subclasses. {@code |
| 25 | + * RelShuttleImpl} inherits a non-thread-safe {@code ArrayDeque<RelNode>} stack used by {@code |
| 26 | + * visitChild}'s push/pop. Any extension that returns the same shuttle instance across {@code |
| 27 | + * plan()} calls is unsafe under concurrent load: cluster threads call {@code plan()} simultaneously |
| 28 | + * and their push/pop on the shared stack interleave, leaving residual entries that surface on a |
| 29 | + * subsequent traversal as {@code NoSuchElementException} at {@code RelShuttleImpl.visitChild} line |
| 30 | + * 67 (the {@code stack.pop()} in the {@code finally} block). |
| 31 | + * |
| 32 | + * <p>The test methods here fire many queries through a thread pool to exercise concurrent {@code |
| 33 | + * plan()} invocations. New planner-level concurrency / state-isolation regressions belong in this |
| 34 | + * class. The current cases cover {@code DatetimeExtension}'s {@code RelHomogeneousShuttle} |
| 35 | + * subclasses ({@code DatetimeUdtNormalizeRule}, {@code DatetimeOutputCastRule}) which were |
| 36 | + * previously returned as static {@code INSTANCE}s and caused the production failure that motivated |
| 37 | + * this suite. |
| 38 | + * |
| 39 | + * <p>Run via: |
| 40 | + * |
| 41 | + * <pre>{@code |
| 42 | + * ./gradlew :integ-test:integTestRemote \ |
| 43 | + * -Dtests.rest.cluster=localhost:9200 -Dtests.cluster=localhost:9300 \ |
| 44 | + * -Dtests.clustername=runTask \ |
| 45 | + * -Dtests.analytics.force_routing=true \ |
| 46 | + * -Dtests.analytics.parquet_indices=true \ |
| 47 | + * --tests org.opensearch.sql.calcite.remote.CalcitePlannerConcurrencyIT |
| 48 | + * }</pre> |
| 49 | + */ |
| 50 | +public class CalcitePlannerConcurrencyIT extends PPLIntegTestCase { |
| 51 | + |
| 52 | + /** Concurrency level — matches the rough parallelism of a dashboard field-stats panel. */ |
| 53 | + private static final int PARALLELISM = 8; |
| 54 | + |
| 55 | + /** Total queries fired per test. */ |
| 56 | + private static final int QUERIES = 80; |
| 57 | + |
| 58 | + @Override |
| 59 | + public void init() throws Exception { |
| 60 | + super.init(); |
| 61 | + enableCalcite(); |
| 62 | + |
| 63 | + // DATE_FORMATS has many datetime columns of different formats/precisions. With |
| 64 | + // -Dtests.analytics.parquet_indices=true the helper provisions it as a parquet-backed |
| 65 | + // composite index — required for analytics-engine routing. |
| 66 | + loadIndex(Index.DATE_FORMATS); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testConcurrentStatsDistinctCountOverDatetime() throws Exception { |
| 71 | + String[] fields = { |
| 72 | + "epoch_millis", "epoch_second", "date_optional_time", "strict_date_optional_time" |
| 73 | + }; |
| 74 | + List<String> queries = new ArrayList<>(QUERIES); |
| 75 | + for (int i = 0; i < QUERIES; i++) { |
| 76 | + String field = fields[i % fields.length]; |
| 77 | + queries.add( |
| 78 | + String.format( |
| 79 | + "source=%s | stats count() as field_count, distinct_count(%s) as distinct_count", |
| 80 | + TEST_INDEX_DATE_FORMATS, field)); |
| 81 | + } |
| 82 | + executeConcurrent(queries); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testConcurrentMixedDatetimePlans() throws Exception { |
| 87 | + // Mix three plan shapes: stats+distinct_count, plain field projection (datetime cast), and |
| 88 | + // stats by a different field. Different plan shapes push the planner's post-analysis shuttles |
| 89 | + // through different visitChild call counts — amplifying any cross-query stack pollution. |
| 90 | + List<String> shapes = |
| 91 | + List.of( |
| 92 | + "source=%s | stats count() as field_count, distinct_count(epoch_millis) as" |
| 93 | + + " distinct_count", |
| 94 | + "source=%s | fields epoch_millis, epoch_second, date_optional_time", |
| 95 | + "source=%s | stats count() as field_count, distinct_count(epoch_second) as" |
| 96 | + + " distinct_count by date_optional_time"); |
| 97 | + List<String> queries = new ArrayList<>(QUERIES); |
| 98 | + for (int i = 0; i < QUERIES; i++) { |
| 99 | + queries.add(String.format(shapes.get(i % shapes.size()), TEST_INDEX_DATE_FORMATS)); |
| 100 | + } |
| 101 | + executeConcurrent(queries); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Fire all queries through a fixed-size thread pool. Asserts every query completes without |
| 106 | + * exception. With a shuttle-state-leak bug present this triggers {@code NoSuchElementException} |
| 107 | + * on at least one task once the stack interleaving corrupts state. |
| 108 | + */ |
| 109 | + private void executeConcurrent(List<String> queries) throws Exception { |
| 110 | + var executor = Executors.newFixedThreadPool(PARALLELISM); |
| 111 | + try { |
| 112 | + List<CompletableFuture<Void>> futures = new ArrayList<>(queries.size()); |
| 113 | + AtomicInteger failures = new AtomicInteger(); |
| 114 | + List<Throwable> errors = new ArrayList<>(); |
| 115 | + for (String query : queries) { |
| 116 | + futures.add( |
| 117 | + CompletableFuture.runAsync( |
| 118 | + () -> { |
| 119 | + try { |
| 120 | + executeQuery(query); |
| 121 | + } catch (Exception e) { |
| 122 | + failures.incrementAndGet(); |
| 123 | + synchronized (errors) { |
| 124 | + errors.add(e); |
| 125 | + } |
| 126 | + } |
| 127 | + }, |
| 128 | + executor)); |
| 129 | + } |
| 130 | + for (CompletableFuture<Void> f : futures) { |
| 131 | + try { |
| 132 | + f.get(60, TimeUnit.SECONDS); |
| 133 | + } catch (ExecutionException e) { |
| 134 | + failures.incrementAndGet(); |
| 135 | + synchronized (errors) { |
| 136 | + errors.add(e.getCause()); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + if (failures.get() > 0) { |
| 141 | + StringBuilder msg = new StringBuilder(); |
| 142 | + msg.append(failures.get()).append("/").append(queries.size()).append(" queries failed:"); |
| 143 | + synchronized (errors) { |
| 144 | + for (int i = 0; i < Math.min(3, errors.size()); i++) { |
| 145 | + msg.append("\n - ").append(errors.get(i)); |
| 146 | + } |
| 147 | + } |
| 148 | + throw new AssertionError(msg.toString()); |
| 149 | + } |
| 150 | + } finally { |
| 151 | + executor.shutdown(); |
| 152 | + executor.awaitTermination(30, TimeUnit.SECONDS); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments