-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Spark] Fix O(n^2) issue in find last complete checkpoint before (#3060)
<!-- Thanks for sending a pull request! Here are some tips for you: 1. If this is your first time, please read our contributor guidelines: https://github.com/delta-io/delta/blob/master/CONTRIBUTING.md 2. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP] Your PR title ...'. 3. Be sure to keep the PR description updated to reflect all changes. 4. Please write your PR title to summarize what this PR proposes. 5. If possible, provide a concise example to reproduce the issue for a faster review. 6. If applicable, include the corresponding issue number in the PR title and link it in the body. --> #### Which Delta project/connector is this regarding? - [x] Spark - [ ] Standalone - [ ] Flink - [ ] Kernel - [ ] Other (fill in here) ## Description This PR fixes an O(n^2) issue in `find last complete checkpoint before` method. Today the `findLastCompleteCheckpointBefore` tries to find the last checkpoint before a given version. In order to do this, it do following: findLastCompleteCheckpointBefore(10000): 1. List from 9000 2. List from 8000 3. List from 7000 ... Each of these listing today lists to the end as they completely ignore delta files and try to list with takeWhile with version clause: ``` listFrom(..) .filter { file => isCheckpointFile(file) && file.getLen != 0 } .map{ file => CheckpointInstance(file.getPath) } .takeWhile(tv => (cur == 0 || tv.version <= cur) && tv < upperBoundCv) ``` This PR tries to fix this issue by terminating each listing early by checking if we have crossed a deltaFile for untilVersion. In addition to this, we also optimize how much to list in each iteration. E.g. After this PR, findLastCompleteCheckpointBefore(10000) will need: 1. Iteration-1 lists from 9000 to 10000. 2. Iteration-2 lists from 8000 to 9000. 3. Iteration-3 lists from 7000 to 8000. 4. and so on... ## How was this patch tested? UT
- Loading branch information
1 parent
3baeb99
commit d0bd28e
Showing
4 changed files
with
518 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
spark/src/main/scala/org/apache/spark/sql/delta/util/DeltaLogGroupingIterator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (2021) The Delta Lake Project Authors. | ||
* | ||
* Licensed 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.spark.sql.delta.util | ||
|
||
import scala.collection.mutable.ArrayBuffer | ||
|
||
import org.apache.spark.sql.delta.util.FileNames.{CheckpointFile, DeltaFile} | ||
import org.apache.hadoop.fs.FileStatus | ||
|
||
/** | ||
* An iterator that groups same types of files by version. | ||
* Note that this class could handle only Checkpoints and Delta files. | ||
* For example for an input iterator: | ||
* - 11.checkpoint.0.1.parquet | ||
* - 11.checkpoint.1.1.parquet | ||
* - 11.json | ||
* - 12.checkpoint.parquet | ||
* - 12.json | ||
* - 13.json | ||
* - 14.json | ||
* - 15.checkpoint.0.1.parquet | ||
* - 15.checkpoint.1.1.parquet | ||
* - 15.checkpoint.<uuid>.parquet | ||
* - 15.json | ||
* This will return: | ||
* - (11, Seq(11.checkpoint.0.1.parquet, 11.checkpoint.1.1.parquet, 11.json)) | ||
* - (12, Seq(12.checkpoint.parquet, 12.json)) | ||
* - (13, Seq(13.json)) | ||
* - (14, Seq(14.json)) | ||
* - (15, Seq(15.checkpoint.0.1.parquet, 15.checkpoint.1.1.parquet, 15.checkpoint.<uuid>.parquet, | ||
* 15.json)) | ||
*/ | ||
class DeltaLogGroupingIterator( | ||
checkpointAndDeltas: Iterator[FileStatus]) extends Iterator[(Long, ArrayBuffer[FileStatus])] { | ||
|
||
private val bufferedIterator = checkpointAndDeltas.buffered | ||
|
||
/** | ||
* Validates that the underlying file is a checkpoint/delta file and returns the corresponding | ||
* version. | ||
*/ | ||
private def getFileVersion(file: FileStatus): Long = { | ||
file match { | ||
case DeltaFile(_, version) => version | ||
case CheckpointFile(_, version) => version | ||
case _ => | ||
throw new IllegalStateException( | ||
s"${file.getPath} is not a valid commit file / checkpoint file") | ||
} | ||
} | ||
|
||
override def hasNext: Boolean = bufferedIterator.hasNext | ||
|
||
override def next(): (Long, ArrayBuffer[FileStatus]) = { | ||
val first = bufferedIterator.next() | ||
val buffer = scala.collection.mutable.ArrayBuffer(first) | ||
val firstFileVersion = getFileVersion(first) | ||
while (bufferedIterator.headOption.exists(getFileVersion(_) == firstFileVersion)) { | ||
buffer += bufferedIterator.next() | ||
} | ||
firstFileVersion -> buffer | ||
} | ||
} |
Oops, something went wrong.