blob: a918f0269a61540238e94220c8fbe04d46919275 [file] [log] [blame]
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -08001package net.onrc.onos.datastore;
2
Yuta HIGUCHI67d3a2e2014-02-12 18:19:50 -08003import java.io.File;
4
5import org.apache.commons.configuration.Configuration;
6import org.apache.commons.configuration.ConfigurationException;
7import org.apache.commons.configuration.PropertiesConfiguration;
8
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -08009import edu.stanford.ramcloud.JRamCloud;
10
11public class RCClient {
12
Yuta HIGUCHI67d3a2e2014-02-12 18:19:50 -080013 private static final String DB_CONFIG_FILE = "conf/ramcloud.conf";
14 public static final Configuration config = getConfiguration();
15
Yuta HIGUCHIad7dba92014-02-03 16:47:15 -080016 // Value taken from RAMCloud's Status.h
17 // FIXME These constants should be defined by JRamCloud
18 public static final int STATUS_OK = 0;
19
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -080020 // FIXME come up with a proper way to retrieve configuration
Yuta HIGUCHI8436c4c2014-02-05 16:55:10 -080021 public static final int MAX_MULTI_READS = Math.max(1, Integer
22 .valueOf(System.getProperty("ramcloud.max_multi_reads", "400")));
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -080023
Yuta HIGUCHI8436c4c2014-02-05 16:55:10 -080024 public static final int MAX_MULTI_WRITES = Math.max(1, Integer
25 .valueOf(System.getProperty("ramcloud.max_multi_writes", "800")));
Yuta HIGUCHIad7dba92014-02-03 16:47:15 -080026
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -080027 private static final ThreadLocal<JRamCloud> tlsRCClient = new ThreadLocal<JRamCloud>() {
28 @Override
29 protected JRamCloud initialValue() {
Yuta HIGUCHI67d3a2e2014-02-12 18:19:50 -080030 return new JRamCloud(getCoordinatorUrl(config));
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -080031 }
32 };
33
34 /**
35 * @return JRamCloud instance intended to be used only within the
36 * SameThread.
37 * @note Do not store the returned instance in a member variable, etc. which
38 * may be accessed later by another thread.
39 */
40 static JRamCloud getClient() {
41 return tlsRCClient.get();
42 }
43
Yuta HIGUCHI67d3a2e2014-02-12 18:19:50 -080044 public static final Configuration getConfiguration() {
45 final File configFile = new File(System.getProperty("ramcloud.config.path", DB_CONFIG_FILE));
46 return getConfiguration(configFile);
47 }
48
49 public static final Configuration getConfiguration(final File configFile) {
50 if (configFile == null) {
51 throw new IllegalArgumentException("Need to specify a configuration file or storage directory");
52 }
53
54 if (!configFile.isFile()) {
55 throw new IllegalArgumentException("Location of configuration must be a file");
56 }
57
58 try {
59 return new PropertiesConfiguration(configFile);
60 } catch (ConfigurationException e) {
61 throw new IllegalArgumentException("Could not load configuration at: " + configFile, e);
62 }
63 }
64
65 public static String getCoordinatorUrl(final Configuration configuration) {
66 final String coordinatorIp = configuration.getString("ramcloud.coordinatorIp", "fast+udp:host=127.0.0.1");
67 final String coordinatorPort = configuration.getString("ramcloud.coordinatorPort", "port=12246");
68 final String coordinatorURL = coordinatorIp + "," + coordinatorPort;
69 return coordinatorURL;
70 }
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -080071}