From commits-return-2848-archive-asf-public=cust-asf.ponee.io@metron.apache.org Wed Apr 18 16:59:37 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id C6E0E1807E4 for ; Wed, 18 Apr 2018 16:59:33 +0200 (CEST) Received: (qmail 93736 invoked by uid 500); 18 Apr 2018 14:59:32 -0000 Mailing-List: contact commits-help@metron.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@metron.apache.org Delivered-To: mailing list commits@metron.apache.org Received: (qmail 93241 invoked by uid 99); 18 Apr 2018 14:59:32 -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; Wed, 18 Apr 2018 14:59:32 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 0FA1DF68FA; Wed, 18 Apr 2018 14:59:31 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: otto@apache.org To: commits@metron.apache.org Date: Wed, 18 Apr 2018 15:00:16 -0000 Message-Id: <8d3e963be5d94e8899ce7f898314f615@git.apache.org> In-Reply-To: <01a9dec83c064e4abd2c287d4c4baab7@git.apache.org> References: <01a9dec83c064e4abd2c287d4c4baab7@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [46/52] [abbrv] metron git commit: METRON-1515: Errors loading stellar functions currently bomb the entire topology, they should be recoverable closes apache/incubator-metron#985 METRON-1515: Errors loading stellar functions currently bomb the entire topology, they should be recoverable closes apache/incubator-metron#985 Project: http://git-wip-us.apache.org/repos/asf/metron/repo Commit: http://git-wip-us.apache.org/repos/asf/metron/commit/3fcbf8b4 Tree: http://git-wip-us.apache.org/repos/asf/metron/tree/3fcbf8b4 Diff: http://git-wip-us.apache.org/repos/asf/metron/diff/3fcbf8b4 Branch: refs/heads/feature/METRON-1211-extensions-parsers-gradual Commit: 3fcbf8b4e4e38f9c50842b8af857092b091c7c40 Parents: 1d3e7fc Author: cstella Authored: Mon Apr 16 15:12:11 2018 -0400 Committer: cstella Committed: Mon Apr 16 15:12:11 2018 -0400 ---------------------------------------------------------------------- .../resolver/ClasspathFunctionResolver.java | 45 +++++++++++++++----- .../resolver/ClasspathFunctionResolverTest.java | 30 +++++++++++++ 2 files changed, 65 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/metron/blob/3fcbf8b4/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/dsl/functions/resolver/ClasspathFunctionResolver.java ---------------------------------------------------------------------- diff --git a/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/dsl/functions/resolver/ClasspathFunctionResolver.java b/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/dsl/functions/resolver/ClasspathFunctionResolver.java index 85aa015..b17233a 100644 --- a/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/dsl/functions/resolver/ClasspathFunctionResolver.java +++ b/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/dsl/functions/resolver/ClasspathFunctionResolver.java @@ -34,6 +34,7 @@ import org.apache.metron.stellar.dsl.Context; import org.apache.metron.stellar.dsl.Stellar; import org.apache.metron.stellar.dsl.StellarFunction; +import org.atteo.classindex.ClassFilter; import org.atteo.classindex.ClassIndex; import org.reflections.util.FilterBuilder; @@ -219,6 +220,17 @@ public class ClasspathFunctionResolver extends BaseFunctionResolver { } } + protected Iterable> getStellarClasses(ClassLoader cl) { + return ClassIndex.getAnnotated(Stellar.class, cl); + } + + protected boolean includeClass(Class c, FilterBuilder filterBuilder) + { + boolean isAssignable = StellarFunction.class.isAssignableFrom(c); + boolean isFiltered = filterBuilder.apply(c.getCanonicalName()); + return isAssignable && isFiltered; + } + /** * Returns a set of classes that should undergo further interrogation for resolution * (aka discovery) of Stellar functions. @@ -254,16 +266,29 @@ public class ClasspathFunctionResolver extends BaseFunctionResolver { Set classes = new HashSet<>(); Set> ret = new HashSet<>(); for(ClassLoader cl : cls) { - for(Class c : ClassIndex.getAnnotated(Stellar.class, cl)) { - LOG.debug("{}: Found class: {}", cl.getClass().getCanonicalName(), c.getCanonicalName()); - boolean isAssignable = StellarFunction.class.isAssignableFrom(c); - boolean isFiltered = filterBuilder.apply(c.getCanonicalName()); - if( isAssignable && isFiltered ) { - String className = c.getName(); - if(!classes.contains(className)) { - LOG.debug("{}: Added class: {}", cl.getClass().getCanonicalName(), className); - ret.add((Class) c); - classes.add(className); + for(Class c : getStellarClasses(cl)) { + try { + LOG.debug("{}: Found class: {}", cl.getClass().getCanonicalName(), c.getCanonicalName()); + if (includeClass(c, filterBuilder)) { + String className = c.getName(); + if (!classes.contains(className)) { + LOG.debug("{}: Added class: {}", cl.getClass().getCanonicalName(), className); + ret.add((Class) c); + classes.add(className); + } + } + } + catch(Error le) { + //we have had some error loading a stellar function. This could mean that + //the classpath is unstable (e.g. old copies of jars are on the classpath). + try { + LOG.error("Skipping class " + c.getName() + ": " + le.getMessage() + + ", please check that there are not old versions of stellar functions on the classpath.", le); + } + catch(Error ie) { + //it's possible that getName() will throw an exception if the class is VERY malformed. + LOG.error("Skipping class: " + le.getMessage() + + ", please check that there are not old versions of stellar functions on the classpath.", le); } } } http://git-wip-us.apache.org/repos/asf/metron/blob/3fcbf8b4/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/dsl/functions/resolver/ClasspathFunctionResolverTest.java ---------------------------------------------------------------------- diff --git a/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/dsl/functions/resolver/ClasspathFunctionResolverTest.java b/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/dsl/functions/resolver/ClasspathFunctionResolverTest.java index 1d37f99..cc5bc7c 100644 --- a/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/dsl/functions/resolver/ClasspathFunctionResolverTest.java +++ b/metron-stellar/stellar-common/src/test/java/org/apache/metron/stellar/dsl/functions/resolver/ClasspathFunctionResolverTest.java @@ -18,20 +18,27 @@ package org.apache.metron.stellar.dsl.functions.resolver; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import org.apache.commons.vfs2.FileSystemException; import org.apache.metron.stellar.dsl.Context; +import org.apache.metron.stellar.dsl.StellarFunction; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; +import org.reflections.util.FilterBuilder; import java.io.File; import java.util.HashSet; import java.util.List; import java.util.Properties; +import java.util.Set; import static org.apache.metron.stellar.dsl.functions.resolver.ClasspathFunctionResolver.Config.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class ClasspathFunctionResolverTest { @@ -121,4 +128,27 @@ public class ClasspathFunctionResolverTest { Assert.assertTrue(functions.contains("NOW")); } + @Test + public void testInvalidStellarClass() throws Exception { + StellarFunction goodFunc = mock(StellarFunction.class); + StellarFunction badFunc = mock(StellarFunction.class); + ClasspathFunctionResolver resolver = new ClasspathFunctionResolver() { + @Override + protected Iterable> getStellarClasses(ClassLoader cl) { + return ImmutableList.of(goodFunc.getClass(), badFunc.getClass()); + } + + @Override + protected boolean includeClass(Class c, FilterBuilder filterBuilder) { + if(c != goodFunc.getClass()) { + throw new LinkageError("failed!"); + } + return true; + } + }; + Set> funcs = resolver.resolvables(); + Assert.assertEquals(1, funcs.size()); + Assert.assertEquals(goodFunc.getClass(), Iterables.getFirst(funcs, null)); + } + }