Skip to content
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

Pubdev-3451: add quiet argument to init #3032

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions h2o-py/h2o/h2o.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def version_check():
def init(url=None, ip=None, port=None, name=None, https=None, insecure=None, username=None, password=None,
cookies=None, proxy=None, start_h2o=True, nthreads=-1, ice_root=None, log_dir=None, log_level=None,
enable_assertions=True, max_mem_size=None, min_mem_size=None, strict_version_check=None, ignore_config=False,
extra_classpath=None, jvm_custom_args=None, bind_to_localhost=True, **kwargs):
extra_classpath=None, jvm_custom_args=None, bind_to_localhost=True, quiet=False, **kwargs):
"""
Attempt to connect to a local server, or if not successful start a new server and connect to it.

Expand All @@ -176,6 +176,7 @@ def init(url=None, ip=None, port=None, name=None, https=None, insecure=None, use
:param strict_version_check: If True, an error will be raised if the client and server versions don't match.
:param ignore_config: Indicates whether a processing of a .h2oconfig file should be conducted or not. Default value is False.
:param extra_classpath: List of paths to libraries that should be included on the Java classpath when starting H2O from Python.
:param quiet: If True, the connection will not show connection status messages. Default value is False.
:param kwargs: (all other deprecated attributes)
:param jvm_custom_args: Customer, user-defined argument's for the JVM H2O is instantiated in. Ignored if there is an instance of H2O already running and the client connects to it.
"""
Expand Down Expand Up @@ -203,6 +204,7 @@ def init(url=None, ip=None, port=None, name=None, https=None, insecure=None, use
assert_is_type(extra_classpath, [str], None)
assert_is_type(jvm_custom_args, [str], None)
assert_is_type(bind_to_localhost, bool)
assert_is_type(quiet, bool)
assert_is_type(kwargs, {"proxies": {str: str}, "max_mem_size_GB": int, "min_mem_size_GB": int,
"force_connect": bool, "as_port": bool})

Expand Down Expand Up @@ -257,13 +259,13 @@ def get_mem_size(mmint, mmgb):
else:
verify_ssl_certificates = not insecure

if not start_h2o:
if not start_h2o and not quiet:
print("Warning: if you don't want to start local H2O server, then use of `h2o.connect()` is preferred.")
try:
h2oconn = H2OConnection.open(url=url, ip=ip, port=port, name=name, https=https,
verify_ssl_certificates=verify_ssl_certificates,
auth=auth, proxy=proxy,cookies=cookies, verbose=True,
_msgs=("Checking whether there is an H2O instance running at {url} ",
auth=auth, proxy=proxy,cookies=cookies, verbose=not quiet,
_msgs=("Checking whether there is an H2O instance running at {url}",
"connected.", "not found."))
except H2OConnectionError:
# Backward compatibility: in init() port parameter really meant "baseport" when starting a local server...
Expand All @@ -276,13 +278,13 @@ def get_mem_size(mmint, mmgb):
min_mem_size=mmin, ice_root=ice_root, log_dir=log_dir, log_level=log_level,
port=port, name=name,
extra_classpath=extra_classpath, jvm_custom_args=jvm_custom_args,
bind_to_localhost=bind_to_localhost)
bind_to_localhost=bind_to_localhost, verbose=not quiet)
h2oconn = H2OConnection.open(server=hs, https=https, verify_ssl_certificates=not insecure,
auth=auth, proxy=proxy,cookies=cookies, verbose=True)
auth=auth, proxy=proxy,cookies=cookies, verbose=not quiet)
if check_version:
version_check()
h2oconn.cluster.timezone = "UTC"
h2oconn.cluster.show_status()
if not quiet: h2oconn.cluster.show_status()

