Github user nickwallen commented on a diff in the pull request:
https://github.com/apache/incubator-metron/pull/233#discussion_r76918718
--- Diff:
metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/FunctionResolverSingleton.java
---
@@ -0,0 +1,206 @@
+/**
+ * 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.metron.common.dsl;
+
+import com.google.common.base.Joiner;
+import org.reflections.Reflections;
+import org.reflections.util.ClasspathHelper;
+import org.reflections.util.ConfigurationBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.URL;
+import java.util.*;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+public class FunctionResolverSingleton implements FunctionResolver {
+ protected static final Logger LOG =
LoggerFactory.getLogger(FunctionResolverSingleton.class);
+ private final Map<String, StellarFunctionInfo> functions = new
HashMap<>();
+ private final AtomicBoolean isInitialized = new AtomicBoolean(false);
+ private static final ReadWriteLock lock = new ReentrantReadWriteLock();
+ private static FunctionResolverSingleton INSTANCE = new
FunctionResolverSingleton();
+
+ private FunctionResolverSingleton() {}
+
+ public static FunctionResolver getInstance() {
+ return INSTANCE;
+ }
+
+
+
+ @Override
+ public Iterable<StellarFunctionInfo> getFunctionInfo() {
+ return _getFunctions().values();
+ }
+
+ @Override
+ public Iterable<String> getFunctions() {
+ return _getFunctions().keySet();
+ }
+
+ @Override
+ public void initialize(Context context) {
+ //forces a load of the stellar functions.
+ _getFunctions();
+ }
+
+ /**
+ * This allows the lazy loading of the functions. We do not want to
take a multi-second hit to analyze the full classpath
+ * every time a unit test starts up. That would cause the runtime of
things to blow right up. Instead, we only want
+ * to take the hit if a function is actually called from a stellar
expression.
+ *
+ *
+ * @return The map of the stellar functions that we found on the
classpath indexed by fully qualified name
+ */
+ private Map<String, StellarFunctionInfo> _getFunctions() {
+ /*
+ * Because we are not doing this in a static block and because this
object is a singleton we have to concern ourselves with
+ * the possiblity that two threads are calling this function at the
same time. Normally, I would consider just making the
+ * function synchronized, but since every stellar statement which uses
a function will be here, I wanted to distinguish
+ * between read locks (that happen often and are quickly resolved) and
write locks (which should happen at initialization time).
+ */
+ lock.readLock().lock();
+ try {
+ if (isInitialized.get()) {
+ return functions;
+ }
+ }
+ finally {
+ lock.readLock().unlock();
+ }
+ //we should VERY rarely get here.
+ lock.writeLock().lock();
+ try {
+ //I have to check again because two threads or more could be waiting
at the lock statement. The loser threads
+ //shouldn't reinitialize.
+ if(!isInitialized.get()) {
+ loadFunctions(functions);
+ isInitialized.set(true);
+ }
+ return functions;
+ }
+ finally {
+ lock.writeLock().unlock();
+ }
+ }
+ /**
+ * Applies this function to the given argument.
+ *
+ * @param s the function argument
+ * @return the function result
+ */
+ @Override
+ public StellarFunction apply(String s) {
+ StellarFunctionInfo ret = _getFunctions().get(s);
+ if(ret == null) {
+ throw new IllegalStateException("Unable to resolve function " + s);
+ }
+ return ret.getFunction();
+ }
+
+ private void loadFunctions(final Map<String, StellarFunctionInfo> ret) {
+ try {
+ ClassLoader classLoader = getClass().getClassLoader();
+ Reflections reflections = new Reflections(new
ConfigurationBuilder().setUrls(effectiveClassPathUrls(classLoader)));
--- End diff --
Would it make sense to introduce some kind of 'import' like functionality?
Instead of searching the entire classpath, the user would specifically define
the locations of Stellar functions that they want to use.
I am curious if a search of the entire classpath will be tenable in all
situations. In terms of performance, its border line acceptable now, but how
might that behave differently in other environments; could it be even slower?
A full classpath search may also increase the chance of unintended naming
collisions. And in the case of a collision, the definition that wins is
non-deterministic. That would cause a major migraine to troubleshoot.
---
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 [email protected] or file a JIRA ticket
with INFRA.
---