Skip to content

Commit

Permalink
Merge pull request #765 from ryankwagner/fixCandidateCalc
Browse files Browse the repository at this point in the history
Add test and modification to bestCandidate Calculation
  • Loading branch information
pranavbhole authored Mar 19, 2021
2 parents 1eab70a + 78462ee commit 955c900
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 28 deletions.
2 changes: 1 addition & 1 deletion api-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<artifactId>maha-parent</artifactId>
<groupId>com.yahoo.maha</groupId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
</parent>

<name>maha api-example</name>
Expand Down
2 changes: 1 addition & 1 deletion api-jersey/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>com.yahoo.maha</groupId>
<artifactId>maha-parent</artifactId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
</parent>

<name>maha api-jersey</name>
Expand Down
2 changes: 1 addition & 1 deletion bigquery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>com.yahoo.maha</groupId>
<artifactId>maha-parent</artifactId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
</parent>

<name>maha bigquery executor</name>
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.yahoo.maha</groupId>
<artifactId>maha-parent</artifactId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
</parent>

<name>maha core</name>
Expand Down
38 changes: 26 additions & 12 deletions core/src/main/scala/com/yahoo/maha/core/query/QueryPipeline.scala
Original file line number Diff line number Diff line change
Expand Up @@ -450,22 +450,36 @@ trait QueryPipelineFactory {

}

case class RollupTuple(name: String, engine: Engine, costEstimate: Long, level: Int, cardinality: Int) {
def getTuple = (name, engine, costEstimate, level, cardinality)
}

