Skip to content

Commit 6f1ec0c

Browse files
jlopezmallapbedia-stratio
authored andcommitted
add timeout for HDFS config files download (apache#79)
1 parent ce58026 commit 6f1ec0c

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

core/src/main/scala/org/apache/spark/security/HDFSConfig.scala

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,48 @@ import org.apache.spark.internal.Logging
2424

2525
object HDFSConfig extends Logging{
2626

27-
private def downloadFile(url: String, fileToDownload: String, outputhPath: String) {
27+
private def downloadFile(url: String,
28+
fileToDownload: String,
29+
outputhPath: String,
30+
connectTimeout: Int,
31+
readTimeout: Int) {
2832
logInfo(s"extracting $url/$fileToDownload")
2933
new File(outputhPath).mkdirs
30-
val src = scala.io.Source.fromURL(s"$url/$fileToDownload")
34+
val src = get(s"$url/$fileToDownload", connectTimeout, readTimeout).mkString("")
3135
val downloadFile = Files.createFile(Paths.get(s"$outputhPath/$fileToDownload"),
3236
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------")))
3337
downloadFile.toFile.deleteOnExit() // just to be sure
34-
Files.write(downloadFile, src.mkString("").getBytes)
38+
Files.write(downloadFile, src.getBytes)
3539
}
3640

41+
private def get(url: String,
42+
connectTimeout: Int,
43+
readTimeout: Int): String = {
44+
import java.net.{URL, HttpURLConnection}
45+
val requestMethod = "GET"
46+
val connection = (new URL(url)).openConnection.asInstanceOf[HttpURLConnection]
47+
connection.setConnectTimeout(connectTimeout)
48+
connection.setReadTimeout(readTimeout)
49+
connection.setRequestMethod(requestMethod)
50+
val inputStream = connection.getInputStream
51+
val content = scala.io.Source.fromInputStream(inputStream).mkString("")
52+
if (inputStream != null) inputStream.close
53+
content
54+
}
55+
56+
3757
def prepareEnviroment(options: Map[String, String]): Map[String, String] = {
3858
require(options.get("HDFS_CONF_URI").isDefined,
3959
"a proper HDFS URI must be configured to get Hadoop Configuration")
4060
require(sys.env.get("HADOOP_CONF_DIR").isDefined,
4161
"a proper Hadoop Conf Dir must be configured to store Hadoop Configuration")
62+
val connectTimeout: Int = options.get("HDFS_CONF_CONNECT_TIMEOUT").getOrElse("5000").toInt
63+
val readTimeout: Int = options.get("HDFS_CONF_READ_TIMEOUT").getOrElse("5000").toInt
4264
val hadoopConfUri = options.get("HDFS_CONF_URI").get
4365
val hadoopConfDir = sys.env.get("HADOOP_CONF_DIR").get
44-
downloadFile(hadoopConfUri, "core-site.xml", hadoopConfDir)
45-
downloadFile(hadoopConfUri, "hdfs-site.xml", hadoopConfDir)
46-
downloadFile(hadoopConfUri, "krb5.conf", "/etc/")
66+
downloadFile(hadoopConfUri, "core-site.xml", hadoopConfDir, connectTimeout, readTimeout)
67+
downloadFile(hadoopConfUri, "hdfs-site.xml", hadoopConfDir, connectTimeout, readTimeout)
68+
downloadFile(hadoopConfUri, "krb5.conf", "/etc/", connectTimeout, readTimeout)
4769
Map.empty[String, String]
4870
}
49-
}
71+
}

0 commit comments

Comments
 (0)