PHOENIX-1691 Allow settting sampling rate while enabling tracing.(Rajeshbabu)

Conflicts:

        phoenix-core/src/main/antlr3/PhoenixSQL.g


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/f977ac6f
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/f977ac6f
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/f977ac6f

Branch: refs/heads/4.3
Commit: f977ac6f351716f4c4143fdef080e0beb938b1ba
Parents: 30ddd77
Author: Rajeshbabu Chintaguntla <rajeshb...@apache.org>
Authored: Wed Mar 18 10:20:22 2015 +0530
Committer: James Taylor <jamestay...@apache.org>
Committed: Tue Mar 24 16:16:19 2015 -0700

----------------------------------------------------------------------
 .../phoenix/trace/PhoenixTracingEndToEndIT.java | 39 +++++++++++++++++---
 phoenix-core/src/main/antlr3/PhoenixSQL.g       |  9 ++++-
 .../apache/phoenix/compile/TraceQueryPlan.java  | 33 ++++++++++++-----
 .../apache/phoenix/jdbc/PhoenixStatement.java   |  8 ++--
 .../apache/phoenix/parse/ParseNodeFactory.java  |  4 +-
 .../apache/phoenix/parse/TraceStatement.java    | 12 ++++--
 6 files changed, 79 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/f977ac6f/phoenix-core/src/it/java/org/apache/phoenix/trace/PhoenixTracingEndToEndIT.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/trace/PhoenixTracingEndToEndIT.java
 
b/phoenix-core/src/it/java/org/apache/phoenix/trace/PhoenixTracingEndToEndIT.java
index 53d22c5..05d9e41 100644
--- 
a/phoenix-core/src/it/java/org/apache/phoenix/trace/PhoenixTracingEndToEndIT.java
+++ 
b/phoenix-core/src/it/java/org/apache/phoenix/trace/PhoenixTracingEndToEndIT.java
@@ -46,6 +46,7 @@ import org.cloudera.htrace.Span;
 import org.cloudera.htrace.SpanReceiver;
 import org.cloudera.htrace.Trace;
 import org.cloudera.htrace.TraceScope;
+import org.cloudera.htrace.impl.ProbabilitySampler;
 import org.junit.After;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -403,18 +404,44 @@ public class PhoenixTracingEndToEndIT extends 
BaseTracingTestIT {
             Statement statement = conn1.createStatement();
             ResultSet  rs = statement.executeQuery("TRACE ON");
             assertTrue(rs.next());
-            long traceId = ((PhoenixConnection) 
conn1).getTraceScope().getSpan()
-            .getTraceId();
-            assertEquals(rs.getLong(1), traceId);
-            assertEquals(rs.getLong("trace_id"), traceId);
+            PhoenixConnection pconn= (PhoenixConnection) conn1;
+            long traceId = pconn.getTraceScope().getSpan().getTraceId();
+            assertEquals(traceId, rs.getLong(1));
+            assertEquals(traceId, rs.getLong("trace_id"));
             assertFalse(rs.next());
+            assertEquals(Sampler.ALWAYS, pconn.getSampler());
 
             rs = statement.executeQuery("TRACE OFF");
             assertTrue(rs.next());
-            assertEquals(rs.getLong(1), traceId);
-            assertEquals(rs.getLong("trace_id"), traceId);
+            assertEquals(traceId, rs.getLong(1));
+            assertEquals(traceId, rs.getLong("trace_id"));
+            assertFalse(rs.next());
+            assertEquals(Sampler.NEVER, pconn.getSampler());
+
+            rs = statement.executeQuery("TRACE OFF");
             assertFalse(rs.next());
 
+            rs = statement.executeQuery("TRACE ON  WITH SAMPLING 0.5");
+            rs.next();
+            assertTrue(((PhoenixConnection) conn1).getSampler() instanceof 
ProbabilitySampler);
+
+            rs = statement.executeQuery("TRACE ON  WITH SAMPLING 1.0");
+            assertTrue(rs.next());
+            traceId = pconn.getTraceScope().getSpan()
+            .getTraceId();
+            assertEquals(traceId, rs.getLong(1));
+            assertEquals(traceId, rs.getLong("trace_id"));
+            assertFalse(rs.next());
+            assertEquals(Sampler.ALWAYS, pconn.getSampler());
+
+            rs = statement.executeQuery("TRACE ON  WITH SAMPLING 0.5");
+            rs.next();
+            assertTrue(((PhoenixConnection) conn1).getSampler() instanceof 
ProbabilitySampler);
+
+            rs = statement.executeQuery("TRACE ON WITH SAMPLING 0.0");
+            rs.next();
+            assertEquals(Sampler.NEVER, pconn.getSampler());
+
             rs = statement.executeQuery("TRACE OFF");
             assertFalse(rs.next());
        } finally {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/f977ac6f/phoenix-core/src/main/antlr3/PhoenixSQL.g
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/antlr3/PhoenixSQL.g 
b/phoenix-core/src/main/antlr3/PhoenixSQL.g
index a007308..b72578c 100644
--- a/phoenix-core/src/main/antlr3/PhoenixSQL.g
+++ b/phoenix-core/src/main/antlr3/PhoenixSQL.g
@@ -109,6 +109,7 @@ tokens
     STATISTICS='statistics';    
     COLUMNS='columns';
     TRACE='trace';
+    SAMPLING='sampling';
 }
 
 
