Repository: cassandra Updated Branches: refs/heads/trunk f7431b432 -> f83bd5ac2
Log keyspace in full query log Patch by marcuse; reviewed by Dinesh Joshi for CASSANDRA-14656 Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/46c33f32 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/46c33f32 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/46c33f32 Branch: refs/heads/trunk Commit: 46c33f324e5f3373d85838f364aece7ca6a6189c Parents: f7431b4 Author: Marcus Eriksson <[email protected]> Authored: Mon Aug 20 18:30:21 2018 +0200 Committer: Marcus Eriksson <[email protected]> Committed: Sat Sep 1 08:23:54 2018 +0200 ---------------------------------------------------------------------- CHANGES.txt | 1 + .../apache/cassandra/audit/FullQueryLogger.java | 13 +++- .../cassandra/audit/FullQueryLoggerTest.java | 72 +++++++++++++++++++- 3 files changed, 82 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/46c33f32/CHANGES.txt ---------------------------------------------------------------------- diff --git a/CHANGES.txt b/CHANGES.txt index b53b986..cd2a14a 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 4.0 + * Log keyspace in full query log (CASSANDRA-14656) * Transient Replication and Cheap Quorums (CASSANDRA-14404) * Log server-generated timestamp and nowInSeconds used by queries in FQL (CASSANDRA-14675) * Add diagnostic events for read repairs (CASSANDRA-14668) http://git-wip-us.apache.org/repos/asf/cassandra/blob/46c33f32/src/java/org/apache/cassandra/audit/FullQueryLogger.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/audit/FullQueryLogger.java b/src/java/org/apache/cassandra/audit/FullQueryLogger.java index 8cd8f4a..c9f8447 100644 --- a/src/java/org/apache/cassandra/audit/FullQueryLogger.java +++ b/src/java/org/apache/cassandra/audit/FullQueryLogger.java @@ -21,6 +21,8 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; +import javax.annotation.Nullable; + import com.google.common.base.Preconditions; import com.google.common.primitives.Ints; @@ -53,6 +55,7 @@ public class FullQueryLogger extends BinLogAuditLogger implements IAuditLogger public static final String GENERATED_TIMESTAMP = "generated-timestamp"; public static final String GENERATED_NOW_IN_SECONDS = "generated-now-in-seconds"; + public static final String KEYSPACE = "keyspace"; public static final String BATCH = "batch"; public static final String SINGLE_QUERY = "single-query"; @@ -262,6 +265,8 @@ public class FullQueryLogger extends BinLogAuditLogger implements IAuditLogger private final long generatedTimestamp; private final int generatedNowInSeconds; + @Nullable + private final String keyspace; AbstractLogEntry(QueryOptions queryOptions, QueryState queryState, long queryStartTime) { @@ -273,6 +278,7 @@ public class FullQueryLogger extends BinLogAuditLogger implements IAuditLogger this.generatedTimestamp = queryState.generatedTimestamp(); this.generatedNowInSeconds = queryState.generatedNowInSeconds(); + this.keyspace = queryState.getClientState().getRawKeyspace(); /* * Struggled with what tradeoff to make in terms of query options which is potentially large and complicated @@ -309,6 +315,8 @@ public class FullQueryLogger extends BinLogAuditLogger implements IAuditLogger wire.write(GENERATED_TIMESTAMP).int64(generatedTimestamp); wire.write(GENERATED_NOW_IN_SECONDS).int32(generatedNowInSeconds); + + wire.write(KEYSPACE).text(keyspace); } @Override @@ -325,7 +333,10 @@ public class FullQueryLogger extends BinLogAuditLogger implements IAuditLogger + 4 // protocolVersion + EMPTY_BYTEBUF_SIZE + queryOptionsBuffer.capacity() // queryOptionsBuffer + 8 // generatedTimestamp - + 4; // generatedNowInSeconds + + 4 // generatedNowInSeconds + + (keyspace != null + ? Ints.checkedCast(ObjectSizes.sizeOf(keyspace)) // keyspace + : OBJECT_REFERENCE_SIZE); // null } protected abstract String type(); http://git-wip-us.apache.org/repos/asf/cassandra/blob/46c33f32/test/unit/org/apache/cassandra/audit/FullQueryLoggerTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/audit/FullQueryLoggerTest.java b/test/unit/org/apache/cassandra/audit/FullQueryLoggerTest.java index 542f8bb..376bde5 100644 --- a/test/unit/org/apache/cassandra/audit/FullQueryLoggerTest.java +++ b/test/unit/org/apache/cassandra/audit/FullQueryLoggerTest.java @@ -28,6 +28,8 @@ import java.util.List; import java.util.concurrent.Semaphore; import java.util.concurrent.atomic.AtomicInteger; +import javax.annotation.Nullable; + import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; @@ -55,6 +57,7 @@ import org.apache.cassandra.utils.binlog.BinLogTest; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.apache.cassandra.audit.FullQueryLogger.BATCH; @@ -416,6 +419,20 @@ public class FullQueryLoggerTest extends CQLTester configureFQL(); logQuery("foo"); Util.spinAssertEquals(true, () -> checkForQueries(Arrays.asList("foo")), 60); + assertRoundTripQuery(null); + } + + @Test + public void testRoundTripQueryWithKeyspace() throws Exception + { + configureFQL(); + logQuery("foo", "abcdefg"); + Util.spinAssertEquals(true, () -> checkForQueries(Arrays.asList("foo")), 60); + assertRoundTripQuery("abcdefg"); + } + + private void assertRoundTripQuery(@Nullable String keyspace) + { try (ChronicleQueue queue = ChronicleQueueBuilder.single(tempDir.toFile()).rollCycle(RollCycles.TEST_SECONDLY).build()) { ExcerptTailer tailer = queue.createTailer(); @@ -432,19 +449,46 @@ public class FullQueryLoggerTest extends CQLTester QueryOptions queryOptions = QueryOptions.codec.decode(Unpooled.wrappedBuffer(wire.read(QUERY_OPTIONS).bytes()), protocolVersion); compareQueryOptions(QueryOptions.DEFAULT, queryOptions); + String wireKeyspace = wire.read(FullQueryLogger.KEYSPACE).text(); + assertEquals(keyspace, wireKeyspace); + assertEquals("foo", wire.read(QUERY).text()); })); } } @Test - public void testRoundTripBatch() throws Exception + public void testRoundTripBatchWithKeyspace() throws Exception + { + configureFQL(); + instance.logBatch(Type.UNLOGGED, + Arrays.asList("foo1", "foo2"), + Arrays.asList(Arrays.asList(ByteBuffer.allocate(1), + ByteBuffer.allocateDirect(2)), + Collections.emptyList()), + QueryOptions.DEFAULT, + queryState("abcdefgh"), + 1); + + Util.spinAssertEquals(true, () -> + { + try (ChronicleQueue queue = ChronicleQueueBuilder.single(tempDir.toFile()).rollCycle(RollCycles.TEST_SECONDLY).build()) + { + return queue.createTailer().readingDocument().isPresent(); + } + }, 60); + + assertRoundTripBatch("abcdefgh"); + } + + @Test + public void testRoundTripBatchWithKeyspaceNull() throws Exception { configureFQL(); instance.logBatch(Type.UNLOGGED, Arrays.asList("foo1", "foo2"), Arrays.asList(Arrays.asList(ByteBuffer.allocate(1), - ByteBuffer.allocateDirect(2)), + ByteBuffer.allocateDirect(2)), Collections.emptyList()), QueryOptions.DEFAULT, queryState(), @@ -458,6 +502,12 @@ public class FullQueryLoggerTest extends CQLTester } }, 60); + assertRoundTripBatch(null); + } + + + private void assertRoundTripBatch(@Nullable String keyspace) + { try (ChronicleQueue queue = ChronicleQueueBuilder.single(tempDir.toFile()).rollCycle(RollCycles.TEST_SECONDLY).build()) { ExcerptTailer tailer = queue.createTailer(); @@ -475,7 +525,7 @@ public class FullQueryLoggerTest extends CQLTester assertEquals(Long.MIN_VALUE, wire.read(GENERATED_TIMESTAMP).int64()); assertEquals(Integer.MIN_VALUE, wire.read(GENERATED_NOW_IN_SECONDS).int32()); - + assertEquals(keyspace, wire.read(FullQueryLogger.KEYSPACE).text()); assertEquals("UNLOGGED", wire.read(BATCH_TYPE).text()); ValueIn in = wire.read(QUERIES); assertEquals(2, in.int32()); @@ -491,6 +541,7 @@ public class FullQueryLoggerTest extends CQLTester } } + @Test public void testQueryWeight() { @@ -522,6 +573,10 @@ public class FullQueryLoggerTest extends CQLTester Batch batch = new Batch(Type.UNLOGGED, new ArrayList<>(), new ArrayList<>(), QueryOptions.DEFAULT, queryState(), 1); assertTrue(batch.weight() > 0); + // make sure that a batch with keyspace set has a higher weight + Batch batch2 = new Batch(Type.UNLOGGED, new ArrayList<>(), new ArrayList<>(), QueryOptions.DEFAULT, queryState("ABABA"), 1); + assertTrue(batch.weight() < batch2.weight()); + StringBuilder sb = new StringBuilder(); for (int ii = 0; ii < 1024 * 1024; ii++) { @@ -642,6 +697,17 @@ public class FullQueryLoggerTest extends CQLTester instance.logQuery(query, QueryOptions.DEFAULT, queryState(), 1); } + private void logQuery(String query, String keyspace) + { + instance.logQuery(query, QueryOptions.DEFAULT, queryState(keyspace), 1); + } + + private QueryState queryState(String keyspace) + { + ClientState clientState = ClientState.forInternalCalls(keyspace); + return new QueryState(clientState); + } + private QueryState queryState() { return new QueryState(ClientState.forInternalCalls()); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
