-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[improvement](fe) Improve audit logs for S3 streaming insert jobs #67489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JNSimba
wants to merge
2
commits into
apache:master
Choose a base branch
from
JNSimba:fix/streaming-insert-audit-log
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+263
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
...t/java/org/apache/doris/job/extensions/insert/streaming/StreamingInsertTaskAuditTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package org.apache.doris.job.extensions.insert.streaming; | ||
|
|
||
| import org.apache.doris.analysis.StmtType; | ||
| import org.apache.doris.analysis.UserIdentity; | ||
| import org.apache.doris.catalog.Env; | ||
| import org.apache.doris.common.Pair; | ||
| import org.apache.doris.common.jmockit.Deencapsulation; | ||
| import org.apache.doris.common.profile.SummaryProfile; | ||
| import org.apache.doris.datasource.CatalogMgr; | ||
| import org.apache.doris.datasource.InternalCatalog; | ||
| import org.apache.doris.job.exception.JobException; | ||
| import org.apache.doris.job.extensions.insert.InsertTask; | ||
| import org.apache.doris.job.offset.Offset; | ||
| import org.apache.doris.job.offset.SourceOffsetProvider; | ||
| import org.apache.doris.job.offset.jdbc.JdbcTvfSourceOffsetProvider; | ||
| import org.apache.doris.job.offset.s3.S3Offset; | ||
| import org.apache.doris.job.offset.s3.S3SourceOffsetProvider; | ||
| import org.apache.doris.nereids.StatementContext; | ||
| import org.apache.doris.nereids.analyzer.UnboundTVFRelation; | ||
| import org.apache.doris.nereids.glue.LogicalPlanAdapter; | ||
| import org.apache.doris.nereids.parser.NereidsParser; | ||
| import org.apache.doris.nereids.trees.expressions.Properties; | ||
| import org.apache.doris.nereids.trees.plans.RelationId; | ||
| import org.apache.doris.nereids.trees.plans.commands.insert.InsertIntoTableCommand; | ||
| import org.apache.doris.plugin.AuditEvent; | ||
| import org.apache.doris.qe.ConnectContext; | ||
| import org.apache.doris.qe.StmtExecutor; | ||
| import org.apache.doris.resource.workloadschedpolicy.WorkloadRuntimeStatusMgr; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.MockedStatic; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.TreeMap; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| public class StreamingInsertTaskAuditTest { | ||
| private static final String ORIGIN_URI = "s3://bucket/input/*.csv"; | ||
| private static final String RESOLVED_URI = "s3://bucket/input/{1.csv,2.csv}"; | ||
| private static final String S3_SQL = "insert into target_table select * from s3(" | ||
| + "\"uri\" = \"" + ORIGIN_URI + "\", " | ||
| + "\"s3.secret_key\" = \"private-value\", " | ||
| + "\"enclose\" = \"\\\"\")"; | ||
|
|
||
| @Test | ||
| public void testS3RunSubmitsAuditEvent() throws Exception { | ||
| AuditEvent auditEvent = runS3Task(null); | ||
|
|
||
| Assert.assertEquals(AuditEvent.EventType.AFTER_QUERY, auditEvent.type); | ||
| Assert.assertEquals(StmtType.INSERT.name(), auditEvent.stmtType); | ||
| Assert.assertFalse(auditEvent.stmt.contains(ORIGIN_URI)); | ||
| Assert.assertTrue(auditEvent.stmt.contains(RESOLVED_URI)); | ||
| Assert.assertFalse(auditEvent.stmt.contains("private-value")); | ||
| Assert.assertEquals("OK", auditEvent.state); | ||
| Assert.assertTrue(auditEvent.isInternal); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFailedS3RunSubmitsErrorAuditEvent() throws Exception { | ||
| AuditEvent auditEvent = runS3Task(new RuntimeException("insert failed")); | ||
|
|
||
| Assert.assertEquals(AuditEvent.EventType.AFTER_QUERY, auditEvent.type); | ||
| Assert.assertEquals(StmtType.INSERT.name(), auditEvent.stmtType); | ||
| Assert.assertEquals("ERR", auditEvent.state); | ||
| Assert.assertTrue(auditEvent.errorMessage.contains("insert failed")); | ||
| Assert.assertTrue(auditEvent.isInternal); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCdcRunDoesNotSubmitAuditEvent() throws Exception { | ||
| String sql = "insert into target_table select * from cdc_stream(" | ||
| + "\"type\" = \"mysql\", \"jdbc_url\" = \"jdbc:mysql://127.0.0.1:3306\", " | ||
| + "\"table\" = \"source_table\", \"offset\" = \"latest\")"; | ||
| runTask(null, sql, Mockito.mock(JdbcTvfSourceOffsetProvider.class), Mockito.mock(Offset.class), false); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRewriteS3UriCaseInsensitive() { | ||
| Map<String, String> originProperties = new HashMap<>(); | ||
| originProperties.put("URI", ORIGIN_URI); | ||
| UnboundTVFRelation originTvf = new UnboundTVFRelation( | ||
| new RelationId(1), "s3", new Properties(originProperties)); | ||
| InsertIntoTableCommand originCommand = Mockito.mock(InsertIntoTableCommand.class); | ||
| Mockito.when(originCommand.getParsedPlan()).thenReturn(Optional.of(originTvf)); | ||
|
|
||
| S3Offset offset = new S3Offset(); | ||
| offset.setFileLists(RESOLVED_URI); | ||
| Env env = Mockito.mock(Env.class); | ||
| Mockito.when(env.isMaster()).thenReturn(false); | ||
| try (MockedStatic<Env> mockedEnv = Mockito.mockStatic(Env.class)) { | ||
| mockedEnv.when(Env::getCurrentEnv).thenReturn(env); | ||
| InsertIntoTableCommand rewritten = new S3SourceOffsetProvider() | ||
| .rewriteTvfParams(originCommand, offset, 1L); | ||
|
|
||
| Map<String, String> rewrittenProperties = | ||
| rewritten.getAllTVFRelation().get(0).getProperties().getMap(); | ||
| Assert.assertEquals(1, rewrittenProperties.size()); | ||
| Assert.assertEquals(RESOLVED_URI, rewrittenProperties.get("URI")); | ||
| } | ||
| } | ||
|
|
||
| private AuditEvent runS3Task(RuntimeException commandFailure) throws Exception { | ||
| S3Offset offset = new S3Offset(); | ||
| offset.setFileLists(RESOLVED_URI); | ||
| return runTask(commandFailure, S3_SQL, new S3SourceOffsetProvider(), offset, true); | ||
| } | ||
|
|
||
| private AuditEvent runTask(RuntimeException commandFailure, String sql, | ||
| SourceOffsetProvider offsetProvider, Offset offset, boolean expectAudit) throws Exception { | ||
| Env env = Mockito.mock(Env.class); | ||
| CatalogMgr catalogMgr = Mockito.mock(CatalogMgr.class); | ||
| InternalCatalog catalog = Mockito.mock(InternalCatalog.class); | ||
| WorkloadRuntimeStatusMgr statusMgr = Mockito.mock(WorkloadRuntimeStatusMgr.class); | ||
| Mockito.when(env.getCatalogMgr()).thenReturn(catalogMgr); | ||
| Mockito.when(env.getInternalCatalog()).thenReturn(catalog); | ||
| Mockito.when(catalogMgr.getCatalog(Mockito.anyString())).thenReturn(catalog); | ||
| Mockito.when(catalog.getName()).thenReturn("internal"); | ||
| Mockito.when(env.getWorkloadRuntimeStatusMgr()).thenReturn(statusMgr); | ||
|
|
||
| try (MockedStatic<Env> mockedEnv = Mockito.mockStatic(Env.class)) { | ||
| mockedEnv.when(Env::getCurrentEnv).thenReturn(env); | ||
|
|
||
| ConnectContext ctx = InsertTask.makeConnectContext(UserIdentity.ROOT, "test_db"); | ||
| ctx.getState().setOk(); | ||
| InsertIntoTableCommand command = Mockito.mock(InsertIntoTableCommand.class); | ||
| if (expectAudit) { | ||
| Map<String, String> rewrittenTvfProps = new HashMap<>(); | ||
| rewrittenTvfProps.put("uri", RESOLVED_URI); | ||
| rewrittenTvfProps.put("s3.secret_key", "private-value"); | ||
| rewrittenTvfProps.put("enclose", "\""); | ||
| UnboundTVFRelation tvf = Mockito.mock(UnboundTVFRelation.class); | ||
| Mockito.when(tvf.getProperties()).thenReturn(new Properties(rewrittenTvfProps)); | ||
| Mockito.when(command.getAllTVFRelation()).thenReturn(Collections.singletonList(tvf)); | ||
| } | ||
| AtomicLong commandStartTime = new AtomicLong(); | ||
| Mockito.doAnswer(invocation -> { | ||
| commandStartTime.set(ctx.getStartTime()); | ||
| if (commandFailure != null) { | ||
| throw commandFailure; | ||
| } | ||
| return null; | ||
| }).when(command).run(Mockito.eq(ctx), Mockito.any(StmtExecutor.class)); | ||
|
|
||
| LogicalPlanAdapter parsedStmt = new LogicalPlanAdapter( | ||
| new NereidsParser().parseSingle(sql), new StatementContext()); | ||
| StmtExecutor executor = Mockito.mock(StmtExecutor.class); | ||
| Mockito.when(executor.getParsedStmt()).thenReturn(parsedStmt); | ||
| Mockito.when(executor.getExternalDmlAuditBackendIds()).thenReturn(Collections.emptySet()); | ||
| Mockito.when(executor.getSummaryProfile()).thenReturn(Mockito.mock(SummaryProfile.class)); | ||
| ctx.setExecutor(executor); | ||
|
|
||
| StreamingInsertTask task = new StreamingInsertTask( | ||
| 1L, 2L, sql, offsetProvider, "test_db", null, | ||
| Collections.emptyMap(), UserIdentity.ROOT, null); | ||
| Deencapsulation.setField(task, "ctx", ctx); | ||
| Deencapsulation.setField(task, "taskCommand", command); | ||
| Deencapsulation.setField(task, "stmtExecutor", executor); | ||
| Deencapsulation.setField(task, "runningOffset", offset); | ||
| if (expectAudit) { | ||
| TreeMap<Pair<Integer, Integer>, String> replacements = | ||
| new TreeMap<>(new Pair.PairComparator<>()); | ||
| new NereidsParser().parseForEncryption(sql, replacements); | ||
| Deencapsulation.setField(task, "auditSql", | ||
| Deencapsulation.invoke(task, "getAuditSql", replacements)); | ||
| } | ||
|
|
||
| if (commandFailure == null) { | ||
| task.run(); | ||
| } else { | ||
| Assert.assertThrows(JobException.class, task::run); | ||
| } | ||
|
|
||
| if (!expectAudit) { | ||
| Mockito.verify(statusMgr, Mockito.never()) | ||
| .submitFinishQueryToAudit(Mockito.any(AuditEvent.class)); | ||
| return null; | ||
| } | ||
| ArgumentCaptor<AuditEvent> auditEventCaptor = ArgumentCaptor.forClass(AuditEvent.class); | ||
| Mockito.verify(statusMgr).submitFinishQueryToAudit(auditEventCaptor.capture()); | ||
|
JNSimba marked this conversation as resolved.
|
||
| AuditEvent auditEvent = auditEventCaptor.getValue(); | ||
| Assert.assertTrue(commandStartTime.get() > 0); | ||
| Assert.assertEquals(commandStartTime.get(), auditEvent.timestamp); | ||
| return auditEvent; | ||
| } finally { | ||
| ConnectContext.remove(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.