object DefaultQueryPipelineFactory extends Logging {

val druidMultiQueryEngineList: Seq[Engine] = List(OracleEngine, PostgresEngine)

private[this] def aLessThanBByLevelAndCostAndCardinality(a: (String, Engine, Long, Int, Int), b: (String, Engine, Long, Int, Int)): Boolean = {
if (a._2 == b._2) {
if (a._4 == b._4) {
a._3 < b._3
def rollupComparator(
f: (RollupTuple, RollupTuple) => Boolean = aLessThanBByLevelAndCostAndCardinality
) = {
f
}

private[this] def aLessThanBByLevelAndCostAndCardinality(a: RollupTuple, b: RollupTuple): Boolean = {
if (a.engine == b.engine) {
if (a.costEstimate == b.costEstimate) {
a.level < b.level
} else {
a._4 < b._4
a.costEstimate < b.costEstimate
}
} else {
if (a._5 == b._5) {
a._3 < b._3
if (a.cardinality == b.cardinality) {
if(a.costEstimate == b.costEstimate){
a.level < b.level
} else {
a.costEstimate < b.costEstimate
}
} else {
a._5 < b._5
a.cardinality < b.cardinality
}
}
}
Expand Down Expand Up @@ -529,7 +543,7 @@ object DefaultQueryPipelineFactory extends Logging {
if (requestModel.isDebugEnabled) {
info(s"disqualifySet = $disqualifySet")
}
val result: IndexedSeq[(String, Engine, Long, Int, Int)] = requestModel.factCost.toIndexedSeq.collect {
val result: IndexedSeq[RollupTuple] = requestModel.factCost.toIndexedSeq.collect {
case ((fn, engine), rowcost) if (!queryGeneratorRegistry.getDefaultGenerator(engine).isDefined || queryGeneratorRegistry.getDefaultGenerator(engine).get.validateEngineConstraints(requestModel)) && (forceEngine.contains(engine) || (!disqualifySet(engine) && forceEngine.isEmpty)) =>
val fact = requestModel.bestCandidates.get.facts(fn).fact
val level = fact.level
Expand All @@ -538,11 +552,11 @@ object DefaultQueryPipelineFactory extends Logging {
if (requestModel.isDebugEnabled) {
info(s"fn=$fn engine=$engine cost=${rowcost.costEstimate} level=$level cardinalityPreference=$dimCardinalityPreference")
}
(fn, engine, rowcost.costEstimate, level, dimCardinalityPreference)
}.sortWith(aLessThanBByLevelAndCostAndCardinality)
RollupTuple(fn, engine, rowcost.costEstimate, level, dimCardinalityPreference)
}.sortWith(rollupComparator())
require(result.nonEmpty,
s"Failed to find best candidate, forceEngine=$forceEngine, engine disqualifyingSet=$disqualifySet, candidates=${requestModel.bestCandidates.get.facts.mapValues(_.fact.engine).toSet}")
requestModel.bestCandidates.get.getFactBestCandidate(result.head._1, requestModel)
requestModel.bestCandidates.get.getFactBestCandidate(result.head.name, requestModel)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,63 @@ class QueryPipelineWithFallbackTest extends AnyFunSuite with Matchers with Befor
pipeline = builder._1.toOption.get.build()
assert(pipeline.fallbackQueryChainOption.isEmpty, s"No fallback query expected: $pipeline")
}

test("Demonstrate the fix in the new result comparator.") {

def aLessThanBByLevelAndCostAndCardinality(a: RollupTuple, b: RollupTuple): Boolean = {
if (a.engine == b.engine) {
if (a.level == b.level) {
a.costEstimate < b.costEstimate
} else {
a.level < b.level
}
} else {
if (a.cardinality == b.cardinality) {
a.costEstimate < b.costEstimate
} else {
a.cardinality < b.cardinality
}
}
}

//(fn, engine, rowcost.costEstimate, level, dimCardinalityPreference)
//(name:String, engine: Engine, cost: Long, level: Int, cardinality: Int)

val t1 = RollupTuple("dr_stats_hourly", DruidEngine, 1600L, 9993, 8675309)
val t2 = RollupTuple("dr_ad_stats_hourly", DruidEngine, 1600L, 9995, 8675309)
val t3 = RollupTuple("oracle_stats", OracleEngine, 1600L, 9992, 8675309)
val t4 = RollupTuple("oracle_ad_stats", OracleEngine, 1600L, 9994, 8675309)
val t5 = RollupTuple("dr_teacher_ad_stats", DruidEngine, 1600L, 9998, 8675309)
val t6 = RollupTuple("oracle_teacher_stats", OracleEngine, 1600L, 9991, 8675309)
val t7 = RollupTuple("dr_teacher_stats_hourly", DruidEngine, 1600L, 9992, 8675309)
val t8 = RollupTuple("dr_teacher_ad_stats_hourly", DruidEngine, 1600L, 9997, 8675309)


val newResult = Vector(t1,t2,t3,t4,t5,t6,t7,t8).sortWith(DefaultQueryPipelineFactory.rollupComparator())
val oldResult = Vector(t1,t2,t3,t4,t5,t6,t7,t8).sortWith(DefaultQueryPipelineFactory.rollupComparator(aLessThanBByLevelAndCostAndCardinality))

//println(oldResult.mkString("\n"))
//println(newResult.mkString("\n"))

assert(oldResult.map(_.getTuple).mkString("\n").equals(
"(dr_stats_hourly,Druid,1600,9993,8675309)\n" +
"(dr_ad_stats_hourly,Druid,1600,9995,8675309)\n" +
"(oracle_stats,Oracle,1600,9992,8675309)\n" +
"(oracle_ad_stats,Oracle,1600,9994,8675309)\n" +
"(dr_teacher_ad_stats,Druid,1600,9998,8675309)\n" +
"(oracle_teacher_stats,Oracle,1600,9991,8675309)\n" +
"(dr_teacher_stats_hourly,Druid,1600,9992,8675309)\n" +
"(dr_teacher_ad_stats_hourly,Druid,1600,9997,8675309)"
))
assert(newResult.map(_.getTuple).mkString("\n").equals(
"(oracle_teacher_stats,Oracle,1600,9991,8675309)\n" +
"(oracle_stats,Oracle,1600,9992,8675309)\n" +
"(dr_teacher_stats_hourly,Druid,1600,9992,8675309)\n" +
"(dr_stats_hourly,Druid,1600,9993,8675309)\n" +
"(oracle_ad_stats,Oracle,1600,9994,8675309)\n" +
"(dr_ad_stats_hourly,Druid,1600,9995,8675309)\n" +
"(dr_teacher_ad_stats_hourly,Druid,1600,9997,8675309)\n" +
"(dr_teacher_ad_stats,Druid,1600,9998,8675309)"
))
}
}
2 changes: 1 addition & 1 deletion db/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.yahoo.maha</groupId>
<artifactId>maha-parent</artifactId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
</parent>

<name>maha db</name>
Expand Down
2 changes: 1 addition & 1 deletion druid-lookups/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>maha-parent</artifactId>
<groupId>com.yahoo.maha</groupId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion druid/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>com.yahoo.maha</groupId>
<artifactId>maha-parent</artifactId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
</parent>

<name>maha druid executor</name>
Expand Down
2 changes: 1 addition & 1 deletion job-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.yahoo.maha</groupId>
<artifactId>maha-parent</artifactId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
</parent>

<name>maha job service</name>
Expand Down
2 changes: 1 addition & 1 deletion oracle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>com.yahoo.maha</groupId>
<artifactId>maha-parent</artifactId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
</parent>

<name>maha oracle executor</name>
Expand Down
2 changes: 1 addition & 1 deletion par-request-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>com.yahoo.maha</groupId>
<artifactId>maha-parent</artifactId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
</parent>


Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.yahoo.maha</groupId>
<artifactId>maha-parent</artifactId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
<packaging>pom</packaging>

<name>maha parent</name>
Expand Down
2 changes: 1 addition & 1 deletion postgres/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>com.yahoo.maha</groupId>
<artifactId>maha-parent</artifactId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
</parent>

<name>maha postgres executor</name>
Expand Down
2 changes: 1 addition & 1 deletion presto/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>com.yahoo.maha</groupId>
<artifactId>maha-parent</artifactId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
</parent>

<name>maha presto executor</name>
Expand Down
2 changes: 1 addition & 1 deletion request-log/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.yahoo.maha</groupId>
<artifactId>maha-parent</artifactId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
</parent>

<name>maha request log</name>
Expand Down
2 changes: 1 addition & 1 deletion service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>com.yahoo.maha</groupId>
<artifactId>maha-parent</artifactId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
</parent>

<name>maha service</name>
Expand Down
2 changes: 1 addition & 1 deletion worker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.yahoo.maha</groupId>
<artifactId>maha-parent</artifactId>
<version>6.37-SNAPSHOT</version>
<version>6.38-SNAPSHOT</version>
</parent>

<name>maha worker</name>
Expand Down

0 comments on commit 955c900

Please sign in to comment.