{"action":"created","comment":{"url":"https://api.github.com/repos/confluentinc/ksql/pulls/comments/195175240","pull_request_review_id":128478988,"id":195175240,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDE5NTE3NTI0MA==","diff_hunk":"@@ -0,0 +1,154 @@\n+/*\n+ * Copyright 2018 Confluent Inc.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package io.confluent.ksql.function;\n+\n+import com.google.common.base.Preconditions;\n+\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+import java.io.File;\n+import java.io.IOException;\n+import java.lang.reflect.Method;\n+import java.nio.file.Files;\n+import java.util.Arrays;\n+import java.util.Objects;\n+import java.util.function.Predicate;\n+import java.util.stream.Collectors;\n+\n+import io.confluent.ksql.function.udf.PluggableUdf;\n+import io.confluent.ksql.function.udf.Udf;\n+import io.confluent.ksql.function.udf.UdfDescription;\n+import io.confluent.ksql.metastore.MetaStore;\n+import io.confluent.ksql.util.KsqlConfig;\n+import io.confluent.ksql.util.KsqlException;\n+import io.confluent.ksql.util.SchemaUtil;\n+import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;\n+\n+public class UdfLoader {\n+\n+  private static final Logger logger = LoggerFactory.getLogger(UdfLoader.class);\n+\n+  private final MetaStore metaStore;\n+  private final File pluginDir;\n+  private final ClassLoader parentClassLoader;\n+  private final Predicate<String> blacklist;\n+  private final UdfCompiler compiler;\n+  private final boolean loadCustomerUdfs;\n+\n+\n+  public UdfLoader(final MetaStore metaStore,\n+                   final File pluginDir,\n+                   final ClassLoader parentClassLoader,\n+                   final Predicate<String> blacklist,\n+                   final UdfCompiler compiler,\n+                   final boolean loadCustomerUdfs) {\n+    this.metaStore = Objects.requireNonNull(metaStore, \"metaStore can't be null\");\n+    this.pluginDir = Objects.requireNonNull(pluginDir, \"pluginDir can't be null\");\n+    this.parentClassLoader = Objects.requireNonNull(parentClassLoader,\n+        \"parentClassLoader can't be null\");\n+    this.blacklist = Objects.requireNonNull(blacklist, \"blacklist can't be null\");\n+    this.compiler = Objects.requireNonNull(compiler, \"compiler can't be null\");\n+    this.loadCustomerUdfs = loadCustomerUdfs;\n+  }\n+\n+  public void load() {\n+    // load udfs packaged as part of ksql first\n+    loadUdfs(parentClassLoader);\n+    if (loadCustomerUdfs) {\n+      try {\n+        Files.find(pluginDir.toPath(), 1, (path, attributes) -> path.toString().endsWith(\".jar\"))\n+            .map(path -> UdfClassLoader.newClassLoader(path, parentClassLoader, blacklist))\n+            .forEach(this::loadUdfs);\n+      } catch (IOException e) {\n+        logger.error(\"Failed to load UDFs from location {}\", pluginDir, e);\n+      }\n+    }\n+  }\n+\n+\n+  private void loadUdfs(final ClassLoader loader) {\n+    new FastClasspathScanner()\n+        .overrideClassLoaders(loader)\n+        .ignoreParentClassLoaders()\n+        // if we are loading from the parent classloader then restrict the name space to only\n+        // jars/dirs containing \"ksql-engine\". This is so we don't end up scanning every jar\n+        .filterClasspathElements(\n+            name -> parentClassLoader != loader || name.contains(\"ksql-engine\"))\n+        .matchClassesWithMethodAnnotation(Udf.class,\n+            (theClass, executable) -> {\n+              final UdfDescription annotation = theClass.getAnnotation(UdfDescription.class);\n+              if (annotation != null) {\n+                final Method method = (Method) executable;","path":"ksql-engine/src/main/java/io/confluent/ksql/function/UdfLoader.java","position":96,"original_position":96,"commit_id":"9855328589e62031730618e0240516e6377184f6","original_commit_id":"9855328589e62031730618e0240516e6377184f6","user":{"login":"big-andy-coates","id":8012398,"node_id":"MDQ6VXNlcjgwMTIzOTg=","avatar_url":"https://avatars0.githubusercontent.com/u/8012398?v=4","gravatar_id":"","url":"https://api.github.com/users/big-andy-coates","html_url":"https://github.com/big-andy-coates","followers_url":"https://api.github.com/users/big-andy-coates/followers","following_url":"https://api.github.com/users/big-andy-coates/following{/other_user}","gists_url":"https://api.github.com/users/big-andy-coates/gists{/gist_id}","starred_url":"https://api.github.com/users/big-andy-coates/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/big-andy-coates/subscriptions","organizations_url":"https://api.github.com/users/big-andy-coates/orgs","repos_url":"https://api.github.com/users/big-andy-coates/repos","events_url":"https://api.github.com/users/big-andy-coates/events{/privacy}","received_events_url":"https://api.github.com/users/big-andy-coates/received_events","type":"User","site_admin":false},"body":"Is there any way this won't be a Method? e.g. misplaced annotation?","created_at":"2018-06-13T17:42:25Z","updated_at":"2018-06-13T17:48:55Z","html_url":"https://github.com/confluentinc/ksql/pull/1366#discussion_r195175240","pull_request_url":"https://api.github.com/repos/confluentinc/ksql/pulls/1366","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/confluentinc/ksql/pulls/comments/195175240"},"html":{"href":"https://github.com/confluentinc/ksql/pull/1366#discussion_r195175240"},"pull_request":{"href":"https://api.github.com/repos/confluentinc/ksql/pulls/1366"}}},"pull_request":{"url":"https://api.github.com/repos/confluentinc/ksql/pulls/1366","id":192014026,"node_id":"MDExOlB1bGxSZXF1ZXN0MTkyMDE0MDI2","html_url":"https://github.com/confluentinc/ksql/pull/1366","diff_url":"https://github.com/confluentinc/ksql/pull/1366.diff","patch_url":"https://github.com/confluentinc/ksql/pull/1366.patch","issue_url":"https://api.github.com/repos/confluentinc/ksql/issues/1366","number":1366,"state":"closed","locked":false,"title":"Create and load Java based UDFs from an ext directory","user":{"login":"dguy","id":54829,"node_id":"MDQ6VXNlcjU0ODI5","avatar_url":"https://avatars2.githubusercontent.com/u/54829?v=4","gravatar_id":"","url":"https://api.github.com/users/dguy","html_url":"https://github.com/dguy","followers_url":"https://api.github.com/users/dguy/followers","following_url":"https://api.github.com/users/dguy/following{/other_user}","gists_url":"https://api.github.com/users/dguy/gists{/gist_id}","starred_url":"https://api.github.com/users/dguy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dguy/subscriptions","organizations_url":"https://api.github.com/users/dguy/orgs","repos_url":"https://api.github.com/users/dguy/repos","events_url":"https://api.github.com/users/dguy/events{/privacy}","received_events_url":"https://api.github.com/users/dguy/received_events","type":"User","site_admin":false},"body":"### Description \r\nAdds the ability to deploy udf jars in an ext directory. Udfs are loaded from the jars by searching for the anotations `@UdfDescription` and `@Udf`. Each method (in a class with `@UdfDescription`) marked with `@Udf` is compiled into a class that can be called via the existing function call code. Each UDF gets it's own child first classloader so that they are free to include versions of jars that are different than those provided by ksql.\r\n\r\nClasses can be \"Blacklisted\" from being called from a UDF, i.e., so they can't call `System.exit()`, for example.\r\n\r\nHave also changed the `SubstringKudf` to this new form and it is now loaded in the same way, but from the KSQL classpath.\r\n\r\nStill to do:\r\n\r\n1.  validate that the udf names are valid java identifiers and are not SQL reserved words\r\n2. Add metric collection\r\n3. Handling of null params in the wrapper, i.e., `PluggableUdf`\r\n4. Docs\r\n5. Change existing CodeGenRunner to compile classes rather than use reflection to invoke functions\r\n\r\n### Testing done \r\nAdded new tests for the new components. Validated that existing tests work. Added a jar with UDFs in it and validate that they can be loaded and invoked.\r\n\r\n### Reviewer checklist\r\n- [ ] Ensure docs are updated if necessary. (eg. if a user visible feature is being added or changed).\r\n- [ ] Ensure relevant issues are linked (description should include text like \"Fixes #<issue number>\")\r\n\r\n","created_at":"2018-06-01T11:58:27Z","updated_at":"2018-06-13T17:48:55Z","closed_at":"2018-06-13T08:56:38Z","merged_at":"2018-06-13T08:56:38Z","merge_commit_sha":"5249c70bca9ddd266092944c2f5a2a2a56c26c05","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/confluentinc/ksql/pulls/1366/commits","review_comments_url":"https://api.github.com/repos/confluentinc/ksql/pulls/1366/comments","review_comment_url":"https://api.github.com/repos/confluentinc/ksql/pulls/comments{/number}","comments_url":"https://api.github.com/repos/confluentinc/ksql/issues/1366/comments","statuses_url":"https://api.github.com/repos/confluentinc/ksql/statuses/9855328589e62031730618e0240516e6377184f6","head":{"label":"dguy:udf-loader","ref":"udf-loader","sha":"9855328589e62031730618e0240516e6377184f6","user":{"login":"dguy","id":54829,"node_id":"MDQ6VXNlcjU0ODI5","avatar_url":"https://avatars2.githubusercontent.com/u/54829?v=4","gravatar_id":"","url":"https://api.github.com/users/dguy","html_url":"https://github.com/dguy","followers_url":"https://api.github.com/users/dguy/followers","following_url":"https://api.github.com/users/dguy/following{/other_user}","gists_url":"https://api.github.com/users/dguy/gists{/gist_id}","starred_url":"https://api.github.com/users/dguy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dguy/subscriptions","organizations_url":"https://api.github.com/users/dguy/orgs","repos_url":"https://api.github.com/users/dguy/repos","events_url":"https://api.github.com/users/dguy/events{/privacy}","received_events_url":"https://api.github.com/users/dguy/received_events","type":"User","site_admin":false},"repo":{"id":105937514,"node_id":"MDEwOlJlcG9zaXRvcnkxMDU5Mzc1MTQ=","name":"ksql","full_name":"dguy/ksql","owner":{"login":"dguy","id":54829,"node_id":"MDQ6VXNlcjU0ODI5","avatar_url":"https://avatars2.githubusercontent.com/u/54829?v=4","gravatar_id":"","url":"https://api.github.com/users/dguy","html_url":"https://github.com/dguy","followers_url":"https://api.github.com/users/dguy/followers","following_url":"https://api.github.com/users/dguy/following{/other_user}","gists_url":"https://api.github.com/users/dguy/gists{/gist_id}","starred_url":"https://api.github.com/users/dguy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dguy/subscriptions","organizations_url":"https://api.github.com/users/dguy/orgs","repos_url":"https://api.github.com/users/dguy/repos","events_url":"https://api.github.com/users/dguy/events{/privacy}","received_events_url":"https://api.github.com/users/dguy/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/dguy/ksql","description":"KSQL - a Streaming SQL Engine for Apache Kafka ","fork":true,"url":"https://api.github.com/repos/dguy/ksql","forks_url":"https://api.github.com/repos/dguy/ksql/forks","keys_url":"https://api.github.com/repos/dguy/ksql/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dguy/ksql/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dguy/ksql/teams","hooks_url":"https://api.github.com/repos/dguy/ksql/hooks","issue_events_url":"https://api.github.com/repos/dguy/ksql/issues/events{/number}","events_url":"https://api.github.com/repos/dguy/ksql/events","assignees_url":"https://api.github.com/repos/dguy/ksql/assignees{/user}","branches_url":"https://api.github.com/repos/dguy/ksql/branches{/branch}","tags_url":"https://api.github.com/repos/dguy/ksql/tags","blobs_url":"https://api.github.com/repos/dguy/ksql/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dguy/ksql/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dguy/ksql/git/refs{/sha}","trees_url":"https://api.github.com/repos/dguy/ksql/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dguy/ksql/statuses/{sha}","languages_url":"https://api.github.com/repos/dguy/ksql/languages","stargazers_url":"https://api.github.com/repos/dguy/ksql/stargazers","contributors_url":"https://api.github.com/repos/dguy/ksql/contributors","subscribers_url":"https://api.github.com/repos/dguy/ksql/subscribers","subscription_url":"https://api.github.com/repos/dguy/ksql/subscription","commits_url":"https://api.github.com/repos/dguy/ksql/commits{/sha}","git_commits_url":"https://api.github.com/repos/dguy/ksql/git/commits{/sha}","comments_url":"https://api.github.com/repos/dguy/ksql/comments{/number}","issue_comment_url":"https://api.github.com/repos/dguy/ksql/issues/comments{/number}","contents_url":"https://api.github.com/repos/dguy/ksql/contents/{+path}","compare_url":"https://api.github.com/repos/dguy/ksql/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dguy/ksql/merges","archive_url":"https://api.github.com/repos/dguy/ksql/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dguy/ksql/downloads","issues_url":"https://api.github.com/repos/dguy/ksql/issues{/number}","pulls_url":"https://api.github.com/repos/dguy/ksql/pulls{/number}","milestones_url":"https://api.github.com/repos/dguy/ksql/milestones{/number}","notifications_url":"https://api.github.com/repos/dguy/ksql/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dguy/ksql/labels{/name}","releases_url":"https://api.github.com/repos/dguy/ksql/releases{/id}","deployments_url":"https://api.github.com/repos/dguy/ksql/deployments","created_at":"2017-10-05T20:33:32Z","updated_at":"2017-10-05T20:33:35Z","pushed_at":"2018-06-13T16:24:18Z","git_url":"git://github.com/dguy/ksql.git","ssh_url":"git@github.com:dguy/ksql.git","clone_url":"https://github.com/dguy/ksql.git","svn_url":"https://github.com/dguy/ksql","homepage":"","size":7834,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"0.1.x"}},"base":{"label":"confluentinc:5.0.x","ref":"5.0.x","sha":"8eece10151113cddc40e971c6729a29e9c9b92d5","user":{"login":"confluentinc","id":9439498,"node_id":"MDEyOk9yZ2FuaXphdGlvbjk0Mzk0OTg=","avatar_url":"https://avatars0.githubusercontent.com/u/9439498?v=4","gravatar_id":"","url":"https://api.github.com/users/confluentinc","html_url":"https://github.com/confluentinc","followers_url":"https://api.github.com/users/confluentinc/followers","following_url":"https://api.github.com/users/confluentinc/following{/other_user}","gists_url":"https://api.github.com/users/confluentinc/gists{/gist_id}","starred_url":"https://api.github.com/users/confluentinc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/confluentinc/subscriptions","organizations_url":"https://api.github.com/users/confluentinc/orgs","repos_url":"https://api.github.com/users/confluentinc/repos","events_url":"https://api.github.com/users/confluentinc/events{/privacy}","received_events_url":"https://api.github.com/users/confluentinc/received_events","type":"Organization","site_admin":false},"repo":{"id":69400326,"node_id":"MDEwOlJlcG9zaXRvcnk2OTQwMDMyNg==","name":"ksql","full_name":"confluentinc/ksql","owner":{"login":"confluentinc","id":9439498,"node_id":"MDEyOk9yZ2FuaXphdGlvbjk0Mzk0OTg=","avatar_url":"https://avatars0.githubusercontent.com/u/9439498?v=4","gravatar_id":"","url":"https://api.github.com/users/confluentinc","html_url":"https://github.com/confluentinc","followers_url":"https://api.github.com/users/confluentinc/followers","following_url":"https://api.github.com/users/confluentinc/following{/other_user}","gists_url":"https://api.github.com/users/confluentinc/gists{/gist_id}","starred_url":"https://api.github.com/users/confluentinc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/confluentinc/subscriptions","organizations_url":"https://api.github.com/users/confluentinc/orgs","repos_url":"https://api.github.com/users/confluentinc/repos","events_url":"https://api.github.com/users/confluentinc/events{/privacy}","received_events_url":"https://api.github.com/users/confluentinc/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/confluentinc/ksql","description":"KSQL - the Streaming SQL Engine for Apache Kafka ","fork":false,"url":"https://api.github.com/repos/confluentinc/ksql","forks_url":"https://api.github.com/repos/confluentinc/ksql/forks","keys_url":"https://api.github.com/repos/confluentinc/ksql/keys{/key_id}","collaborators_url":"https://api.github.com/repos/confluentinc/ksql/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/confluentinc/ksql/teams","hooks_url":"https://api.github.com/repos/confluentinc/ksql/hooks","issue_events_url":"https://api.github.com/repos/confluentinc/ksql/issues/events{/number}","events_url":"https://api.github.com/repos/confluentinc/ksql/events","assignees_url":"https://api.github.com/repos/confluentinc/ksql/assignees{/user}","branches_url":"https://api.github.com/repos/confluentinc/ksql/branches{/branch}","tags_url":"https://api.github.com/repos/confluentinc/ksql/tags","blobs_url":"https://api.github.com/repos/confluentinc/ksql/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/confluentinc/ksql/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/confluentinc/ksql/git/refs{/sha}","trees_url":"https://api.github.com/repos/confluentinc/ksql/git/trees{/sha}","statuses_url":"https://api.github.com/repos/confluentinc/ksql/statuses/{sha}","languages_url":"https://api.github.com/repos/confluentinc/ksql/languages","stargazers_url":"https://api.github.com/repos/confluentinc/ksql/stargazers","contributors_url":"https://api.github.com/repos/confluentinc/ksql/contributors","subscribers_url":"https://api.github.com/repos/confluentinc/ksql/subscribers","subscription_url":"https://api.github.com/repos/confluentinc/ksql/subscription","commits_url":"https://api.github.com/repos/confluentinc/ksql/commits{/sha}","git_commits_url":"https://api.github.com/repos/confluentinc/ksql/git/commits{/sha}","comments_url":"https://api.github.com/repos/confluentinc/ksql/comments{/number}","issue_comment_url":"https://api.github.com/repos/confluentinc/ksql/issues/comments{/number}","contents_url":"https://api.github.com/repos/confluentinc/ksql/contents/{+path}","compare_url":"https://api.github.com/repos/confluentinc/ksql/compare/{base}...{head}","merges_url":"https://api.github.com/repos/confluentinc/ksql/merges","archive_url":"https://api.github.com/repos/confluentinc/ksql/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/confluentinc/ksql/downloads","issues_url":"https://api.github.com/repos/confluentinc/ksql/issues{/number}","pulls_url":"https://api.github.com/repos/confluentinc/ksql/pulls{/number}","milestones_url":"https://api.github.com/repos/confluentinc/ksql/milestones{/number}","notifications_url":"https://api.github.com/repos/confluentinc/ksql/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/confluentinc/ksql/labels{/name}","releases_url":"https://api.github.com/repos/confluentinc/ksql/releases{/id}","deployments_url":"https://api.github.com/repos/confluentinc/ksql/deployments","created_at":"2016-09-27T21:30:15Z","updated_at":"2018-06-13T17:07:13Z","pushed_at":"2018-06-13T17:07:10Z","git_url":"git://github.com/confluentinc/ksql.git","ssh_url":"git@github.com:confluentinc/ksql.git","clone_url":"https://github.com/confluentinc/ksql.git","svn_url":"https://github.com/confluentinc/ksql","homepage":"","size":7955,"stargazers_count":1714,"watchers_count":1714,"language":"Java","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":320,"mirror_url":null,"archived":false,"open_issues_count":305,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":320,"open_issues":305,"watchers":1714,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/confluentinc/ksql/pulls/1366"},"html":{"href":"https://github.com/confluentinc/ksql/pull/1366"},"issue":{"href":"https://api.github.com/repos/confluentinc/ksql/issues/1366"},"comments":{"href":"https://api.github.com/repos/confluentinc/ksql/issues/1366/comments"},"review_comments":{"href":"https://api.github.com/repos/confluentinc/ksql/pulls/1366/comments"},"review_comment":{"href":"https://api.github.com/repos/confluentinc/ksql/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/confluentinc/ksql/pulls/1366/commits"},"statuses":{"href":"https://api.github.com/repos/confluentinc/ksql/statuses/9855328589e62031730618e0240516e6377184f6"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"big-andy-coates"}}