|
17 | 17 |
|
18 | 18 | package org.apache.doris.qe; |
19 | 19 |
|
| 20 | +import org.apache.doris.analysis.UserIdentity; |
| 21 | +import org.apache.doris.catalog.Env; |
20 | 22 | import org.apache.doris.catalog.MysqlColType; |
21 | 23 | import org.apache.doris.common.Config; |
| 24 | +import org.apache.doris.datasource.CatalogMgr; |
22 | 25 | import org.apache.doris.mysql.MysqlChannel; |
23 | 26 | import org.apache.doris.mysql.MysqlSerializer; |
24 | 27 | import org.apache.doris.mysql.protocol.MysqlProtocolAdapter; |
|
39 | 42 | import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; |
40 | 43 | import org.apache.doris.nereids.types.StringType; |
41 | 44 | import org.apache.doris.nereids.types.VarcharType; |
| 45 | +import org.apache.doris.plugin.AuditEvent; |
| 46 | +import org.apache.doris.resource.workloadschedpolicy.WorkloadRuntimeStatusMgr; |
42 | 47 | import org.apache.doris.thrift.TUniqueId; |
43 | 48 |
|
44 | 49 | import org.junit.jupiter.api.Assertions; |
45 | 50 | import org.junit.jupiter.api.Test; |
46 | 51 | import org.junit.jupiter.api.function.Executable; |
| 52 | +import org.mockito.ArgumentCaptor; |
47 | 53 | import org.mockito.MockedConstruction; |
48 | 54 | import org.mockito.MockedStatic; |
49 | 55 | import org.mockito.Mockito; |
@@ -226,9 +232,11 @@ public void testAlwaysOnAuditsExecuteWithoutSessionSwitch() throws Throwable { |
226 | 232 | withAlwaysOn(true, () -> { |
227 | 233 | Harness h = new Harness(MysqlColType.MYSQL_TYPE_LONG); |
228 | 234 | Assertions.assertFalse(h.context.getSessionVariable().isEnablePreparedStmtAuditLog()); |
| 235 | + h.context.setSqlHash("previous-query-hash"); |
229 | 236 | h.execute(new byte[] {0, 1, 3, 0, 42, 0, 0, 0}, audit -> audit.verify(() -> AuditLogHelper.logAuditLog( |
230 | 237 | Mockito.same(h.context), Mockito.eq("EXECUTE `7` USING 42 /*originalSql = select ?*/"), |
231 | 238 | Mockito.isNull(), Mockito.isNull(), Mockito.eq(true)))); |
| 239 | + Assertions.assertEquals("1fe1379fe2a31b8d16219655761820a2", h.context.getSqlHash()); |
232 | 240 | }); |
233 | 241 | } |
234 | 242 |
|
@@ -283,6 +291,61 @@ public void testAlwaysOnAuditsExecuteFailedOnBinding() throws Throwable { |
283 | 291 | }); |
284 | 292 | } |
285 | 293 |
|
| 294 | + @Test |
| 295 | + public void testAlwaysOnEarlyFailuresDoNotReusePreviousStatementIdentity() throws Throwable { |
| 296 | + withAlwaysOn(true, () -> { |
| 297 | + for (MysqlColType type : new MysqlColType[] {MysqlColType.MYSQL_TYPE_LONG, |
| 298 | + MysqlColType.MYSQL_TYPE_VARSTRING}) { |
| 299 | + Harness h = new Harness(type); |
| 300 | + h.context.getSessionVariable().maxAllowedPacket = 1; |
| 301 | + h.upload(7, 0, new byte[] {65}); |
| 302 | + if (type == MysqlColType.MYSQL_TYPE_VARSTRING) { |
| 303 | + h.upload(7, 0, new byte[] {66}); |
| 304 | + } |
| 305 | + // An unrelated COM_QUERY left this identity on the same connection. |
| 306 | + h.context.setStmtId(42); |
| 307 | + h.context.setSqlHash("previous-query-hash"); |
| 308 | + h.context.setCurrentUserIdentity(UserIdentity.ROOT); |
| 309 | + h.context.setStartTime(); |
| 310 | + h.context.getState().reset(); |
| 311 | + |
| 312 | + Env env = Mockito.mock(Env.class); |
| 313 | + Mockito.when(env.getCatalogMgr()).thenReturn(Mockito.mock(CatalogMgr.class)); |
| 314 | + WorkloadRuntimeStatusMgr statusMgr = Mockito.mock(WorkloadRuntimeStatusMgr.class); |
| 315 | + Mockito.when(env.getWorkloadRuntimeStatusMgr()).thenReturn(statusMgr); |
| 316 | + try (MockedStatic<Env> mockedEnv = Mockito.mockStatic(Env.class)) { |
| 317 | + mockedEnv.when(Env::getCurrentEnv).thenReturn(env); |
| 318 | + h.processor.handleExecute(h.prepared.command, 7, h.prepared, |
| 319 | + ByteBuffer.wrap(new byte[] {0, 0}).order(ByteOrder.LITTLE_ENDIAN), null); |
| 320 | + |
| 321 | + ArgumentCaptor<AuditEvent> event = ArgumentCaptor.forClass(AuditEvent.class); |
| 322 | + Mockito.verify(statusMgr).submitFinishQueryToAudit(event.capture()); |
| 323 | + Assertions.assertEquals("ERR", event.getValue().state); |
| 324 | + Assertions.assertEquals("select ?", event.getValue().stmt); |
| 325 | + Assertions.assertEquals("NaN", event.getValue().queryId); |
| 326 | + Assertions.assertEquals(0, event.getValue().stmtId); |
| 327 | + Assertions.assertEquals("1fe1379fe2a31b8d16219655761820a2", event.getValue().sqlHash); |
| 328 | + } |
| 329 | + h.context.setQueryId(new TUniqueId(8L, 8L)); |
| 330 | + h.execute(new byte[] {0, 1, 3, 0, 42, 0, 0, 0}); |
| 331 | + Assertions.assertEquals("1fe1379fe2a31b8d16219655761820a2", h.context.getSqlHash()); |
| 332 | + } |
| 333 | + }); |
| 334 | + } |
| 335 | + |
| 336 | + @Test |
| 337 | + public void testAlwaysOnExecutePreservesEffectivePreparationHash() throws Throwable { |
| 338 | + withAlwaysOn(true, () -> { |
| 339 | + ConnectContext context = new ConnectContext(); |
| 340 | + context.setSqlHash("effective-sql-conversion-fallback-hash"); |
| 341 | + Harness h = new Harness(context, List.of( |
| 342 | + new Placeholder(new PlaceholderId(0), MysqlColType.MYSQL_TYPE_LONG.getCode()))); |
| 343 | + context.setSqlHash("another-command-hash"); |
| 344 | + h.execute(new byte[] {0, 1, 3, 0, 42, 0, 0, 0}); |
| 345 | + Assertions.assertEquals("effective-sql-conversion-fallback-hash", context.getSqlHash()); |
| 346 | + }); |
| 347 | + } |
| 348 | + |
286 | 349 | // With the policy off, neither failure is audited, as before. |
287 | 350 | @Test |
288 | 351 | public void testFailedExecuteIsNotAuditedWhenAlwaysOnIsOff() throws Throwable { |
|
0 commit comments