-
Notifications
You must be signed in to change notification settings - Fork 966
[KYUUBI #7192] Fix filestatus not cached #7191
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
Open
flaming-archer
wants to merge
10
commits into
apache:master
Choose a base branch
from
flaming-archer:master_cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+524
−23
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1da81f2
cached fileindex
flaming-archer 9353f0c
fix failed tests
flaming-archer 9f2168c
Improve the lifecycle management of cache
92cbdd9
review change and format code
flaming-archer c43d9c5
format code
flaming-archer 2488479
fix code style
flaming-archer ad428cb
fix failed tests
flaming-archer 53d91c0
Merge branch 'master' of https://github.com/apache/kyuubi into master…
flaming-archer d4f4d14
fix failed ut
flaming-archer b5aaec0
fix altertable catalogTable copy not support spark v4.0
flaming-archer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
157 changes: 157 additions & 0 deletions
157
...hive/src/main/scala/org/apache/kyuubi/spark/connector/hive/read/HiveFileStatusCache.scala
This file contains hidden or 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,157 @@ | ||
/* | ||
* 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.spark.connector.hive.read | ||
|
||
import java.util.concurrent.TimeUnit | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
import com.google.common.cache._ | ||
import org.apache.hadoop.fs.{FileStatus, Path} | ||
import org.apache.spark.internal.Logging | ||
import org.apache.spark.sql.SparkSession | ||
import org.apache.spark.sql.execution.datasources.{FileStatusCache, NoopCache} | ||
import org.apache.spark.util.SizeEstimator | ||
|
||
import org.apache.kyuubi.spark.connector.hive.KyuubiHiveConnectorConf.HIVE_FILE_STATUS_CACHE_SCOPE | ||
|
||
/** | ||
* Forked from Apache Spark's [[org.apache.spark.sql.execution.datasources.FileStatusCache]] 3.5.5. | ||
* | ||
* Because the original FileStatusCache cannot take effect (see https://github.com/apache/kyuubi | ||
* /issues/7192). | ||
* | ||
* The main modification point is that at the globally level, the cache key is the qualified name | ||
* of the table (in the form of `catalog.database.table`) + path. The previous key was an | ||
* object + path generated during initialization, and the current scenario is that FileStatusCache | ||
* is not preserved by the outside, resulting in different keys and ineffective caching. | ||
* | ||
* Use [[HiveFileStatusCache.getOrCreate()]] to construct a globe/none shared file status cache. | ||
*/ | ||
object HiveFileStatusCache { | ||
private var sharedCache: HiveSharedInMemoryCache = _ | ||
|
||
/** | ||
* @return a new FileStatusCache based on session configuration. Cache memory quota is | ||
* shared across all clients. | ||
*/ | ||
def getOrCreate(session: SparkSession, qualifiedName: String): FileStatusCache = | ||
synchronized { | ||
val conf = session.sessionState.conf | ||
if (conf.manageFilesourcePartitions && conf.filesourcePartitionFileCacheSize > 0) { | ||
if (sharedCache == null) { | ||
sharedCache = new HiveSharedInMemoryCache( | ||
session.sessionState.conf.filesourcePartitionFileCacheSize, | ||
session.sessionState.conf.metadataCacheTTL) | ||
} | ||
conf.getConf(HIVE_FILE_STATUS_CACHE_SCOPE) match { | ||
case "GLOBE" => sharedCache.createForNewClient(qualifiedName) | ||
case "NONE" => NoopCache | ||
} | ||
} else { | ||
NoopCache | ||
} | ||
} | ||
|
||
def resetForTesting(): Unit = synchronized { | ||
sharedCache = null | ||
} | ||
} | ||
|
||
/** | ||
* An implementation that caches partition file statuses in memory. | ||
* | ||
* @param maxSizeInBytes max allowable cache size before entries start getting evicted | ||
*/ | ||
private class HiveSharedInMemoryCache(maxSizeInBytes: Long, cacheTTL: Long) extends Logging { | ||
|
||
// Opaque object that uniquely identifies a shared cache user | ||
private type ClientId = Object | ||
|
||
private val warnedAboutEviction = new AtomicBoolean(false) | ||
|
||
// we use a composite cache key in order to distinguish entries inserted by different clients | ||
private val cache: Cache[(ClientId, Path), Array[FileStatus]] = { | ||
// [[Weigher]].weigh returns Int so we could only cache objects < 2GB | ||
// instead, the weight is divided by this factor (which is smaller | ||
// than the size of one [[FileStatus]]). | ||
// so it will support objects up to 64GB in size. | ||
val weightScale = 32 | ||
val weigher = new Weigher[(ClientId, Path), Array[FileStatus]] { | ||
override def weigh(key: (ClientId, Path), value: Array[FileStatus]): Int = { | ||
val estimate = (SizeEstimator.estimate(key) + SizeEstimator.estimate(value)) / weightScale | ||
if (estimate > Int.MaxValue) { | ||
logWarning(s"Cached table partition metadata size is too big. Approximating to " + | ||
s"${Int.MaxValue.toLong * weightScale}.") | ||
Int.MaxValue | ||
} else { | ||
estimate.toInt | ||
} | ||
} | ||
} | ||
val removalListener = new RemovalListener[(ClientId, Path), Array[FileStatus]]() { | ||
override def onRemoval( | ||
removed: RemovalNotification[(ClientId, Path), Array[FileStatus]]): Unit = { | ||
if (removed.getCause == RemovalCause.SIZE && | ||
warnedAboutEviction.compareAndSet(false, true)) { | ||
logWarning( | ||
"Evicting cached table partition metadata from memory due to size constraints " + | ||
"(spark.sql.hive.filesourcePartitionFileCacheSize = " | ||
+ maxSizeInBytes + " bytes). This may impact query planning performance.") | ||
} | ||
} | ||
} | ||
|
||
var builder = CacheBuilder.newBuilder() | ||
.weigher(weigher) | ||
.removalListener(removalListener) | ||
.maximumWeight(maxSizeInBytes / weightScale) | ||
|
||
if (cacheTTL > 0) { | ||
builder = builder.expireAfterWrite(cacheTTL, TimeUnit.SECONDS) | ||
} | ||
|
||
builder.build[(ClientId, Path), Array[FileStatus]]() | ||
} | ||
|
||
/** | ||
* @return a FileStatusCache that does not share any entries with any other client, but does | ||
* share memory resources for the purpose of cache eviction. | ||
*/ | ||
def createForNewClient(clientId: Object): HiveFileStatusCache = new HiveFileStatusCache { | ||
|
||
override def getLeafFiles(path: Path): Option[Array[FileStatus]] = { | ||
Option(cache.getIfPresent((clientId, path))) | ||
} | ||
|
||
override def putLeafFiles(path: Path, leafFiles: Array[FileStatus]): Unit = { | ||
cache.put((clientId, path), leafFiles) | ||
} | ||
|
||
override def invalidateAll(): Unit = { | ||
cache.asMap.asScala.foreach { case (key, value) => | ||
if (key._1 == clientId) { | ||
cache.invalidate(key) | ||
} | ||
} | ||
} | ||
} | ||
|
||
abstract class HiveFileStatusCache extends FileStatusCache {} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.