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

    https://github.com/apache/storm/pull/1783#discussion_r88943466
  
    --- Diff: 
external/storm-hbase/src/main/java/org/apache/storm/hbase/bolt/HBaseLookupBolt.java
 ---
    @@ -40,51 +48,85 @@
      *
      */
     public class HBaseLookupBolt extends AbstractHBaseBolt {
    -    private static final Logger LOG = 
LoggerFactory.getLogger(HBaseLookupBolt.class);
    +   private static final Logger LOG = 
LoggerFactory.getLogger(HBaseLookupBolt.class);
    +
    +   private HBaseValueMapper rowToTupleMapper;
    +   private HBaseProjectionCriteria projectionCriteria;
    +   private transient LoadingCache<byte[], Result> cache;
    +   private transient boolean cacheEnabled;
     
    -    private HBaseValueMapper rowToTupleMapper;
    +   public HBaseLookupBolt(String tableName, HBaseMapper mapper, 
HBaseValueMapper rowToTupleMapper) {
    +           super(tableName, mapper);
    +           Validate.notNull(rowToTupleMapper, "rowToTupleMapper can not be 
null");
    +           this.rowToTupleMapper = rowToTupleMapper;
    +   }
     
    -    private HBaseProjectionCriteria projectionCriteria;
    +   public HBaseLookupBolt withConfigKey(String configKey) {
    +           this.configKey = configKey;
    +           return this;
    +   }
     
    -    public HBaseLookupBolt(String tableName, HBaseMapper mapper, 
HBaseValueMapper rowToTupleMapper){
    -        super(tableName, mapper);
    -        Validate.notNull(rowToTupleMapper, "rowToTupleMapper can not be 
null");
    -        this.rowToTupleMapper = rowToTupleMapper;
    -    }
    +   public HBaseLookupBolt withProjectionCriteria(HBaseProjectionCriteria 
projectionCriteria) {
    +           this.projectionCriteria = projectionCriteria;
    +           return this;
    +   }
     
    -    public HBaseLookupBolt withConfigKey(String configKey){
    -        this.configKey = configKey;
    -        return this;
    -    }
    +   @SuppressWarnings({ "unchecked", "rawtypes" })
    +   @Override
    +   public void prepare(Map config, TopologyContext topologyContext, 
OutputCollector collector) {
    +           super.prepare(config, topologyContext, collector);
    +           cacheEnabled = 
Boolean.parseBoolean(config.getOrDefault("hbase.cache.enable", 
"false").toString());
    +           int cacheTTL = 
Integer.parseInt(config.getOrDefault("hbase.cache.ttl.seconds", 
"300").toString());
    +           int maxCacheSize = 
Integer.parseInt(config.getOrDefault("hbase.cache.size", "1000").toString());
    +           if (cacheEnabled) {
    +                   cache = 
CacheBuilder.newBuilder().maximumSize(maxCacheSize).expireAfterWrite(cacheTTL, 
TimeUnit.SECONDS)
    +                                   .build(new CacheLoader<byte[], 
Result>() {
     
    -    public HBaseLookupBolt withProjectionCriteria(HBaseProjectionCriteria 
projectionCriteria) {
    -        this.projectionCriteria = projectionCriteria;
    -        return this;
    -    }
    +                                           @Override
    +                                           public Result load(byte[] 
rowKey) throws Exception {
    +                                                   Get get = 
hBaseClient.constructGetRequests(rowKey, projectionCriteria);
    +                                                   return 
hBaseClient.batchGet(Lists.newArrayList(get))[0];
    +                                           }
     
    -    @Override
    -    public void execute(Tuple tuple) {
    -        if (TupleUtils.isTick(tuple)) {
    -            collector.ack(tuple);
    -            return;
    -        }
    -        byte[] rowKey = this.mapper.rowKey(tuple);
    -        Get get = hBaseClient.constructGetRequests(rowKey, 
projectionCriteria);
    +                                   });
    +           }
    +   }
     
    -        try {
    -            Result result = 
hBaseClient.batchGet(Lists.newArrayList(get))[0];
    -            for(Values values : rowToTupleMapper.toValues(tuple, result)) {
    -                this.collector.emit(tuple, values);
    -            }
    -            this.collector.ack(tuple);
    -        } catch (Exception e) {
    -            this.collector.reportError(e);
    -            this.collector.fail(tuple);
    -        }
    -    }
    +   @Override
    +   public void execute(Tuple tuple) {
    +           if (TupleUtils.isTick(tuple)) {
    +                   collector.ack(tuple);
    +                   return;
    +           }
    +           byte[] rowKey = this.mapper.rowKey(tuple);
    +           if (cacheEnabled) {
    +                   try {
    +                           Result result = cache.get(rowKey);
    +                           for (Values values : 
rowToTupleMapper.toValues(tuple, result)) {
    +                                   this.collector.emit(tuple, values);
    +                           }
    +                           this.collector.ack(tuple);
    +                   } catch (Exception e) {
    +                           this.collector.reportError(e);
    +                           this.collector.fail(tuple);
    +                   }
    +           } else {
    +                   Get get = hBaseClient.constructGetRequests(rowKey, 
projectionCriteria);
    +                   try {
    +                           Result result = 
hBaseClient.batchGet(Lists.newArrayList(get))[0];
    +                           for (Values values : 
rowToTupleMapper.toValues(tuple, result)) {
    +                                   this.collector.emit(tuple, values);
    +                           }
    +                           this.collector.ack(tuple);
    +                   } catch (Exception e) {
    +                           this.collector.reportError(e);
    +                           this.collector.fail(tuple);
    +                   }
    +           }
    --- End diff --
    
    There appears to be a lot of overlap in the code here, between the cache 
version and the non-cache version.  It would really be nice to at least combine 
most of this.  Alternatively we might want to use inheritance in some way to 
avoid the branching on the cacheEnabled config at all.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to