Druid源码解析-Segment文件

IncrementalIndex

IncrementalIndex类有两个重要的成员metricDescs和dimensionDescs

private final Map<String, MetricDesc> metricDescs;
private final Map<String, DimensionDesc> dimensionDescs;

metricDescs和dimensionDescs在IncrementalIndex的构造函数中被初始化。

MetricDesc

每个MetricDesc中有几个重要的成员:

private final int index;                              // metric序号
private final String name;                            // metric名字
private final String type:                            // metric类型
private final ColumnCapabilitiesImpl capabilities     // metric能力

MetricDesc的构造函数:

public MetricDesc(int index, AggregatorFactory factory) 
{
  this.index = index;
  this.name = factory.getName();

  String typeInfo = factory.getTypeName();
  this.capabilities = new ColumnCapabilitiesImpl();
  if ("float".equalsIgnoreCase(typeInfo)) {
    capabilities.setType(ValueType.FLOAT);
    this.type = typeInfo;
  } else if ("long".equalsIgnoreCase(typeInfo)) {
    capabilities.setType(ValueType.LONG);
    this.type = typeInfo;
  } else if ("double".equalsIgnoreCase(typeInfo)) {
    capabilities.setType(ValueType.DOUBLE);
    this.type = typeInfo;
  } else {
    capabilities.setType(ValueType.COMPLEX);
    this.type = ComplexMetrics.getSerdeForType(typeInfo).getTypeName();
  }
}

每个AggregatorFactory的实例都有一个名字,通过getTypeName()方法获取。这是接口中的函数,实现AggregatorFactory需要实现这个函数。比如CountAggregatorFactory的getTypeName()方法返回“long”,HyperUniquesAggregatorFactory的getTypeName返回的是“hyperUnique”。
如果对AggregatorFactory调用getTypeName返回的名字不是”float“,”long“,”double“普通的类型,而是复杂的自定义的类型,例如HyperUniqueAggregatorFactory或者Datasketch。
IncrementalIndex中通过如下代码构造每个metric的MetricDesc:

for (AggregatorFactory metric : metrics) {
  MetricDesc metricDesc = new MetricDesc(metricDesc.size(), metric);
  metricDescs.put(metricDesc.getName(), metricDesc);
}

DimensionDesc

每个DimensionDesc中有几个重要的成员:

private final int index;      // dimension序号
private final String name;    // dimnesion名字
private final ColumnCapabilitiesImpl capabilities    // dimension能力
private final DimensionHandler handler;
private final DimensionIndexer indexer;

DimensionHandller

DimensionHandller对象封装了特定于某一个dimension的索引。列合并,创建,以及查询操作。这些操作
通过DimensionHandler方法创建的对象(DimensionIndexer通过makeIndexer创建),
DimensionMerge通过makeMerger创建,DimensionColumnReader)handle。每个Diemension的handler
对象都特定于一个单独的dimension.

DimensionIndexer

每个dimension对应一个DimensionIndexer,用于内存中处理注入的一行数据。

ColumnCapabilitiesImpl
IncrementalIndex的构造函数中定义了每个dimension的capabilities:

  private ColumnCapabilitiesImpl makeCapabilitiesFromValueType(ValueType type) {
        ColumnCapabilitiesImpl capabilities = new ColumnCapabilitiesImpl();
        capabilities.setDictionaryEncoded(type == ValueType.STRING);
        capabilities.setHasBitmapIndexes(type == ValueType.STRING);
        capabilities.setType(type);
        return capabilities;
    }

可见只有string类型的dimension才支持字典编码。
根据不同的capabilities生成不同的DimensionHandler:

  DimensionHandler handler = DimensionHandlerUtils.getHandlerFromCapabilities(
                       dimName,
                       capabilities,
                       dimSchema.getMultiValueHandling()
               );
               addNewDimension(dimName, capabilities, handler);

一般这里都是StringDimensionHandler。实际应用中,String类型数据居多。

IncrementalIndex中写入一行数据

首先会经过parseBatch等流程,解析一行数据,最终生成MapBasedInputRow,然后调用 index.add(InputRow row)方法,添加数据,开始之后一系列处理。
对于一行数据中的某一列的值,调用

   Object dimsKey = indexer.processRowValsToUnsortedEncodedKeyComponent(
                        row.getRaw(dimension),
                        reportParseExceptions
                );

这里的row.getRaw(dimension)解析出一行数据中的,dimension这一列的数据。
indexer在DimensionHandler的类型为DimensionIndexer的成员。一般都是String类型的,实际类型是
StringDimensionIndexer。

看下StringDimensionIndexer的processRowValsToUnsortedEncodedKeyComponent方法。主要是维护了键-值映射。

private final Object2IntMap<String> valueToId = new Object2IntOpenHashMap<>();
private final List<String> idToValue = new ArrayList<>();

valueToId存储了一个string值对应一个int编码,idToValue则维护了id到值的关系。

processRowValsToUnsortedEncodedKeyComponent方法

