|
7 | 7 |
|
8 | 8 | import java.util.Map; |
9 | 9 | import lombok.RequiredArgsConstructor; |
| 10 | +import org.apache.logging.log4j.LogManager; |
| 11 | +import org.apache.logging.log4j.Logger; |
10 | 12 | import org.apache.logging.log4j.ThreadContext; |
| 13 | +import org.opensearch.OpenSearchTimeoutException; |
11 | 14 | import org.opensearch.common.unit.TimeValue; |
| 15 | +import org.opensearch.sql.common.setting.Settings; |
12 | 16 | import org.opensearch.sql.executor.QueryId; |
13 | 17 | import org.opensearch.sql.executor.QueryManager; |
14 | 18 | import org.opensearch.sql.executor.execution.AbstractPlan; |
| 19 | +import org.opensearch.threadpool.Scheduler; |
15 | 20 | import org.opensearch.threadpool.ThreadPool; |
16 | 21 | import org.opensearch.transport.client.node.NodeClient; |
17 | 22 |
|
18 | 23 | /** QueryManager implemented in OpenSearch cluster. */ |
19 | 24 | @RequiredArgsConstructor |
20 | 25 | public class OpenSearchQueryManager implements QueryManager { |
21 | 26 |
|
| 27 | + private static final Logger LOG = LogManager.getLogger(OpenSearchQueryManager.class); |
| 28 | + |
22 | 29 | private final NodeClient nodeClient; |
23 | 30 |
|
| 31 | + private final Settings settings; |
| 32 | + |
24 | 33 | public static final String SQL_WORKER_THREAD_POOL_NAME = "sql-worker"; |
25 | 34 | public static final String SQL_BACKGROUND_THREAD_POOL_NAME = "sql_background_io"; |
26 | 35 |
|
27 | 36 | @Override |
28 | 37 | public QueryId submit(AbstractPlan queryPlan) { |
29 | | - schedule(nodeClient, () -> queryPlan.execute()); |
| 38 | + TimeValue timeout = settings.getSettingValue(Settings.Key.PPL_QUERY_TIMEOUT); |
| 39 | + schedule(nodeClient, queryPlan::execute, timeout); |
30 | 40 |
|
31 | 41 | return queryPlan.getQueryId(); |
32 | 42 | } |
33 | 43 |
|
34 | | - private void schedule(NodeClient client, Runnable task) { |
| 44 | + private void schedule(NodeClient client, Runnable task, TimeValue timeout) { |
35 | 45 | ThreadPool threadPool = client.threadPool(); |
36 | | - threadPool.schedule(withCurrentContext(task), new TimeValue(0), SQL_WORKER_THREAD_POOL_NAME); |
| 46 | + |
| 47 | + Runnable wrappedTask = |
| 48 | + withCurrentContext( |
| 49 | + () -> { |
| 50 | + final Thread executionThread = Thread.currentThread(); |
| 51 | + |
| 52 | + Scheduler.ScheduledCancellable timeoutTask = |
| 53 | + threadPool.schedule( |
| 54 | + () -> { |
| 55 | + LOG.warn( |
| 56 | + "Query execution timed out after {}. Interrupting execution thread.", |
| 57 | + timeout); |
| 58 | + executionThread.interrupt(); |
| 59 | + }, |
| 60 | + timeout, |
| 61 | + ThreadPool.Names.GENERIC); |
| 62 | + |
| 63 | + try { |
| 64 | + task.run(); |
| 65 | + timeoutTask.cancel(); |
| 66 | + // Clear any leftover thread interrupts to keep the thread pool clean |
| 67 | + Thread.interrupted(); |
| 68 | + } catch (Exception e) { |
| 69 | + timeoutTask.cancel(); |
| 70 | + |
| 71 | + // Special-case handling of timeout-related interruptions |
| 72 | + if (Thread.interrupted() || e.getCause() instanceof InterruptedException) { |
| 73 | + LOG.error("Query was interrupted due to timeout after {}", timeout); |
| 74 | + throw new OpenSearchTimeoutException( |
| 75 | + "Query execution timed out after " + timeout); |
| 76 | + } |
| 77 | + |
| 78 | + throw e; |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + threadPool.schedule(wrappedTask, new TimeValue(0), SQL_WORKER_THREAD_POOL_NAME); |
37 | 83 | } |
38 | 84 |
|
39 | 85 | private Runnable withCurrentContext(final Runnable task) { |
|
0 commit comments