-
Notifications
You must be signed in to change notification settings - Fork 304
Expand file tree
/
Copy pathCometSparkSessionExtensions.scala
More file actions
303 lines (271 loc) · 12.2 KB
/
CometSparkSessionExtensions.scala
File metadata and controls
303 lines (271 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* 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.comet
import java.nio.ByteOrder
import org.apache.spark.SparkConf
import org.apache.spark.internal.Logging
import org.apache.spark.network.util.ByteUnit
import org.apache.spark.sql.{SparkSession, SparkSessionExtensions}
import org.apache.spark.sql.catalyst.rules.Rule
import org.apache.spark.sql.catalyst.trees.TreeNode
import org.apache.spark.sql.comet._
import org.apache.spark.sql.execution._
import org.apache.spark.sql.internal.SQLConf
import org.apache.comet.CometConf._
import org.apache.comet.rules.{CometExecRule, CometPlanAdaptiveDynamicPruningFilters, CometScanRule, EliminateRedundantTransitions}
import org.apache.comet.shims.ShimCometSparkSessionExtensions
/**
* CometDriverPlugin will register an instance of this class with Spark.
*
* This class is responsible for injecting Comet rules and extensions into Spark.
*/
class CometSparkSessionExtensions
extends (SparkSessionExtensions => Unit)
with Logging
with ShimCometSparkSessionExtensions {
override def apply(extensions: SparkSessionExtensions): Unit = {
extensions.injectColumnar { session => CometScanColumnar(session) }
extensions.injectColumnar { session => CometExecColumnar(session) }
extensions.injectColumnar { session => CometDPPColumnar(session) }
extensions.injectQueryStagePrepRule { session => CometScanRule(session) }
extensions.injectQueryStagePrepRule { session => CometExecRule(session) }
}
case class CometScanColumnar(session: SparkSession) extends ColumnarRule {
override def preColumnarTransitions: Rule[SparkPlan] = CometScanRule(session)
}
case class CometExecColumnar(session: SparkSession) extends ColumnarRule {
override def preColumnarTransitions: Rule[SparkPlan] = CometExecRule(session)
override def postColumnarTransitions: Rule[SparkPlan] =
EliminateRedundantTransitions(session)
}
case class CometDPPColumnar(session: SparkSession) extends ColumnarRule {
override def postColumnarTransitions: Rule[SparkPlan] =
CometPlanAdaptiveDynamicPruningFilters(session)
}
}
object CometSparkSessionExtensions extends Logging {
lazy val isBigEndian: Boolean = ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN)
/**
* Checks whether Comet extension should be loaded for Spark.
*/
private[comet] def isCometLoaded(conf: SQLConf): Boolean = {
if (isBigEndian) {
logInfo("Comet extension is disabled because platform is big-endian")
return false
}
if (!COMET_ENABLED.get(conf)) {
logInfo(s"Comet extension is disabled, please turn on ${COMET_ENABLED.key} to enable it")
return false
}
// We don't support INT96 timestamps written by Apache Impala in a different timezone yet
if (conf.getConf(SQLConf.PARQUET_INT96_TIMESTAMP_CONVERSION)) {
logWarning(
"Comet extension is disabled, because it currently doesn't support" +
s" ${SQLConf.PARQUET_INT96_TIMESTAMP_CONVERSION} setting to true.")
return false
}
try {
// This will load the Comet native lib on demand, and if success, should set
// `NativeBase.loaded` to true
NativeBase.isLoaded
} catch {
case e: Throwable =>
if (COMET_NATIVE_LOAD_REQUIRED.get(conf)) {
throw new CometRuntimeException(
"Error when loading native library. Please fix the error and try again, or fallback " +
s"to Spark by setting ${COMET_ENABLED.key} to false",
e)
} else {
logWarning(
"Comet extension is disabled because of error when loading native lib. " +
"Falling back to Spark",
e)
}
false
}
}
// Check whether Comet shuffle is enabled:
// 1. `COMET_EXEC_SHUFFLE_ENABLED` is true
// 2. `spark.shuffle.manager` is set to `CometShuffleManager`
// 3. Off-heap memory is enabled || Spark/Comet unit testing
def isCometShuffleEnabled(conf: SQLConf): Boolean =
COMET_EXEC_SHUFFLE_ENABLED.get(conf) && isCometShuffleManagerEnabled(conf)
def isCometShuffleManagerEnabled(conf: SQLConf): Boolean = {
conf.contains("spark.shuffle.manager") && conf.getConfString("spark.shuffle.manager") ==
"org.apache.spark.sql.comet.execution.shuffle.CometShuffleManager"
}
def isCometScan(op: SparkPlan): Boolean = {
op.isInstanceOf[CometBatchScanExec] || op.isInstanceOf[CometScanExec]
}
def isSpark35Plus: Boolean = {
org.apache.spark.SPARK_VERSION >= "3.5"
}
def isSpark40Plus: Boolean = {
org.apache.spark.SPARK_VERSION >= "4.0"
}
/**
* Whether we should override Spark memory configuration for Comet. This only returns true when
* Comet native execution is enabled and/or Comet shuffle is enabled and Comet doesn't use
* off-heap mode (unified memory manager).
*/
def shouldOverrideMemoryConf(conf: SparkConf): Boolean = {
val cometEnabled = getBooleanConf(conf, CometConf.COMET_ENABLED)
val cometShuffleEnabled = getBooleanConf(conf, CometConf.COMET_EXEC_SHUFFLE_ENABLED)
val cometExecEnabled = getBooleanConf(conf, CometConf.COMET_EXEC_ENABLED)
val offHeapMode = CometSparkSessionExtensions.isOffHeapEnabled(conf)
cometEnabled && (cometShuffleEnabled || cometExecEnabled) && !offHeapMode
}
/**
* Determines required memory overhead in MB per executor process for Comet when running in
* on-heap mode.
*/
def getCometMemoryOverheadInMiB(sparkConf: SparkConf): Long = {
if (isOffHeapEnabled(sparkConf)) {
// when running in off-heap mode we use unified memory management to share
// off-heap memory with Spark so do not add overhead
return 0
}
ConfigHelpers.byteFromString(
sparkConf.get(
COMET_ONHEAP_MEMORY_OVERHEAD.key,
COMET_ONHEAP_MEMORY_OVERHEAD.defaultValueString),
ByteUnit.MiB)
}
private def getBooleanConf(conf: SparkConf, entry: ConfigEntry[Boolean]) =
conf.getBoolean(entry.key, entry.defaultValue.get)
/**
* Calculates required memory overhead in bytes per executor process for Comet when running in
* on-heap mode.
*/
def getCometMemoryOverhead(sparkConf: SparkConf): Long = {
ByteUnit.MiB.toBytes(getCometMemoryOverheadInMiB(sparkConf))
}
/**
* Calculates required shuffle memory size in bytes per executor process for Comet when running
* in on-heap mode.
*/
def getCometShuffleMemorySize(sparkConf: SparkConf, conf: SQLConf = SQLConf.get): Long = {
assert(!isOffHeapEnabled(sparkConf))
val cometMemoryOverhead = getCometMemoryOverheadInMiB(sparkConf)
val overheadFactor = COMET_ONHEAP_SHUFFLE_MEMORY_FACTOR.get(conf)
val shuffleMemorySize = (overheadFactor * cometMemoryOverhead).toLong
if (shuffleMemorySize > cometMemoryOverhead) {
logWarning(
s"Configured shuffle memory size $shuffleMemorySize is larger than Comet memory overhead " +
s"$cometMemoryOverhead, using Comet memory overhead instead.")
ByteUnit.MiB.toBytes(cometMemoryOverhead)
} else {
ByteUnit.MiB.toBytes(shuffleMemorySize)
}
}
def isOffHeapEnabled(sparkConf: SparkConf): Boolean = {
sparkConf.getBoolean("spark.memory.offHeap.enabled", false)
}
/**
* Record a fallback reason on a `TreeNode` (a Spark operator or expression) explaining why
* Comet cannot accelerate it. Reasons recorded here are surfaced in extended explain output
* (see `ExtendedExplainInfo`) and, when `COMET_LOG_FALLBACK_REASONS` is enabled, logged as
* warnings. The reasons are also rolled up from child nodes so that the operator that remains
* in the Spark plan carries the reasons from its converted-away subtree.
*
* Call this in any code path where Comet decides not to convert a given node - serde `convert`
* methods returning `None`, unsupported data types, disabled configs, etc. Do not use this for
* informational messages that are not fallback reasons: anything tagged here is treated by the
* rules as a signal that the node falls back to Spark.
*
* @param node
* The Spark operator or expression that is falling back to Spark.
* @param info
* The fallback reason. Optional, may be null or empty - pass empty only when the call is used
* purely to roll up reasons from `exprs`.
* @param exprs
* Child nodes whose own fallback reasons should be rolled up into `node`. Pass the
* sub-expressions or child operators whose failure caused `node` to fall back.
* @tparam T
* The type of the TreeNode. Typically `SparkPlan`, `AggregateExpression`, or `Expression`.
* @return
* `node` with fallback reasons attached (as a side effect on its tag map).
*/
def withInfo[T <: TreeNode[_]](node: T, info: String, exprs: T*): T = {
// support existing approach of passing in multiple infos in a newline-delimited string
val infoSet = if (info == null || info.isEmpty) {
Set.empty[String]
} else {
info.split("\n").toSet
}
withInfos(node, infoSet, exprs: _*)
}
/**
* Record one or more fallback reasons on a `TreeNode` and roll up reasons from any child nodes.
* This is the set-valued form of [[withInfo]]; see that overload for the full contract.
*
* Reasons are accumulated (never overwritten) on the node's `EXTENSION_INFO` tag and are
* surfaced in extended explain output. When `COMET_LOG_FALLBACK_REASONS` is enabled, each new
* reason is also emitted as a warning.
*
* @param node
* The Spark operator or expression that is falling back to Spark.
* @param info
* The fallback reasons for this node. May be empty when the call is used purely to roll up
* child reasons.
* @param exprs
* Child nodes whose own fallback reasons should be rolled up into `node`.
* @tparam T
* The type of the TreeNode. Typically `SparkPlan`, `AggregateExpression`, or `Expression`.
* @return
* `node` with fallback reasons attached (as a side effect on its tag map).
*/
def withInfos[T <: TreeNode[_]](node: T, info: Set[String], exprs: T*): T = {
if (CometConf.COMET_LOG_FALLBACK_REASONS.get()) {
for (reason <- info) {
logWarning(s"Comet cannot accelerate ${node.getClass.getSimpleName} because: $reason")
}
}
val existingNodeInfos = node.getTagValue(CometExplainInfo.EXTENSION_INFO)
val newNodeInfo = (existingNodeInfos ++ exprs
.flatMap(_.getTagValue(CometExplainInfo.EXTENSION_INFO))).flatten.toSet
node.setTagValue(CometExplainInfo.EXTENSION_INFO, newNodeInfo ++ info)
node
}
/**
* Roll up fallback reasons from `exprs` onto `node` without adding a new reason of its own. Use
* this when a parent operator is itself falling back and wants to preserve the reasons recorded
* on its child expressions/operators so they appear together in explain output.
*
* @param node
* The parent operator or expression falling back to Spark.
* @param exprs
* Child nodes whose fallback reasons should be aggregated onto `node`.
* @tparam T
* The type of the TreeNode. Typically `SparkPlan`, `AggregateExpression`, or `Expression`.
* @return
* `node` with the rolled-up reasons attached (as a side effect on its tag map).
*/
def withInfo[T <: TreeNode[_]](node: T, exprs: T*): T = {
withInfos(node, Set.empty, exprs: _*)
}
/**
* True if any fallback reason has been recorded on `node` (via [[withInfo]] / [[withInfos]]).
* Callers that need to short-circuit when a prior rule pass has already decided a node falls
* back can use this as the sticky signal.
*/
def hasExplainInfo(node: TreeNode[_]): Boolean = {
node.getTagValue(CometExplainInfo.EXTENSION_INFO).exists(_.nonEmpty)
}
}