|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.kyuubi.spark.connector.hive.read |
| 19 | + |
| 20 | +import java.util.concurrent.TimeUnit |
| 21 | +import java.util.concurrent.atomic.AtomicBoolean |
| 22 | + |
| 23 | +import scala.collection.JavaConverters._ |
| 24 | + |
| 25 | +import com.google.common.cache._ |
| 26 | +import org.apache.hadoop.fs.{FileStatus, Path} |
| 27 | +import org.apache.spark.internal.Logging |
| 28 | +import org.apache.spark.sql.SparkSession |
| 29 | +import org.apache.spark.sql.execution.datasources.{FileStatusCache, NoopCache} |
| 30 | +import org.apache.spark.util.SizeEstimator |
| 31 | + |
| 32 | +/** |
| 33 | + * Use [[HiveFileStatusCache.getOrCreate()]] to construct a globally shared file status cache. |
| 34 | + */ |
| 35 | +object HiveFileStatusCache { |
| 36 | + private var sharedCache: HiveSharedInMemoryCache = _ |
| 37 | + |
| 38 | + /** |
| 39 | + * @return a new FileStatusCache based on session configuration. Cache memory quota is |
| 40 | + * shared across all clients. |
| 41 | + */ |
| 42 | + def getOrCreate(session: SparkSession, qualifiedName: String): FileStatusCache = |
| 43 | + synchronized { |
| 44 | + if (session.sessionState.conf.manageFilesourcePartitions && |
| 45 | + session.sessionState.conf.filesourcePartitionFileCacheSize > 0) { |
| 46 | + if (sharedCache == null) { |
| 47 | + sharedCache = new HiveSharedInMemoryCache( |
| 48 | + session.sessionState.conf.filesourcePartitionFileCacheSize, |
| 49 | + session.sessionState.conf.metadataCacheTTL) |
| 50 | + } |
| 51 | + sharedCache.createForNewClient(qualifiedName) |
| 52 | + } else { |
| 53 | + NoopCache |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + def resetForTesting(): Unit = synchronized { |
| 58 | + sharedCache = null |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * An implementation that caches partition file statuses in memory. |
| 64 | + * |
| 65 | + * @param maxSizeInBytes max allowable cache size before entries start getting evicted |
| 66 | + */ |
| 67 | +private class HiveSharedInMemoryCache(maxSizeInBytes: Long, cacheTTL: Long) extends Logging { |
| 68 | + |
| 69 | + // Opaque object that uniquely identifies a shared cache user |
| 70 | + private type ClientId = Object |
| 71 | + |
| 72 | + private val warnedAboutEviction = new AtomicBoolean(false) |
| 73 | + |
| 74 | + // we use a composite cache key in order to distinguish entries inserted by different clients |
| 75 | + private val cache: Cache[(ClientId, Path), Array[FileStatus]] = { |
| 76 | + // [[Weigher]].weigh returns Int so we could only cache objects < 2GB |
| 77 | + // instead, the weight is divided by this factor (which is smaller |
| 78 | + // than the size of one [[FileStatus]]). |
| 79 | + // so it will support objects up to 64GB in size. |
| 80 | + val weightScale = 32 |
| 81 | + val weigher = new Weigher[(ClientId, Path), Array[FileStatus]] { |
| 82 | + override def weigh(key: (ClientId, Path), value: Array[FileStatus]): Int = { |
| 83 | + val estimate = (SizeEstimator.estimate(key) + SizeEstimator.estimate(value)) / weightScale |
| 84 | + if (estimate > Int.MaxValue) { |
| 85 | + logWarning(s"Cached table partition metadata size is too big. Approximating to " + |
| 86 | + s"${Int.MaxValue.toLong * weightScale}.") |
| 87 | + Int.MaxValue |
| 88 | + } else { |
| 89 | + estimate.toInt |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + val removalListener = new RemovalListener[(ClientId, Path), Array[FileStatus]]() { |
| 94 | + override def onRemoval( |
| 95 | + removed: RemovalNotification[(ClientId, Path), Array[FileStatus]]): Unit = { |
| 96 | + if (removed.getCause == RemovalCause.SIZE && |
| 97 | + warnedAboutEviction.compareAndSet(false, true)) { |
| 98 | + logWarning( |
| 99 | + "Evicting cached table partition metadata from memory due to size constraints " + |
| 100 | + "(spark.sql.hive.filesourcePartitionFileCacheSize = " |
| 101 | + + maxSizeInBytes + " bytes). This may impact query planning performance.") |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + var builder = CacheBuilder.newBuilder() |
| 107 | + .weigher(weigher) |
| 108 | + .removalListener(removalListener) |
| 109 | + .maximumWeight(maxSizeInBytes / weightScale) |
| 110 | + |
| 111 | + if (cacheTTL > 0) { |
| 112 | + builder = builder.expireAfterWrite(cacheTTL, TimeUnit.SECONDS) |
| 113 | + } |
| 114 | + |
| 115 | + builder.build[(ClientId, Path), Array[FileStatus]]() |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @return a FileStatusCache that does not share any entries with any other client, but does |
| 120 | + * share memory resources for the purpose of cache eviction. |
| 121 | + */ |
| 122 | + def createForNewClient(clientId: Object): HiveFileStatusCache = new HiveFileStatusCache { |
| 123 | + |
| 124 | + override def getLeafFiles(path: Path): Option[Array[FileStatus]] = { |
| 125 | + Option(cache.getIfPresent((clientId, path))) |
| 126 | + } |
| 127 | + |
| 128 | + override def putLeafFiles(path: Path, leafFiles: Array[FileStatus]): Unit = { |
| 129 | + cache.put((clientId, path), leafFiles) |
| 130 | + } |
| 131 | + |
| 132 | + override def invalidateAll(): Unit = { |
| 133 | + cache.asMap.asScala.foreach { case (key, value) => |
| 134 | + if (key._1 == clientId) { |
| 135 | + cache.invalidate(key) |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + abstract class HiveFileStatusCache extends FileStatusCache {} |
| 142 | +} |
0 commit comments