Skip to content

Commit

Permalink
Merge a77a362 into a7a0baa
Browse files Browse the repository at this point in the history
  • Loading branch information
kyotoYaho committed Mar 26, 2018
2 parents a7a0baa + a77a362 commit 8046552
Show file tree
Hide file tree
Showing 31 changed files with 447 additions and 1,609 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@

package org.apache.kylin.cube.cuboid.algorithm;

import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public abstract class AbstractRecommendAlgorithm implements CuboidRecommendAlgorithm {
private static final Logger logger = LoggerFactory.getLogger(AbstractRecommendAlgorithm.class);

private CuboidStats cuboidStats;
private BenefitPolicy benefitPolicy;
protected final CuboidStats cuboidStats;
protected final BenefitPolicy benefitPolicy;

private AtomicBoolean cancelRequested = new AtomicBoolean(false);
private AtomicBoolean canceled = new AtomicBoolean(false);
Expand All @@ -44,14 +45,19 @@ public AbstractRecommendAlgorithm(final long timeout, BenefitPolicy benefitPolic
this.benefitPolicy = benefitPolicy;
}

@Override
public List<Long> recommend(double expansionRate) {
double spaceLimit = cuboidStats.getBaseCuboidSize() * expansionRate;
return start(spaceLimit);
}

@Override
public void cancel() {
cancelRequested.set(true);
}

/**
* Checks whether the algorithm has been canceled or timed out.
*
*/
protected boolean shouldCancel() {
if (canceled.get()) {
Expand All @@ -71,12 +77,4 @@ protected boolean shouldCancel() {
}
return false;
}

public CuboidStats getCuboidStats() {
return cuboidStats;
}

public BenefitPolicy getBenefitPolicy() {
return benefitPolicy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

package org.apache.kylin.cube.cuboid.algorithm;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.util.Map;
import java.util.Set;

/**
* Calculate the benefit based on Benefit Per Unit Space.
Expand All @@ -35,17 +34,24 @@ public class BPUSCalculator implements BenefitPolicy {

private static Logger logger = LoggerFactory.getLogger(BPUSCalculator.class);

protected CuboidStats cuboidStats;
protected Map<Long, Long> cuboidAggCostMap;
protected final CuboidStats cuboidStats;
protected final ImmutableMap<Long, Long> initCuboidAggCostMap;
protected final Map<Long, Long> processCuboidAggCostMap;

public BPUSCalculator(CuboidStats cuboidStats) {
this.cuboidStats = cuboidStats;
this.cuboidAggCostMap = Maps.newHashMap();
this.initCuboidAggCostMap = ImmutableMap.copyOf(initCuboidAggCostMap());
this.processCuboidAggCostMap = Maps.newHashMap(initCuboidAggCostMap);
}

@Override
public void initBeforeStart() {
cuboidAggCostMap.clear();
protected BPUSCalculator(CuboidStats cuboidStats, ImmutableMap<Long, Long> initCuboidAggCostMap) {
this.cuboidStats = cuboidStats;
this.initCuboidAggCostMap = initCuboidAggCostMap;
this.processCuboidAggCostMap = Maps.newHashMap(initCuboidAggCostMap);
}

private Map<Long, Long> initCuboidAggCostMap() {
Map<Long, Long> cuboidAggCostMap = Maps.newHashMap();
//Initialize stats for mandatory cuboids
for (Long cuboid : cuboidStats.getAllCuboidsForMandatory()) {
if (getCuboidCost(cuboid) != null) {
Expand All @@ -66,6 +72,7 @@ public void initBeforeStart() {
}
cuboidAggCostMap.put(cuboid, leastCost);
}
return cuboidAggCostMap;
}

@Override
Expand All @@ -88,22 +95,21 @@ public CuboidBenefitModel.BenefitModel calculateBenefit(long cuboid, Set<Long> s
}

@Override
public CuboidBenefitModel.BenefitModel calculateBenefitTotal(List<Long> cuboidsToAdd, Set<Long> selected) {
public CuboidBenefitModel.BenefitModel calculateBenefitTotal(Set<Long> cuboidsToAdd, Set<Long> selected) {
Set<Long> selectedInner = Sets.newHashSet(selected);
Map<Long, Long> cuboidAggCostMapSnapshot = Maps.newHashMap(cuboidAggCostMap);
Map<Long, Long> cuboidAggCostMapCopy = Maps.newHashMap(processCuboidAggCostMap);
for (Long cuboid : cuboidsToAdd) {
selectedInner.add(cuboid);
propagateAggregationCost(cuboid, selectedInner);
propagateAggregationCost(cuboid, selectedInner, cuboidAggCostMapCopy);
}
double totalCostSaving = 0;
int benefitCount = 0;
for (Long cuboid : cuboidAggCostMap.keySet()) {
if (cuboidAggCostMap.get(cuboid) < cuboidAggCostMapSnapshot.get(cuboid)) {
totalCostSaving += cuboidAggCostMapSnapshot.get(cuboid) - cuboidAggCostMap.get(cuboid);
for (Long cuboid : cuboidAggCostMapCopy.keySet()) {
if (cuboidAggCostMapCopy.get(cuboid) < processCuboidAggCostMap.get(cuboid)) {
totalCostSaving += processCuboidAggCostMap.get(cuboid) - cuboidAggCostMapCopy.get(cuboid);
benefitCount++;
}
}
cuboidAggCostMap = cuboidAggCostMapSnapshot;

double benefitPerUnitSpace = totalCostSaving;
return new CuboidBenefitModel.BenefitModel(benefitPerUnitSpace, benefitCount);
Expand All @@ -120,7 +126,7 @@ protected Long getCuboidCost(long cuboid) {
}

private long getCuboidAggregationCost(long cuboid) {
return cuboidAggCostMap.get(cuboid);
return processCuboidAggCostMap.get(cuboid);
}

@Override
Expand All @@ -139,11 +145,15 @@ public double getMinBenefitRatio() {

@Override
public void propagateAggregationCost(long cuboid, Set<Long> selected) {
propagateAggregationCost(cuboid, selected, processCuboidAggCostMap);
}

public void propagateAggregationCost(long cuboid, Set<Long> selected, Map<Long, Long> processCuboidAggCostMap) {
long aggregationCost = getCuboidCost(cuboid);
Set<Long> childrenCuboids = cuboidStats.getAllDescendants(cuboid);
for (Long child : childrenCuboids) {
if (!selected.contains(child) && (aggregationCost < getCuboidAggregationCost(child))) {
cuboidAggCostMap.put(child, aggregationCost);
processCuboidAggCostMap.put(child, aggregationCost);
}
}
}
Expand All @@ -158,8 +168,6 @@ public double calculateSpaceCost(long cuboid) {

@Override
public BenefitPolicy getInstance() {
BPUSCalculator bpusCalculator = new BPUSCalculator(this.cuboidStats);
bpusCalculator.cuboidAggCostMap.putAll(this.cuboidAggCostMap);
return bpusCalculator;
return new BPUSCalculator(this.cuboidStats, this.initCuboidAggCostMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package org.apache.kylin.cube.cuboid.algorithm;

import java.util.List;
import java.util.Set;

/**
Expand All @@ -27,15 +26,29 @@
*/
public interface BenefitPolicy {

/**
* @return a cloned instance with initial status
*/
public BenefitPolicy getInstance();

public void initBeforeStart();

/**
* @param selected should not be changed
* Will not change the inner instance status
*/
public CuboidBenefitModel.BenefitModel calculateBenefit(long cuboid, Set<Long> selected);

public CuboidBenefitModel.BenefitModel calculateBenefitTotal(List<Long> cuboidsToAdd, Set<Long> selected);
/**
* @param cuboidsToAdd should not be changed
* @param selected should not be changed
* Will not change the inner instance status
*/
public CuboidBenefitModel.BenefitModel calculateBenefitTotal(Set<Long> cuboidsToAdd, Set<Long> selected);

public boolean ifEfficient(CuboidBenefitModel best);

/**
* @param selected should not be changed
* Will update the inner instance status
*/
public void propagateAggregationCost(long cuboid, Set<Long> selected);
}

0 comments on commit 8046552

Please sign in to comment.