thetrialthatrockedtheworld总结_the average hourly wage

thetrialthatrockedtheworld总结_the average hourly wage聊聊storm WindowTridentProcessor的FreshCollector

大家好,又见面了,我是你们的朋友全栈君。

本文主要研究一下storm WindowTridentProcessor的FreshCollector

实例

        TridentTopology topology = new TridentTopology();
        topology.newStream("spout1", spout)
                .partitionBy(new Fields("user"))
                .window(windowConfig,windowsStoreFactory,new Fields("user","score"),new UserCountAggregator(),new Fields("aggData"))
                .parallelismHint(1)
                .each(new Fields("aggData"), new PrintEachFunc(),new Fields());
  • 这个实例在window操作之后跟了一个each操作

WindowTridentProcessor

storm-core-1.2.2-sources.jar!/org/apache/storm/trident/windowing/WindowTridentProcessor.java

public class WindowTridentProcessor implements TridentProcessor {
    
    private FreshCollector collector;

    //......

    public void prepare(Map stormConf, TopologyContext context, TridentContext tridentContext) {
        this.topologyContext = context;
        List<TridentTuple.Factory> parents = tridentContext.getParentTupleFactories();
        if (parents.size() != 1) {
            throw new RuntimeException("Aggregation related operation can only have one parent");
        }

        Long maxTuplesCacheSize = getWindowTuplesCacheSize(stormConf);

        this.tridentContext = tridentContext;
        collector = new FreshCollector(tridentContext);
        projection = new TridentTupleView.ProjectionFactory(parents.get(0), inputFields);

        windowStore = windowStoreFactory.create(stormConf);
        windowTaskId = windowId + WindowsStore.KEY_SEPARATOR + topologyContext.getThisTaskId() + WindowsStore.KEY_SEPARATOR;
        windowTriggerInprocessId = getWindowTriggerInprocessIdPrefix(windowTaskId);

        tridentWindowManager = storeTuplesInStore ?
                new StoreBasedTridentWindowManager(windowConfig, windowTaskId, windowStore, aggregator, tridentContext.getDelegateCollector(), maxTuplesCacheSize, inputFields)
                : new InMemoryTridentWindowManager(windowConfig, windowTaskId, windowStore, aggregator, tridentContext.getDelegateCollector());

        tridentWindowManager.prepare();
    }

    public void finishBatch(ProcessorContext processorContext) {

        Object batchId = processorContext.batchId;
        Object batchTxnId = getBatchTxnId(batchId);

        LOG.debug("Received finishBatch of : [{}] ", batchId);
        // get all the tuples in a batch and add it to trident-window-manager
        List<TridentTuple> tuples = (List<TridentTuple>) processorContext.state[tridentContext.getStateIndex()];
        tridentWindowManager.addTuplesBatch(batchId, tuples);

        List<Integer> pendingTriggerIds = null;
        List<String> triggerKeys = new ArrayList<>();
        Iterable<Object> triggerValues = null;

        if (retriedAttempt(batchId)) {
            pendingTriggerIds = (List<Integer>) windowStore.get(inprocessTriggerKey(batchTxnId));
            if (pendingTriggerIds != null) {
                for (Integer pendingTriggerId : pendingTriggerIds) {
                    triggerKeys.add(triggerKey(pendingTriggerId));
                }
                triggerValues = windowStore.get(triggerKeys);
            }
        }

        // if there are no trigger values in earlier attempts or this is a new batch, emit pending triggers.
        if(triggerValues == null) {
            pendingTriggerIds = new ArrayList<>();
            Queue<StoreBasedTridentWindowManager.TriggerResult> pendingTriggers = tridentWindowManager.getPendingTriggers();
            LOG.debug("pending triggers at batch: [{}] and triggers.size: [{}] ", batchId, pendingTriggers.size());
            try {
                Iterator<StoreBasedTridentWindowManager.TriggerResult> pendingTriggersIter = pendingTriggers.iterator();
                List<Object> values = new ArrayList<>();
                StoreBasedTridentWindowManager.TriggerResult triggerResult = null;
                while (pendingTriggersIter.hasNext()) {
                    triggerResult = pendingTriggersIter.next();
                    for (List<Object> aggregatedResult : triggerResult.result) {
                        String triggerKey = triggerKey(triggerResult.id);
                        triggerKeys.add(triggerKey);
                        values.add(aggregatedResult);
                        pendingTriggerIds.add(triggerResult.id);
                    }
                    pendingTriggersIter.remove();
                }
                triggerValues = values;
            } finally {
                // store inprocess triggers of a batch in store for batch retries for any failures
                if (!pendingTriggerIds.isEmpty()) {
                    windowStore.put(inprocessTriggerKey(batchTxnId), pendingTriggerIds);
                }
            }
        }

        collector.setContext(processorContext);
        int i = 0;
        for (Object resultValue : triggerValues) {
            collector.emit(new ConsList(new TriggerInfo(windowTaskId, pendingTriggerIds.get(i++)), (List<Object>) resultValue));
        }
        collector.setContext(null);
    }
}
  • WindowTridentProcessor在prepare的时候创建了FreshCollector
  • finishBatch的时候,调用FreshCollector.emit将窗口的aggregate的结果集传递过去
  • 传递的数据结构为ConsList,其实是个AbstractList的实现,由Object类型的first元素,以及List<Object>结构的_elems组成

