Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1k
PHOENIX-6456 Support query logging for DDL and DML#1210
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
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
248 changes: 248 additions & 0 deletions
248 phoenix-core/src/it/java/org/apache/phoenix/end2end/AuditLoggingIT.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,248 @@ | ||
| /* | ||
| * 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.phoenix.end2end; | ||
| import org.apache.phoenix.log.LogLevel; | ||
| import org.apache.phoenix.query.QueryServices; | ||
| import org.apache.phoenix.util.PropertiesUtil; | ||
| import org.junit.Test; | ||
| import java.sql.*; | ||
| import java.util.Properties; | ||
| import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assert.assertEquals; | ||
| public class AuditLoggingIT extends ParallelStatsDisabledIT { | ||
| @Test | ||
| public void testEmptyLogging() throws Exception { | ||
| String createqQery = "create table test1 (mykey integer not null primary key," + | ||
| " mycolumn varchar)"; | ||
| String upsertQuery = "upsert into test1 values (1,'Hello')"; | ||
| String selectQuery = "select * from test1"; | ||
| String getLogsQuery = "select * from SYSTEM.LOG WHERE TABLE_NAME='TEST1' order by start_time"; | ||
| Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); | ||
| Connection conn = DriverManager.getConnection(getUrl(), props); | ||
| conn.setAutoCommit(true); | ||
| try { | ||
| Statement stmt = conn.createStatement(); | ||
| stmt.execute(createqQery); | ||
| stmt.execute(upsertQuery); | ||
| stmt.executeQuery(selectQuery); | ||
| conn.commit(); | ||
| ResultSet rs = stmt.executeQuery(getLogsQuery); | ||
| assertFalse(rs.next()); | ||
| } finally { | ||
| conn.close(); | ||
| } | ||
| } | ||
| @Test | ||
| public void testLoggingSelect() throws Exception { | ||
| String createqQery = "create table test2 (mykey integer not null primary key," + | ||
| " mycolumn varchar)"; | ||
| String upsertQuery = "upsert into test2 values (1,'Hello')"; | ||
| String selectQuery = "select * from test2"; | ||
| String getLogsQuery = "select * from SYSTEM.LOG WHERE TABLE_NAME='TEST2' order by start_time"; | ||
| Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); | ||
| props.setProperty(QueryServices.LOG_LEVEL, LogLevel.TRACE.name()); | ||
| Connection conn = DriverManager.getConnection(getUrl(), props); | ||
| conn.setAutoCommit(true); | ||
| try { | ||
| conn.createStatement().execute(createqQery); | ||
| conn.createStatement().execute(upsertQuery); | ||
| ResultSet rs = conn.createStatement().executeQuery(selectQuery); | ||
| assertTrue(rs.next()); | ||
| assertFalse(rs.next()); | ||
| rs.close(); | ||
| ResultSet rs2 = conn.createStatement().executeQuery(getLogsQuery); | ||
| assertTrue(rs2.next()); | ||
| assertEquals(rs2.getString(7), selectQuery); | ||
| assertFalse(rs2.next()); | ||
| } finally { | ||
| conn.close(); | ||
| } | ||
| } | ||
| @Test | ||
| public void testLoggingDMLAandDDL() throws Exception { | ||
| String createqQery = "create table test3 (mykey integer not null primary key," + | ||
| " mycolumn varchar)"; | ||
| String upsertQuery = "upsert into test3 values (1,'Hello')"; | ||
| String selectQuery = "select * from test3"; | ||
| String getLogsQuery = "select * from SYSTEM.LOG WHERE TABLE_NAME='TEST3' order by start_time"; | ||
| Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); | ||
| props.setProperty(QueryServices.AUDIT_LOG_LEVEL, LogLevel.INFO.name()); | ||
| Connection conn = DriverManager.getConnection(getUrl(), props); | ||
| conn.setAutoCommit(true); | ||
| try { | ||
| conn.createStatement().execute(createqQery); | ||
| conn.createStatement().execute(upsertQuery); | ||
| ResultSet rs = conn.createStatement().executeQuery(selectQuery); | ||
| assertTrue(rs.next()); | ||
| assertFalse(rs.next()); | ||
| rs.close(); | ||
| ResultSet rs2 = conn.createStatement().executeQuery(getLogsQuery); | ||
| assertTrue(rs2.next()); | ||
| assertEquals(rs2.getString(7), createqQery); | ||
| assertTrue(rs2.next()); | ||
| assertEquals(rs2.getString(7), upsertQuery); | ||
| assertFalse(rs2.next()); | ||
| } finally { | ||
| conn.close(); | ||
| } | ||
| } | ||
| @Test | ||
| public void testLoggingDMLAandDDLandSelect() throws Exception { | ||
| String createqQery = "create table test4 (mykey integer not null primary key," + | ||
| " mycolumn varchar)"; | ||
| String upsertQuery = "upsert into test4 values (1,'Hello')"; | ||
| String selectQuery = "select * from test4"; | ||
| String getLogsQuery = "select * from SYSTEM.LOG WHERE TABLE_NAME='TEST4' order by start_time"; | ||
| Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); | ||
| props.setProperty(QueryServices.AUDIT_LOG_LEVEL, LogLevel.INFO.name()); | ||
| props.setProperty(QueryServices.LOG_LEVEL, LogLevel.TRACE.name()); | ||
| Connection conn = DriverManager.getConnection(getUrl(), props); | ||
| conn.setAutoCommit(true); | ||
| try { | ||
| Statement stat = conn.createStatement(); | ||
| stat.execute(createqQery); | ||
| stat.execute(upsertQuery); | ||
| ResultSet rs = stat.executeQuery(selectQuery); | ||
| assertTrue(rs.next()); | ||
| assertFalse(rs.next()); | ||
| rs.close(); | ||
| ResultSet rs2 = conn.createStatement().executeQuery(getLogsQuery); | ||
| assertTrue(rs2.next()); | ||
| assertEquals(rs2.getString(7), createqQery); | ||
| assertTrue(rs2.next()); | ||
| assertEquals(rs2.getString(7), upsertQuery); | ||
| assertTrue(rs2.next()); | ||
| assertEquals(rs2.getString(7), selectQuery); | ||
| assertFalse(rs2.next()); | ||
| } finally { | ||
| conn.close(); | ||
| } | ||
| } | ||
| @Test | ||
| public void testLogginParameterizedUpsert() throws Exception { | ||
| String createqQery = "create table test5 (mykey integer not null primary key," + | ||
| " mycolumn varchar)"; | ||
| String upsertQuery = "upsert into test5 values (?, ?)"; | ||
| String selectQuery = "select * from test5"; | ||
| String getLogsQuery = "select * from SYSTEM.LOG WHERE TABLE_NAME='TEST5' order by start_time"; | ||
| Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); | ||
| props.setProperty(QueryServices.AUDIT_LOG_LEVEL, LogLevel.INFO.name()); | ||
| props.setProperty(QueryServices.LOG_LEVEL, LogLevel.TRACE.name()); | ||
| Connection conn = DriverManager.getConnection(getUrl(), props); | ||
| conn.setAutoCommit(true); | ||
| try { | ||
| Statement stat = conn.createStatement(); | ||
| stat.execute(createqQery); | ||
| PreparedStatement p = conn.prepareStatement(upsertQuery); | ||
| p.setInt(1, 1); | ||
| p.setString(2, "foo"); | ||
| p.execute(); | ||
| p.setInt(1, 2); | ||
| p.setString(2, "bar"); | ||
| p.execute(); | ||
| ResultSet rs = stat.executeQuery(selectQuery); | ||
| assertTrue(rs.next()); | ||
| assertTrue(rs.next()); | ||
| assertFalse(rs.next()); | ||
| rs.close(); | ||
| ResultSet rs2 = conn.createStatement().executeQuery(getLogsQuery); | ||
| assertTrue(rs2.next()); | ||
| assertTrue(rs2.next()); | ||
| assertEquals("1,foo", rs2.getString(13)); | ||
| assertTrue(rs2.next()); | ||
| assertEquals( "2,bar", rs2.getString(13)); | ||
| assertTrue(rs2.next()); | ||
| assertFalse(rs2.next()); | ||
| } finally { | ||
| conn.close(); | ||
| } | ||
| } | ||
| @Test | ||
| public void testlogSamplingRate() throws Exception { | ||
| String createqQery = "create table test6 (mykey integer not null primary key," + | ||
| " mycolumn varchar)"; | ||
| String selectQuery = "select * from test6"; | ||
| String getLogsQuery = "select * from SYSTEM.LOG WHERE TABLE_NAME='TEST6' order by start_time"; | ||
| Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); | ||
| props.setProperty(QueryServices.AUDIT_LOG_LEVEL, LogLevel.INFO.name()); | ||
| props.setProperty(QueryServices.LOG_LEVEL, LogLevel.TRACE.name()); | ||
| props.setProperty(QueryServices.LOG_SAMPLE_RATE, "0.5"); | ||
| Connection conn = DriverManager.getConnection(getUrl(), props); | ||
| conn.setAutoCommit(true); | ||
| try { | ||
| Statement stat = conn.createStatement(); | ||
| stat.execute(createqQery); | ||
| String upsertQuery; | ||
| for (int i = 0; i<100; i++) { | ||
| upsertQuery = "upsert into test6 values (" + i + ",'asd')"; | ||
| stat.execute(upsertQuery); | ||
| ResultSet rs = stat.executeQuery(selectQuery); | ||
| assertTrue(rs.next()); | ||
| rs.close(); | ||
| } | ||
| ResultSet rs2 = conn.createStatement().executeQuery(getLogsQuery); | ||
| int numOfUpserts = 0; | ||
| int numOfSelects = 0; | ||
| while (rs2.next()) { | ||
| String query = rs2.getString(7); | ||
| if (query.equals(selectQuery)) { | ||
| numOfSelects++; | ||
| } | ||
| else if (query.contains("upsert into test6 values (")) { | ||
| numOfUpserts++; | ||
| } | ||
| } | ||
| assertEquals(numOfUpserts, 100); | ||
| assertTrue(numOfSelects > 0 && numOfSelects < 100); | ||
| System.out.println(numOfSelects); | ||
| } finally { | ||
| conn.close(); | ||
| } | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7 phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixConnection.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
2 changes: 1 addition & 1 deletion
2 phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixEmbeddedDriver.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
4 changes: 2 additions & 2 deletions
4 phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixPreparedStatement.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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.