Skip to content

Commit 6154aac

Browse files
authored
Merge pull request IQSS#8830 from poikilotherm/7000-mpconfig-rserve
7000 mpconfig rserve
2 parents e6c033b + 6a1984d commit 6154aac

File tree

5 files changed

+93
-75
lines changed

5 files changed

+93
-75
lines changed

doc/sphinx-guides/source/installation/config.rst

+38-5
Original file line numberDiff line numberDiff line change
@@ -1573,27 +1573,60 @@ Can also be set via *MicroProfile Config API* sources, e.g. the environment vari
15731573
dataverse.rserve.host
15741574
+++++++++++++++++++++
15751575

1576-
Host name for Rserve, used for tasks that require use of R (to ingest RData files and to save tabular data as RData frames).
1576+
Host name for Rserve, used for tasks that require use of R (to ingest RData
1577+
files and to save tabular data as RData frames).
1578+
1579+
Defaults to ``localhost``.
1580+
1581+
Can also be set via *MicroProfile Config API* sources, e.g. the environment
1582+
variable ``DATAVERSE_RSERVE_HOST``.
15771583

15781584
dataverse.rserve.port
15791585
+++++++++++++++++++++
15801586

1581-
Port number for Rserve, used for tasks that require use of R (to ingest RData files and to save tabular data as RData frames).
1587+
Port number for Rserve, used for tasks that require use of R (to ingest RData
1588+
files and to save tabular data as RData frames).
1589+
1590+
Defaults to ``6311`` when not configured or no valid integer.
1591+
1592+
Can also be set via *MicroProfile Config API* sources, e.g. the environment
1593+
variable ``DATAVERSE_RSERVE_PORT``.
15821594

15831595
dataverse.rserve.user
15841596
+++++++++++++++++++++
15851597

1586-
Username for Rserve, used for tasks that require use of R (to ingest RData files and to save tabular data as RData frames).
1598+
Username for Rserve, used for tasks that require use of R (to ingest RData
1599+
files and to save tabular data as RData frames).
1600+
1601+
Defaults to ``rserve``.
1602+
1603+
Can also be set via *MicroProfile Config API* sources, e.g. the environment
1604+
variable ``DATAVERSE_RSERVE_USER``.
15871605

15881606
dataverse.rserve.password
15891607
+++++++++++++++++++++++++
15901608

1591-
Password for Rserve, used for tasks that require use of R (to ingest RData files and to save tabular data as RData frames).
1609+
Password for Rserve, used for tasks that require use of R (to ingest RData
1610+
files and to save tabular data as RData frames).
1611+
1612+
Defaults to ``rserve``.
1613+
1614+
Can also be set via *MicroProfile Config API* sources, e.g. the environment
1615+
variable ``DATAVERSE_RSERVE_PASSWORD``.
15921616

15931617
dataverse.rserve.tempdir
15941618
++++++++++++++++++++++++
15951619

1596-
Temporary directory used by Rserve (defaults to /tmp/Rserv). Note that this location is local to the host on which Rserv is running (specified in ``dataverse.rserve.host`` above). When talking to Rserve, Dataverse needs to know this location in order to generate absolute path names of the files on the other end.
1620+
Temporary directory used by Rserve (defaults to ``/tmp/Rserv``). Note that this
1621+
location is local to the host on which Rserv is running (specified in
1622+
``dataverse.rserve.host`` above). When talking to Rserve, Dataverse needs to
1623+
know this location in order to generate absolute path names of the files on the
1624+
other end.
1625+
1626+
Defaults to ``/tmp/Rserv``.
1627+
1628+
Can also be set via *MicroProfile Config API* sources, e.g. the environment
1629+
variable ``DATAVERSE_RSERVE_TEMPDIR``.
15971630

15981631
.. _dataverse.dropbox.key:
15991632

src/main/java/edu/harvard/iq/dataverse/ingest/tabulardata/impl/plugins/rdata/RDATAFileReader.java

+19-23
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import javax.inject.Inject;
3232