FreshCollector

storm-core-1.2.2-sources.jar!/org/apache/storm/trident/planner/processor/FreshCollector.java

public class FreshCollector implements TridentCollector {
    FreshOutputFactory _factory;
    TridentContext _triContext;
    ProcessorContext context;
    
    public FreshCollector(TridentContext context) {
        _triContext = context;
        _factory = new FreshOutputFactory(context.getSelfOutputFields());
    }
                
    public void setContext(ProcessorContext pc) {
        this.context = pc;
    }

    @Override
    public void emit(List<Object> values) {
        TridentTuple toEmit = _factory.create(values);
        for(TupleReceiver r: _triContext.getReceivers()) {
            r.execute(context, _triContext.getOutStreamId(), toEmit);
        }            
    }

    @Override
    public void reportError(Throwable t) {
        _triContext.getDelegateCollector().reportError(t);
    } 

    public Factory getOutputFactory() {
        return _factory;
    }    
}
  • FreshCollector在构造器里头根据context的selfOutputFields(第一个field固定为_task_info,之后的几个field为用户在window方法定义的functionFields)构造FreshOutputFactory
  • emit方法,首先使用FreshOutputFactory根据outputFields构造TridentTupleView,之后获取TupleReceiver,调用TupleReceiver的execute方法把TridentTupleView传递过去
  • 这里的TupleReceiver有ProjectedProcessor、PartitionPersistProcessor

TridentTupleView.FreshOutputFactory

storm-core-1.2.2-sources.jar!/org/apache/storm/trident/tuple/TridentTupleView.java

    public static class FreshOutputFactory  implements Factory {
        Map<String, ValuePointer> _fieldIndex;
        ValuePointer[] _index;

        public FreshOutputFactory(Fields selfFields) {
            _fieldIndex = new HashMap<>();
            for(int i=0; i<selfFields.size(); i++) {
                String field = selfFields.get(i);
                _fieldIndex.put(field, new ValuePointer(0, i, field));
            }
            _index = ValuePointer.buildIndex(selfFields, _fieldIndex);
        }
        
        public TridentTuple create(List<Object> selfVals) {
            return new TridentTupleView(PersistentVector.EMPTY.cons(selfVals), _index, _fieldIndex);
        }

        @Override
        public Map<String, ValuePointer> getFieldIndex() {
            return _fieldIndex;
        }

        @Override
        public int numDelegates() {
            return 1;
        }
        
        @Override
        public List<String> getOutputFields() {
            return indexToFieldsList(_index);
        }        
    }
  • FreshOutputFactory是TridentTupleView的一个静态类,其构造方法主要是计算_index以及_fieldIndex
  • _fieldIndex是一个map,key是field字段,value是ValuePointer,记录其delegateIndex(这里固定为0)、index及field信息;第一个field为_task_info,index为0;之后的fields为用户在window方法定义的functionFields
  • 这里的create方法主要是构造TridentTupleView,其构造器第一个值为IPersistentVector,第二个值为_index,第三个值为_fieldIndex

ValuePointer

storm-core-1.2.2-sources.jar!/org/apache/storm/trident/tuple/ValuePointer.java

