Skip to content

Commit 314afd0

Browse files
yhuailiancheng
authored andcommitted
[SPARK-6618][SQL] HiveMetastoreCatalog.lookupRelation should use fine-grained lock
JIRA: https://issues.apache.org/jira/browse/SPARK-6618 Author: Yin Huai <yhuai@databricks.com> Closes apache#5281 from yhuai/lookupRelationLock and squashes the following commits: 591b4be [Yin Huai] A test? b3a9625 [Yin Huai] Just protect client.
1 parent b80a030 commit 314afd0

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveMetastoreCatalog.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,16 @@ private[hive] class HiveMetastoreCatalog(hive: HiveContext) extends Catalog with
172172

173173
def lookupRelation(
174174
tableIdentifier: Seq[String],
175-
alias: Option[String]): LogicalPlan = synchronized {
175+
alias: Option[String]): LogicalPlan = {
176176
val tableIdent = processTableIdentifier(tableIdentifier)
177177
val databaseName = tableIdent.lift(tableIdent.size - 2).getOrElse(
178178
hive.sessionState.getCurrentDatabase)
179179
val tblName = tableIdent.last
180-
val table = try client.getTable(databaseName, tblName) catch {
180+
val table = try {
181+
synchronized {
182+
client.getTable(databaseName, tblName)
183+
}
184+
} catch {
181185
case te: org.apache.hadoop.hive.ql.metadata.InvalidTableException =>
182186
throw new NoSuchTableException
183187
}
@@ -199,7 +203,9 @@ private[hive] class HiveMetastoreCatalog(hive: HiveContext) extends Catalog with
199203
} else {
200204
val partitions: Seq[Partition] =
201205
if (table.isPartitioned) {
202-
HiveShim.getAllPartitionsOf(client, table).toSeq
206+
synchronized {
207+
HiveShim.getAllPartitionsOf(client, table).toSeq
208+
}
203209
} else {
204210
Nil
205211
}

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/SQLQuerySuite.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,4 +433,15 @@ class SQLQuerySuite extends QueryTest {
433433
dropTempTable("data")
434434
setConf("spark.sql.hive.convertCTAS", originalConf)
435435
}
436+
437+
test("sanity test for SPARK-6618") {
438+
(1 to 100).par.map { i =>
439+
val tableName = s"SPARK_6618_table_$i"
440+
sql(s"CREATE TABLE $tableName (col1 string)")
441+
catalog.lookupRelation(Seq(tableName))
442+
table(tableName)
443+
tables()
444+
sql(s"DROP TABLE $tableName")
445+
}
446+
}
436447
}

0 commit comments

Comments
 (0)