|
14 | 14 | package io.trino.gateway.ha.handler; |
15 | 15 |
|
16 | 16 | import com.google.common.collect.ImmutableList; |
| 17 | +import io.trino.gateway.ha.config.RequestAnalyzerConfig; |
| 18 | +import jakarta.servlet.ReadListener; |
| 19 | +import jakarta.servlet.ServletInputStream; |
17 | 20 | import jakarta.servlet.http.HttpServletRequest; |
| 21 | +import jakarta.ws.rs.HttpMethod; |
18 | 22 | import org.junit.jupiter.api.Test; |
19 | 23 | import org.junit.jupiter.api.TestInstance; |
20 | 24 | import org.junit.jupiter.api.TestInstance.Lifecycle; |
21 | 25 | import org.mockito.Mockito; |
22 | 26 |
|
| 27 | +import java.io.BufferedReader; |
| 28 | +import java.io.ByteArrayInputStream; |
23 | 29 | import java.io.IOException; |
| 30 | +import java.io.StringReader; |
24 | 31 | import java.util.List; |
25 | 32 |
|
26 | 33 | import static io.trino.gateway.ha.handler.ProxyUtils.extractQueryIdIfPresent; |
27 | 34 | import static io.trino.gateway.ha.handler.ProxyUtils.getQueryUser; |
28 | 35 | import static io.trino.gateway.ha.handler.QueryIdCachingProxyHandler.AUTHORIZATION; |
29 | 36 | import static io.trino.gateway.ha.handler.QueryIdCachingProxyHandler.USER_HEADER; |
| 37 | +import static java.nio.charset.StandardCharsets.UTF_8; |
30 | 38 | import static org.assertj.core.api.Assertions.assertThat; |
| 39 | +import static org.mockito.Mockito.when; |
31 | 40 |
|
32 | 41 | @TestInstance(Lifecycle.PER_CLASS) |
33 | 42 | public class TestQueryIdCachingProxyHandler |
@@ -66,6 +75,186 @@ public void testExtractQueryIdFromUrl() |
66 | 75 | .isNull(); |
67 | 76 | } |
68 | 77 |
|
| 78 | + @Test |
| 79 | + void testQueryIdFromKill() |
| 80 | + throws IOException |
| 81 | + { |
| 82 | + RequestAnalyzerConfig requestAnalyzerConfig = new RequestAnalyzerConfig(); |
| 83 | + requestAnalyzerConfig.setAnalyzeRequest(true); |
| 84 | + assertThat( |
| 85 | + extractQueryIdIfPresent( |
| 86 | + prepareMockRequestWithBody("CALL system.runtime.kill_query(query_id => '20200416_160256_03078_6b4yt', message => 'If he dies, he dies')"), |
| 87 | + ImmutableList.of(), requestAnalyzerConfig)) |
| 88 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 89 | + |
| 90 | + assertThat( |
| 91 | + extractQueryIdIfPresent( |
| 92 | + prepareMockRequestWithBody("CALL system.runtime.kill_query(Query_id => '20200416_160256_03078_6b4yt', Message => 'If he dies, he dies')"), |
| 93 | + ImmutableList.of(), |
| 94 | + requestAnalyzerConfig)) |
| 95 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 96 | + |
| 97 | + assertThat( |
| 98 | + extractQueryIdIfPresent( |
| 99 | + prepareMockRequestWithBody("CALL kill_query('20200416_160256_03078_6b4yt', 'If he dies, he dies')"), |
| 100 | + ImmutableList.of(), |
| 101 | + requestAnalyzerConfig)) |
| 102 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 103 | + |
| 104 | + assertThat( |
| 105 | + extractQueryIdIfPresent( |
| 106 | + prepareMockRequestWithBody("CALL runtime.kill_query('20200416_160256_03078_6b4yt', '20200416_160256_03078_7n5uy')"), |
| 107 | + ImmutableList.of(), |
| 108 | + requestAnalyzerConfig)) |
| 109 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 110 | + |
| 111 | + assertThat( |
| 112 | + extractQueryIdIfPresent( |
| 113 | + prepareMockRequestWithBody("CALL system.runtime.kill_query('20200416_160256_03078_6b4yt', 'kill_query(''20200416_160256_03078_7n5uy'')')"), |
| 114 | + ImmutableList.of(), |
| 115 | + requestAnalyzerConfig)) |
| 116 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 117 | + |
| 118 | + assertThat( |
| 119 | + extractQueryIdIfPresent( |
| 120 | + prepareMockRequestWithBody("CALL system.runtime.kill_query('20200416_160256_03078_6b4yt', '20200416_160256_03078_7n5uy')"), |
| 121 | + ImmutableList.of(), |
| 122 | + requestAnalyzerConfig)) |
| 123 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 124 | + |
| 125 | + assertThat(extractQueryIdIfPresent(prepareMockRequestWithBody("CALL system.runtime.kill_query(query_id=>'20200416_160256_03078_6b4yt')"), |
| 126 | + ImmutableList.of(), |
| 127 | + requestAnalyzerConfig)) |
| 128 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 129 | + |
| 130 | + assertThat(extractQueryIdIfPresent(prepareMockRequestWithBody("CALL system.runtime.kill_query('20200416_160256_03078_6b4yt')"), |
| 131 | + ImmutableList.of(), |
| 132 | + requestAnalyzerConfig)) |
| 133 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 134 | + |
| 135 | + assertThat(extractQueryIdIfPresent(prepareMockRequestWithBody("CALL kill_query('20200416_160256_03078_6b4yt')"), ImmutableList.of(), requestAnalyzerConfig)) |
| 136 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 137 | + |
| 138 | + assertThat(extractQueryIdIfPresent(prepareMockRequestWithBody("call Kill_Query('20200416_160256_03078_6b4yt')"), ImmutableList.of(), requestAnalyzerConfig)) |
| 139 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 140 | + |
| 141 | + assertThat(extractQueryIdIfPresent(prepareMockRequestWithBody("SELECT * FROM postgres.query_logs.queries WHERE sql LIKE '%kill_query(''20200416_160256%' "), |
| 142 | + ImmutableList.of(), |
| 143 | + requestAnalyzerConfig)) |
| 144 | + .isNull(); |
| 145 | + |
| 146 | + assertThat(extractQueryIdIfPresent( |
| 147 | + prepareMockRequestWithBody("select * from postgres.query_logs.queries where sql like '%kill_query(''20200416_160256_03078_6b4yt' "), |
| 148 | + ImmutableList.of(), |
| 149 | + requestAnalyzerConfig)) |
| 150 | + .isNull(); |
| 151 | + |
| 152 | + assertThat(extractQueryIdIfPresent( |
| 153 | + prepareMockRequestWithBody("select * from postgres.query_logs.queries where sql LIKE 'CALL kill_query(_20200416_160256_03078_6b4yt_)' "), |
| 154 | + ImmutableList.of(), |
| 155 | + requestAnalyzerConfig)) |
| 156 | + .isNull(); |
| 157 | + |
| 158 | + assertThat( |
| 159 | + extractQueryIdIfPresent( |
| 160 | + prepareMockRequestWithBody(""" |
| 161 | + --CALL kill_query('20200416_160256_03078_6b4yt', 'If he dies, he dies') |
| 162 | + SELECT 1 |
| 163 | + """), |
| 164 | + ImmutableList.of(), |
| 165 | + requestAnalyzerConfig)) |
| 166 | + .isNull(); |
| 167 | + |
| 168 | + assertThat( |
| 169 | + extractQueryIdIfPresent( |
| 170 | + prepareMockRequestWithBody(""" |
| 171 | + /* |
| 172 | + CALL kill_query('20200416_160256_03078_6b4yt', 'If he dies, he dies') |
| 173 | + */ |
| 174 | + SELECT 1 |
| 175 | + """), |
| 176 | + ImmutableList.of(), |
| 177 | + requestAnalyzerConfig)) |
| 178 | + .isNull(); |
| 179 | + |
| 180 | + assertThat( |
| 181 | + extractQueryIdIfPresent( |
| 182 | + prepareMockRequestWithBody(""" |
| 183 | + CALL KILL_QUERY('20200416_160256_03078_6b4yt', 'If he dies, he dies') |
| 184 | + """), |
| 185 | + ImmutableList.of(), |
| 186 | + requestAnalyzerConfig)) |
| 187 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 188 | + |
| 189 | + assertThat( |
| 190 | + extractQueryIdIfPresent( |
| 191 | + prepareMockRequestWithBody(""" |
| 192 | + CALL KILL_QUERY ('20200416_160256_03078_6b4yt', 'If he dies, he dies') |
| 193 | + """), |
| 194 | + ImmutableList.of(), |
| 195 | + requestAnalyzerConfig)) |
| 196 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 197 | + |
| 198 | + assertThat( |
| 199 | + extractQueryIdIfPresent( |
| 200 | + prepareMockRequestWithBody(""" |
| 201 | + CALL |
| 202 | + KILL_QUERY |
| 203 | + ( |
| 204 | + -- this is a comment |
| 205 | + '20200416_160256_03078_6b4yt' --this is a trailing comment |
| 206 | + , |
| 207 | + /* |
| 208 | + this is |
| 209 | + a multiline comment |
| 210 | + */ |
| 211 | + 'If he dies, he dies |
| 212 | + ') |
| 213 | + """), |
| 214 | + ImmutableList.of(), |
| 215 | + requestAnalyzerConfig)) |
| 216 | + .isEqualTo("20200416_160256_03078_6b4yt"); |
| 217 | + } |
| 218 | + |
| 219 | + private static HttpServletRequest prepareMockRequestWithBody(String query) |
| 220 | + throws IOException |
| 221 | + { |
| 222 | + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); |
| 223 | + |
| 224 | + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(query.getBytes(UTF_8)); |
| 225 | + when(request.getMethod()).thenReturn(HttpMethod.POST); |
| 226 | + when(request.getInputStream()).thenReturn(new ServletInputStream() |
| 227 | + { |
| 228 | + @Override |
| 229 | + public boolean isFinished() |
| 230 | + { |
| 231 | + return byteArrayInputStream.available() > 0; |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + public boolean isReady() |
| 236 | + { |
| 237 | + return true; |
| 238 | + } |
| 239 | + |
| 240 | + @Override |
| 241 | + public void setReadListener(ReadListener readListener) |
| 242 | + {} |
| 243 | + |
| 244 | + public int read() |
| 245 | + throws IOException |
| 246 | + { |
| 247 | + return byteArrayInputStream.read(); |
| 248 | + } |
| 249 | + }); |
| 250 | + |
| 251 | + when(request.getReader()).thenReturn(new BufferedReader(new StringReader(query))); |
| 252 | + |
| 253 | + when(request.getQueryString()).thenReturn(""); |
| 254 | + |
| 255 | + return request; |
| 256 | + } |
| 257 | + |
69 | 258 | @Test |
70 | 259 | public void testUserFromRequest() |
71 | 260 | throws IOException |
|
0 commit comments