在DimensionIndexer中好几个处理数据的encoder的方法。这个方法是处理原始值。
如果传如的是null,返回-1.
否则返回编码后的值:

  final int[] encodedDimensionValues;
    final int oldDictSize = dimLookup.size();

    if (dimValues == null) {
      dimLookup.add(null);
      encodedDimensionValues = null;
    } else if (dimValues instanceof List) {
      List<Object> dimValuesList = (List) dimValues;
      if (dimValuesList.isEmpty()) {
        dimLookup.add(null);
        encodedDimensionValues = EMPTY_INT_ARRAY;
      } else if (dimValuesList.size() == 1) {
        encodedDimensionValues = new int[]{dimLookup.add(STRING_TRANSFORMER.apply(dimValuesList.get(0)))};
      } else {
        final String[] dimensionValues = new String[dimValuesList.size()];
        for (int i = 0; i < dimValuesList.size(); i++) {
          dimensionValues[i] = STRING_TRANSFORMER.apply(dimValuesList.get(i));
        }
        if (multiValueHandling.needSorting()) {
          // Sort multival row by their unencoded values first.
          Arrays.sort(dimensionValues, Comparators.naturalNullsFirst());
        }

        final int[] retVal = new int[dimensionValues.length];

        int prevId = -1;
        int pos = 0;
        for (String dimensionValue : dimensionValues) {
          if (multiValueHandling != MultiValueHandling.SORTED_SET) {
            retVal[pos++] = dimLookup.add(dimensionValue);
            continue;
          }
          int index = dimLookup.add(dimensionValue);
          if (index != prevId) {
            prevId = retVal[pos++] = index;
          }
        }

        encodedDimensionValues = pos == retVal.length ? retVal : Arrays.copyOf(retVal, pos);
      }
    } else {
      encodedDimensionValues = new int[]{dimLookup.add(STRING_TRANSFORMER.apply(dimValues))};
    }

processRowValsToUnsortedEncodedKeyComponent最终返回的是当前这行数据特定列的值在valueToId的映射中的id,也是idToValue中的索引。
每个dimension对应一个DimensionDesc,每个DimensionDesc中有一个DimensionIndexer,每个DimensionIndexer中有一个DimensionDictionary,每个DimensionDictionary中有一个valueToId,idToValue。
如果当前有10行数据,维度dim列的值'a','b','c','d','e','a','a','b','f',那么在这10列数据都掉用processRowValsToUnsortedEncodedKeyComponent之后,idToValue中的值为[a,b,c,d,e,f],valueToid
中的值为[a->0,b->1,c->2,d->3,e->4,f->5],
processRowValsToUnsortedEncodedKeyComponent返回的值为0,1,2,3,4,0,0,1,5。就是每一列都一个全局的字典。用来编码于索引数据。毕竟处理int类型方便高效。
在IncrementalIndex中,toTimeAndDims用来处理原始的一行数据,最后生成TimeAndDims包装类。

@VisibleForTesting
    TimeAndDims toTimeAndDims(InputRow row) {
        row = formatRow(row);
        if (row.getTimestampFromEpoch() < minTimestamp) {
            throw new IAE("Cannot add row[%s] because it is below the minTimestamp[%s]", row, DateTimes.utc(minTimestamp));
        }

        final List<String> rowDimensions = row.getDimensions();

        Object[] dims;
        List<Object> overflow = null;
        synchronized (dimensionDescs) {
            dims = new Object[dimensionDescs.size()];
            for (String dimension : rowDimensions) {
                boolean wasNewDim = false;
                ColumnCapabilitiesImpl capabilities;
                DimensionDesc desc = dimensionDescs.get(dimension);
                if (desc != null) {
                    capabilities = desc.getCapabilities();
                } else {
                    wasNewDim = true;
                    capabilities = columnCapabilities.get(dimension);
                    if (capabilities == null) {
                        capabilities = new ColumnCapabilitiesImpl();
                        // For schemaless type discovery, assume everything is a String for now, can change later.
                        capabilities.setType(ValueType.STRING);
                        capabilities.setDictionaryEncoded(true);
                        capabilities.setHasBitmapIndexes(true);
                        columnCapabilities.put(dimension, capabilities);
                    }
                    DimensionHandler handler = DimensionHandlerUtils.getHandlerFromCapabilities(dimension, capabilities, null);
                    desc = addNewDimension(dimension, capabilities, handler);
                }
                DimensionHandler handler = desc.getHandler();
                DimensionIndexer indexer = desc.getIndexer();
                Object dimsKey = indexer.processRowValsToUnsortedEncodedKeyComponent(
                        row.getRaw(dimension),
                        reportParseExceptions
                );

                // Set column capabilities as data is coming in
                if (!capabilities.hasMultipleValues() && dimsKey != null && handler.getLengthOfEncodedKeyComponent(dimsKey) > 1) {
                    capabilities.setHasMultipleValues(true);
                }

                if (wasNewDim) {
                    if (overflow == null) {
                        overflow = Lists.newArrayList();
                    }
                    overflow.add(dimsKey);
                } else if (desc.getIndex() > dims.length || dims[desc.getIndex()] != null) {
                    /*
                     * index > dims.length requires that we saw this dimension and added it to the dimensionOrder map,
                     * otherwise index is null. Since dims is initialized based on the size of dimensionOrder on each call to add,
                     * it must have been added to dimensionOrder during this InputRow.
                     *
                     * if we found an index for this dimension it means we've seen it already. If !(index > dims.length) then
                     * we saw it on a previous input row (this its safe to index into dims). If we found a value in
                     * the dims array for this index, it means we have seen this dimension already on this input row.
                     */
                    throw new ISE("Dimension[%s] occurred more than once in InputRow", dimension);
                } else {
                    dims[desc.getIndex()] = dimsKey;
                }
            }
        }

        if (overflow != null) {
            // Merge overflow and non-overflow
            Object[] newDims = new Object[dims.length + overflow.size()];
            System.arraycopy(dims, 0, newDims, 0, dims.length);
            for (int i = 0; i < overflow.size(); ++i) {
                newDims[dims.length + i] = overflow.get(i);
            }
            dims = newDims;
        }

        long truncated = 0;
        if (row.getTimestamp() != null) {
            truncated = gran.bucketStart(row.getTimestamp()).getMillis();
        }
        return new TimeAndDims(Math.max(truncated, minTimestamp), dims, dimensionDescsList);
    }

