db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r1479552 - in /db/derby/code/trunk/java: testing/org/apache/derbyTesting/functionTests/tests/lang/ tools/org/apache/derby/impl/tools/optional/ tools/org/apache/derby/loc/
Date Mon, 06 May 2013 12:46:32 GMT
Author: rhillegas
Date: Mon May  6 12:46:32 2013
New Revision: 1479552

URL: http://svn.apache.org/r1479552
Log:
DERBY-6211: Add ability to plug custom trace logic into the optimizer.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DummyOptTrace.java
  (with props)
Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/OptionalToolsTest.java
    db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/optional/OptimizerTracer.java
    db/derby/code/trunk/java/tools/org/apache/derby/loc/toolsmessages.properties

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DummyOptTrace.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DummyOptTrace.java?rev=1479552&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DummyOptTrace.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DummyOptTrace.java
Mon May  6 12:46:32 2013
@@ -0,0 +1,169 @@
+/*
+
+   Derby - Class org.apache.derbyTesting.functionTests.tests.lang.DummyOptTrace
+
+   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.derbyTesting.functionTests.tests.lang;
+
+import java.io.PrintWriter;
+
+import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
+import org.apache.derby.iapi.util.JBitSet;
+import org.apache.derby.iapi.sql.compile.AccessPath;
+import org.apache.derby.iapi.sql.compile.CostEstimate;
+import org.apache.derby.iapi.sql.compile.JoinStrategy;
+import org.apache.derby.iapi.sql.compile.OptTrace;
+import org.apache.derby.iapi.sql.compile.Optimizable;
+import org.apache.derby.iapi.sql.compile.RequiredRowOrdering;
+
+/**
+ * Dummy implementation of OptTrace to test the loading of custom
+ * trace logic for the Optimizer.
+ */
+public  class   DummyOptTrace   implements  OptTrace
+{
+    ////////////////////////////////////////////////////////////////////////
+    //
+    //	CONSTANTS
+    //
+    ////////////////////////////////////////////////////////////////////////
+
+    ////////////////////////////////////////////////////////////////////////
+    //
+    //	STATE
+    //
+    ////////////////////////////////////////////////////////////////////////
+
+    private StringBuffer    _buffer;
+    private static  String  _fullTrace;
+
+    ////////////////////////////////////////////////////////////////////////
+    //
+    //	CONSTRUCTOR
+    //
+    ////////////////////////////////////////////////////////////////////////
+
+    /** 0-arg constructor required by OptTrace contract */
+    public  DummyOptTrace()
+    {
+        _buffer = new StringBuffer();
+    }
+
+    ////////////////////////////////////////////////////////////////////////
+    //
+    //	FUNCTION FOR RETRIEVING THE FULL TRACE
+    //
+    ////////////////////////////////////////////////////////////////////////
+
+    public  static  String  fullTrace()    { return _fullTrace; }
+
+    ////////////////////////////////////////////////////////////////////////
+    //
+    //	BEHAVIOR
+    //
+    ////////////////////////////////////////////////////////////////////////
+
+    //
+    // We only provide implementation for these methods.
+    //
+    public  void    traceStartStatement( String statementText ) { _buffer.append( "<text>"
+ statementText + "</text>" ); }
+    public  void    printToWriter( PrintWriter out )   { _fullTrace = _buffer.toString();
}
+
+    //
+    // Don't need to bother implementing the rest of the behavior.
+    //
+    public  void    traceStart( long timeOptimizationStarted, int optimizerID ) {}
+    public  void    traceTimeout( long currentTime, CostEstimate bestCost ) {}
+    public  void    traceVacuous() {}
+    public  void    traceCompleteJoinOrder() {}
+    public  void    traceSortCost( CostEstimate sortCost, CostEstimate currentCost ) {}
+    public  void    traceNoBestPlan() {}
+    public  void    traceModifyingAccessPaths( int optimizerID ) {}
+    public  void    traceShortCircuiting( boolean timeExceeded, Optimizable thisOpt, int
joinPosition ) {}
+    public  void    traceSkippingJoinOrder( int nextOptimizable, int joinPosition, int[]
proposedJoinOrder, JBitSet assignedTableMap ) {}
+    public  void    traceIllegalUserJoinOrder() {}
+    public  void    traceUserJoinOrderOptimized() {}
+    public  void    traceJoinOrderConsideration( int joinPosition, int[] proposedJoinOrder,
JBitSet assignedTableMap ) {}
+    public  void    traceCostWithoutSortAvoidance( CostEstimate currentCost ) {}
+    public  void    traceCostWithSortAvoidance( CostEstimate currentSortAvoidanceCost ) {}
+    public  void    traceCurrentPlanAvoidsSort( CostEstimate bestCost, CostEstimate currentSortAvoidanceCost
) {}
+    public  void    traceCheapestPlanSoFar( int planType, CostEstimate currentCost ) {}
+    public  void    traceSortNeededForOrdering( int planType, RequiredRowOrdering requiredRowOrdering
) {}
+    public  void    traceRememberingBestJoinOrder( int joinPosition, int[] bestJoinOrder,
JBitSet assignedTableMap ) {}
+    public  void    traceSkippingBecauseTooMuchMemory( int maxMemoryPerTable ) {}
+    public  void    traceCostOfNScans( int tableNumber, double rowCount, CostEstimate cost
) {}
+    public  void    traceSkipUnmaterializableHashJoin() {}
+    public  void    traceSkipHashJoinNoHashKeys() {}
+    public  void    traceHashKeyColumns( int[] hashKeyColumns ) {}
+    public  void    traceOptimizingJoinNode() {}
+    public  void    traceConsideringJoinStrategy( JoinStrategy js, int tableNumber ) {}
+    public  void    traceRememberingBestAccessPath( AccessPath accessPath, int tableNumber,
int planType ) {}
+    public  void    traceNoMoreConglomerates( int tableNumber ) {}
+    public  void    traceConsideringConglomerate( ConglomerateDescriptor cd, int tableNumber
) {}
+    public  void    traceScanningHeapWithUniqueKey() {}
+    public  void    traceAddingUnorderedOptimizable( int predicateCount ) {}
+    public  void    traceChangingAccessPathForTable( int tableNumber ) {}
+    public  void    traceNoStartStopPosition() {}
+    public  void    traceNonCoveringIndexCost( double cost, int tableNumber ) {}
+    public  void    traceConstantStartStopPositions() {}
+    public  void    traceEstimatingCostOfConglomerate( ConglomerateDescriptor cd, int tableNumber
) {}
+    public  void    traceLookingForSpecifiedIndex( String indexName, int tableNumber ) {}
+    public  void    traceSingleMatchedRowCost( double cost, int tableNumber ) {}
+    public  void    traceCostIncludingExtra1stColumnSelectivity( CostEstimate cost, int tableNumber
) {}
+    public  void    traceNextAccessPath( String baseTable, int predicateCount ) {}
+    public  void    traceCostIncludingExtraStartStop( CostEstimate cost, int tableNumber
) {}
+    public  void    traceCostIncludingExtraQualifierSelectivity( CostEstimate cost, int tableNumber
) {}
+    public  void    traceCostIncludingExtraNonQualifierSelectivity( CostEstimate cost, int
tableNumber ) {}
+    public  void    traceCostOfNoncoveringIndex( CostEstimate cost, int tableNumber ) {}
+    public  void    traceRememberingJoinStrategy( JoinStrategy joinStrategy, int tableNumber
) {}
+    public  void    traceRememberingBestAccessPathSubstring( AccessPath ap, int tableNumber
) {}
+    public  void    traceRememberingBestSortAvoidanceAccessPathSubstring( AccessPath ap,
int tableNumber ) {}
+    public  void    traceRememberingBestUnknownAccessPathSubstring( AccessPath ap, int tableNumber
) {}
+    public  void    traceCostOfConglomerateScan
+        (
+         int    tableNumber,
+         ConglomerateDescriptor cd,
+         CostEstimate   costEstimate,
+         int    numExtraFirstColumnPreds,
+         double    extraFirstColumnSelectivity,
+         int    numExtraStartStopPreds,
+         double    extraStartStopSelectivity,
+         int    startStopPredCount,
+         double    statStartStopSelectivity,
+         int    numExtraQualifiers,
+         double    extraQualifierSelectivity,
+         int    numExtraNonQualifiers,
+         double    extraNonQualifierSelectivity
+         )  {}
+    public  void    traceCostIncludingCompositeSelectivityFromStats( CostEstimate cost, int
tableNumber ) {}
+    public  void    traceCompositeSelectivityFromStatistics( double statCompositeSelectivity
) {}
+    public  void    traceCostIncludingStatsForIndex( CostEstimate cost, int tableNumber )
{}
+
+    
+    ////////////////////////////////////////////////////////////////////////
+    //
+    //	NESTED SUBCLASS WHICH IS MISSING THE 0-ARG CONSTRUCTOR
+    //
+    ////////////////////////////////////////////////////////////////////////
+
+    public  static  final   class   BadSubclass extends DummyOptTrace
+    {
+        public  BadSubclass( int dummy ) {}
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DummyOptTrace.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/OptionalToolsTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/OptionalToolsTest.java?rev=1479552&r1=1479551&r2=1479552&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/OptionalToolsTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/OptionalToolsTest.java
Mon May  6 12:46:32 2013
@@ -514,4 +514,102 @@ public class OptionalToolsTest  extends 
 
     }
     
+    /**
+     * <p>
+     * Test loading a customized optimizer tracer. See DERBY-6211.
+     * </p>
+     */
+    public void test_04_customOptimizerTrace() throws Exception
+    {
+        Connection  dboConnection = openUserConnection( TEST_DBO );
+
+        goodStatement
+            (
+             dboConnection,
+             "create function fullTrace() returns varchar( 32672 )\n" +
+             "language java parameter style java no sql\n" +
+             "external name 'org.apache.derbyTesting.functionTests.tests.lang.DummyOptTrace.fullTrace'\n"
+             );
+
+        // install a custom tracer for the optimizer
+        goodStatement
+            (
+             dboConnection,
+             "call syscs_util.syscs_register_tool\n" +
+             "(\n" +
+             "    'optimizerTracing', true, 'custom',\n" +
+             "    'org.apache.derbyTesting.functionTests.tests.lang.DummyOptTrace'\n" +
+             ")\n"
+             );
+        // run a couple queries
+        goodStatement
+            (
+             dboConnection,
+             "select tablename from sys.systables where 1=2"
+             );
+        goodStatement
+            (
+             dboConnection,
+             "select columnname from sys.syscolumns where 1=2"
+             );
+        // unload the tracer
+        goodStatement
+            (
+             dboConnection,
+             "call syscs_util.syscs_register_tool( 'optimizerTracing', false )"
+             );
+
+        // verify that it actually did something
+        assertResults
+            (
+             dboConnection,
+             "values fullTrace()",
+             new String[][]
+             {
+                 { "<text>select tablename from sys.systables where 1=2</text><text>select
columnname from sys.syscolumns where 1=2</text><text>call syscs_util.syscs_register_tool(
'optimizerTracing', false )</text>" },
+             },
+             false
+             );
+
+        // drop the function
+        goodStatement
+            (
+             dboConnection,
+             "drop function fullTrace"
+             );
+
+        // no classname given
+        expectExecutionError
+            (
+             dboConnection,
+             UNEXPECTED_USER_EXCEPTION,
+             "call syscs_util.syscs_register_tool( 'optimizerTracing', true, 'custom' )"
+             );
+        // class can't be found
+        expectExecutionError
+            (
+             dboConnection,
+             UNEXPECTED_USER_EXCEPTION,
+             "call syscs_util.syscs_register_tool( 'optimizerTracing', true, 'custom', 'foo.bar.Wibble'
)"
+             );
+        // error because class doesn't implement OptTrace
+        expectExecutionError
+            (
+             dboConnection,
+             UNEXPECTED_USER_EXCEPTION,
+             "call syscs_util.syscs_register_tool( 'optimizerTracing', true, 'custom', 'java.lang.String'
)"
+             );
+        // error because class doesn't have a 0-arg constructor
+        expectExecutionError
+            (
+             dboConnection,
+             UNEXPECTED_USER_EXCEPTION,
+             "call syscs_util.syscs_register_tool\n" +
+             "(\n" +
+             "    'optimizerTracing', true, 'custom',\n" +
+             "    'org.apache.derbyTesting.functionTests.tests.lang.DummyOptTrace$BadSubclass'\n"
+
+             ")\n"
+             );
+    }
+
 }

