mmiklavc commented on a change in 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#discussion_r293130214
########## File path: metron-platform/metron-enrichment/metron-enrichment-common/src/main/java/org/apache/metron/enrichment/stellar/EnrichmentObjectGet.java ########## @@ -0,0 +1,114 @@ +/** + * 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.enrichment.stellar; + +import org.apache.metron.enrichment.cache.ObjectCache; +import org.apache.metron.enrichment.cache.ObjectCacheConfig; +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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.invoke.MethodHandles; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import static org.apache.metron.enrichment.cache.ObjectCacheConfig.OBJECT_CACHE_EXPIRATION_KEY; +import static org.apache.metron.enrichment.cache.ObjectCacheConfig.OBJECT_CACHE_MAX_FILE_SIZE_KEY; +import static org.apache.metron.enrichment.cache.ObjectCacheConfig.OBJECT_CACHE_SIZE_KEY; +import static org.apache.metron.enrichment.cache.ObjectCacheConfig.OBJECT_CACHE_TIME_UNIT_KEY; + +@Stellar(namespace="ENRICHMENT" + ,name="OBJECT_GET" + ,description="Retrieve and deserialize a serialized object from HDFS and stores it in the ObjectCache, " + + "then returns the value associated with the indicator." + , params = { + "path - The path in HDFS to the serialized object" + + "indicator - The string indicator to look up" + } + , returns="Value associated with the indicator." +) +public class EnrichmentObjectGet implements StellarFunction { + private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + public final static String ENRICHMENT_OBJECT_GET_SETTINGS = "enrichment.object.get.settings"; + + private ObjectCache objectCache; + + @Override + public Object apply(List<Object> args, Context context) throws ParseException { + if(args.size() != 2) { + throw new IllegalArgumentException("All parameters are mandatory, submit 'hdfs path', 'indicator'"); + } + if(!isInitialized()) { + return null; + } + + String path = (String) args.get(0); + String indicator = (String) args.get(1); + if(path == null || indicator == null) { + return null; + } + + Object value; + try { + Map cachedMap = (Map) objectCache.get(path); + LOG.debug("Looking up value from object at path '{}' using indicator {}", path, indicator); + value = cachedMap.get(indicator); + } catch(ClassCastException e) { + throw new ClassCastException(String.format("The object stored in HDFS at '%s' must be serialized in JSON format.", path)); + } + + return value; + } + + @SuppressWarnings("unchecked") + @Override + public void initialize(Context context) { + Map<String, Object> config = (Map<String, Object>) context.getCapability(Context.Capabilities.GLOBAL_CONFIG, false) + .orElse(new HashMap<>()); + ObjectCacheConfig objectCacheConfig = ObjectCacheConfig.fromGlobalConfig(config); Review comment: Ah geeze, I see what's happening now. I had to go back over this a few times to grok what's going on. Ok, so I wasn't aware of or clear on what was previously in global config from the original [OBJECT_GET](https://github.com/apache/metron/pull/880/files#diff-012627e3746ecebc132287015b1be553R809) work versus what was added in this new feature. Link provided for context. I get wanting to keep OBJECT_GET's original config around in global config for existing functionality. I'm a little foggy on why the in-memory function wouldn't just have its own config completely independent of the original - ie just cut the cable on the config overrides, even though you're sharing similar infrastructure for the object cache. The 2 different Stellar functions can instantiate the underlying cache object with their own config however they want. I'm all about code reuse, and I like what you've done with the cache refactoring. I also like providing users options with sensible defaults, as we've done many other places in the application. But in this case I'm unclear what the added value is for this extra bit of extra complexity with the config inheritance - can we just let the config for the 2 different functions work independently and get rid of the override? ---------------------------------------------------------------- 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
