Skip to content

Commit

Permalink
KYLIN-1936 improve enable limit logic (exactAggregation is too strict)
Browse files Browse the repository at this point in the history
  • Loading branch information
binmahone committed Aug 10, 2016
1 parent c67891d commit 28e9423
Show file tree
Hide file tree
Showing 42 changed files with 1,811 additions and 447 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import java.io.InputStream;
import java.io.Serializable;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.SortedSet;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -470,6 +470,10 @@ public float getHBaseHFileSizeGB() {
return Float.parseFloat(getOptional("kylin.hbase.hfile.size.gb", "2.0"));
}

public int getStoragePushDownLimitMax() {
return Integer.parseInt(getOptional("kylin.query.pushdown.limit.max", "10000"));
}

public int getScanThreshold() {
return Integer.parseInt(getOptional("kylin.query.scan.threshold", "10000000"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.kylin.cube.model.CubeDesc;
import org.apache.kylin.gridtable.GTRecord;
import org.apache.kylin.gridtable.GTScanRequest;
import org.apache.kylin.gridtable.GTScanRequestBuilder;
import org.apache.kylin.gridtable.GridTable;
import org.apache.kylin.gridtable.IGTScanner;
import org.apache.kylin.metadata.model.TblColRef;
Expand Down Expand Up @@ -83,7 +84,7 @@ public void run() {

protected void outputCuboid(long cuboidId, GridTable gridTable, ICuboidWriter output) throws IOException {
long startTime = System.currentTimeMillis();
GTScanRequest req = new GTScanRequest(gridTable.getInfo(), null, null, null);
GTScanRequest req = new GTScanRequestBuilder().setInfo(gridTable.getInfo()).setRanges(null).setDimensions(null).setFilterPushDown(null).createGTScanRequest();
IGTScanner scanner = gridTable.scan(req);
for (GTRecord record : scanner) {
output.write(cuboidId, record);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.apache.kylin.common.util.MemoryBudgetController;
import org.apache.kylin.cube.model.CubeDesc;
import org.apache.kylin.gridtable.GTRecord;
import org.apache.kylin.gridtable.GTScanRequest;
import org.apache.kylin.gridtable.GTScanRequestBuilder;
import org.apache.kylin.gridtable.IGTScanner;
import org.apache.kylin.measure.MeasureAggregators;
import org.apache.kylin.metadata.model.TblColRef;
Expand Down Expand Up @@ -399,7 +399,7 @@ public boolean fetchNext() throws IOException {
if (cuboidIterator.hasNext()) {
CuboidResult cuboid = cuboidIterator.next();
currentCuboidId = cuboid.cuboidId;
scanner = cuboid.table.scan(new GTScanRequest(cuboid.table.getInfo(), null, null, null));
scanner = cuboid.table.scan(new GTScanRequestBuilder().setInfo(cuboid.table.getInfo()).setRanges(null).setDimensions(null).setFilterPushDown(null).createGTScanRequest());
recordIterator = scanner.iterator();
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import org.apache.kylin.common.util.Dictionary;
import org.apache.kylin.common.util.ImmutableBitSet;
import org.apache.kylin.common.util.MemoryBudgetController;
import org.apache.kylin.common.util.MemoryBudgetController.MemoryWaterLevel;
import org.apache.kylin.common.util.Pair;
import org.apache.kylin.common.util.MemoryBudgetController.MemoryWaterLevel;
import org.apache.kylin.cube.cuboid.Cuboid;
import org.apache.kylin.cube.cuboid.CuboidScheduler;
import org.apache.kylin.cube.gridtable.CubeGridTable;
Expand All @@ -42,6 +42,7 @@
import org.apache.kylin.gridtable.GTInfo;
import org.apache.kylin.gridtable.GTRecord;
import org.apache.kylin.gridtable.GTScanRequest;
import org.apache.kylin.gridtable.GTScanRequestBuilder;
import org.apache.kylin.gridtable.GridTable;
import org.apache.kylin.gridtable.IGTScanner;
import org.apache.kylin.measure.topn.Counter;
Expand Down Expand Up @@ -329,8 +330,8 @@ private CuboidResult createBaseCuboid(BlockingQueue<List<String>> input) throws
IGTScanner baseInput = new InputConverter(baseCuboid.getInfo(), input);

Pair<ImmutableBitSet, ImmutableBitSet> dimensionMetricsBitSet = InMemCubeBuilderUtils.getDimensionAndMetricColumnBitSet(baseCuboidId, measureCount);
GTScanRequest req = new GTScanRequest(baseCuboid.getInfo(), null, null, dimensionMetricsBitSet.getFirst(), dimensionMetricsBitSet.getSecond(), metricsAggrFuncs, null);
GTAggregateScanner aggregationScanner = new GTAggregateScanner(baseInput, req);
GTScanRequest req = new GTScanRequestBuilder().setInfo(baseCuboid.getInfo()).setRanges(null).setDimensions(null).setAggrGroupBy(dimensionMetricsBitSet.getFirst()).setAggrMetrics(dimensionMetricsBitSet.getSecond()).setAggrMetricsFuncs(metricsAggrFuncs).setFilterPushDown(null).createGTScanRequest();
GTAggregateScanner aggregationScanner = new GTAggregateScanner(baseInput, req, Long.MAX_VALUE);
aggregationScanner.trackMemoryLevel(baseCuboidMemTracker);

int count = 0;
Expand Down Expand Up @@ -397,7 +398,7 @@ private CuboidResult aggregateCuboid(CuboidResult parent, long cuboidId) throws

private GTAggregateScanner prepareGTAggregationScanner(GridTable gridTable, long parentId, long cuboidId, ImmutableBitSet aggregationColumns, ImmutableBitSet measureColumns) throws IOException {
GTInfo info = gridTable.getInfo();
GTScanRequest req = new GTScanRequest(info, null, null, aggregationColumns, measureColumns, metricsAggrFuncs, null);
GTScanRequest req = new GTScanRequestBuilder().setInfo(info).setRanges(null).setDimensions(null).setAggrGroupBy(aggregationColumns).setAggrMetrics(measureColumns).setAggrMetricsFuncs(metricsAggrFuncs).setFilterPushDown(null).createGTScanRequest();
GTAggregateScanner scanner = (GTAggregateScanner) gridTable.scan(req);

// for child cuboid, some measures don't need aggregation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,28 @@ public class GTAggregateScanner implements IGTScanner {
final IGTScanner inputScanner;
final AggregationCache aggrCache;
final long spillThreshold;
final int storagePushDownLimit;//default to be Int.MAX
final long deadline;

private int aggregatedRowCount = 0;
private MemoryWaterLevel memTracker;
private boolean[] aggrMask;

public GTAggregateScanner(IGTScanner inputScanner, GTScanRequest req) {
public GTAggregateScanner(IGTScanner inputScanner, GTScanRequest req, long deadline) {
if (!req.hasAggregation())
throw new IllegalStateException();

this.info = inputScanner.getInfo();
this.dimensions = req.getColumns().andNot(req.getAggrMetrics());
this.dimensions = req.getDimensions();
this.groupBy = req.getAggrGroupBy();
this.metrics = req.getAggrMetrics();
this.metricsAggrFuncs = req.getAggrMetricsFuncs();
this.inputScanner = inputScanner;
this.aggrCache = new AggregationCache();
this.spillThreshold = (long) (req.getAggrCacheGB() * MemoryBudgetController.ONE_GB);
this.spillThreshold = (long) (req.getAggCacheMemThreshold() * MemoryBudgetController.ONE_GB);
this.aggrMask = new boolean[metricsAggrFuncs.length];
this.storagePushDownLimit = req.getStoragePushDownLimit();
this.deadline = deadline;

Arrays.fill(aggrMask, true);
}
Expand Down Expand Up @@ -133,8 +137,25 @@ public void close() throws IOException {
public Iterator<GTRecord> iterator() {
long count = 0;
for (GTRecord r : inputScanner) {

count++;
aggrCache.aggregate(r);

if (getNumOfSpills() == 0) {
//check limit
boolean ret = aggrCache.aggregate(r, storagePushDownLimit);

if (!ret) {
logger.info("abort reading inputScanner because storage push down limit is hit");
break;//limit is hit
}
} else {//else if dumps is not empty, it means a lot of row need aggregated, so it's less likely that limit clause is helping
aggrCache.aggregate(r, Integer.MAX_VALUE);
}

//check deadline
if (count % 10000 == 1 && System.currentTimeMillis() > deadline) {
throw new GTScanTimeoutException("Timeout in GTAggregateScanner with scanned count " + count);
}
}
logger.info("GTAggregateScanner input rows: " + count);
return aggrCache.iterator();
Expand Down Expand Up @@ -241,7 +262,7 @@ private byte[] createKey(GTRecord record) {
return result;
}

void aggregate(GTRecord r) {
boolean aggregate(GTRecord r, int stopForLimit) {
if (++aggregatedRowCount % 100000 == 0) {
if (memTracker != null) {
memTracker.markHigh();
Expand All @@ -257,6 +278,12 @@ void aggregate(GTRecord r) {
final byte[] key = createKey(r);
MeasureAggregator[] aggrs = aggBufMap.get(key);
if (aggrs == null) {

//for storage push down limit
if (aggBufMap.size() >= stopForLimit) {
return false;
}

aggrs = newAggregators();
aggBufMap.put(key, aggrs);
}
Expand All @@ -267,6 +294,7 @@ void aggregate(GTRecord r) {
aggrs[i].aggregate(metrics);
}
}
return true;
}

private void spillBuffMap() throws RuntimeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void validateColRef(TblColRef ref) {
if (!expected.equals(ref))
throw new IllegalArgumentException();
}

void validate() {
if (codeSystem == null)
throw new IllegalStateException();
Expand Down
33 changes: 30 additions & 3 deletions core-cube/src/main/java/org/apache/kylin/gridtable/GTRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import org.apache.kylin.common.util.ByteArray;
import org.apache.kylin.common.util.ImmutableBitSet;

import com.google.common.base.Preconditions;

public class GTRecord implements Comparable<GTRecord> {
public class GTRecord implements Comparable<GTRecord>, Cloneable {

final transient GTInfo info;
final ByteArray[] cols;
Expand All @@ -54,6 +55,11 @@ public GTRecord(GTRecord other) {
}
}

@Override
public Object clone() {
return new GTRecord(this);
}

public GTInfo getInfo() {
return info;
}
Expand Down Expand Up @@ -189,12 +195,33 @@ public int hashCode() {

@Override
public int compareTo(GTRecord o) {
return compareToInternal(o, info.colAll);
}

public int compareToOnPrimaryKey(GTRecord o) {
return compareToInternal(o, info.primaryKey);
}

public static Comparator<GTRecord> getPrimaryKeyComparator() {
return new Comparator<GTRecord>() {
@Override
public int compare(GTRecord o1, GTRecord o2) {
if (o1 == null || o2 == null) {
throw new IllegalStateException("Cannot handle null");
}

return o1.compareToOnPrimaryKey(o2);
}
};
}

private int compareToInternal(GTRecord o, ImmutableBitSet participateCols) {
assert this.info == o.info; // reference equal for performance
IGTComparator comparator = info.codeSystem.getComparator();

int comp = 0;
for (int i = 0; i < info.colAll.trueBitCount(); i++) {
int c = info.colAll.trueBitAt(i);
for (int i = 0; i < participateCols.trueBitCount(); i++) {
int c = participateCols.trueBitAt(i);
comp = comparator.compare(cols[c], o.cols[c]);
if (comp != 0)
return comp;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.kylin.gridtable;

public class GTScanExceedThresholdException extends RuntimeException {

public GTScanExceedThresholdException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public GTScanRequest planScanRequest() {
GTScanRequest scanRequest;
List<GTScanRange> scanRanges = this.planScanRanges();
if (scanRanges != null && scanRanges.size() != 0) {
scanRequest = new GTScanRequest(gtInfo, scanRanges, gtDimensions, gtAggrGroups, gtAggrMetrics, gtAggrFuncs, gtFilter);
scanRequest = new GTScanRequestBuilder().setInfo(gtInfo).setRanges(scanRanges).setDimensions(gtDimensions).setAggrGroupBy(gtAggrGroups).setAggrMetrics(gtAggrMetrics).setAggrMetricsFuncs(gtAggrFuncs).setFilterPushDown(gtFilter).createGTScanRequest();
} else {
scanRequest = null;
}
Expand Down
Loading

0 comments on commit 28e9423

Please sign in to comment.