Elffuss BraidHub

kikocis/java-driver public

Merge branch 'java1040' into 2.1
Changesetc2cdc3b9eac6bfd66a30c9d11d0cbe7eaff8c8bdcef89049ae1503a8795ac636
Authorolim7t <omichallat+github@gmail.com> · 2016-03-02 18:55 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-03-02 18:55 +01:00
Parentsb1d33724c50e, 8d6440c29005

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -24,6 +24,7 @@
2424 - [improvement] JAVA-879: Mapper.map() accepts mapper-generated and user queries.
2525 - [bug] JAVA-1100: Exception when connecting with shaded java driver in OSGI
2626 - [bug] JAVA-819: Expose more errors in RetryPolicy + provide idempotent-aware wrapper.
27+- [improvement] JAVA-1040: SimpleStatement parameters support in QueryLogger.
2728
2829 Merged from 2.0 branch:
2930
modified driver-core/src/main/java/com/datastax/driver/core/QueryLogger.java +76 −7
@@ -148,8 +148,9 @@
148148 * within a configurable threshold in milliseconds.
149149 * <p/>
150150 * This logger is activated by setting its level to {@code DEBUG} or {@code TRACE}.
151- * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement},
152- * then the query parameters (if any) will be logged as well (names and actual values).
151+ * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement}
152+ * or a {@link SimpleStatement}, then the query parameters (if any) will be logged. For a {@link BoundStatement}
153+ * names and actual values are logged and for a {@link SimpleStatement} values are logged in positional order.
153154 * <p/>
154155 * The name of this logger is {@code com.datastax.driver.core.QueryLogger.NORMAL}.
155156 */
@@ -160,8 +161,9 @@
160161 * but whose execution time exceeded a configurable threshold in milliseconds.
161162 * <p/>
162163 * This logger is activated by setting its level to {@code DEBUG} or {@code TRACE}.
163- * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement},
164- * then the query parameters (if any) will be logged as well (names and actual values).
164+ * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement}
165+ * or a {@link SimpleStatement}, then the query parameters (if any) will be logged. For a {@link BoundStatement}
166+ * names and actual values are logged and for a {@link SimpleStatement} values are logged in positional order.
165167 * <p/>
166168 * The name of this logger is {@code com.datastax.driver.core.QueryLogger.SLOW}.
167169 */
@@ -171,8 +173,9 @@
171173 * The logger used to log unsuccessful queries, i.e., queries that did not complete normally and threw an exception.
172174 * <p/>
173175 * This logger is activated by setting its level to {@code DEBUG} or {@code TRACE}.
174- * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement},
175- * then the query parameters (if any) will be logged as well (names and actual values).
176+ * Additionally, if the level is set to {@code TRACE} and the statement being logged is a {@link BoundStatement}
177+ * or a {@link SimpleStatement}, then the query parameters (if any) will be logged. For a {@link BoundStatement}
178+ * names and actual values are logged and for a {@link SimpleStatement} values are logged in positional order.
176179 * Note this this logger will also print the full stack trace of the reported exception.
177180 * <p/>
178181 * The name of this logger is {@code com.datastax.driver.core.QueryLogger.ERROR}.
@@ -646,13 +649,19 @@
646649 StringBuilder params = new StringBuilder();
647650 if (statement instanceof BoundStatement) {
648651 appendParameters((BoundStatement) statement, params, maxLoggedParameters);
649- } else if (statement instanceof BatchStatement) {
652+ } else if (statement instanceof SimpleStatement) {
653+ appendParameters((SimpleStatement) statement, params, maxLoggedParameters);
654+ }
655+ else if (statement instanceof BatchStatement) {
650656 BatchStatement batchStatement = (BatchStatement) statement;
651657 int remaining = maxLoggedParameters;
652658 for (Statement inner : batchStatement.getStatements()) {
653659 if (inner instanceof BoundStatement) {
654660 remaining = appendParameters((BoundStatement) inner, params, remaining);
655661 }
662+ else if (inner instanceof SimpleStatement) {
663+ remaining = appendParameters((SimpleStatement) inner, params, remaining);
664+ }
656665 }
657666 }
658667 if (params.length() > 0)
@@ -673,6 +682,9 @@
673682 } else if (statement instanceof BoundStatement) {
674683 int boundValues = ((BoundStatement) statement).wrapper.values.length;
675684 sb.append("[" + boundValues + " bound values] ");
685+ } else if (statement instanceof SimpleStatement) {
686+ int boundValues = ((SimpleStatement) statement).valuesCount();
687+ sb.append("[" + boundValues + " bound values] ");
676688 }
677689
678690 append(statement, sb, maxQueryStringLength);
@@ -684,6 +696,8 @@
684696 for (Statement s : bs.getStatements()) {
685697 if (s instanceof BoundStatement)
686698 count += ((BoundStatement) s).wrapper.values.length;
699+ else if (s instanceof SimpleStatement)
700+ count += ((SimpleStatement) s).valuesCount();
687701 }
688702 return count;
689703 }
@@ -746,6 +760,61 @@
746760 return valueStr;
747761 }
748762
763+ protected int appendParameters(SimpleStatement statement, StringBuilder buffer, int remaining) {
764+ if (remaining == 0)
765+ return 0;
766+ int numberOfParameters = statement.valuesCount();
767+ if (numberOfParameters > 0) {
768+ int numberOfLoggedParameters;
769+ if (remaining == -1) {
770+ numberOfLoggedParameters = numberOfParameters;
771+ } else {
772+ numberOfLoggedParameters = remaining > numberOfParameters ? numberOfParameters : remaining;
773+ remaining -= numberOfLoggedParameters;
774+ }
775+ for (int i = 0; i < numberOfLoggedParameters; i++) {
776+ if (buffer.length() == 0)
777+ buffer.append(" [");
778+ else
779+ buffer.append(", ");
780+ buffer.append(parameterValueAsString(statement.getObject(i)));
781+ }
782+ if (numberOfLoggedParameters < numberOfParameters) {
783+ buffer.append(FURTHER_PARAMS_OMITTED);
784+ }
785+ }
786+ return remaining;
787+ }
788+
789+ protected String parameterValueAsString(Object value) {
790+ String valueStr;
791+ if (value == null) {
792+ valueStr = "NULL";
793+ } else {
794+ DataType type = TypeCodec.getDataTypeFor(value);
795+ int maxParameterValueLength = this.maxParameterValueLength;
796+ if (type.equals(DataType.blob()) && maxParameterValueLength != -1) {
797+ // prevent large blobs from being converted to strings
798+ ByteBuffer buf = (ByteBuffer) value;
799+ int maxBufferLength = Math.max(2, (maxParameterValueLength - 2) / 2);
800+ boolean bufferTooLarge = buf.remaining() > maxBufferLength;
801+ if (bufferTooLarge) {
802+ value = (ByteBuffer) buf.duplicate().limit(maxBufferLength);
803+ }
804+ valueStr = type.format(value);
805+ if (bufferTooLarge) {
806+ valueStr = valueStr + TRUNCATED_OUTPUT;
807+ }
808+ } else {
809+ valueStr = type.format(value);
810+ if (maxParameterValueLength != -1 && valueStr.length() > maxParameterValueLength) {
811+ valueStr = valueStr.substring(0, maxParameterValueLength) + TRUNCATED_OUTPUT;
812+ }
813+ }
814+ }
815+ return valueStr;
816+ }
817+
749818 private ProtocolVersion protocolVersion() {
750819 // Since the QueryLogger can be registered before the Cluster was initialized, we can't retrieve
751820 // it at construction time. Cache it field at first use (a volatile field is good enough since we
modified driver-core/src/test/java/com/datastax/driver/core/QueryLoggerTest.java +239 −11
@@ -372,7 +372,7 @@
372372
373373 @Test(groups = "short")
374374 @CassandraVersion(major = 2.0)
375- public void should_log_non_null_named_parameter() throws Exception {
375+ public void should_log_non_null_named_parameter_bound_statements() throws Exception {
376376 // given
377377 normal.setLevel(TRACE);
378378 queryLogger = QueryLogger.builder(cluster())
@@ -398,7 +398,7 @@
398398 }
399399
400400 @Test(groups = "short")
401- public void should_log_non_null_positional_parameter() throws Exception {
401+ public void should_log_non_null_positional_parameter_bound_statements() throws Exception {
402402 // given
403403 normal.setLevel(TRACE);
404404 queryLogger = QueryLogger.builder(cluster()).build();
@@ -421,7 +421,27 @@
421421 }
422422
423423 @Test(groups = "short")
424- public void should_log_null_parameter() throws Exception {
424+ public void should_log_non_null_positional_parameter_simple_statements() throws Exception {
425+ // given
426+ normal.setLevel(TRACE);
427+ queryLogger = QueryLogger.builder(cluster()).build();
428+ cluster().register(queryLogger);
429+ // when
430+ String query = "UPDATE test SET c_text = ? WHERE pk = ?";
431+ SimpleStatement ss = new SimpleStatement(query, "foo", 42);
432+ session().execute(ss);
433+ // then
434+ String line = normalAppender.waitAndGet(10000);
435+ assertThat(line)
436+ .contains("Query completed normally")
437+ .contains(ipOfNode(1))
438+ .contains(query)
439+ .contains("42")
440+ .contains("'foo'");
441+ }
442+
443+ @Test(groups = "short")
444+ public void should_log_null_parameter_bound_statements() throws Exception {
425445 // given
426446 normal.setLevel(TRACE);
427447 queryLogger = QueryLogger.builder(cluster()).build();
@@ -443,6 +463,26 @@
443463 .contains("c_text:NULL");
444464 }
445465
466+ @Test(groups = "short")
467+ public void should_log_null_parameter_simple_statements() throws Exception {
468+ // given
469+ normal.setLevel(TRACE);
470+ queryLogger = QueryLogger.builder(cluster()).build();
471+ cluster().register(queryLogger);
472+ // when
473+ String query = "UPDATE test SET c_text = ? WHERE pk = ?";
474+ SimpleStatement ss = new SimpleStatement(query, null, 42);
475+ session().execute(ss);
476+ // then
477+ String line = normalAppender.waitAndGet(10000);
478+ assertThat(line)
479+ .contains("Query completed normally")
480+ .contains(ipOfNode(1))
481+ .contains(query)
482+ .contains("42")
483+ .contains("NULL");
484+ }
485+
446486 @Test(groups = "short")
447487 @CassandraVersion(major = 2.0)
448488 public void should_log_bound_statement_parameters_inside_batch_statement() throws Exception {
@@ -469,10 +509,38 @@
469509 .contains("c_text:'foo'")
470510 .contains("c_int:12345");
471511 }
512+
513+ @Test(groups = "short")
514+ @CassandraVersion(major = 2.0)
515+ public void should_log_simple_statement_parameters_inside_batch_statement() throws Exception {
516+ // given
517+ normal.setLevel(TRACE);
518+ queryLogger = QueryLogger.builder(cluster()).build();
519+ cluster().register(queryLogger);
520+ // when
521+ String query1 = "UPDATE test SET c_text = ? WHERE pk = ?";
522+ String query2 = "UPDATE test SET c_int = ? WHERE pk = ?";
523+ BatchStatement batch = new BatchStatement();
524+ batch.add(new SimpleStatement(query1, "foo", 42));
525+ batch.add(new SimpleStatement(query2, 12345, 43));
526+ session().execute(batch);
527+ // then
528+ String line = normalAppender.waitAndGet(10000);
529+ assertThat(line)
530+ .contains("Query completed normally")
531+ .contains(ipOfNode(1))
532+ .contains(query1)
533+ .contains(query2)
534+ .contains("42")
535+ .contains("43")
536+ .contains("'foo'")
537+ .contains("12345");
538+ }
539+
472540 // Test different CQL types
473541
474542 @Test(groups = "short")
475- public void should_log_all_parameter_types() throws Exception {
543+ public void should_log_all_parameter_types_bound_statements() throws Exception {
476544 // given
477545 normal.setLevel(TRACE);
478546 queryLogger = QueryLogger.builder(cluster())
@@ -495,6 +563,29 @@
495563 }
496564 }
497565
566+ @Test(groups = "short")
567+ public void should_log_all_parameter_types_simple_statements() throws Exception {
568+ // given
569+ normal.setLevel(TRACE);
570+ queryLogger = QueryLogger.builder(cluster())
571+ .withMaxParameterValueLength(Integer.MAX_VALUE)
572+ .build();
573+ cluster().register(queryLogger);
574+ // when
575+ String query = "UPDATE test SET " + assignments + " WHERE pk = 42";
576+ SimpleStatement ss = new SimpleStatement(query, values.toArray());
577+ session().execute(ss);
578+ // then
579+ String line = normalAppender.waitAndGet(10000);
580+ assertThat(line)
581+ .contains("Query completed normally")
582+ .contains(ipOfNode(1))
583+ .contains(query);
584+ for (DataType type : dataTypes) {
585+ assertThat(line).contains(type.format(getFixedValue(type)));
586+ }
587+ }
588+
498589 // Tests for truncation of query strings and parameter values
499590
500591 @Test(groups = "short")
@@ -566,7 +657,7 @@
566657
567658 @CassandraVersion(major = 2.0)
568659 @Test(groups = "short")
569- public void should_truncate_parameter_when_max_length_exceeded() throws Exception {
660+ public void should_truncate_parameter_when_max_length_exceeded_bound_statements() throws Exception {
570661 // given
571662 normal.setLevel(TRACE);
572663 queryLogger = QueryLogger.builder(cluster())
@@ -589,8 +680,30 @@
589680 .doesNotContain("123456");
590681 }
591682
683+ @CassandraVersion(major = 2.0)
684+ @Test(groups = "short")
685+ public void should_truncate_parameter_when_max_length_exceeded_simple_statements() throws Exception {
686+ // given
687+ normal.setLevel(TRACE);
688+ queryLogger = QueryLogger.builder(cluster())
689+ .withMaxParameterValueLength(5)
690+ .build();
691+ cluster().register(queryLogger);
692+ // when
693+ String query = "UPDATE test SET c_int = ? WHERE pk = ?";
694+ SimpleStatement ss = new SimpleStatement(query, 123456, 42);
695+ session().execute(ss);
696+ // then
697+ String line = normalAppender.waitAndGet(10000);
698+ assertThat(line)
699+ .contains("Query completed normally")
700+ .contains(ipOfNode(1))
701+ .contains("12345" + TRUNCATED_OUTPUT)
702+ .doesNotContain("123456");
703+ }
704+
592705 @Test(groups = "short")
593- public void should_truncate_blob_parameter_when_max_length_exceeded() throws Exception {
706+ public void should_truncate_blob_parameter_when_max_length_exceeded_bound_statements() throws Exception {
594707 // given
595708 normal.setLevel(TRACE);
596709 queryLogger = QueryLogger.builder(cluster())
@@ -610,11 +723,32 @@
610723 .contains("Query completed normally")
611724 .contains(ipOfNode(1))
612725 .contains("c_blob:0x0102" + TRUNCATED_OUTPUT)
613- .doesNotContain("123456");
726+ .doesNotContain("0x010203");
727+ }
728+
729+ @Test(groups = "short")
730+ public void should_truncate_blob_parameter_when_max_length_exceeded_simple_statements() throws Exception {
731+ // given
732+ normal.setLevel(TRACE);
733+ queryLogger = QueryLogger.builder(cluster())
734+ .withMaxParameterValueLength(6)
735+ .build();
736+ cluster().register(queryLogger);
737+ // when
738+ String query = "UPDATE test SET c_blob = ? WHERE pk = ?";
739+ SimpleStatement ss = new SimpleStatement(query, ByteBuffer.wrap(Bytes.toArray(Lists.newArrayList(1, 2, 3))), 42);
740+ session().execute(ss);
741+ // then
742+ String line = normalAppender.waitAndGet(10000);
743+ assertThat(line)
744+ .contains("Query completed normally")
745+ .contains(ipOfNode(1))
746+ .contains("0x0102" + TRUNCATED_OUTPUT)
747+ .doesNotContain("0x010203");
614748 }
615749
616750 @Test(groups = "short")
617- public void should_not_truncate_parameter_when_max_length_unlimited() throws Exception {
751+ public void should_not_truncate_parameter_when_max_length_unlimited_bound_statements() throws Exception {
618752 // given
619753 normal.setLevel(TRACE);
620754 queryLogger = QueryLogger.builder(cluster())
@@ -638,7 +772,28 @@
638772 }
639773
640774 @Test(groups = "short")
641- public void should_not_log_exceeding_number_of_parameters() throws Exception {
775+ public void should_not_truncate_parameter_when_max_length_unlimited_simple_statements() throws Exception {
776+ // given
777+ normal.setLevel(TRACE);
778+ queryLogger = QueryLogger.builder(cluster())
779+ .withMaxParameterValueLength(-1)
780+ .build();
781+ cluster().register(queryLogger);
782+ // when
783+ String query = "UPDATE test SET c_int = ? WHERE pk = ?";
784+ SimpleStatement ss = new SimpleStatement(query, 123456, 42);
785+ session().execute(ss);
786+ // then
787+ String line = normalAppender.waitAndGet(10000);
788+ assertThat(line)
789+ .contains("Query completed normally")
790+ .contains(ipOfNode(1))
791+ .contains("123456")
792+ .doesNotContain(TRUNCATED_OUTPUT);
793+ }
794+
795+ @Test(groups = "short")
796+ public void should_not_log_exceeding_number_of_parameters_bound_statements() throws Exception {
642797 // given
643798 normal.setLevel(TRACE);
644799 queryLogger = QueryLogger.builder(cluster())
@@ -662,9 +817,31 @@
662817 .contains(FURTHER_PARAMS_OMITTED);
663818 }
664819
820+ @Test(groups = "short")
821+ public void should_not_log_exceeding_number_of_parameters_simple_statements() throws Exception {
822+ // given
823+ normal.setLevel(TRACE);
824+ queryLogger = QueryLogger.builder(cluster())
825+ .withMaxLoggedParameters(1)
826+ .build();
827+ cluster().register(queryLogger);
828+ // when
829+ String query = "UPDATE test SET c_int = ? WHERE pk = ?";
830+ SimpleStatement ss = new SimpleStatement(query, 123456, 42);
831+ session().execute(ss);
832+ // then
833+ String line = normalAppender.waitAndGet(10000);
834+ assertThat(line)
835+ .contains("Query completed normally")
836+ .contains(ipOfNode(1))
837+ .contains("123456")
838+ .doesNotContain("42")
839+ .contains(FURTHER_PARAMS_OMITTED);
840+ }
841+
665842 @Test(groups = "short")
666843 @CassandraVersion(major = 2.0)
667- public void should_not_log_exceeding_number_of_parameters_in_batch_statement() throws Exception {
844+ public void should_not_log_exceeding_number_of_parameters_in_batch_statement_bound_statements() throws Exception {
668845 // given
669846 normal.setLevel(TRACE);
670847 queryLogger = QueryLogger.builder(cluster())
@@ -693,7 +870,37 @@
693870 }
694871
695872 @Test(groups = "short")
696- public void should_log_all_parameters_when_max_unlimited() throws Exception {
873+ @CassandraVersion(major = 2.0)
874+ public void should_not_log_exceeding_number_of_parameters_in_batch_statement_simple_statements() throws Exception {
875+ // given
876+ normal.setLevel(TRACE);
877+ queryLogger = QueryLogger.builder(cluster())
878+ .withMaxLoggedParameters(1)
879+ .build();
880+ cluster().register(queryLogger);
881+ // when
882+ String query1 = "UPDATE test SET c_text = ? WHERE pk = ?";
883+ String query2 = "UPDATE test SET c_int = ? WHERE pk = ?";
884+ BatchStatement batch = new BatchStatement();
885+ batch.add(new SimpleStatement(query1, "foo", 42));
886+ batch.add(new SimpleStatement(query2, 12345, 43));
887+ session().execute(batch);
888+ // then
889+ String line = normalAppender.waitAndGet(10000);
890+ assertThat(line)
891+ .contains("Query completed normally")
892+ .contains(ipOfNode(1))
893+ .contains(query1)
894+ .contains(query2)
895+ .contains("'foo'")
896+ .doesNotContain("42")
897+ .doesNotContain("12345")
898+ .doesNotContain("43")
899+ .contains(FURTHER_PARAMS_OMITTED);
900+ }
901+
902+ @Test(groups = "short")
903+ public void should_log_all_parameters_when_max_unlimited_bound_statements() throws Exception {
697904 // given
698905 normal.setLevel(TRACE);
699906 queryLogger = QueryLogger.builder(cluster())
@@ -716,6 +923,27 @@
716923 .contains("pk:42");
717924 }
718925
926+ @Test(groups = "short")
927+ public void should_log_all_parameters_when_max_unlimited_simple_statements() throws Exception {
928+ // given
929+ normal.setLevel(TRACE);
930+ queryLogger = QueryLogger.builder(cluster())
931+ .withMaxLoggedParameters(-1)
932+ .build();
933+ cluster().register(queryLogger);
934+ // when
935+ String query = "UPDATE test SET c_int = ? WHERE pk = ?";
936+ SimpleStatement ss = new SimpleStatement(query, 123456, 42);
937+ session().execute(ss);
938+ // then
939+ String line = normalAppender.waitAndGet(10000);
940+ assertThat(line)
941+ .contains("Query completed normally")
942+ .contains(ipOfNode(1))
943+ .contains("123456")
944+ .contains("42");
945+ }
946+
719947 @Override
720948 public void onTestContextInitialized() {
721949 execute("CREATE TABLE test (pk int PRIMARY KEY, " + definitions + ")");