public class ValuePointer {
    public static Map<String, ValuePointer> buildFieldIndex(ValuePointer[] pointers) {
        Map<String, ValuePointer> ret = new HashMap<String, ValuePointer>();
        for(ValuePointer ptr: pointers) {
            ret.put(ptr.field, ptr);
        }
        return ret;        
    }

    public static ValuePointer[] buildIndex(Fields fieldsOrder, Map<String, ValuePointer> pointers) {
        if(fieldsOrder.size()!=pointers.size()) {
            throw new IllegalArgumentException("Fields order must be same length as pointers map");
        }
        ValuePointer[] ret = new ValuePointer[pointers.size()];
        for(int i=0; i<fieldsOrder.size(); i++) {
            ret[i] = pointers.get(fieldsOrder.get(i));
        }
        return ret;
    }    
    
    public int delegateIndex;
    protected int index;
    protected String field;
    
    public ValuePointer(int delegateIndex, int index, String field) {
        this.delegateIndex = delegateIndex;
        this.index = index;
        this.field = field;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }    
}
  • 这里的buildIndex,主要是根据selfOutputFields的顺序返回ValuePointer数组

ProjectedProcessor

storm-core-1.2.2-sources.jar!/org/apache/storm/trident/planner/processor/ProjectedProcessor.java

public class ProjectedProcessor implements TridentProcessor {
    Fields _projectFields;
    ProjectionFactory _factory;
    TridentContext _context;
    
    public ProjectedProcessor(Fields projectFields) {
        _projectFields = projectFields;
    }
    
    @Override
    public void prepare(Map conf, TopologyContext context, TridentContext tridentContext) {
        if(tridentContext.getParentTupleFactories().size()!=1) {
            throw new RuntimeException("Projection processor can only have one parent");
        }
        _context = tridentContext;
        _factory = new ProjectionFactory(tridentContext.getParentTupleFactories().get(0), _projectFields);
    }

    @Override
    public void cleanup() {
    }

    @Override
    public void startBatch(ProcessorContext processorContext) {
    }

    @Override
    public void execute(ProcessorContext processorContext, String streamId, TridentTuple tuple) {
        TridentTuple toEmit = _factory.create(tuple);
        for(TupleReceiver r: _context.getReceivers()) {
            r.execute(processorContext, _context.getOutStreamId(), toEmit);
        }
    }

    @Override
    public void finishBatch(ProcessorContext processorContext) {
    }

    @Override
    public Factory getOutputFactory() {
        return _factory;
    }
}
  • ProjectedProcessor在prepare的时候,创建了ProjectionFactory,其_projectFields就是window方法定义的functionFields,这里还使用tridentContext.getParentTupleFactories().get(0)提取了parent的第一个Factory,由于是FreshCollector传递过来的,因而这里是TridentTupleView.FreshOutputFactory
  • execute的时候,首先调用ProjectionFactory.create方法,对TridentTupleView进行字段提取操作,toEmit就是根据window方法定义的functionFields重新提取的TridentTupleView
  • execute方法之后对_context.getReceivers()挨个调用execute操作,将toEmit传递过去,这里的receiver就是window操作之后的各种processor了,比如EachProcessor

TridentTupleView.ProjectionFactory

storm-core-1.2.2-sources.jar!/org/apache/storm/trident/tuple/TridentTupleView.java

public static class ProjectionFactory implements Factory {
        Map<String, ValuePointer> _fieldIndex;
        ValuePointer[] _index;
        Factory _parent;

        public ProjectionFactory(Factory parent, Fields projectFields) {
            _parent = parent;
            if(projectFields==null) projectFields = new Fields();
            Map<String, ValuePointer> parentFieldIndex = parent.getFieldIndex();
            _fieldIndex = new HashMap<>();
            for(String f: projectFields) {
                _fieldIndex.put(f, parentFieldIndex.get(f));
            }            
            _index = ValuePointer.buildIndex(projectFields, _fieldIndex);
        }
        
        public TridentTuple create(TridentTuple parent) {
            if(_index.length==0) return EMPTY_TUPLE;
            else return new TridentTupleView(((TridentTupleView)parent)._delegates, _index, _fieldIndex);
        }