@@ -504,8 +505,8 @@ alter_index_node returns [AlterIndexStatement ret]
 
 // Parse a trace statement.
 trace_node returns [TraceStatement ret]
-    :   TRACE (flag = ON| flag = OFF)
-       {ret = factory.trace(Tracing.isTraceOn(flag.getText()));}
+    :   TRACE ((flag = ON  ( WITH SAMPLING s = sampling_rate)?) | flag = OFF)
+       {ret = factory.trace(Tracing.isTraceOn(flag.getText()), s == null ? 
Tracing.isTraceOn(flag.getText()) ? 1.0 : 0.0 : 
(((BigDecimal)s.getValue())).doubleValue());}
     ;
 
 // Parse an alter table statement.
@@ -623,6 +624,10 @@ limit returns [LimitNode ret]
     | l=int_literal { $ret = factory.limit(l); }
     ;
     
+sampling_rate returns [LiteralParseNode ret]
+    : l=literal { $ret = l; }
+    ;
+
 hintClause returns [HintNode ret]
     :  c=ML_HINT { $ret = factory.hint(c.getText()); }
     ;

http://git-wip-us.apache.org/repos/asf/phoenix/blob/f977ac6f/phoenix-core/src/main/java/org/apache/phoenix/compile/TraceQueryPlan.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/compile/TraceQueryPlan.java 
b/phoenix-core/src/main/java/org/apache/phoenix/compile/TraceQueryPlan.java
index 9eb5877..815ac1e 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/TraceQueryPlan.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/TraceQueryPlan.java
@@ -60,6 +60,7 @@ import org.apache.phoenix.util.ByteUtil;
 import org.apache.phoenix.util.SizedUtil;
 import org.cloudera.htrace.Sampler;
 import org.cloudera.htrace.TraceScope;
