Skip to content
This repository was archived by the owner on Aug 20, 2025. It is now read-only.

Conversation

@mmiklavc
Copy link
Contributor

@mmiklavc mmiklavc commented Nov 2, 2018

Contributor Comments

https://issues.apache.org/jira/browse/METRON-1853

Noodling on a method to add shutdown hooks to Stellar. Modified the StellarFunction interface to include a teardown method. Added a no-op to BaseStellarFunction to make it easy for implementations that don't need a teardown.

Rationale - some functions may have long-lived activities that include closeable resources. Adding the teardown method enables function implementers to close any resources opened during init gracefully.

Pull Request Checklist

Thank you for submitting a contribution to Apache Metron.
Please refer to our Development Guidelines for the complete guide to follow for contributions.
Please refer also to our Build Verification Guidelines for complete smoke testing guides.

In order to streamline the review of the contribution we ask you follow these guidelines and ask you to double check the following:

For all changes:

  • Is there a JIRA ticket associated with this PR? If not one needs to be created at Metron Jira.
  • Does your PR title start with METRON-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
  • Has your PR been rebased against the latest commit within the target branch (typically master)?

For code changes:

  • Have you included steps to reproduce the behavior or problem that is being changed or addressed?

  • Have you included steps or a guide to how the change may be verified and tested manually?

  • Have you ensured that the full suite of tests and checks have been executed in the root metron folder via:

    mvn -q clean integration-test install && dev-utilities/build-utils/verify_licenses.sh 
    
  • Have you written or updated unit tests and or integration tests to verify your changes?

  • n/a If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?

  • Have you verified the basic functionality of the build by building and running locally with Vagrant full-dev environment or the equivalent?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered by building and verifying the site-book? If not then run the following commands and the verify changes via site-book/target/site/index.html:

    cd site-book
    mvn site
    

Note:

Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.
It is also recommended that travis-ci is set up for your personal repository such that your branches are built there before submitting a pull request.

@mmiklavc mmiklavc mentioned this pull request Nov 2, 2018
10 tasks
Copy link
Contributor

@ottobackwards ottobackwards left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments

@ottobackwards
Copy link
Contributor

Should the resolver throw if there is a call after it is closed?
or just return nothing? Do we have a test for that? Either way we should.

@mmiklavc
Copy link
Contributor Author

mmiklavc commented Nov 5, 2018

Should the resolver throw if there is a call after it is closed?
or just return nothing? Do we have a test for that? Either way we should.

What do you think of a no-op on multiple invocations of close? This would be consistent with the Closeable interface.

/**
 * Closes this stream and releases any system resources associated
 * with it. If the stream is already closed then invoking this
 * method has no effect.
...
 */
public void close() throws IOException;

@ottobackwards
Copy link
Contributor

That is great

@mmiklavc
Copy link
Contributor Author

mmiklavc commented Nov 5, 2018

@ottobackwards Ok, pushed out an update for that change along with accompanying unit test.

@mmiklavc
Copy link
Contributor Author

mmiklavc commented Nov 6, 2018

Testing

Note - this test script can be used in conjunction with #1250 because this PR exposes the close() capability, but it does NOT add it to the parser/enrichment bolts or Stellar shell executor. i.e. you won't see the messages until that implementation is exposed. 1250 exposes the close() invocations.

Create a custom Stellar function

See the following for reference - https://github.com/apache/metron/blob/master/metron-stellar/stellar-common/3rdPartyStellar.md

  1. Setup project dirs

    mkdir custom-stellar && cd custom-stellar
    mkdir -p src/main/java/com/thirdparty/stellar
    
  2. Create a pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.thirdparty</groupId>
      <artifactId>stellar-funcs</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>Stellar Functions</name>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.apache.metron</groupId>
          <artifactId>stellar-common</artifactId>
          <version>0.6.1</version>
          <!-- NOTE: We will want to depend on the deployed common on the classpath. -->
          <scope>provided</scope>
        </dependency>
      </dependencies>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    
  3. Create a custom Stellar function

    package com.thirdparty.stellar;
    
    import org.apache.metron.stellar.common.utils.ConversionUtils;
    import org.apache.metron.stellar.dsl.Context;
    import org.apache.metron.stellar.dsl.ParseException;
    import org.apache.metron.stellar.dsl.Stellar;
    import org.apache.metron.stellar.dsl.StellarFunction;
    
    import java.io.IOException;
    import java.util.List;
    import org.jboss.netty.util.internal.ConversionUtil;
    
    public class StellarCustom {
    
      @Stellar(name = "NOW",
          description = "Simple now time",
          params = {
          "throwException - whether we should throw an exception on close()"
          },
          returns = "Time in millis"
      )
      public static class Now implements StellarFunction {
    
        private boolean isInitialized = false;
        private boolean throwException = false;
    
        public Object apply(List<Object> args, Context context) throws ParseException {
          if (args.size() == 1) {
            throwException = ConversionUtils.convert(args.get(0), Boolean.class);
          }
          return System.currentTimeMillis();
        }
    
        public void initialize(Context context) {
          try {
            System.out.println("Initializing function.");
          } finally {
            isInitialized = true;
          }
        }
    
        public boolean isInitialized() {
          return isInitialized;
        }
    
        public void close() throws IOException {
          System.out.println("Test function called close()!");
          if (throwException) {
            NullPointerException cause = new NullPointerException("Don't point at me!");
            throw new IOException("Something icky happened.", cause);
          }
        }
      }
    
    }
    
  4. Build it

    mvn clean install

  5. Ship it

    scp custom-stellar/target/stellar-funcs-1.0-SNAPSHOT.jar root@node1:/tmp/

  6. Setup HDFS stellar

    sudo -u hdfs hdfs dfs -mkdir /apps/metron/stellar
    sudo -u hdfs hdfs dfs -put /tmp/stellar-funcs-1.0-SNAPSHOT.jar /apps/metron/stellar
    sudo -u hdfs hdfs dfs -chown -R metron:metron /apps/metron/stellar
    
  7. Setup global config for Stellar HDFS location

    $METRON_HOME/bin/zk_load_configs.sh -m PULL -o $METRON_HOME/config/zookeeper -z $ZOOKEEPER -f
    vim $METRON_HOME/config/zookeeper/global.json
    # add this -> "stellar.function.paths" : "hdfs://node1:8020/apps/metron/stellar/.*.jar"
    # update global.json in ZK
    $METRON_HOME/bin/zk_load_configs.sh -m PUSH -i $METRON_HOME/config/zookeeper -z $ZOOKEEPER
    
  8. Setup a parser or enrichment with the new function. (more specific details to come)

    Don't want an exception thrown
    NOW(false)
    Restart parser or enrichment using this function
    Should get data still
    Stop the topology - should see a notice about shutdown in the logs.

  9. Same as before but throw exception on shutdown. (more specific details to come)

    WANT an exception thrown
    NOW(true)
    Restart parser or enrichment using this function
    Should get data still
    Stop the topology - should see our exception in the logs.

@merrimanr
Copy link
Contributor

+1

@mmiklavc
Copy link
Contributor Author

mmiklavc commented Nov 6, 2018

@ottobackwards Any other feedback on this?

@ottobackwards
Copy link
Contributor

+1

@asfgit asfgit closed this in 85cd21a Nov 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants