| 1 |
library(parallelly) |
| 2 |
|
| 3 |
options(parallelly.debug = TRUE) |
| 4 |
|
| 5 |
is_fqdn <- parallelly:::is_fqdn |
| 6 |
is_ip_number <- parallelly:::is_ip_number |
| 7 |
is_localhost <- parallelly:::is_localhost |
| 8 |
find_rshcmd <- parallelly:::find_rshcmd |
| 9 |
|
| 10 |
useXDR <- isTRUE(parallelly:::getOption2("parallelly.makeNodePSOCK.useXDR")) |
| 11 |
|
| 12 |
message("*** makeClusterPSOCK() ...") |
| 13 |
|
| 14 |
message("- makeClusterPSOCK() - internal utility functions") |
| 15 |
|
| 16 |
stopifnot( |
| 17 |
is_fqdn("a.b"), |
| 18 |
is_fqdn("a.b.c"), |
| 19 |
!is_fqdn("a") |
| 20 |
) |
| 21 |
|
| 22 |
stopifnot( |
| 23 |
is_ip_number("1.2.3.4"), |
| 24 |
!is_ip_number("a"), |
| 25 |
!is_ip_number("1.2.3"), |
| 26 |
!is_ip_number("1.2.3.256"), |
| 27 |
!is_ip_number("1.2.3.-1"), |
| 28 |
!is_ip_number("1.2.3.a") |
| 29 |
) |
| 30 |
|
| 31 |
## Reset internal cache |
| 32 |
stopifnot(is.na(is_localhost(worker = NULL, hostname = NULL))) |
| 33 |
stopifnot( |
| 34 |
is_localhost("localhost"), |
| 35 |
is_localhost("127.0.0.1"), |
| 36 |
is_localhost(Sys.info()[["nodename"]]), |
| 37 |
is_localhost(Sys.info()[["nodename"]]), ## cache hit |
| 38 |
!is_localhost("not.a.localhost.hostname") |
| 39 |
) |
| 40 |
|
| 41 |
cmd <- find_rshcmd(must_work = FALSE) |
| 42 |
print(cmd) |
| 43 |
|
| 44 |
|
| 45 |
message("- makeClusterPSOCK()") |
| 46 |
|
| 47 |
cl <- makeClusterPSOCK("<a-non-existing-hostname>", user = "johndoe", master = NULL, revtunnel = FALSE, rshcmd = "my_ssh", renice = TRUE, manual = TRUE, dryrun = TRUE) |
| 48 |
print(cl) |
| 49 |
|
| 50 |
cl <- makeClusterPSOCK(1L, port = "random", dryrun = TRUE) |
| 51 |
print(cl) |
| 52 |
|
| 53 |
cl <- makeClusterPSOCK(1L) |
| 54 |
print(cl) |
| 55 |
node <- cl[[1]] |
| 56 |
print(node) |
| 57 |
utils::str(node) |
| 58 |
stopifnot(isTRUE(attr(node[["host"]], "localhost"))) |
| 59 |
print(cl) |
| 60 |
parallel::stopCluster(cl) |
| 61 |
|
| 62 |
cl <- makeClusterPSOCK(2L, autoStop = TRUE) |
| 63 |
print(cl) |
| 64 |
## Mockup different hostnames for print() |
| 65 |
node <- cl[[1]] |
| 66 |
node[["host"]] <- "localhost2" |
| 67 |
cl[[1]] <- node |
| 68 |
print(cl) |
| 69 |
parallel::stopCluster(cl) |
| 70 |
|
| 71 |
message("- makeClusterPSOCK() - useXDR = TRUE/FALSE") |
| 72 |
|
| 73 |
for (xdr in c(TRUE, FALSE)) { |
| 74 |
cl <- makeClusterPSOCK(1L, useXDR = xdr) |
| 75 |
node <- cl[[1]] |
| 76 |
stopifnot( |
| 77 |
is.list(node), |
| 78 |
inherits(node, if (xdr) "SOCKnode" else "SOCK0node"), |
| 79 |
"session_info" %in% names(node) |
| 80 |
) |
| 81 |
si <- node[["session_info"]] |
| 82 |
stopifnot(is.list(si)) |
| 83 |
parallel::stopCluster(cl) |
| 84 |
} |
| 85 |
|
| 86 |
message("- makeClusterPSOCK() - argument 'socketOptions'") |
| 87 |
|
| 88 |
for (value in list(NULL, "NULL", "no-delay")) { |
| 89 |
cl <- makeClusterPSOCK(1L, socketOptions = value) |
| 90 |
y <- parallel::clusterEvalQ(cl, 42L)[[1]] |
| 91 |
stopifnot(identical(y, 42L)) |
| 92 |
parallel::stopCluster(cl) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
message("- makeClusterPSOCK() - argument 'rscript_startup'") |
| 97 |
|
| 98 |
for (value in list(NULL, "options(abc = 42L)", quote(options(abc = 42L)))) { |
| 99 |
cl <- makeClusterPSOCK(1L, rscript_startup = value) |
| 100 |
y <- parallel::clusterEvalQ(cl, getOption("abc", NA_integer_))[[1]] |
| 101 |
stopifnot(is.integer(y), length(y) == 1L) |
| 102 |
if (!is.null(value)) stopifnot(identical(y, 42L)) |
| 103 |
parallel::stopCluster(cl) |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
message("- makeClusterPSOCK() - argument 'user'") |
| 108 |
|
| 109 |
## Test user = "*" (wildcard for system default username) with localhost |
| 110 |
## The "*" wildcard means "use system default", which should work for localhost |
| 111 |
cl <- makeClusterPSOCK(1L, user = "*") |
| 112 |
print(cl) |
| 113 |
y <- parallel::clusterEvalQ(cl, Sys.info()[["user"]])[[1]] |
| 114 |
stopifnot(is.character(y), length(y) == 1L) |
| 115 |
parallel::stopCluster(cl) |
| 116 |
|
| 117 |
## Test user = "*" with multiple workers (single user for all) |
| 118 |
cl <- makeClusterPSOCK(2L, user = "*") |
| 119 |
print(cl) |
| 120 |
y <- parallel::clusterEvalQ(cl, Sys.info()[["user"]]) |
| 121 |
stopifnot(length(y) == 2L) |
| 122 |
parallel::stopCluster(cl) |
| 123 |
|
| 124 |
## Test user = "*" per worker (vector form with 2 workers) |
| 125 |
cl <- makeClusterPSOCK(2L, user = c("*", "*")) |
| 126 |
print(cl) |
| 127 |
y <- parallel::clusterEvalQ(cl, Sys.info()[["user"]]) |
| 128 |
stopifnot(length(y) == 2L) |
| 129 |
parallel::stopCluster(cl) |
| 130 |
|
| 131 |
|
| 132 |
message("- makeClusterPSOCK() - setup_strategy = TRUE/FALSE") |
| 133 |
|
| 134 |
for (setup_strategy in c("sequential", "parallel")) { |
| 135 |
dt <- system.time({ |
| 136 |
cl <- makeClusterPSOCK(2L, setup_strategy = setup_strategy) |
| 137 |
}) |
| 138 |
print(dt) |
| 139 |
print(cl) |
| 140 |
parallel::stopCluster(cl) |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
message("- makeClusterPSOCK() - with and w/out validation") |
| 145 |
|
| 146 |
cl <- makeClusterPSOCK(1L, validate = TRUE) ## default |
| 147 |
node <- cl[[1]] |
| 148 |
stopifnot( |
| 149 |
is.list(node), |
| 150 |
inherits(node, if (useXDR) "SOCKnode" else "SOCK0node"), |
| 151 |
"session_info" %in% names(node) |
| 152 |
) |
| 153 |
si <- node[["session_info"]] |
| 154 |
stopifnot(is.list(si)) |
| 155 |
parallel::stopCluster(cl) |
| 156 |
|
| 157 |
cl <- makeClusterPSOCK(1L, validate = FALSE) |
| 158 |
node <- cl[[1]] |
| 159 |
stopifnot( |
| 160 |
is.list(node), |
| 161 |
inherits(node, if (useXDR) "SOCKnode" else "SOCK0node"), |
| 162 |
! "session_info" %in% names(node) |
| 163 |
) |
| 164 |
parallel::stopCluster(cl) |
| 165 |
|
| 166 |
|
| 167 |
message("- makeClusterPSOCK() - w/out 'parallelly' on worker") |
| 168 |
|
| 169 |
ovalue <- Sys.getenv("R_LIBS_USER") |
| 170 |
Sys.setenv(R_LIBS_USER = tempdir()) |
| 171 |
cl <- makeClusterPSOCK(1L, outfile = "") |
| 172 |
print(cl) |
| 173 |
parallel::stopCluster(cl) |
| 174 |
Sys.setenv(R_LIBS_USER = ovalue) |
| 175 |
|
| 176 |
|
| 177 |
message("- makeClusterPSOCK() - assert 'parallelly' is not loaded") |
| 178 |
|
| 179 |
cl <- makeClusterPSOCK(1L) |
| 180 |
ns <- parallel::clusterCall(cl, function() { loadedNamespaces() }) |
| 181 |
print(ns) |
| 182 |
stopifnot(!is.element("parallelly", ns)) |
| 183 |
parallel::stopCluster(cl) |
| 184 |
|
| 185 |
|
| 186 |
message("- makeClusterPSOCK() - launch via the R executable") |
| 187 |
|
| 188 |
if (on_windows) { |
| 189 |
## R and R.exe fails on MS Windows, cf. R-devel thread "MS Windows: R does |
| 190 |
## not escape quotes in CLI options the same way as Rterm and Rscript" |
| 191 |
## on 2021-12-15. |
| 192 |
rscripts <- file.path(R.home("bin"), c("Rterm", "Rterm.exe")) |
| 193 |
} else { |
| 194 |
rscripts <- file.path(R.home("bin"), "R") |
| 195 |
} |
| 196 |
|
| 197 |
for (rscript in rscripts) { |
| 198 |
message(" Launcher: ", sQuote(rscript)) |
| 199 |
rscript_args <- c("--no-echo", "--no-restore", "*", "--args") |
| 200 |
cl <- tryCatch({ |
| 201 |
makeClusterPSOCK(1L, rscript = rscript, rscript_args = rscript_args) |
| 202 |
}, warning = identity) |
| 203 |
stopifnot(inherits(cl, "cluster")) |
| 204 |
parallel::stopCluster(cl) |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
message("- makeClusterPSOCK() - default packages") |
| 209 |
|
| 210 |
if (on_windows) { |
| 211 |
## R and R.exe fails on MS Windows, cf. R-devel thread "MS Windows: R does |
| 212 |
## not escape quotes in CLI options the same way as Rterm and Rscript" |
| 213 |
## on 2021-12-15. |
| 214 |
rscripts <- file.path(R.home("bin"), c("Rscript", "Rterm", "Rterm.exe")) |
| 215 |
} else { |
| 216 |
rscripts <- file.path(R.home("bin"), c("Rscript", "R")) |
| 217 |
} |
| 218 |
default_packages <- c("utils", "tools") |
| 219 |
for (rscript in rscripts) { |
| 220 |
message(" Launcher: ", sQuote(rscript)) |
| 221 |
if (tools::file_path_sans_ext(basename(rscript)) %in% c("R", "Rterm")) { |
| 222 |
rscript_args <- c("--no-echo", "--no-restore", "*", "--args") |
| 223 |
} else { |
| 224 |
rscript_args <- NULL |
| 225 |
} |
| 226 |
cl <- tryCatch({ |
| 227 |
makeClusterPSOCK(1L, rscript = rscript, rscript_args = rscript_args, default_packages = default_packages) |
| 228 |
}, warning = identity) |
| 229 |
stopifnot(inherits(cl, "cluster")) |
| 230 |
pkgs <- parallel::clusterEvalQ(cl, { getOption("defaultPackages") })[[1]] |
| 231 |
stopifnot(identical(pkgs, default_packages)) |
| 232 |
parallel::stopCluster(cl) |
| 233 |
} |
| 234 |
|
| 235 |
if (on_windows) { |
| 236 |
## R and R.exe fails on MS Windows, cf. R-devel thread "MS Windows: R does |
| 237 |
## not escape quotes in CLI options the same way as Rterm and Rscript" |
| 238 |
## on 2021-12-15. |
| 239 |
rscripts <- file.path(R.home("bin"), c("Rscript", "Rterm", "Rterm.exe")) |
| 240 |
} else { |
| 241 |
rscripts <- file.path(R.home("bin"), c("Rscript", "R")) |
| 242 |
} |
| 243 |
default_packages <- c("parallelly", "*") |
| 244 |
truth <- unique(c("parallelly", getOption("defaultPackages"))) |
| 245 |
for (rscript in rscripts) { |
| 246 |
message(" Launcher: ", sQuote(rscript)) |
| 247 |
if (tools::file_path_sans_ext(basename(rscript)) %in% c("R", "Rterm")) { |
| 248 |
rscript_args <- c("--no-echo", "--no-restore", "*", "--args") |
| 249 |
} else { |
| 250 |
rscript_args <- NULL |
| 251 |
} |
| 252 |
cl <- tryCatch({ |
| 253 |
makeClusterPSOCK(1L, rscript = rscript, rscript_args = rscript_args, default_packages = default_packages) |
| 254 |
}, warning = identity) |
| 255 |
stopifnot(inherits(cl, "cluster")) |
| 256 |
pkgs <- parallel::clusterEvalQ(cl, { getOption("defaultPackages") })[[1]] |
| 257 |
stopifnot(identical(pkgs, truth)) |
| 258 |
parallel::stopCluster(cl) |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
message("- makeClusterPSOCK() - exceptions") |
| 263 |
|
| 264 |
res <- tryCatch({ |
| 265 |
cl <- makeClusterPSOCK(1:2) |
| 266 |
}, error = identity) |
| 267 |
print(res) |
| 268 |
stopifnot(inherits(res, "error")) |
| 269 |
|
| 270 |
res <- tryCatch({ |
| 271 |
cl <- makeClusterPSOCK(0L) |
| 272 |
}, error = identity) |
| 273 |
print(res) |
| 274 |
stopifnot(inherits(res, "error")) |
| 275 |
|
| 276 |
res <- tryCatch({ |
| 277 |
cl <- makeClusterPSOCK(1L, rshcmd = character(0L)) |
| 278 |
}, error = identity) |
| 279 |
print(res) |
| 280 |
stopifnot(inherits(res, "error")) |
| 281 |
|
| 282 |
res <- tryCatch({ |
| 283 |
cl <- makeClusterPSOCK(1L, port = integer(0L)) |
| 284 |
}, error = identity) |
| 285 |
print(res) |
| 286 |
stopifnot(inherits(res, "error")) |
| 287 |
|
| 288 |
res <- tryCatch({ |
| 289 |
cl <- makeClusterPSOCK(1L, port = NA_integer_) |
| 290 |
}, error = identity) |
| 291 |
print(res) |
| 292 |
stopifnot(inherits(res, "error")) |
| 293 |
|
| 294 |
|
| 295 |
message("- makeClusterPSOCK() - exceptions") |
| 296 |
|
| 297 |
## Non-existing hostname |
| 298 |
res <- tryCatch({ |
| 299 |
cl <- makeNodePSOCK("not.a.localhost.hostname", revtunnel = TRUE) |
| 300 |
}, error = identity) |
| 301 |
print(res) |
| 302 |
stopifnot(inherits(res, "error")) |
| 303 |
|
| 304 |
## Invalid port |
| 305 |
res <- tryCatch({ |
| 306 |
cl <- makeNodePSOCK("localhost", port = NA_integer_) |
| 307 |
}, error = identity) |
| 308 |
print(res) |
| 309 |
stopifnot(inherits(res, "error")) |
| 310 |
|
| 311 |
## Broken connection |
| 312 |
cl <- makeClusterPSOCK(2L, user = "*") |
| 313 |
node <- cl[[1]] |
| 314 |
close(node[["con"]]) |
| 315 |
out <- utils::capture.output(print(cl)) |
| 316 |
parallel::stopCluster(cl[2]) |
| 317 |
stopifnot(any(grepl("broken connection", out))) |
| 318 |
|
| 319 |
## More workers than connections available |
| 320 |
ncons <- freeConnections() |
| 321 |
if (ncons < 200) local({ |
| 322 |
opts <- options(parallelly.maxWorkers.localhost = c(Inf, Inf)) |
| 323 |
res <- tryCatch(cl <- makeClusterPSOCK(ncons + 1L), error = identity) |
| 324 |
print(res) |
| 325 |
stopifnot(inherits(res, "error")) |
| 326 |
options(opts) |
| 327 |
}) |
| 328 |
|
| 329 |
## Don't test on CRAN |
| 330 |
if (fullTest || covr_testing) { |
| 331 |
## Occupied/blocked port |
| 332 |
res <- tryCatch( |
| 333 |
cl <- parallelly::makeClusterPSOCK("localhost", port = 80L, tries = 1L), |
| 334 |
error = identity) |
| 335 |
print(res) |
| 336 |
## Skip error assertion in case this actually works on some machine. |
| 337 |
## But where it fails, we are testing the port-failure exception code. |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
## https://github.com/futureverse/parallelly/issues/95 |
| 342 |
if (getRversion() >= "4.0.0") { |
| 343 |
res <- tryCatch({ |
| 344 |
parallelly::makeClusterPSOCK(1L, rscript_startup = quote(Sys.sleep(6.0)), |
| 345 |
connectTimeout = 0.1, timeout = 7.0) |
| 346 |
}, error = identity) |
| 347 |
print(res) |
| 348 |
stopifnot( |
| 349 |
inherits(res, "error"), |
| 350 |
grepl("^Cluster setup failed", conditionMessage(res)) |
| 351 |
) |
| 352 |
|
| 353 |
## Make sure to wait for background process to timeout before continuing, |
| 354 |
## when on MS Windows |
| 355 |
if (on_windows) Sys.sleep(5.0) |
| 356 |
} |
| 357 |
|
| 358 |
message("*** makeClusterPSOCK() ... DONE") |