Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id A8CFB200BF8 for ; Fri, 9 Dec 2016 05:36:02 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id A67A0160B27; Fri, 9 Dec 2016 04:36:02 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id C9314160B1F for ; Fri, 9 Dec 2016 05:36:01 +0100 (CET) Received: (qmail 24983 invoked by uid 500); 9 Dec 2016 04:36:00 -0000 Mailing-List: contact dev-help@drill.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@drill.apache.org Delivered-To: mailing list dev@drill.apache.org Received: (qmail 24872 invoked by uid 99); 9 Dec 2016 04:36:00 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 09 Dec 2016 04:36:00 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 0DAE3E040F; Fri, 9 Dec 2016 04:35:59 +0000 (UTC) From: paul-rogers To: dev@drill.apache.org Reply-To: dev@drill.apache.org References: In-Reply-To: Subject: [GitHub] drill pull request #660: DRILL-5052: Option to debug generated Java code usi... Content-Type: text/plain Message-Id: <20161209043600.0DAE3E040F@git1-us-west.apache.org> Date: Fri, 9 Dec 2016 04:35:59 +0000 (UTC) archived-at: Fri, 09 Dec 2016 04:36:02 -0000 Github user paul-rogers commented on a diff in the pull request: https://github.com/apache/drill/pull/660#discussion_r91634477 --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/compile/ClassCompilerSelector.java --- @@ -0,0 +1,146 @@ +/** + * 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.drill.exec.compile; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Map; + +import org.apache.drill.common.config.DrillConfig; +import org.apache.drill.common.exceptions.UserException; +import org.apache.drill.exec.compile.ClassTransformer.ClassNames; +import org.apache.drill.exec.exception.ClassTransformationException; +import org.apache.drill.exec.server.options.OptionManager; +import org.apache.drill.exec.server.options.OptionValidator; +import org.apache.drill.exec.server.options.OptionValue; +import org.apache.drill.exec.server.options.TypeValidators.BooleanValidator; +import org.apache.drill.exec.server.options.TypeValidators.LongValidator; +import org.apache.drill.exec.server.options.TypeValidators.StringValidator; +import org.codehaus.commons.compiler.CompileException; + +/** + * Selects between the two supported Java compilers: Janino and + * the build-in Java compiler. + * + *

Session Options

+ *
+ *
exec.java_compiler
+ *
The compiler to use. Valid options are defined in the + * {@link ClassCompilerSelector.CompilerPolicy} enum.
+ *
exec.java_compiler_debug
+ *
If debug logging is enabled, then {@link AbstractClassCompiler} writes the + * generated Java code to the log file prior to compilation. This option + * adds line numbers to the logged code.
+ *
exec.java_compiler_janino_maxsize
+ *
The maximum size of code that the Janio compiler can handle. Larger code is + * handled by the JDK compiler. Defaults to 256K.
+ *
+ *

Configuration Options

+ * Configuration options are used when the above session options are unset. + *
+ *
drill.exec.compile.compiler
+ *
Default for exec.java_compiler
+ *
drill.exec.compile.debug
+ *
Default for exec.java_compiler_debug
+ *
drill.exec.compile.janino_maxsize
+ *
Default for exec.java_compiler_janino_maxsize
+ *
+ */ + +public class ClassCompilerSelector { + public enum CompilerPolicy { + DEFAULT, JDK, JANINO; + } + + public static final String JAVA_COMPILER_JANINO_MAXSIZE_CONFIG = CodeCompiler.COMPILE_BASE + ".janino_maxsize"; + public static final String JAVA_COMPILER_DEBUG_CONFIG = CodeCompiler.COMPILE_BASE + ".debug"; + public static final String JAVA_COMPILER_CONFIG = CodeCompiler.COMPILE_BASE + ".compiler"; + + public static final String JAVA_COMPILER_OPTION = "exec.java_compiler"; + public static final String JAVA_COMPILER_JANINO_MAXSIZE_OPTION = "exec.java_compiler_janino_maxsize"; + public static final OptionValidator JAVA_COMPILER_JANINO_MAXSIZE = new LongValidator(JAVA_COMPILER_JANINO_MAXSIZE_OPTION, 256*1024); + + public static final String JAVA_COMPILER_DEBUG_OPTION = "exec.java_compiler_debug"; + public static final OptionValidator JAVA_COMPILER_DEBUG = new BooleanValidator(JAVA_COMPILER_DEBUG_OPTION, true); + + public static final StringValidator JAVA_COMPILER_VALIDATOR = new StringValidator(JAVA_COMPILER_OPTION, CompilerPolicy.DEFAULT.toString()) { + @Override + public void validate(final OptionValue v, final OptionManager manager) { + super.validate(v, manager); + try { + CompilerPolicy.valueOf(v.string_val.toUpperCase()); + } catch (IllegalArgumentException e) { + throw UserException.validationError() + .message("Invalid value '%s' specified for option '%s'. Valid values are %s.", + v.string_val, getOptionName(), Arrays.toString(CompilerPolicy.values())) + .build(QueryClassLoader.logger); + } + } + }; + + private final CompilerPolicy policy; + private final long janinoThreshold; + + private final AbstractClassCompiler jdkClassCompiler; + private final AbstractClassCompiler janinoClassCompiler; + + public ClassCompilerSelector(ClassLoader classLoader, DrillConfig config, OptionManager sessionOptions) { + OptionValue value = sessionOptions.getOption(JAVA_COMPILER_OPTION); + this.policy = CompilerPolicy.valueOf((value != null) ? value.string_val.toUpperCase() : config.getString(JAVA_COMPILER_CONFIG).toUpperCase()); + + value = sessionOptions.getOption(JAVA_COMPILER_JANINO_MAXSIZE_OPTION); + this.janinoThreshold = (value != null) ? value.num_val : config.getLong(JAVA_COMPILER_JANINO_MAXSIZE_CONFIG); + + value = sessionOptions.getOption(JAVA_COMPILER_DEBUG_OPTION); + boolean debug = (value != null) ? value.bool_val : config.getBoolean(JAVA_COMPILER_DEBUG_CONFIG); + + this.janinoClassCompiler = (policy == CompilerPolicy.JANINO || policy == CompilerPolicy.DEFAULT) ? new JaninoClassCompiler(classLoader, debug) : null; + this.jdkClassCompiler = (policy == CompilerPolicy.JDK || policy == CompilerPolicy.DEFAULT) ? JDKClassCompiler.newInstance(classLoader, debug) : null; + } + + byte[][] getClassByteCode(ClassNames className, String sourceCode) + throws CompileException, ClassNotFoundException, ClassTransformationException, IOException { + + byte[][] bc = getCompiler( sourceCode ).getClassByteCode(className, sourceCode); + + // Uncomment the following to save the generated byte codes. + /* + * final String baseDir = System.getProperty("java.io.tmpdir") + File.separator + classCompiler.getClass().getSimpleName(); + * File classFile = new File(baseDir + className.clazz); + * classFile.getParentFile().mkdirs(); + * BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(classFile)); --- End diff -- Indeed. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---