-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Support Optimized Write #1198
base: master
Are you sure you want to change the base?
Support Optimized Write #1198
Conversation
Thank you @sezruby for creating this PR! This is a very useful feature. We are currently busy with the next release of Delta Lake. Will be reviewing the PR after the release. |
Can you please fix the conflicts? |
Signed-off-by: Eunjin Song <sezruby@gmail.com>
8aefaca
to
72e6555
Compare
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.
Hello @sezruby my apologies for not being able to review this so long. i took a first pass and i think my biggest feedback is that i dont understand the overall optimization algorithm and the parameters. Typically for a feature like this we write design docs (see Optimize Zorder design doc) where we discuss the design choices and come to an agreement. Could you write a short doc explaining the algorithm? I want to understand the behavior and edge cases of this optimization.
}) | ||
} | ||
|
||
private[sql] def removeTopRepartition(plan: SparkPlan): SparkPlan = { |
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.
Can you explain each of the cases with comments... these are pretty complicated on to reason about
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.
Thanks for the review! I'll add some comments & classdoc and try to improve documentation in #1158
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.
@tdas I added some example for OptimizeWrite partitioned data.
#1158 (comment)
Could you have a look and let me know if there is something unclear?
For non-partitioned data, here I use RoundRobinPartitioning but it could be inefficient in some cases as it distributes all rows into all partitions which is unnecessary. I think we could improve it later.
private[sql] def removeTopRepartition(plan: SparkPlan): SparkPlan = { | ||
plan match { | ||
case p@AdaptiveSparkPlanExec(inputPlan: ShuffleExchangeExec, _, _, _, _) | ||
if !inputPlan.shuffleOrigin.equals(ENSURE_REQUIREMENTS) => |
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.
I am trying to understand this and map it spark code. what you are trying to do is remove a shuffle if it wasnt added automatically by the planner to ensure requirement. Doesnt that mean if user asked for repartitioning by a certain way with an explicit programmatic API (e.g., DataFrame.repartition) we will be ignoring that completely?
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.
Yes it's because for Optimize Write, we add repartition(partitionColumns)( + rebalancing) at top of the plan, so unnecessary repartition(n) or coalesce(n) could be removed.
#1158 - Things to do - 3 for detail
core/src/main/scala/org/apache/spark/sql/delta/util/DeltaShufflePartitionsUtil.scala
Show resolved
Hide resolved
core/src/main/scala/org/apache/spark/sql/delta/util/DeltaShufflePartitionsUtil.scala
Outdated
Show resolved
Hide resolved
core/src/main/scala/org/apache/spark/sql/delta/util/DeltaShufflePartitionsUtil.scala
Show resolved
Hide resolved
import scala.concurrent.Future | ||
import scala.concurrent.duration.Duration | ||
|
||
case class OptimizeWriteExchangeExec( |
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.
Class docs are needed with a full explanation of how the optimization occurs, what the algorithm is like, what are the parameters. Its really hard to understand the over all algorithm from the code without an overview here.
13927b5
to
a2cb8b6
Compare
Signed-off-by: Eunjin Song <sezruby@gmail.com>
a2cb8b6
to
e2d12dc
Compare
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.
Would be great to get some momentum going on this again. Seems like it makes sense after understanding how Spark does rebalancing. My question would be if it works for streaming writes, and if so might be good to add a test for that?
} else { | ||
mapStartIndices(i + 1) | ||
} | ||
val dataSize = startMapIndex.until(endMapIndex).map(mapPartitionSizes(_)).sum |
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.
There was an update on the Spark side to make this more performant: apache/spark@9e1d00c
Is the main reason to not just use the Spark versions directly to add the configurable mergedPartitionFactor, or to not rely on those internal helper functions?
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.
Yes it's for both; make configurable and not rely on spark internal util which can change frequently. But I'm also okay to use Spark one. I'm just waiting for databricks team to get back on this. (and auto compaction too)
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.
https://learn.microsoft.com/en-us/azure/synapse-analytics/spark/optimize-write-for-apache-spark
FYI, the code is being used in prod at least 6 months and no major issue so far.
We might need to increase binSize config / PARQUET COMPRESSION RATIO for larger files. (60~80mb avg for now)
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.
Hmm I tried building and using this myself and I don't seem to be getting my large partitions split, gonna add some more logging to try to see why/what's happening.
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.
Yes it's the case this approach (Spark rebalance logic) cannot handle. Because the unit of rebalancing is determined by the source partition layout. If the data is skewed or only few number of partitions, it cannot be rebalanced properly.
Please refer the figure in #1158 (comment)
If the source dataframe consists of all same key=1 and 10GB of 1 partition, it cannot be split.
e.g. df.repartition(1, col("key")).select(col("key")).write.format("delta").parquet("path")
I removed redundant repartition execution plan on top of the child plan, but it cannot cover all the cases.
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.
The smallest unit of splitting is the single task map output for a single reducer ID right? That wasn't what I was seeing, where I had all my map tasks shuffle write < 1 GB, but I had some reducer tasks reading > 10 GB of shuffle data. After digging through how map output sizes work a little bit, I'm gonna try again and see if this is some weird side effect of HighlyCompressedMapStatus
for large numbers of reducing partitions (> 2000 by default). Only thought is some weird effect of a lot of small blocks being "averaged out" to compute map output sizes per reducer.
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.
That appeared to be it. I dropped my shuffle partitions to 1k and it behaved as I would expect. Not sure how common my case would be with particular types of data skew, but maybe it would be good to log a warning if your shuffle partitions exceeds the threshold for using HighlyCompressedMapStatus
, it can limit the ability to properly split skewed partitions, because average map output size is used for individual map reducer outputs, instead of the real value.
Gentle ping on this again, just started using this in our production environment and would be great not to have to maintain my own Delta fork 😅 |
Please let me know if someone is ready to review. I'll rebase the PR then. |
any update on this PR? |
Any update on this PR?. |
Hi @tdas @scottsand-db Any update? So no plan to deliver this feature to OSS delta? |
I am using your PR for optimize write in my EMR streaming use cases. I want that optimize write should create around 128m parquet files but it is topping at 64m. Configuration I am using is as below 2023-09-22 08:22:54 61.9 MiB part-00000-eb8c145c-7ac4-420c-8b85-c6fd58861e40.c000.snappy.parquet Any suggestions? |
@adityakumar84 you can try to increase the binSize config. It's used for row format size in memory. |
Description
Support OptimizeWrite described in https://docs.databricks.com/delta/optimizations/auto-optimize.html#how-optimized-writes-work
Fixes #1158
If OptimizeWrite is enabled, inject
OptimizeWriteExchangeExec
on top of the write plan and removeShuffleExchangeExec
orCoalesceExchange
operation at the top of the plan to avoid unnecessary shuffle / stage.In OptimizeWriteExchangeExec,
RoundRobinPartitining
for non partitioned data,HashPartitioning
for partitioned data.spark.sql.shuffle.partitions
for partitioning. We can introduce a new config likespark.sql.adaptive.coalescePartitions.initialPartitionNum
if needed.CoalescedPartitionSpec
)PartialReducerPartitionSpec
)spark.databricks.delta.optimizeWrite.binSize
(default: 128MB)How to enable
Ref: https://docs.databricks.com/delta/optimizations/auto-optimize.html#enable-auto-optimize
We can enable OptimizeWrite using Spark session config or table property.
spark.databricks.delta.optimizeWrite.enabled
= truedelta.autoOptimize.optimizeWrite
= trueSpark session config is prior to the table property.
How was this patch tested?
Unit tests (+ more tests will be added)
Does this PR introduce any user-facing changes?
Yes, support OptimizeWrite