metron-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (METRON-396) Make Stellar function resolution happen via an annotation and classpath search
Date Thu, 01 Sep 2016 06:44:20 GMT

    [ https://issues.apache.org/jira/browse/METRON-396?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15454507#comment-15454507
] 

ASF GitHub Bot commented on METRON-396:
---------------------------------------

Github user cestella commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/233#discussion_r77121424
  
    --- 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 --
    
    I think that's a fine addition as a separate JIRA.  At the moment, this is executed once
per topology start, so I'm ok with this default implementation.  Also, keep in mind that there
are edge cases here, specifically around jars with other jars referenced in their manifest
that the Reflections API is handling under the hood.  It's a worthy task, but one that should
be a follow-on to this task.


> Make Stellar function resolution happen via an annotation and classpath search
> ------------------------------------------------------------------------------
>
>                 Key: METRON-396
>                 URL: https://issues.apache.org/jira/browse/METRON-396
>             Project: Metron
>          Issue Type: Improvement
>            Reporter: Casey Stella
>            Assignee: Casey Stella
>              Labels: stellar
>   Original Estimate: 48h
>  Remaining Estimate: 48h
>
> At the moment, the only way to add a stellar function is to implement StellarFunction
and add the instance to the StellarFunctions enum.  This is deficient in a few ways:
> * It requires you to modify Metron code to add a stellar capability
> * It requires you to make metron-common depend on your project
> * There is no way to enforce documentation of the function
> Instead, we should create an annotation which will define the name, namespace, description,
parameters and returns.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message