之后在add()方法中调用。本质上调用了addToFacts方法。

  public int add(InputRow row, boolean skipMaxRowsInMemoryCheck) throws IndexSizeExceededException {
        TimeAndDims key = toTimeAndDims(row);
        final int rv = addToFacts(
                metrics,
                deserializeComplexMetrics,
                reportParseExceptions,
                row,
                numEntries,
                key,
                in,
                rowSupplier,
                skipMaxRowsInMemoryCheck
        );
        updateMaxIngestedTime(row.getTimestamp());
        return rv;
    }

AddToFacts方法是真正开始聚合数据的入口:

protected AddToFacts(
  InputRow row,
  IncrementalIndexRow key,
  ThreadLocal<InputRow> rowContainer,
  Supplier<InputRow> rowSupplier,
  boolean skipMaxRowsInMemoryCheck
)

从AggregatorFactory产出Aggregtor

首先对metrics(类型是AggregatorFactory数组),阐述Aggregator数组:

  aggs = new Aggregator[metrics.length];
            factorizeAggs(metrics, aggs, rowContainer, row);
            doAggregate(metrics, aggs, rowContainer, row, reportParseExceptions);
            final int rowIndex = indexIncrement.getAndIncrement();
            concurrentSet(rowIndex, aggs);
            // Last ditch sanity checks
            if (numEntries.get() >= maxRowCount
                    && facts.getPriorIndex(key) == TimeAndDims.EMPTY_ROW_INDEX
                    && !skipMaxRowsInMemoryCheck) {
                throw new IndexSizeExceededException("Maximum number of rows [%d] reached", maxRowCount);
            }
      
            final int prev = facts.putIfAbsent(key, rowIndex);
            if (TimeAndDims.EMPTY_ROW_INDEX == prev) {
                numEntries.incrementAndGet();
            } else {
                // We lost a race
                aggs = concurrentGet(prev);
               //取出上一行的聚合器,聚合到当前行
                doAggregate(metrics, aggs, rowContainer, row, reportParseExceptions);
                // Free up the misfire
                concurrentRemove(rowIndex);
                // This is expected to occur ~80% of the time in the worst scenarios
            }

factorizeAggs(metrics, aggs, rowContainer, row);
不同的聚合,实现aggregate方法不同。例如,CountAggregator.

public void aggregate() 
{
  ++count;
}

其他的复杂聚合需要自己实现。每一行都生成一个聚合器,然后对这些聚合器进行聚合合并操作。
这里主要调用了doAggregate方法,这个方法里面会真正调用实现聚合的方法。


        for (int i = 0; i < aggs.length; i++) {
            final Aggregator agg = aggs[i];//传入的,能够实现聚合的聚合器
            synchronized (agg) {
                try {
                    agg.aggregate();//聚合方法开始调用。
                } catch (ParseException e) {
                    // "aggregate" can throw ParseExceptions if a selector expects something but gets something else.
                    if (reportParseExceptions) {
                        throw new ParseException(e, "Encountered parse error for aggregator[%s]", metrics[i].getName());
                    } else {
                        log.debug(e, "Encountered parse error, skipping aggregator[%s].", metrics[i].getName());
                    }
                }
            }
        }

生成持久化一个segment

在IndexMergeV9的persist方法中,调用merge方法:

return merge(
  Collections.singletonList(
    new IncrementalIndexAdapter(
      dataInterval,
      index,
      indexSpec.getBitmapSerdeFactory().getBitmapFactory()
    ),
    false,    // rollup, no neet to rollup again
    index.getMetricAggs(),    // AggregatorFactory[]
    outDir,
    indexSpec,
    progress,
    segmentWriteOutMediumFactory
  )
);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341