+import org.cloudera.htrace.impl.ProbabilitySampler;
 
 public class TraceQueryPlan implements QueryPlan {
 
@@ -122,22 +123,29 @@ public class TraceQueryPlan implements QueryPlan {
             public Tuple next() throws SQLException {
                 if(!first) return null;
                 TraceScope traceScope = conn.getTraceScope();
-                if(traceStatement.isTraceOn()) {
-                    if(!conn.getSampler().equals(Sampler.ALWAYS)) {
+                if (traceStatement.isTraceOn()) {
+                    double samplingRate = traceStatement.getSamplingRate();
+                    if (samplingRate >= 1.0) {
                         conn.setSampler(Sampler.ALWAYS);
+                    } else if (samplingRate < 1.0 && samplingRate > 0.0) {
+                        conn.setSampler(new ProbabilitySampler(samplingRate));
+                    } else {
+                        closeTraceScope(conn);
+                        conn.setSampler(Sampler.NEVER);
                     }
-                    if (traceScope == null) {
+                    if (traceScope == null && 
!conn.getSampler().equals(Sampler.NEVER)) {
                         traceScope = Tracing.startNewSpan(conn, "Enabling 
trace");
-                        conn.setTraceScope(traceScope);
+                        if (traceScope.getSpan() != null) {
+                            conn.setTraceScope(traceScope);
+                        } else {
+                            closeTraceScope(conn);
+                        }
                     }
                 } else {
-                    if (traceScope != null) {
-                        conn.getTraceScope().close();
-                        conn.setTraceScope(null);
-                    }
+                    closeTraceScope(conn);
                     conn.setSampler(Sampler.NEVER);
                 }
-                if(traceScope == null) return null;
+                if (traceScope == null || traceScope.getSpan() == null) return 
null;
                 first = false;
                 ImmutableBytesWritable ptr = new ImmutableBytesWritable();
                 ParseNodeFactory factory = new ParseNodeFactory();
@@ -157,6 +165,13 @@ public class TraceQueryPlan implements QueryPlan {
                 return new ResultTuple(Result.create(cells));
             }
 
+            private void closeTraceScope(final PhoenixConnection conn) {
+                if(conn.getTraceScope()!=null) {
+                    conn.getTraceScope().close();
+                    conn.setTraceScope(null);
+                }
+            }
+
             @Override
             public void explain(List<String> planSteps) {
             }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/f977ac6f/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java 
b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java
index 15aceec..fb0986e 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java
@@ -708,8 +708,8 @@ public class PhoenixStatement implements Statement, 
SQLCloseable, org.apache.pho
     
     private static class ExecutableTraceStatement extends TraceStatement 
implements CompilableStatement {
 
-        public ExecutableTraceStatement(boolean isTraceOn) {
-            super(isTraceOn);
+        public ExecutableTraceStatement(boolean isTraceOn, double 
samplingRate) {
+            super(isTraceOn, samplingRate);
         }
 
         @SuppressWarnings("unchecked")
@@ -913,8 +913,8 @@ public class PhoenixStatement implements Statement, 
SQLCloseable, org.apache.pho
         }
 
         @Override
-        public TraceStatement trace(boolean isTraceOn) {
-            return new ExecutableTraceStatement(isTraceOn);
+        public TraceStatement trace(boolean isTraceOn, double samplingRate) {
+            return new ExecutableTraceStatement(isTraceOn, samplingRate);
         }
 
         @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/f977ac6f/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java 
b/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java
index ff5aaf4..e6560d7 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java
@@ -325,8 +325,8 @@ public class ParseNodeFactory {
         return new AlterIndexStatement(indexTableNode, dataTableName, 
ifExists, state);
     }
 
-    public TraceStatement trace(boolean isTraceOn) {
-        return new TraceStatement(isTraceOn);
+    public TraceStatement trace(boolean isTraceOn, double samplingRate) {
+        return new TraceStatement(isTraceOn, samplingRate);
     }
 
     public TableName table(String schemaName, String tableName) {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/f977ac6f/phoenix-core/src/main/java/org/apache/phoenix/parse/TraceStatement.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/parse/TraceStatement.java 
b/phoenix-core/src/main/java/org/apache/phoenix/parse/TraceStatement.java
index 7460a85..301fa56 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/TraceStatement.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/TraceStatement.java
@@ -21,10 +21,12 @@ import org.apache.phoenix.jdbc.PhoenixStatement.Operation;
 
 public class TraceStatement implements BindableStatement {
 
-    private boolean traceOn = false;
+    private final boolean traceOn;
+    private final double samplingRate;
 
-    public TraceStatement(boolean isOn) {
+    public TraceStatement(boolean isOn, double samplingRate) {
         this.traceOn = isOn;
+        this.samplingRate = samplingRate;
     }
 
     @Override
@@ -38,6 +40,10 @@ public class TraceStatement implements BindableStatement {
     }
 
     public boolean isTraceOn() {
-        return traceOn == true;
+        return traceOn;
+    }
+
+    public double getSamplingRate() {
+        return samplingRate;
     }
 }

Reply via email to