Skip to content

Commit 179ab38

Browse files
committed
add try counts and increase time interval
1 parent 4cd7d3f commit 179ab38

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

pkg/R/sparkR.R

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ sparkR.init <- function(
9090
sparkExecutorEnv = list(),
9191
sparkJars = "",
9292
sparkRLibDir = "",
93-
sparkRBackendPort = 12345) {
93+
sparkRBackendPort = 12345,
94+
sparkRRetryCount = 6) {
9495

9596
if (exists(".sparkRjsc", envir = .sparkREnv)) {
9697
cat("Re-using existing Spark Context. Please stop SparkR with sparkR.stop() or restart R to create a new Spark Context\n")
@@ -111,18 +112,28 @@ sparkR.init <- function(
111112
args = as.character(sparkRBackendPort),
112113
javaOpts = paste("-Xmx", sparkMem, sep = ""))
113114

114-
cat("Waiting JVM bring up ...\n")
115-
while(TRUE) {
115+
.sparkREnv$sparkRBackendPort <- sparkRBackendPort
116+
cat("Waiting for JVM to come up...\n")
117+
tries <- 0
118+
while(tries < sparkRRetryCount) {
116119
if(!connExists(.sparkREnv)) {
117-
Sys.sleep(1)
118-
cat(".")
119-
connectBackend("localhost", sparkRBackendPort) # Connect to it
120+
Sys.sleep(2 ^ tries)
121+
tryCatch({
122+
connectBackend("localhost", .sparkREnv$sparkRBackendPort)
123+
}, error = function(err) {
124+
cat("Error in Connection, retrying...\n")
125+
}, warning = function(war) {
126+
cat("No Connection Found, retrying...\n")
127+
})
128+
tries <- tries + 1
120129
} else {
121-
cat(" ok.\n")
130+
cat("Connect ok.\n")
122131
break
123132
}
124133
}
125-
.sparkREnv$sparkRBackendPort <- sparkRBackendPort
134+
if (tries == sparkRRetryCount) {
135+
stop(sprintf("Failed to connect JVM after %d tries.\n", sparkRRetryCount))
136+
}
126137

127138
if (nchar(sparkHome) != 0) {
128139
sparkHome <- normalizePath(sparkHome)

0 commit comments

Comments
 (0)