|
| 1 | +/* |
| 2 | + * Copyright (2025) The Delta Lake Project Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.delta.unity |
| 18 | + |
| 19 | +import java.lang.{Long => JLong} |
| 20 | +import java.net.URI |
| 21 | +import java.util.Optional |
| 22 | +import java.util.concurrent.ConcurrentHashMap |
| 23 | + |
| 24 | +import scala.collection.JavaConverters._ |
| 25 | +import scala.collection.mutable.ArrayBuffer |
| 26 | + |
| 27 | +import io.delta.storage.commit.{Commit, CommitFailedException, GetCommitsResponse} |
| 28 | +import io.delta.storage.commit.actions.{AbstractMetadata, AbstractProtocol} |
| 29 | +import io.delta.storage.commit.uccommitcoordinator.{InvalidTargetTableException, UCClient} |
| 30 | + |
| 31 | +object InMemoryUCClient { |
| 32 | + |
| 33 | + /** |
| 34 | + * Internal data structure to track table state including commits and version information. |
| 35 | + * |
| 36 | + * Thread Safety: All public methods are synchronized to ensure thread-safe access to the |
| 37 | + * internal mutable state. This class is designed to be safely accessed by multiple threads |
| 38 | + * concurrently. |
| 39 | + */ |
| 40 | + class TableData { |
| 41 | + private var maxRatifiedVersion = -1L |
| 42 | + private val commits: ArrayBuffer[Commit] = ArrayBuffer.empty |
| 43 | + |
| 44 | + /** @return the maximum ratified version, or -1 if no commits have been made. */ |
| 45 | + def getMaxRatifiedVersion: Long = synchronized { |
| 46 | + maxRatifiedVersion |
| 47 | + } |
| 48 | + |
| 49 | + /** @return An immutable list of all commits. */ |
| 50 | + def getCommits: List[Commit] = synchronized { commits.toList } |
| 51 | + |
| 52 | + /** @return commits filtered by version range. */ |
| 53 | + def getCommitsInRange( |
| 54 | + startVersion: Optional[JLong], |
| 55 | + endVersion: Optional[JLong]): List[Commit] = synchronized { |
| 56 | + commits |
| 57 | + .filter { commit => |
| 58 | + startVersion.orElse(0L) <= commit.getVersion && |
| 59 | + commit.getVersion <= endVersion.orElse(Long.MaxValue) |
| 60 | + } |
| 61 | + .toList |
| 62 | + } |
| 63 | + |
| 64 | + /** Appends a new commit to this table. */ |
| 65 | + def appendCommit(commit: Commit): Unit = synchronized { |
| 66 | + val expectedCommitVersion = maxRatifiedVersion + 1 |
| 67 | + |
| 68 | + if (commit.getVersion != expectedCommitVersion) { |
| 69 | + throw new CommitFailedException( |
| 70 | + false, /* retryable */ |
| 71 | + false, /* conflict */ |
| 72 | + s"Expected commit version $expectedCommitVersion but got ${commit.getVersion}") |
| 73 | + } |
| 74 | + |
| 75 | + commits += commit |
| 76 | + maxRatifiedVersion += 1 |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * In-memory Unity Catalog client implementation for testing. |
| 83 | + * |
| 84 | + * Provides a mock implementation of UCClient that stores all table data in memory. This is useful |
| 85 | + * for unit tests that need to simulate Unity Catalog operations without connecting to an actual UC |
| 86 | + * service. |
| 87 | + * |
| 88 | + * Thread Safety: This implementation is thread-safe for concurrent access. Multiple threads can |
| 89 | + * safely perform operations on different tables simultaneously. Operations on the same table are |
| 90 | + * internally synchronized by the [[TableData]] class. |
| 91 | + */ |
| 92 | +class InMemoryUCClient(ucMetastoreId: String) extends UCClient { |
| 93 | + |
| 94 | + import InMemoryUCClient._ |
| 95 | + |
| 96 | + /** Map from UC_TABLE_ID to TABLE_DATA */ |
| 97 | + private val tables = new ConcurrentHashMap[String, TableData]() |
| 98 | + |
| 99 | + override def getMetastoreId: String = ucMetastoreId |
| 100 | + |
| 101 | + /** Convenience method for tests to commit with default parameters. */ |
| 102 | + def commitWithDefaults( |
| 103 | + tableId: String, |
| 104 | + tableUri: URI, |
| 105 | + commit: Optional[Commit], |
| 106 | + lastKnownBackfilledVersion: Optional[JLong] = Optional.empty(), |
| 107 | + disown: Boolean = false, |
| 108 | + newMetadata: Optional[AbstractMetadata] = Optional.empty(), |
| 109 | + newProtocol: Optional[AbstractProtocol] = Optional.empty()): Unit = { |
| 110 | + this.commit( |
| 111 | + tableId, |
| 112 | + tableUri, |
| 113 | + commit, |
| 114 | + lastKnownBackfilledVersion, |
| 115 | + disown, |
| 116 | + newMetadata, |
| 117 | + newProtocol) |
| 118 | + } |
| 119 | + |
| 120 | + override def commit( |
| 121 | + tableId: String, |
| 122 | + tableUri: URI, |
| 123 | + commit: Optional[Commit] = Optional.empty(), |
| 124 | + lastKnownBackfilledVersion: Optional[JLong], |
| 125 | + disown: Boolean, |
| 126 | + newMetadata: Optional[AbstractMetadata], |
| 127 | + newProtocol: Optional[AbstractProtocol]): Unit = { |
| 128 | + Seq( |
| 129 | + (lastKnownBackfilledVersion.isPresent, "lastKnownBackfilledVersion"), |
| 130 | + (disown, "disown"), |
| 131 | + (newMetadata.isPresent, "newMetadata"), |
| 132 | + (newProtocol.isPresent, "newProtocol")).foreach { case (isUnsupported, name) => |
| 133 | + if (isUnsupported) { |
| 134 | + throw new UnsupportedOperationException(s"$name not supported yet in InMemoryUCClient") |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if (!commit.isPresent) return |
| 139 | + |
| 140 | + getOrCreateTableIfNotExists(tableId).appendCommit(commit.get()) |
| 141 | + } |
| 142 | + |
| 143 | + override def getCommits( |
| 144 | + tableId: String, |
| 145 | + tableUri: URI, |
| 146 | + startVersion: Optional[JLong], |
| 147 | + endVersion: Optional[JLong]): GetCommitsResponse = { |
| 148 | + val tableData = getTableDataElseThrow(tableId) |
| 149 | + val filteredCommits = tableData.getCommitsInRange(startVersion, endVersion) |
| 150 | + new GetCommitsResponse(filteredCommits.asJava, tableData.getMaxRatifiedVersion) |
| 151 | + } |
| 152 | + |
| 153 | + override def close(): Unit = {} |
| 154 | + |
| 155 | + private[unity] def getTablesCopy: Map[String, TableData] = { |
| 156 | + tables.asScala.toMap |
| 157 | + } |
| 158 | + |
| 159 | + /** Retrieves the table data for the given table ID, creating it if it does not exist. */ |
| 160 | + private def getOrCreateTableIfNotExists(tableId: String): TableData = { |
| 161 | + tables.computeIfAbsent(tableId, _ => new TableData) |
| 162 | + } |
| 163 | + |
| 164 | + /** Retrieves table data for the given table ID or throws an exception if not found. */ |
| 165 | + private def getTableDataElseThrow(tableId: String): TableData = { |
| 166 | + Option(tables.get(tableId)) |
| 167 | + .getOrElse(throw new InvalidTargetTableException(s"Table not found: $tableId")) |
| 168 | + } |
| 169 | +} |
0 commit comments