def lazy_import(path, pattern=None):
"""
Expand Down
32 changes: 32 additions & 0 deletions h2o-py/tests/testdir_jira/pyunit_pubdev_3451_quiet_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import sys
sys.path.insert(1,"../../")
import h2o
from tests import pyunit_utils
import io
import sys

def pubdev_3451():

# We call h2o.init, and see if there is any output.
# In a normal test, h2o.init will have been called before, but the output
# the first and the second time this function gets called is comparable

# Setup a trap
stdout_backup = sys.stdout
text_trap = io.StringIO()
sys.stdout = text_trap

# Run function, expecting no output
#h2o.init(quiet = True)
init(quiet = True, strict_version_check = False)

# Restore stdout
sys.stdout = stdout_backup

assert text_trap.getvalue() == ""


if __name__ == "__main__":
pyunit_utils.standalone_test(pubdev_3451)
else:
pubdev_3451()
62 changes: 36 additions & 26 deletions h2o-r/h2o-package/R/connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#' @param extra_classpath (Optional) A vector of paths to libraries to be added to the Java classpath when H2O is started from R.
#' @param jvm_custom_args (Optional) A \code{character} list of custom arguments for the JVM where new H2O instance is going to run, if started. Ignored when connecting to an existing instance.
#' @param bind_to_localhost (Optional) A \code{logical} flag indicating whether access to the H2O instance should be restricted to the local machine (default) or if it can be reached from other computers on the network. Only applicable when H2O is started from R.
#' @param quiet (Optional) A \code{logical} flag indicating whether output from the initialization should be shown (default) or surpressed.
#' @return this method will load it and return a \code{H2OConnection} object containing the IP address and port number of the H2O server.
#' @note Users may wish to manually upgrade their package (rather than waiting until being prompted), which requires
#' that they fully uninstall and reinstall the H2O package, and the H2O client package. You must unload packages running
Expand Down Expand Up @@ -66,7 +67,7 @@ h2o.init <- function(ip = "localhost", port = 54321, name = NA_character_, start
https = FALSE, insecure = FALSE, username = NA_character_, password = NA_character_,
cookies = NA_character_, context_path = NA_character_, ignore_config = FALSE,
extra_classpath = NULL, jvm_custom_args = NULL,
bind_to_localhost = TRUE) {
bind_to_localhost = TRUE, quiet = FALSE) {

if(!(ignore_config)){
# Check for .h2oconfig file
Expand All @@ -77,7 +78,7 @@ h2o.init <- function(ip = "localhost", port = 54321, name = NA_character_, start
#Read in config if available
if(!(is.null(config_path))){

h2oconfig <- .parse.h2oconfig(config_path,print_path=TRUE)
h2oconfig <- .parse.h2oconfig(config_path,print_path=!quiet)

#Check for each `allowed_config_keys` in the config file and set to counterparts in `h2o.init()`
if(strict_version_check == TRUE && "init.check_version" %in% colnames(h2oconfig)){
Expand Down Expand Up @@ -155,6 +156,8 @@ h2o.init <- function(ip = "localhost", port = 54321, name = NA_character_, start
stop("`context_path` must be a character string or NA_character_")
if(!is.null(extra_classpath) && !is.character(extra_classpath))
stop("`extra_classpath` must be a character vector or NULL")
if(!is.logical(quiet) || length(quiet) != 1L || is.na(quiet))
stop("`quiet` must be TRUE or FALSE")

if ((R.Version()$major == "3") && (R.Version()$minor == "1.0")) {
stop("H2O is not compatible with R 3.1.0\n",
Expand All @@ -175,7 +178,7 @@ h2o.init <- function(ip = "localhost", port = 54321, name = NA_character_, start
if (!startH2O)
stop("Cannot connect to H2O server. Please check that H2O is running at ", h2o.getBaseURL(tmpConn))
else if (ip == "localhost" || ip == "127.0.0.1") {
cat("\nH2O is not running yet, starting it now...\n")
if (!quiet) cat("\nH2O is not running yet, starting it now...\n")

if (nthreads == -2) {
warnNthreads <- TRUE
Expand All @@ -187,24 +190,27 @@ h2o.init <- function(ip = "localhost", port = 54321, name = NA_character_, start
enable_assertions = enable_assertions, forceDL = forceDL, license = license,
extra_classpath = extra_classpath, ice_root = ice_root, stdout = stdout,
log_dir = log_dir, log_level = log_level, context_path = context_path,
jvm_custom_args = jvm_custom_args, bind_to_localhost = bind_to_localhost)
jvm_custom_args = jvm_custom_args, bind_to_localhost = bind_to_localhost,
quiet = quiet)

count <- 0L
cat("Starting H2O JVM and connecting: ")
if (!quiet) cat("Starting H2O JVM and connecting: ")
while(!h2o.clusterIsUp(conn = tmpConn) && (count < 60L)) {
cat(".")
if (!quiet) cat(".")
Sys.sleep(1L)
count <- count + 1L
}

if (!h2o.clusterIsUp(conn = tmpConn)) {
cat(paste(readLines(stdout), collapse="\n"), "\n")
print(tmpConn@ip)
print(tmpConn@port)
rv <- .h2o.doRawGET(conn = tmpConn, urlSuffix = "")
print(rv$curlError)
print(rv$httpStatusCode)
print(rv$curlErrorMessage)
if (!quiet) {
cat(paste(readLines(stdout), collapse="\n"), "\n")
print(tmpConn@ip)
print(tmpConn@port)
rv <- .h2o.doRawGET(conn = tmpConn, urlSuffix = "")
print(rv$curlError)
print(rv$httpStatusCode)
print(rv$curlErrorMessage)
}

stop("H2O failed to start, stopping execution.")
}
Expand All @@ -215,10 +221,12 @@ h2o.init <- function(ip = "localhost", port = 54321, name = NA_character_, start
conn <- new("H2OConnection", ip = ip, port = port, name = .h2o.jar.env$name, proxy = proxy, https = https, insecure = insecure,
username = username, password = password,cookies = cookies, context_path = context_path)
assign("SERVER", conn, .pkg.env)
cat(" Connection successful!\n\n")
.h2o.jar.env$port <- port #Ensure right port is called when quitting R
h2o.clusterInfo()
cat("\n")
if (!quiet) {
cat(" Connection successful!\n\n")
h2o.clusterInfo()
cat("\n")
}

if( strict_version_check && !nchar(Sys.getenv("H2O_DISABLE_STRICT_VERSION_CHECK"))) {
verH2O <- h2o.getVersion()
Expand Down Expand Up @@ -247,7 +255,7 @@ h2o.init <- function(ip = "localhost", port = 54321, name = NA_character_, start
}
}

if (warnNthreads) {
if (warnNthreads && !quiet) {
cat("Note: As started, H2O is limited to the CRAN default of 2 CPUs.\n")
cat(" Shut down and restart H2O as shown below to use all your CPUs.\n")
cat(" > h2o.shutdown()\n")
Expand Down Expand Up @@ -531,7 +539,7 @@ h2o.clusterStatus <- function() {
max_memory = NULL, min_memory = NULL,
enable_assertions = TRUE, forceDL = FALSE, license = NULL, extra_classpath = NULL,
ice_root, stdout, log_dir, log_level, context_path, jvm_custom_args = NULL,
bind_to_localhost) {
bind_to_localhost, quiet = FALSE) {
command <- .h2o.checkJava()

if (! is.null(license)) {
Expand Down Expand Up @@ -618,15 +626,17 @@ h2o.clusterStatus <- function() {
if(nthreads > 0L) args <- c(args, "-nthreads", nthreads)
if(!is.null(license)) args <- c(args, "-license", license)

cat("\n")
cat( "Note: In case of errors look at the following log files:\n")
cat(sprintf(" %s\n", stdout))
cat(sprintf(" %s\n", stderr))
cat("\n")
if(!quiet) {
cat("\n")
cat( "Note: In case of errors look at the following log files:\n")
cat(sprintf(" %s\n", stdout))
cat(sprintf(" %s\n", stderr))
cat("\n")

# Print a java -version to the console
system2(command, c(mem_args, "-version"))
cat("\n")
# Print a java -version to the console
system2(command, c(mem_args, "-version"))
cat("\n")
}

# Run the real h2o java command
rc = system2(command,
Expand Down
15 changes: 15 additions & 0 deletions h2o-r/tests/testdir_jira/runit_pubdev_3451_quiet_init.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
setwd(normalizePath(dirname(R.utils::commandArgs(asValues=TRUE)$"f")))
source("../../scripts/h2o-r-test-setup.R")


test.quiet.init <- function() {

# We call h2o.init, and see if there is any output.
# In a normal test, h2o.init will have been called before, but the output
# the first and the second time this function gets called is comparable

expect_silent(h2o.init(quiet = TRUE))

}

doTest("h2o.init with quiet option", test.quiet.init)