-
Notifications
You must be signed in to change notification settings - Fork 991
Add dangerous join condition watchdog to prevent Cartesian and oversi… #7368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,34 @@ | ||||||
| <!-- | ||||||
| - 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. | ||||||
| --> | ||||||
|
|
||||||
| # Deployment Settings for Dangerous Join Watchdog | ||||||
|
|
||||||
| ## Spark SQL Extensions | ||||||
|
|
||||||
| ```properties | ||||||
| spark.sql.extensions=org.apache.kyuubi.sql.KyuubiSparkSQLExtension,org.apache.kyuubi.sql.watchdog.KyuubiDangerousJoinExtension | ||||||
| ``` | ||||||
|
|
||||||
| ## Dangerous Join Configurations | ||||||
|
|
||||||
| | Name | Default | Description | | ||||||
| |------------------------------------------------|---------|-----------------------------------------------------------| | ||||||
| | `kyuubi.watchdog.dangerousJoin.enabled` | `true` | Enable dangerous join watchdog | | ||||||
|
||||||
| | `kyuubi.watchdog.dangerousJoin.enabled` | `true` | Enable dangerous join watchdog | | |
| | `kyuubi.watchdog.dangerousJoin.enabled` | `false` | Enable dangerous join watchdog | |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,105 @@ | ||||||||||||||||||
| <!-- | ||||||||||||||||||
| - 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. | ||||||||||||||||||
| --> | ||||||||||||||||||
|
|
||||||||||||||||||
| # Dangerous Join Watchdog | ||||||||||||||||||
|
|
||||||||||||||||||
| Kyuubi Dangerous Join Watchdog detects risky join planning patterns before query execution. | ||||||||||||||||||
| It helps reduce accidental Cartesian products, oversized broadcast attempts, and long-running nested loop joins. | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Background | ||||||||||||||||||
|
|
||||||||||||||||||
| In shared SQL gateway environments, a single risky join can consume excessive driver memory or create very slow jobs. | ||||||||||||||||||
| The Dangerous Join Watchdog adds planning-time checks for these high-risk patterns. | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Risk Rules | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Equi-Join | ||||||||||||||||||
|
|
||||||||||||||||||
| - Rule 1: Equi-join is marked dangerous when it degrades to a Cartesian pattern. | ||||||||||||||||||
| - Rule 2: Equi-join is marked dangerous when the estimated build side exceeds the configured broadcast ratio threshold. | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Non-Equi Join | ||||||||||||||||||
|
|
||||||||||||||||||
| - Rule 1: Non-equi join is marked dangerous when both sides exceed broadcast threshold and effectively become Cartesian risk. | ||||||||||||||||||
| - Rule 2: Non-equi join is marked dangerous when build side is not selectable and the plan falls back to a second BNLJ pattern. | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Configurations | ||||||||||||||||||
|
|
||||||||||||||||||
| | Name | Default | Meaning | | ||||||||||||||||||
| |------------------------------------------------|---------|---------------------------------------------------------------------------| | ||||||||||||||||||
| | `kyuubi.watchdog.dangerousJoin.enabled` | `true` | Enable or disable dangerous join detection | | ||||||||||||||||||
| | `kyuubi.watchdog.dangerousJoin.broadcastRatio` | `0.8` | Ratio against Spark broadcast threshold for warning/reject decision | | ||||||||||||||||||
| | `kyuubi.watchdog.dangerousJoin.action` | `WARN` | `WARN` logs diagnostics; `REJECT` throws exception and rejects submission | | ||||||||||||||||||
|
Comment on lines
+42
to
+46
|
||||||||||||||||||
|
|
||||||||||||||||||
| ## Usage | ||||||||||||||||||
|
|
||||||||||||||||||
| 1. Put Kyuubi Spark extension jar into Spark classpath. | ||||||||||||||||||
| 2. Configure SQL extensions: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```properties | ||||||||||||||||||
| spark.sql.extensions=org.apache.kyuubi.sql.KyuubiSparkSQLExtension,org.apache.kyuubi.sql.watchdog.KyuubiDangerousJoinExtension | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
Comment on lines
+50
to
+55
|
||||||||||||||||||
|
|
||||||||||||||||||
| 3. Configure action: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```properties | ||||||||||||||||||
| kyuubi.watchdog.dangerousJoin.action=WARN | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| or | ||||||||||||||||||
|
|
||||||||||||||||||
| ```properties | ||||||||||||||||||
| kyuubi.watchdog.dangerousJoin.action=REJECT | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Sample WARN Log | ||||||||||||||||||
|
|
||||||||||||||||||
| When action is `WARN`, Kyuubi writes a structured JSON payload: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```text | ||||||||||||||||||
| KYUUBI_LOG_KEY={"sql":"SELECT ...","joinType":"INNER","reason":"Cartesian","leftSize":10485760,"rightSize":15728640,"broadcastThreshold":10485760,"broadcastRatio":0.8} | ||||||||||||||||||
|
Comment on lines
+71
to
+74
|
||||||||||||||||||
| When action is `WARN`, Kyuubi writes a structured JSON payload: | |
| ```text | |
| KYUUBI_LOG_KEY={"sql":"SELECT ...","joinType":"INNER","reason":"Cartesian","leftSize":10485760,"rightSize":15728640,"broadcastThreshold":10485760,"broadcastRatio":0.8} | |
| When action is `WARN`, Kyuubi writes a structured JSON payload (the `sql` field contains the logical plan string from `plan.toString()`): | |
| ```text | |
| KYUUBI_LOG_KEY={"sql":"Project [*]\n+- LogicalRDD [id#0, value#1], false","joinType":"INNER","reason":"Cartesian","leftSize":10485760,"rightSize":15728640,"broadcastThreshold":10485760,"broadcastRatio":0.8} |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||
| * 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.kyuubi.sql.watchdog | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| import scala.collection.mutable.ArrayBuffer | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| object DangerousJoinCounter { | ||||||||||||||||||||||||||||||
| case class Entry( | ||||||||||||||||||||||||||||||
| sqlText: String, | ||||||||||||||||||||||||||||||
| joinType: String, | ||||||||||||||||||||||||||||||
| reason: String, | ||||||||||||||||||||||||||||||
| leftSize: BigInt, | ||||||||||||||||||||||||||||||
| rightSize: BigInt, | ||||||||||||||||||||||||||||||
| broadcastThreshold: Long, | ||||||||||||||||||||||||||||||
| broadcastRatio: Double) { | ||||||||||||||||||||||||||||||
| def toJson: String = { | ||||||||||||||||||||||||||||||
| val pairs = Seq( | ||||||||||||||||||||||||||||||
| "sql" -> escape(sqlText), | ||||||||||||||||||||||||||||||
| "joinType" -> escape(joinType), | ||||||||||||||||||||||||||||||
| "reason" -> escape(reason), | ||||||||||||||||||||||||||||||
| "leftSize" -> leftSize.toString, | ||||||||||||||||||||||||||||||
| "rightSize" -> rightSize.toString, | ||||||||||||||||||||||||||||||
| "broadcastThreshold" -> broadcastThreshold.toString, | ||||||||||||||||||||||||||||||
| "broadcastRatio" -> broadcastRatio.toString) | ||||||||||||||||||||||||||||||
| pairs.map { case (k, v) => | ||||||||||||||||||||||||||||||
| if (k == "leftSize" || k == "rightSize" || k == "broadcastThreshold" || k == "broadcastRatio") { | ||||||||||||||||||||||||||||||
| s""""$k":$v""" | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| s""""$k":"$v"""" | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }.mkString("{", ",", "}") | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private val entries = ArrayBuffer.empty[Entry] | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def add(entry: Entry): Unit = synchronized { | ||||||||||||||||||||||||||||||
|
Comment on lines
+49
to
+52
|
||||||||||||||||||||||||||||||
| private val entries = ArrayBuffer.empty[Entry] | |
| def add(entry: Entry): Unit = synchronized { | |
| // Limit the number of stored entries to avoid unbounded memory growth. | |
| private val MaxEntries = 1000 | |
| private val entries = ArrayBuffer.empty[Entry] | |
| def add(entry: Entry): Unit = synchronized { | |
| if (entries.size >= MaxEntries) { | |
| // Evict the oldest entry to keep the buffer bounded. | |
| entries.remove(0) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This table documents
kyuubi.watchdog.dangerousJoin.enableddefault astrue, but the Spark extension modules introduced in this PR mostly default it tofalse. Please align the defaults here with the code (and keep consistent across Spark versions) to avoid misleading operators.