        @Override
        public Map<String, ValuePointer> getFieldIndex() {
            return _fieldIndex;
        }

        @Override
        public int numDelegates() {
            return _parent.numDelegates();
        }

        @Override
        public List<String> getOutputFields() {
            return indexToFieldsList(_index);
        }
    }
  • ProjectionFactory是TridentTupleView的静态类,它在构造器里头根据projectFields构造_index及_fieldIndex,这样create方法就能根据所需的字段创建TridentTupleView

EachProcessor

storm-core-1.2.2-sources.jar!/org/apache/storm/trident/planner/processor/EachProcessor.java

public class EachProcessor implements TridentProcessor {
    Function _function;
    TridentContext _context;
    AppendCollector _collector;
    Fields _inputFields;
    ProjectionFactory _projection;
    
    public EachProcessor(Fields inputFields, Function function) {
        _function = function;
        _inputFields = inputFields;
    }
    
    @Override
    public void prepare(Map conf, TopologyContext context, TridentContext tridentContext) {
        List<Factory> parents = tridentContext.getParentTupleFactories();
        if(parents.size()!=1) {
            throw new RuntimeException("Each operation can only have one parent");
        }
        _context = tridentContext;
        _collector = new AppendCollector(tridentContext);
        _projection = new ProjectionFactory(parents.get(0), _inputFields);
        _function.prepare(conf, new TridentOperationContext(context, _projection));
    }

    @Override
    public void cleanup() {
        _function.cleanup();
    }    

    @Override
    public void execute(ProcessorContext processorContext, String streamId, TridentTuple tuple) {
        _collector.setContext(processorContext, tuple);
        _function.execute(_projection.create(tuple), _collector);
    }

    @Override
    public void startBatch(ProcessorContext processorContext) {
    }

    @Override
    public void finishBatch(ProcessorContext processorContext) {
    }

    @Override
    public Factory getOutputFactory() {
        return _collector.getOutputFactory();
    }    
}
  • EachProcessor的execute方法,首先设置_collector的context为processorContext,然后调用_function.execute方法
  • 这里调用了_projection.create(tuple)来提取字段,主要是根据_function定义的inputFields来提取
  • 这里传递给_function的collector为AppendCollector

AppendCollector

storm-core-1.2.2-sources.jar!/org/apache/storm/trident/planner/processor/AppendCollector.java

public class AppendCollector implements TridentCollector {
    OperationOutputFactory _factory;
    TridentContext _triContext;
    TridentTuple tuple;
    ProcessorContext context;
    
    public AppendCollector(TridentContext context) {
        _triContext = context;
        _factory = new OperationOutputFactory(context.getParentTupleFactories().get(0), context.getSelfOutputFields());
    }
                
    public void setContext(ProcessorContext pc, TridentTuple t) {
        this.context = pc;
        this.tuple = t;
    }

    @Override
    public void emit(List<Object> values) {
        TridentTuple toEmit = _factory.create((TridentTupleView) tuple, values);
        for(TupleReceiver r: _triContext.getReceivers()) {
            r.execute(context, _triContext.getOutStreamId(), toEmit);
        }
    }

    @Override
    public void reportError(Throwable t) {
        _triContext.getDelegateCollector().reportError(t);
    } 
    
    public Factory getOutputFactory() {
        return _factory;
    }
}
  • AppendCollector在构造器里头创建了OperationOutputFactory,其emit方法也是提取OperationOutputFields,然后挨个调用_triContext.getReceivers()的execute方法;如果each之后没有其他操作,那么AppendCollector的_triContext.getReceivers()就为空