3333
// Rosuda Wrappers and Methods for R-calls to Rserve
34+
import edu.harvard.iq.dataverse.settings.JvmSettings;
3435
import org.rosuda.REngine.REXP;
3536
import org.rosuda.REngine.REXPMismatchException;
3637
import org.rosuda.REngine.RList;
@@ -88,10 +89,10 @@ public class RDATAFileReader extends TabularDataFileReader {
8889
static private String RSCRIPT_WRITE_DVN_TABLE = "";
8990

9091
// RServe static variables
91-
private static String RSERVE_HOST = System.getProperty("dataverse.rserve.host");
92-
private static String RSERVE_USER = System.getProperty("dataverse.rserve.user");
93-
private static String RSERVE_PASSWORD = System.getProperty("dataverse.rserve.password");
94-
private static int RSERVE_PORT;
92+
private final String RSERVE_HOST;
93+
private final int RSERVE_PORT;
94+
private final String RSERVE_USER;
95+
private final String RSERVE_PASSWORD;
9596

9697
// TODO:
9798
// we're not using these time/data formats for anything, are we?
@@ -138,24 +139,6 @@ public class RDATAFileReader extends TabularDataFileReader {
138139
* This is primarily to construct the R-Script
139140
*/
140141
static {
141-
/*
142-
* Set defaults fallbacks for class properties
143-
*/
144-
if (RSERVE_HOST == null)
145-
RSERVE_HOST = "localhost";
146-
147-
if (RSERVE_USER == null)
148-
RSERVE_USER = "rserve";
149-
150-
if (RSERVE_PASSWORD == null)
151-
RSERVE_PASSWORD = "rserve";
152-
153-
if (System.getProperty("dataverse.ingest.rserve.port") == null)
154-
RSERVE_PORT = 6311;
155-
else
156-
RSERVE_PORT = Integer.parseInt(System.getProperty("dataverse.rserve.port"));
157-
158-
159142
// Load R Scripts into memory, so that we can run them via R-serve
160143
RSCRIPT_WRITE_DVN_TABLE = readLocalResource("scripts/write.table.R");
161144
RSCRIPT_GET_DATASET = readLocalResource("scripts/get.dataset.R");
@@ -451,7 +434,20 @@ public RDATAFileReader(TabularDataFileReaderSpi originator) {
451434

452435
super(originator);
453436

454-
437+
// These settings have sane defaults in resources/META-INF/microprofile-config.properties,
438+
// ready to be overridden by a sysadmin. Every time a file would be read with this file reader,
439+
// a new reader will be created, reading from the cached config source settings with minimal overhead.
440+
this.RSERVE_HOST = JvmSettings.RSERVE_HOST.lookup();
441+
int port;
442+
try {
443+
port = JvmSettings.RSERVE_PORT.lookup(Integer.class);
444+
} catch (IllegalArgumentException e) {
445+
LOG.log(Level.SEVERE, "Could not parse value for " + JvmSettings.RSERVE_PORT.getScopedKey() + ", defaulting to 6311", e);
446+
port = 6311;
447+
}
448+
this.RSERVE_PORT = port;
449+
this.RSERVE_USER = JvmSettings.RSERVE_USER.lookup();
450+
this.RSERVE_PASSWORD = JvmSettings.RSERVE_PASSWORD.lookup();
455451

456452
LOG.fine("RDATAFileReader: INSIDE RDATAFileReader");
457453

src/main/java/edu/harvard/iq/dataverse/rserve/RemoteDataFrameService.java

+21-47
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.Set;
4242
import java.util.logging.Logger;
4343

44+
import edu.harvard.iq.dataverse.settings.JvmSettings;
4445
import org.apache.commons.io.IOUtils;
4546

4647
import org.apache.commons.lang3.RandomStringUtils;
@@ -72,57 +73,33 @@ public class RemoteDataFrameService {
7273

7374
private static String TMP_TABDATA_FILE_EXT = ".tab";
7475
private static String TMP_RDATA_FILE_EXT = ".RData";
75-
76-
private static String RSERVE_HOST = null;
77-
private static String RSERVE_USER = null;
78-
private static String RSERVE_PWD = null;
79-
private static int RSERVE_PORT = -1;
76+
77+
// These settings have sane defaults in resources/META-INF/microprofile-config.properties,
78+
// ready to be overridden by a sysadmin
79+
private final String RSERVE_HOST;
80+
private final String RSERVE_USER;
81+
private final String RSERVE_PWD;
82+
private final int RSERVE_PORT;
83+
private final String RSERVE_TMP_DIR;
8084

8185
private static String DATAVERSE_R_FUNCTIONS = "scripts/dataverse_r_functions.R";
8286
private static String DATAVERSE_R_PREPROCESSING = "scripts/preprocess.R";
83-
84-
public static String LOCAL_TEMP_DIR = System.getProperty("java.io.tmpdir");
85-
public static String RSERVE_TMP_DIR=null;
8687

8788
public String PID = null;
8889
public String tempFileNameIn = null;
8990
public String tempFileNameOut = null;
90-
91-
static {
92-
93-
RSERVE_TMP_DIR = System.getProperty("dataverse.rserve.tempdir");
94-
95-
if (RSERVE_TMP_DIR == null){
96-
RSERVE_TMP_DIR = "/tmp/";
97-
}
98-
99-
RSERVE_HOST = System.getProperty("dataverse.rserve.host");
100-
if (RSERVE_HOST == null){
101-
RSERVE_HOST= "localhost";
102-
}
103-
104-
RSERVE_USER = System.getProperty("dataverse.rserve.user");
105-
if (RSERVE_USER == null){
106-
RSERVE_USER= "rserve";
107-
}
108-
109-
RSERVE_PWD = System.getProperty("dataverse.rserve.password");
110-
if (RSERVE_PWD == null){
111-
RSERVE_PWD= "rserve";
112-
}
113-
114-
115-
if (System.getProperty("dataverse.rserve.port") == null ){
116-
RSERVE_PORT= 6311;
117-
} else {
118-
RSERVE_PORT = Integer.parseInt(System.getProperty("dataverse.rserve.port"));
119-
}
120-
121-
}
122-
123-
12491

12592
public RemoteDataFrameService() {
93+
// These settings have sane defaults in resources/META-INF/microprofile-config.properties,
94+
// ready to be overridden by a sysadmin. Config sources have their own caches, so adding
95+
// these here means the setting can be changed dynamically without too much overhead.
96+
this.RSERVE_HOST = JvmSettings.RSERVE_HOST.lookup();
97+
this.RSERVE_USER = JvmSettings.RSERVE_USER.lookup();
98+
this.RSERVE_PWD = JvmSettings.RSERVE_PASSWORD.lookup();
99+
this.RSERVE_PORT = JvmSettings.RSERVE_PORT.lookup(Integer.class);
100+
this.RSERVE_TMP_DIR = JvmSettings.RSERVE_TEMPDIR.lookup();
101+
102+
126103
// initialization
127104
PID = RandomStringUtils.randomNumeric(6);
128105

@@ -703,15 +680,12 @@ public Map<String, String> runDataFrameRequest(RJobRequest jobRequest, RConnecti
703680
public File transferRemoteFile(RConnection connection, String targetFilename,
704681
String tmpFilePrefix, String tmpFileExt, int fileSize) {
705682

706-
// set up a local temp file:
707-
683+
// set up a local temp file:
708684
File tmpResultFile = null;
709-
String resultFile = tmpFilePrefix + PID + "." + tmpFileExt;
710-
711685
RFileInputStream rInStream = null;
712686
OutputStream outbr = null;
713687
try {
714-
tmpResultFile = new File(LOCAL_TEMP_DIR, resultFile);
688+
tmpResultFile = File.createTempFile(tmpFilePrefix + PID, "."+tmpFileExt);
715689
outbr = new BufferedOutputStream(new FileOutputStream(tmpResultFile));
716690
// open the input stream
717691
rInStream = connection.openFile(targetFilename);

src/main/java/edu/harvard/iq/dataverse/settings/JvmSettings.java

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ public enum JvmSettings {
4444
FQDN(PREFIX, "fqdn"),
4545
SITE_URL(PREFIX, "siteUrl"),
4646

47+
// RSERVE CONNECTION
48+
SCOPE_RSERVE(PREFIX, "rserve"),
49+
RSERVE_HOST(SCOPE_RSERVE, "host"),
50+
RSERVE_PORT(SCOPE_RSERVE, "port", "dataverse.ingest.rserve.port"),
51+
RSERVE_USER(SCOPE_RSERVE, "user"),
52+
RSERVE_PASSWORD(SCOPE_RSERVE, "password"),
53+
RSERVE_TEMPDIR(SCOPE_RSERVE, "tempdir"),
54+
4755
// API SETTINGS
4856
SCOPE_API(PREFIX, "api"),
4957
API_SIGNING_SECRET(SCOPE_API, "signing-secret"),

src/main/resources/META-INF/microprofile-config.properties

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ dataverse.db.port=5432
1414
dataverse.db.user=dataverse
1515
dataverse.db.name=dataverse
1616

17+
# RSERVE
18+
dataverse.rserve.host=localhost
19+
dataverse.rserve.port=6311
20+
dataverse.rserve.user=rserve
21+
dataverse.rserve.password=rserve
22+
dataverse.rserve.tempdir=/tmp/Rserv
23+
1724
# OAI SERVER
1825
dataverse.oai.server.maxidentifiers=100
1926
dataverse.oai.server.maxrecords=10

0 commit comments

Comments
 (0)