Modified: db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/optional/OptimizerTracer.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/optional/OptimizerTracer.java?rev=1479552&r1=1479551&r2=1479552&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/optional/OptimizerTracer.java
(original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/optional/OptimizerTracer.java
Mon May  6 12:46:32 2013
@@ -25,9 +25,15 @@ import java.io.PrintWriter;
 import java.sql.SQLException;
 
 import org.apache.derby.iapi.db.OptimizerTrace;
+import org.apache.derby.iapi.tools.i18n.LocalizedResource;
+import org.apache.derby.iapi.services.loader.ClassFactory;
+import org.apache.derby.iapi.services.context.ContextService;
+import org.apache.derby.iapi.sql.compile.CompilerContext;
 import org.apache.derby.iapi.sql.compile.OptTrace;
 import org.apache.derby.iapi.sql.dictionary.OptionalTool;
 
+import org.apache.derby.impl.sql.compile.DefaultOptTrace;
+
 /**
  * <p>
  * OptionalTool to create wrapper functions and views for all of the user tables
@@ -65,13 +71,50 @@ public	class   OptimizerTracer  implemen
 
     /**
      * <p>
-     * Turns on optimizer tracing.
+     * Turns on optimizer tracing. May take optional parameters:
      * </p>
+     *
+     * <ul>
+     * <li>custom, $class - If the first arg is the "custom" literal, then the next
arg must be
+     * the name of a class which implements org.apache.derby.iapi.sql.compile.OptTrace
+     * and which has a 0-arg constructor. The 0-arg constructor is called and the resulting
+     * OptTrace object is plugged in to trace the optimizer.</li>
+     * </ul>
      */
     public  void    loadTool( String... configurationParameters )
         throws SQLException
     {
-        OptimizerTrace.setOptimizerTrace( true );
+        OptTrace    tracer;
+
+        if ( (configurationParameters == null) || (configurationParameters.length == 0) )
+        {
+            tracer = new DefaultOptTrace();
+        }
+        else if ( "custom".equals( configurationParameters[ 0 ] ) )
+        {
+            if ( configurationParameters.length != 2 )
+            { throw wrap( LocalizedResource.getMessage( "OT_BadLoadUnloadArgs" ) ); }
+
+            String  customOptTraceName = configurationParameters[ 1 ];
+
+            try {
+                CompilerContext cc = (CompilerContext) ContextService.getContext( CompilerContext.CONTEXT_ID
);
+                ClassFactory    classFactory = cc.getClassFactory();
+
+                tracer = (OptTrace) classFactory.loadApplicationClass( customOptTraceName
).newInstance();
+            }
+            catch (InstantiationException cnfe) { throw cantInstantiate( customOptTraceName
); }
+            catch (ClassNotFoundException cnfe) { throw cantInstantiate( customOptTraceName
); }
+            catch (IllegalAccessException cnfe) { throw cantInstantiate( customOptTraceName
); }
+            catch (Throwable t) { throw wrap( t ); }
+        }
+        else { throw wrap( LocalizedResource.getMessage( "OT_BadLoadUnloadArgs" ) ); }
+                     
+        OptimizerTrace.setOptimizerTracer( tracer );
+    }
+    private SQLException    cantInstantiate( String className )
+    {
+        return wrap( LocalizedResource.getMessage( "OT_CantInstantiateClass", className )
);
     }
 
     /**
@@ -127,5 +170,12 @@ public	class   OptimizerTracer  implemen
     {
         return new SQLException( t.getMessage(), t );
     }
+    
+    private SQLException    wrap( String errorMessage )
+    {
+        String  sqlState = org.apache.derby.shared.common.reference.SQLState.JAVA_EXCEPTION.substring(
0, 5 );
+
+        return new SQLException( errorMessage, sqlState );
+    }
 }
 

Modified: db/derby/code/trunk/java/tools/org/apache/derby/loc/toolsmessages.properties
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/loc/toolsmessages.properties?rev=1479552&r1=1479551&r2=1479552&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/loc/toolsmessages.properties (original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/loc/toolsmessages.properties Mon May 
6 12:46:32 2013
@@ -406,4 +406,5 @@ OT_BadLoadUnloadArgs=Bad arguments passe
 # arg0 = a JDBC type ID from java.sql.Types
 # arg1 = the name of a foreign data type returned by java.sql.DatabaseMetaData.getColumns().getString(
6 )
 OT_UnknownForeignDataType=Unknown external data type. JDBC type = {0}, external type name
= {1}.
+OT_CantInstantiateClass=Cannot create an instance of {0}. Maybe this class is not visible
on the classpath. Maybe it doesn not have a 0-arg constructor.
 



Mime
View raw message