小结

  • WindowTridentProcessor里头使用的是FreshCollector,WindowTridentProcessor在finishBatch的时候,会从TridentWindowManager提取window创建的pendingTriggers(提取之后会将其数据从pendingTriggers移除),里头包含了窗口累积的数据,然后使用FreshCollector发射这些数据,默认第一个value为TriggerInfo,第二个value就是窗口累积发射的values
  • FreshCollector的emit方法首先使用TridentTupleView.FreshOutputFactory根据selfOutputFields(第一个field固定为_task_info,之后的几个field为用户在window方法定义的functionFields)构建TridentTupleView,然后挨个调用_triContext.getReceivers()的execute方法
  • 后续的receivers中有一个ProjectedProcessor,用于根据window方法定义的functionFields重新提取的TridentTupleView,它的execute方法也类似FreshCollector.emit方法,先提取所需字段构造TridentTupleView,然后挨个调用_triContext.getReceivers()的execute方法(比如EachProcessor.execute)
  • EachProcessor使用的collector为AppendCollector,它的emit方法也类似FreshCollector的emit方法,先进行字段提取构造TridentTupleView,然后挨个调用_triContext.getReceivers()的execute方法
  • FreshCollector的emit方法与ProjectedProcessor的execute方法以及AppendCollector的emit方法都非常类似,首先是使用Factory提取所需字段构建TridentTupleView,然后挨个调用_triContext.getReceivers()的execute方法;当一个_triContext没有receiver的时候,tuple的传递也就停止了

doc

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107199.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 计算机网络基础知识整理大全_计算机基础知识题库

    计算机网络基础知识整理大全_计算机基础知识题库计算机网络基础知识一.因特网概述1.网络,互联网和因特网2.因特网发展的三个阶段3.因特网的标准化工作4.因特网的组成二.三种交换方式1.电路交换(CircuitSwitching)2.分组交换(PacketSwitching)3.报文交换(MessageSwitching)4.电路交换,报文交换,分组交换三者区别三.计算机网络的定义和分类1.计算机网络的定义2.计算机网络的分类一.因特网概述1.网络,互联网和因特网网络:由若干个结点和连接这些结点的链路(有限链路和无

    2025年11月7日
    3
  • 全套电商ERP源代码出售,电商ERP管理系统、电商仓储管理系统、电商分销管理系统[通俗易懂]

    全套电商ERP源代码出售,电商ERP管理系统、电商仓储管理系统、电商分销管理系统[通俗易懂]技术架构:后端C#.net4.5ORMWEBAPI分布式REDIS,前端:VUEAntDesignAggrid(前后端完全分离)1,产品定位:电商ERP管理系统、电商仓储管理系统、电商分销管理系统2,对接了淘宝天猫、京东、拼多多等主流电商平台50+3,多租户4,分布式5,单客户日均100万数据处理能力6,完整导入导出方案7,前端界面自动生成(表格、搜索条件、编辑字段)8,完整开放平台9,微信小程序10,高度可定制的打印方案11,核心功能模块:订单管

    2026年2月1日
    3
  • Redis 主从复制

    Redis 主从复制大家好,我是小林哥。又来图解Redis啦。我在前两篇已经给大家图解了AOF和RDB,这两个持久化技术保证了即使在服务器重启的情况下也不会丢失数据(或少量损失)。不过,由于数据都是存储在一台服务器上,如果出事就完犊子了,比如:如果服务器发生了宕机,由于数据恢复是需要点时间,那么这个期间是无法服务新的请求的;如果这台服务器的硬盘出现了故障,可能数据就都丢失了。要避免这种单点故障,最好的办法是将数据备份到其他服务器上,让这些服务器也可以对外提供服务,这样即使有一台服务器出现了故障,其他服

    2022年8月13日
    4
  • 在菜鸟教程学 HTML(一)[通俗易懂]

    在菜鸟教程学 HTML(一)[通俗易懂]注意:对于中文网页需要使用 <metacharset="utf-8"> 声明编码,否则会出现乱码。有些浏览器会设置GBK为默认编码,则你需要设

    2022年8月3日
    6
  • 正确理解ThreadLocal[通俗易懂]

    正确理解ThreadLocal

    2022年2月1日
    50
  • 数学函数图像处理_matlab基本图像处理

    数学函数图像处理_matlab基本图像处理文章目录1.imdilate2.imresize3.imfinfo4.imcomplement总结1.imdilate功能:进行膨胀操作介绍用法:IM2=imdilate(IM,SE)对灰度图像或二值图像IM进行膨胀操作,返回结果图像IM2。SE为由strel函数生成的结构元素对象。IM2=imdilate(IM,NHOOD)对灰度图像或二值图像IM进行膨胀操作,返回结果图像IM2。参量NHOOD是一个由O和1组成的矩阵,指定邻域。IM2=imdilate(…,SHAPE)对图

    2022年10月5日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号