-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
[SPARK-3809][SQL]fix HiveThriftServer2Suite to make it work correctly #2671
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import scala.collection.mutable.ArrayBuffer | |
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.duration._ | ||
import scala.concurrent.{Await, Future, Promise} | ||
import scala.io.Source | ||
import scala.sys.process.{Process, ProcessLogger} | ||
|
||
import java.io.File | ||
|
@@ -30,19 +31,19 @@ import java.util.concurrent.TimeoutException | |
|
||
import org.apache.hadoop.hive.conf.HiveConf.ConfVars | ||
import org.apache.hive.jdbc.HiveDriver | ||
import org.scalatest.FunSuite | ||
import org.scalatest.{BeforeAndAfterAll, FunSuite} | ||
|
||
import org.apache.spark.Logging | ||
import org.apache.spark.sql.catalyst.util.getTempFilePath | ||
import org.apache.spark.sql.catalyst.util._ | ||
|
||
/** | ||
* Tests for the HiveThriftServer2 using JDBC. | ||
*/ | ||
class HiveThriftServer2Suite extends FunSuite with Logging { | ||
class HiveThriftServer2Suite extends FunSuite with BeforeAndAfterAll with Logging { | ||
Class.forName(classOf[HiveDriver].getCanonicalName) | ||
|
||
private val listeningHost = "localhost" | ||
private val listeningPort = { | ||
private val listeningPort = { | ||
// Let the system to choose a random available port to avoid collision with other parallel | ||
// builds. | ||
val socket = new ServerSocket(0) | ||
|
@@ -54,10 +55,12 @@ class HiveThriftServer2Suite extends FunSuite with Logging { | |
private val warehousePath = getTempFilePath("warehouse") | ||
private val metastorePath = getTempFilePath("metastore") | ||
private val metastoreJdbcUri = s"jdbc:derby:;databaseName=$metastorePath;create=true" | ||
val jdbcUri = s"jdbc:hive2://$listeningHost:$listeningPort/" | ||
val user = System.getProperty("user.name") | ||
|
||
def startThriftServerWithin(timeout: FiniteDuration = 30.seconds)(f: Statement => Unit) { | ||
override def beforeAll(): Unit = { | ||
val timeout: FiniteDuration = 30.seconds | ||
val serverScript = "../../sbin/start-thriftserver.sh".split("/").mkString(File.separator) | ||
|
||
val command = | ||
s"""$serverScript | ||
| --master local | ||
|
@@ -70,37 +73,39 @@ class HiveThriftServer2Suite extends FunSuite with Logging { | |
|
||
val serverStarted = Promise[Unit]() | ||
val buffer = new ArrayBuffer[String]() | ||
val startString = | ||
"starting org.apache.spark.sql.hive.thriftserver.HiveThriftServer2, logging to " | ||
val maxTries = 30 | ||
|
||
def captureOutput(source: String)(line: String) { | ||
buffer += s"$source> $line" | ||
if (line.contains("ThriftBinaryCLIService listening on")) { | ||
serverStarted.success(()) | ||
if (line.contains(startString)) { | ||
val logFile = new File(line.substring(startString.length)) | ||
var tryNum = 0 | ||
// This is a hack to wait logFile ready | ||
Thread.sleep(5000) | ||
// logFile may have not finished, try every second | ||
while (!logFile.exists() || (!fileToString(logFile).contains( | ||
"ThriftBinaryCLIService listening on") && tryNum < maxTries)) { | ||
Thread.sleep(1000) | ||
} | ||
if (fileToString(logFile).contains("ThriftBinaryCLIService listening on")) { | ||
serverStarted.success(()) | ||
} else { | ||
throw new TimeoutException() | ||
} | ||
} | ||
} | ||
|
||
val process = Process(command).run( | ||
ProcessLogger(captureOutput("stdout"), captureOutput("stderr"))) | ||
|
||
Future { | ||
val exitValue = process.exitValue() | ||
logInfo(s"Spark SQL Thrift server process exit value: $exitValue") | ||
logInfo(s"Start Spark SQL Thrift server process exit value: $exitValue") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why "Start" here? When this line is executed, the server process has already ended. |
||
} | ||
|
||
val jdbcUri = s"jdbc:hive2://$listeningHost:$listeningPort/" | ||
val user = System.getProperty("user.name") | ||
|
||
try { | ||
Await.result(serverStarted.future, timeout) | ||
|
||
val connection = DriverManager.getConnection(jdbcUri, user, "") | ||
val statement = connection.createStatement() | ||
|
||
try { | ||
f(statement) | ||
} finally { | ||
statement.close() | ||
connection.close() | ||
} | ||
} catch { | ||
case cause: Exception => | ||
cause match { | ||
|
@@ -123,14 +128,45 @@ class HiveThriftServer2Suite extends FunSuite with Logging { | |
|========================================= | ||
""".stripMargin, cause) | ||
} finally { | ||
warehousePath.delete() | ||
metastorePath.delete() | ||
process.destroy() | ||
} | ||
} | ||
|
||
override def afterAll() { | ||
warehousePath.delete() | ||
metastorePath.delete() | ||
stopThriftserver | ||
} | ||
|
||
def stopThriftserver: Unit = { | ||
val stopScript = "../../sbin/stop-thriftserver.sh".split("/").mkString(File.separator) | ||
val builder = new ProcessBuilder(stopScript) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Scala process API can be much simpler :) |
||
val process = builder.start() | ||
new Thread("read stderr") { | ||
override def run() { | ||
for (line <- Source.fromInputStream(process.getErrorStream).getLines()) { | ||
System.err.println(line) | ||
} | ||
} | ||
}.start() | ||
val output = new StringBuffer | ||
val stdoutThread = new Thread("read stdout") { | ||
override def run() { | ||
for (line <- Source.fromInputStream(process.getInputStream).getLines()) { | ||
output.append(line) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} | ||
} | ||
stdoutThread.start() | ||
val exitValue = process.waitFor() | ||
logInfo(s"Stop Spark SQL Thrift server process exit value: $exitValue") | ||
} | ||
|
||
test("Test JDBC query execution") { | ||
startThriftServerWithin() { statement => | ||
val connection = DriverManager.getConnection(jdbcUri, user, "") | ||
val statement = connection.createStatement() | ||
|
||
try { | ||
val dataFilePath = | ||
Thread.currentThread().getContextClassLoader.getResource("data/files/small_kv.txt") | ||
|
||
|
@@ -146,11 +182,17 @@ class HiveThriftServer2Suite extends FunSuite with Logging { | |
resultSet.next() | ||
resultSet.getInt(1) | ||
} | ||
} finally { | ||
statement.close() | ||
connection.close() | ||
} | ||
} | ||
|
||
test("SPARK-3004 regression: result set containing NULL") { | ||
startThriftServerWithin() { statement => | ||
val connection = DriverManager.getConnection(jdbcUri, user, "") | ||
val statement = connection.createStatement() | ||
|
||
try { | ||
val dataFilePath = | ||
Thread.currentThread().getContextClassLoader.getResource( | ||
"data/files/small_kv_with_null.txt") | ||
|
@@ -169,8 +211,10 @@ class HiveThriftServer2Suite extends FunSuite with Logging { | |
assert(resultSet.getInt(1) === 0) | ||
assert(resultSet.wasNull()) | ||
} | ||
|
||
assert(!resultSet.next()) | ||
} finally { | ||
statement.close() | ||
connection.close() | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tryNum
is never increased.