0001 #
0002 # Licensed to the Apache Software Foundation (ASF) under one or more
0003 # contributor license agreements. See the NOTICE file distributed with
0004 # this work for additional information regarding copyright ownership.
0005 # The ASF licenses this file to You under the Apache License, Version 2.0
0006 # (the "License"); you may not use this file except in compliance with
0007 # the License. You may obtain a copy of the License at
0008 #
0009 # http://www.apache.org/licenses/LICENSE-2.0
0010 #
0011 # Unless required by applicable law or agreed to in writing, software
0012 # distributed under the License is distributed on an "AS IS" BASIS,
0013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014 # See the License for the specific language governing permissions and
0015 # limitations under the License.
0016 #
0017
0018 # Client code to connect to SparkRBackend
0019
0020 # Creates a SparkR client connection object
0021 # if one doesn't already exist
0022 connectBackend <- function(hostname, port, timeout, authSecret) {
0023 if (exists(".sparkRcon", envir = .sparkREnv)) {
0024 if (isOpen(.sparkREnv[[".sparkRCon"]])) {
0025 cat("SparkRBackend client connection already exists\n")
0026 return(get(".sparkRcon", envir = .sparkREnv))
0027 }
0028 }
0029
0030 con <- socketConnection(host = hostname, port = port, server = FALSE,
0031 blocking = TRUE, open = "wb", timeout = timeout)
0032 doServerAuth(con, authSecret)
0033 assign(".sparkRCon", con, envir = .sparkREnv)
0034 con
0035 }
0036
0037 determineSparkSubmitBin <- function() {
0038 if (.Platform$OS.type == "unix") {
0039 sparkSubmitBinName <- "spark-submit"
0040 } else {
0041 sparkSubmitBinName <- "spark-submit2.cmd"
0042 }
0043 sparkSubmitBinName
0044 }
0045
0046 generateSparkSubmitArgs <- function(args, sparkHome, jars, sparkSubmitOpts, packages) {
0047 jars <- paste0(jars, collapse = ",")
0048 if (jars != "") {
0049 # construct the jars argument with a space between --jars and comma-separated values
0050 jars <- paste0("--jars ", jars)
0051 }
0052
0053 packages <- paste0(packages, collapse = ",")
0054 if (packages != "") {
0055 # construct the packages argument with a space between --packages and comma-separated values
0056 packages <- paste0("--packages ", packages)
0057 }
0058
0059 combinedArgs <- paste(jars, packages, sparkSubmitOpts, args, sep = " ")
0060 combinedArgs
0061 }
0062
0063 checkJavaVersion <- function() {
0064 javaBin <- "java"
0065 javaHome <- Sys.getenv("JAVA_HOME")
0066 javaReqs <- utils::packageDescription(utils::packageName(), fields = c("SystemRequirements"))
0067 sparkJavaVersions <- strsplit(javaReqs, "[(,)]")[[1]]
0068 minJavaVersion <- as.numeric(strsplit(sparkJavaVersions[[2]], ">= ", fixed = TRUE)[[1]][[2]])
0069 maxJavaVersion <- as.numeric(strsplit(sparkJavaVersions[[3]], "< ", fixed = TRUE)[[1]][[2]])
0070 if (javaHome != "") {
0071 javaBin <- file.path(javaHome, "bin", javaBin)
0072 }
0073
0074 # If java is missing from PATH, we get an error in Unix and a warning in Windows
0075 javaVersionOut <- tryCatch(
0076 if (is_windows()) {
0077 # See SPARK-24535
0078 system2(javaBin, "-version", wait = TRUE, stdout = TRUE, stderr = TRUE)
0079 } else {
0080 launchScript(javaBin, "-version", wait = TRUE, stdout = TRUE, stderr = TRUE)
0081 },
0082 error = function(e) {
0083 stop("Java version check failed. Please make sure Java is installed",
0084 " and set JAVA_HOME to point to the installation directory.", e)
0085 },
0086 warning = function(w) {
0087 stop("Java version check failed. Please make sure Java is installed",
0088 " and set JAVA_HOME to point to the installation directory.", w)
0089 })
0090 javaVersionFilter <- Filter(
0091 function(x) {
0092 grepl(" version", x, fixed = TRUE)
0093 }, javaVersionOut)
0094
0095 javaVersionStr <- strsplit(javaVersionFilter[[1]], '"', fixed = TRUE)[[1L]][2]
0096 # javaVersionStr is of the form 1.8.0_92/9.0.x/11.0.x.
0097 # We are using 8, 9, 10, 11 for sparkJavaVersion.
0098 versions <- strsplit(javaVersionStr, ".", fixed = TRUE)[[1L]]
0099 if ("1" == versions[1]) {
0100 javaVersionNum <- as.integer(versions[2])
0101 } else {
0102 javaVersionNum <- as.integer(versions[1])
0103 }
0104 if (javaVersionNum < minJavaVersion || javaVersionNum >= maxJavaVersion) {
0105 stop("Java version, greater than or equal to ", minJavaVersion,
0106 " and less than ", maxJavaVersion, ", is required for this ",
0107 "package; found version: ", javaVersionStr)
0108 }
0109 return(javaVersionNum)
0110 }
0111
0112 launchBackend <- function(args, sparkHome, jars, sparkSubmitOpts, packages) {
0113 sparkSubmitBinName <- determineSparkSubmitBin()
0114 if (sparkHome != "") {
0115 sparkSubmitBin <- file.path(sparkHome, "bin", sparkSubmitBinName)
0116 } else {
0117 sparkSubmitBin <- sparkSubmitBinName
0118 }
0119
0120 combinedArgs <- generateSparkSubmitArgs(args, sparkHome, jars, sparkSubmitOpts, packages)
0121 cat("Launching java with spark-submit command", sparkSubmitBin, combinedArgs, "\n")
0122 invisible(launchScript(sparkSubmitBin, combinedArgs))
0123 }