metron-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nickwallen <...@git.apache.org>
Subject [GitHub] incubator-metron pull request #208: METRON-309 Create a normalcy profiler
Date Thu, 11 Aug 2016 19:55:46 GMT
Github user nickwallen commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/208#discussion_r74491645
  
    --- Diff: metron-analytics/metron-profiler/src/main/java/org/apache/metron/profiler/bolt/ProfileBuilderBolt.java
---
    @@ -0,0 +1,265 @@
    +/*
    + *
    + *  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.profiler.bolt;
    +
    +import backtype.storm.Config;
    +import backtype.storm.Constants;
    +import backtype.storm.task.OutputCollector;
    +import backtype.storm.task.TopologyContext;
    +import backtype.storm.topology.OutputFieldsDeclarer;
    +import backtype.storm.tuple.Fields;
    +import backtype.storm.tuple.Tuple;
    +import backtype.storm.tuple.Values;
    +import org.apache.metron.common.bolt.ConfiguredProfilerBolt;
    +import org.apache.metron.common.configuration.profiler.ProfileConfig;
    +import org.apache.metron.profiler.ProfileMeasurement;
    +import org.apache.metron.profiler.stellar.StellarExecutor;
    +import org.json.simple.JSONObject;
    +import org.json.simple.parser.JSONParser;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.IOException;
    +import java.util.Map;
    +
    +/**
    + * A bolt that is responsible for building a Profile.
    + *
    + * This bolt maintains the state required to build a Profile.  When the window
    + * period expires, the data is summarized as a ProfileMeasurement, all state is
    + * flushed, and the ProfileMeasurement is emitted.
    + *
    + * Each instance of this bolt is responsible for maintaining the state for a single
    + * Profile-Entity pair.
    + */
    +public class ProfileBuilderBolt extends ConfiguredProfilerBolt {
    +
    +  protected static final Logger LOG = LoggerFactory.getLogger(ProfileBuilderBolt.class);
    +
    +  /**
    +   * Executes Stellar code and maintains state across multiple invocations.
    +   */
    +  private StellarExecutor executor;
    +
    +  /**
    +   * The number of seconds between when the Profile is flushed.
    +   */
    +  private int flushFrequency;
    +
    +  /**
    +   * A ProfileMeasurement is created and emitted each window period.  A Profile
    +   * itself is composed of many ProfileMeasurements.
    +   */
    +  private transient ProfileMeasurement measurement;
    +
    +  /**
    +   * The definition of the Profile that the bolt is building.
    +   */
    +  private transient ProfileConfig profileConfig;
    +
    +  /**
    +   * Parses JSON messages.
    +   */
    +  private transient JSONParser parser;
    +
    +  private OutputCollector collector;
    +
    +  /**
    +   * @param zookeeperUrl The Zookeeper URL that contains the configuration data.
    +   */
    +  public ProfileBuilderBolt(String zookeeperUrl) {
    +    super(zookeeperUrl);
    +  }
    +
    +  /**
    +   * Defines the frequency at which the bolt will receive tick tuples.  Tick tuples are
    +   * used to control how often a profile is flushed.
    +   */
    +  @Override
    +  public Map<String, Object> getComponentConfiguration() {
    +    Config conf = new Config();
    +    conf.put(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS, getFlushFrequency());
    +    return conf;
    +  }
    +
    +  @Override
    +  public void prepare(Map stormConf, TopologyContext context, OutputCollector collector)
{
    +    super.prepare(stormConf, context, collector);
    +    this.collector = collector;
    +    this.parser = new JSONParser();
    +  }
    +
    +  @Override
    +  public void declareOutputFields(OutputFieldsDeclarer declarer) {
    +    // once the time window expires, a complete ProfileMeasurement is emitted
    +    declarer.declare(new Fields("measurement"));
    +  }
    +
    +  @Override
    +  public void execute(Tuple input) {
    +    try {
    +      doExecute(input);
    +      collector.ack(input);
    +
    +    } catch (IOException e) {
    +      LOG.error("exception: {}", e);
    +      collector.reportError(e);
    +    }
    +  }
    +
    +  /**
    +   * Update the execution environment based on data contained in the
    +   * message.  If the tuple is a tick tuple, then flush the profile
    +   * and reset the execution environment.
    +   * @param input The tuple to execute.
    +   */
    +  private void doExecute(Tuple input) throws IOException {
    +
    +    if(!isTickTuple(input)) {
    +
    +      // if this is the first tuple in a window period, initialization is needed
    +      if (!isInitialized()) {
    +        init(input);
    +      }
    +
    +      // update the profile with data from a new message
    +      update(input);
    +
    +    } else {
    +
    +      // flush the profile - can only flush if it has been initialized
    +      if(isInitialized()) {
    +        flush(input);
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Initialize the bolt.  Occurs when the first tuple is received at the start
    +   * of each window period.
    +   * @param input The input tuple
    +   */
    +  private void init(Tuple input) throws IOException {
    +
    +    // save the profile definition - needed later during a flush
    +    profileConfig = (ProfileConfig) input.getValueByField("profile");
    +
    +    // create the measurement which will be saved at the end of the window period
    +    measurement = new ProfileMeasurement();
    +    measurement.setStart(getTimestamp());
    +    measurement.setEntity(input.getStringByField("entity"));
    +    measurement.setProfileName(profileConfig.getProfile());
    +
    +    // execute the 'init' expression
    +    JSONObject message = (JSONObject) input.getValueByField("message");
    +    Map<String, String> expressions = profileConfig.getInit();
    +    expressions.forEach((var, expr) -> executor.assign(var, expr, message));
    +  }
    +
    +  /**
    +   * Update the Profile based on data contained in a new message.
    +   * @param input The tuple containing a new message.
    +   */
    +  private void update(Tuple input) throws IOException {
    +    JSONObject message = (JSONObject) input.getValueByField("message");
    +
    +    // execute each of the 'update' expressions
    +    Map<String, String> expressions = profileConfig.getUpdate();
    +    expressions.forEach((var, expr) -> executor.assign(var, expr, message));
    +  }
    +
    +  /**
    +   * Flush the Profile.
    +   *
    +   * Executed on a fixed time period when a tick tuple is received.  Completes
    +   * and emits the ProfileMeasurement.  Clears all state in preparation for
    +   * the next window period.
    +   */
    +  private void flush(Tuple tickTuple) throws IOException {
    +    LOG.info(String.format("Flushing profile: profile=%s, entity=%s, start=%d",
    +            measurement.getProfileName(), measurement.getEntity(), measurement.getStart()));
    +
    +    // execute the 'result' expression
    +    String resultExpr = profileConfig.getResult();
    +    Long result = executor.execute(resultExpr, new JSONObject(), Long.class);
    --- End diff --
    
    I intended for this to be a Double, not a Long.  We expect the profiler to produce a time
series, so a large floating point value is what I would expect to be most useful to users.
    
    On the other hand, do I even need knowledge of what type the resulting value is?  It seems
more user-friendly If I just treat it as an Object, and then allow the HBaseBolt to serialize
it to HBase.  Then each profile can produce a time series of whatever values they prefer.
    
    The only ugly part is that I'd have to an "if-else instanceof" chain to cast it to the
appropriate type in ProfileHBaseMapper.columns(Tuple).  I'd have to do this because Java will
not be able to resolve Bytes.toBytes(Object).


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message