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

[SPARK-21208][R] Adds setLocalProperty and getLocalProperty in R #20075

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 3 additions & 1 deletion R/pkg/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ exportMethods("glm",
export("setJobGroup",
"clearJobGroup",
"cancelJobGroup",
"setJobDescription")
"setJobDescription",
"setLocalProperty",
"getLocalProperty")

# Export Utility methods
export("setLogLevel")
Expand Down
45 changes: 45 additions & 0 deletions R/pkg/R/sparkR.R
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,55 @@ cancelJobGroup <- function(sc, groupId) {
#'}
#' @note setJobDescription since 2.3.0
setJobDescription <- function(value) {
if (!is.null(value)) {
value <- as.character(value)
}
sc <- getSparkContext()
invisible(callJMethod(sc, "setJobDescription", value))
}

#' Set a local property that affects jobs submitted from this thread, such as the
#' Spark fair scheduler pool.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to make a better description for a while but then just ended up with copying Python's:

def setLocalProperty(self, key, value):
"""
Set a local property that affects jobs submitted from this thread, such as the
Spark fair scheduler pool.
"""
self._jsc.setLocalProperty(key, value)

#'
#' @param key The key for a local property.
#' @param value The value for a local property.
#' @rdname setLocalProperty
#' @name setLocalProperty
#' @examples
#'\dontrun{
#' setLocalProperty("spark.scheduler.pool", "poolA")
#'}
#' @note setLocalProperty since 2.3.0
setLocalProperty <- function(key, value) {
if (is.null(key) || is.na(key)) {
stop("key should not be NULL or NA.")
}
if (!is.null(value)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be clear, is.na case seems fine with passing it with as.character. I added the test.

value <- as.character(value)
}
sc <- getSparkContext()
invisible(callJMethod(sc, "setLocalProperty", as.character(key), value))
}

#' Get a local property set in this thread, or \code{NULL} if it is missing. See
#' \code{setLocalProperty}.
#'
#' @param key The key for a local property.
#' @rdname getLocalProperty
#' @name getLocalProperty
#' @examples
#'\dontrun{
#' getLocalProperty("spark.scheduler.pool")
#'}
#' @note getLocalProperty since 2.3.0
getLocalProperty <- function(key) {
if (is.null(key) || is.na(key)) {
stop("key should not be NULL or NA.")
}
sc <- getSparkContext()
callJMethod(sc, "getLocalProperty", as.character(key))
}

sparkConfToSubmitOps <- new.env()
sparkConfToSubmitOps[["spark.driver.memory"]] <- "--driver-memory"
sparkConfToSubmitOps[["spark.driver.extraClassPath"]] <- "--driver-class-path"
Expand Down
33 changes: 32 additions & 1 deletion R/pkg/tests/fulltests/test_context.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,45 @@ test_that("job group functions can be called", {
setJobGroup("groupId", "job description", TRUE)
cancelJobGroup("groupId")
clearJobGroup()
setJobDescription("job description")

suppressWarnings(setJobGroup(sc, "groupId", "job description", TRUE))
suppressWarnings(cancelJobGroup(sc, "groupId"))
suppressWarnings(clearJobGroup(sc))
sparkR.session.stop()
})

test_that("job description and local properties can be set and got", {
sc <- sparkR.sparkContext(master = sparkRTestMaster)
setJobDescription("job description")
expect_equal(getLocalProperty("spark.job.description"), "job description")
setJobDescription(1234)
expect_equal(getLocalProperty("spark.job.description"), "1234")
setJobDescription(NULL)
expect_equal(getLocalProperty("spark.job.description"), NULL)
setJobDescription(NA)
expect_equal(getLocalProperty("spark.job.description"), NULL)

setLocalProperty("spark.scheduler.pool", "poolA")
expect_equal(getLocalProperty("spark.scheduler.pool"), "poolA")
setLocalProperty("spark.scheduler.pool", NULL)
expect_equal(getLocalProperty("spark.scheduler.pool"), NULL)
setLocalProperty("spark.scheduler.pool", NA)
expect_equal(getLocalProperty("spark.scheduler.pool"), NULL)

setLocalProperty(4321, 1234)
expect_equal(getLocalProperty(4321), "1234")
setLocalProperty(4321, NULL)
expect_equal(getLocalProperty(4321), NULL)
setLocalProperty(4321, NA)
expect_equal(getLocalProperty(4321), NULL)

expect_error(setLocalProperty(NULL, "should fail"), "key should not be NULL or NA")
expect_error(getLocalProperty(NULL), "key should not be NULL or NA")
expect_error(setLocalProperty(NA, "should fail"), "key should not be NULL or NA")
expect_error(getLocalProperty(NA), "key should not be NULL or NA")
sparkR.session.stop()
})

test_that("utility function can be called", {
sparkR.sparkContext(master = sparkRTestMaster)
setLogLevel("ERROR")
Expand Down