merrimanr opened a new pull request #1399: METRON-2073: Create in-memory use case for enrichment with map type and flatfile summarizer URL: https://github.com/apache/metron/pull/1399 ## Contributor Comments This PR adds a Stellar function for performing in-memory enrichments. Similar to `ENRICHMENT_GET`, which performs lookups in HBase, this function performs lookups from an in-memory map that is loaded from HDFS. The `nosql_table`, `enrichment_type`, and `column_family` parameters in `ENRICHMENT_GET` are similar to the `ENRICHMENT_IN_MEMORY_GET` `path` parameter in that they uniquely identify the data source. The `indicator` parameter is the same (the key used to lookup an enrichment). The new function is very similar to the `OBJECT_GET` function (they use the same cache abstraction) except that it performs the lookup, offers configuration specifically for in-memory enrichments, and potentially more. The in-memory objects are lazy-loaded, meaning they are loaded the first time an `ENRICHMENT_IN_MEMORY_GET` is executed. The Jira states the first pass should be a 1-time load but it wasn't difficult to expose cache expiration settings. This allows cached enrichments to be refreshed at an interval and will support streaming enrichments. Cache settings are applied in the following order: 1. Hardcoded defaults defined in constants 2. Top-level global config settings (used by the `OBJECT_GET` function) 3. Global config settings specific for `ENRICHMENT_IN_MEMORY_GET` These settings are passed into the Guava `CacheBuilder` when the function is initialized. ### Changes Included - An `ObjectCache` abstraction was created from the code in the Stellar `OBJECT_GET` function. This abstraction is shared by both the `OBJECT_GET` and `ENRICHMENT_IN_MEMORY_GET` functions. The current settings used in `OBJECT_GET` should be backwards compatible. - Dedicated cache settings for `ENRICHMENT_IN_MEMORY_GET` can be configured in the global config with the `in.memory.enrichment.settings` key. - Unit tests were added for new code and expanded for existing code. - Logging was added in various places to make it easier to determine when entries are being flushed and for what reason. ### Testing Instructions This has been tested in full dev with the Stellar CLI. 1. Spin up full dev and ensure data is flowing with no errors. 2. Create a `enrichments.csv` file with enrichment data: ``` key,value ``` 3. Create a `enrichments_updated.csv` file with updated enrichment data: ``` key,updated value ``` 4. Create a `summarizer.json` file that will serialize this data as a map in HDFS: ``` { "config" : { "columns" : { "key" : 0, "value" : 1 }, "state_init" : "{}", "state_update" : { "state" : "MAP_PUT(key, value, state)" }, "separator" : "," }, "extractor" : "CSV" } ``` 5. Use the `flatfile_summarizer.sh` script to create serialized files: ``` /usr/metron/0.7.1/bin/flatfile_summarizer.sh -i ./enrichments.csv -o ./enrichments.ser -e summarizer.json -p 1 /usr/metron/0.7.1/bin/flatfile_summarizer.sh -i ./enrichments_updated.csv -o ./enrichments_updated.ser -e summarizer.json -p 1 ``` 6. Put the first file in HDFS: ``` hdfs dfs -put enrichments.ser /tmp/enrichments.ser ``` 7. Start the Stellar shell and define a field we can use for the enrichments: ``` [root@node1 tmp]# /usr/metron/0.7.1/bin/stellar --zookeeper node1:2181 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/metron/0.7.1/lib/metron-profiler-repl-0.7.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/hdp/2.6.5.1050-37/hadoop/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] Stellar, Go! Functions are loading lazily in the background and will be unavailable until loaded fully. {es.clustername=metron, es.ip=node1:9200, es.date.format=yyyy.MM.dd.HH, parser.error.topic=indexing, update.hbase.table=metron_update, update.hbase.cf=t, es.client.settings={}, profiler.client.period.duration=15, profiler.client.period.duration.units=MINUTES, enrichment.list.hbase.provider.impl=org.apache.metron.hbase.HTableProvider, enrichment.list.hbase.table=enrichment_list, enrichment.list.hbase.cf=t, user.settings.hbase.table=user_settings, user.settings.hbase.cf=cf, bootstrap.servers=node1:6667, source.type.field=source:type, threat.triage.score.field=threat:triage:score, enrichment.writer.batchSize=15, enrichment.writer.batchTimeout=0, profiler.writer.batchSize=15, profiler.writer.batchTimeout=0, geo.hdfs.file=/apps/metron/geo/default/GeoLite2-City.tar.gz, asn.hdfs.file=/apps/metron/asn/default/GeoLite2-ASN.tar.gz, object.cache.expiration.minutes=1, in.memory.enrichment.settings={cache.expiration=15, time.unit=SECONDS}} [Stellar]>>> field := 'key' key ``` 7. Perform an in-memory enrichment. There should be a slight delay (for the initial load) but subsequent calls should return immediately: ``` [Stellar]>>> ENRICHMENT_IN_MEMORY_GET('/tmp/enrichments.ser', field) value ``` 8. The default cache expiration is 24 hours. Exit the Stellar CLI and set the object cache expiration to 1 minutes in the global config: ``` curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ ... "object.cache.expiration.minutes": 1 }' 'http://user:password@node1:8082/api/v1/global/config' ``` Note the `object.cache.expiration.minutes` setting is maintained for backwards compatibility. You can also set the expiration to 1 minute with these settings: ``` { "object.cache.expiration": 1, "object.cache.time.unit": "MINUTES" } ``` 9. Start the Stellar CLI again and perform the enrichment again. You should get the same result: ``` [root@node1 tmp]# /usr/metron/0.7.1/bin/stellar --zookeeper node1:2181 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/metron/0.7.1/lib/metron-profiler-repl-0.7.1.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/hdp/2.6.5.1050-37/hadoop/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] Stellar, Go! Functions are loading lazily in the background and will be unavailable until loaded fully. {es.clustername=metron, es.ip=node1:9200, es.date.format=yyyy.MM.dd.HH, parser.error.topic=indexing, update.hbase.table=metron_update, update.hbase.cf=t, es.client.settings={}, profiler.client.period.duration=15, profiler.client.period.duration.units=MINUTES, enrichment.list.hbase.provider.impl=org.apache.metron.hbase.HTableProvider, enrichment.list.hbase.table=enrichment_list, enrichment.list.hbase.cf=t, user.settings.hbase.table=user_settings, user.settings.hbase.cf=cf, bootstrap.servers=node1:6667, source.type.field=source:type, threat.triage.score.field=threat:triage:score, enrichment.writer.batchSize=15, enrichment.writer.batchTimeout=0, profiler.writer.batchSize=15, profiler.writer.batchTimeout=0, geo.hdfs.file=/apps/metron/geo/default/GeoLite2-City.tar.gz, asn.hdfs.file=/apps/metron/asn/default/GeoLite2-ASN.tar.gz, object.cache.expiration.minutes=1, in.memory.enrichment.settings={cache.expiration=15, time.unit=SECONDS}} [Stellar]>>> field := 'key' key [Stellar]>>> ENRICHMENT_IN_MEMORY_GET('/tmp/enrichments.ser', field) value ``` 10. In a separate window, upload the updated enrichment file to HDFS: ``` hdfs dfs -put -f /tmp/enrichments_updated.ser /tmp/enrichments.ser ``` 11. After a minute the value should change to the value in `enrichments_updated.ser`: ``` [Stellar]>>> ENRICHMENT_IN_MEMORY_GET('/tmp/enrichments.ser', field) updated value ``` 12. The cache expiration can also be set specifically for `ENRICHMENT_IN_MEMORY_GET`. Put the previous `enrichments.ser` back in HDFS and change the cache settings to 15 seconds: ``` curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ ... "object.cache.expiration.minutes": 1, "in.memory.enrichment.settings": { "cache.expiration": 15, "time.unit": "SECONDS" } }' 'http://user:password@node1:8082/api/v1/global/config' ``` 13. Perform steps 9 - 11. Now the value should be updated after 15 seconds. ### Next Steps This is intended to be a first pass and there are still some outstanding items. I am planning on adding javadocs and a section in our READMEs describing how to do in-memory enrichments. Are there other features people would like to see added here? I think it could be useful to set cache settings for each object in HDFS (currently they are global to all objects and can only be changed on initialization). This would make things more complex and require some design work. Does anyone have suggestions for more appropriate function and setting names? What else would you change about this? ## Pull Request Checklist Thank you for submitting a contribution to Apache Metron. Please refer to our [Development Guidelines](https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=61332235) for the complete guide to follow for contributions. Please refer also to our [Build Verification Guidelines](https://cwiki.apache.org/confluence/display/METRON/Verifying+Builds?show-miniview) 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: - [x] Is there a JIRA ticket associated with this PR? If not one needs to be created at [Metron Jira](https://issues.apache.org/jira/browse/METRON/?selectedTab=com.atlassian.jira.jira-projects-plugin:summary-panel). - [x] 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. - [x] Has your PR been rebased against the latest commit within the target branch (typically master)? ### For code changes: - [x] Have you included steps to reproduce the behavior or problem that is being changed or addressed? - [x] Have you included steps or a guide to how the change may be verified and tested manually? - [x] 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 ``` - [x] Have you written or updated unit tests and or integration tests to verify your changes? - [x] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)? - [x] 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](https://travis-ci.org) is set up for your personal repository such that your branches are built there before submitting a pull request.
---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
