Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 1 | package net.onrc.onos.datastore; |
| 2 | |
Yuta HIGUCHI | 67d3a2e | 2014-02-12 18:19:50 -0800 | [diff] [blame] | 3 | import java.io.File; |
| 4 | |
| 5 | import org.apache.commons.configuration.Configuration; |
| 6 | import org.apache.commons.configuration.ConfigurationException; |
| 7 | import org.apache.commons.configuration.PropertiesConfiguration; |
| 8 | |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 9 | import edu.stanford.ramcloud.JRamCloud; |
| 10 | |
| 11 | public class RCClient { |
| 12 | |
Yuta HIGUCHI | 67d3a2e | 2014-02-12 18:19:50 -0800 | [diff] [blame] | 13 | private static final String DB_CONFIG_FILE = "conf/ramcloud.conf"; |
| 14 | public static final Configuration config = getConfiguration(); |
| 15 | |
Yuta HIGUCHI | ad7dba9 | 2014-02-03 16:47:15 -0800 | [diff] [blame] | 16 | // 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 HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 20 | // FIXME come up with a proper way to retrieve configuration |
Yuta HIGUCHI | 8436c4c | 2014-02-05 16:55:10 -0800 | [diff] [blame] | 21 | public static final int MAX_MULTI_READS = Math.max(1, Integer |
| 22 | .valueOf(System.getProperty("ramcloud.max_multi_reads", "400"))); |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 23 | |
Yuta HIGUCHI | 8436c4c | 2014-02-05 16:55:10 -0800 | [diff] [blame] | 24 | public static final int MAX_MULTI_WRITES = Math.max(1, Integer |
| 25 | .valueOf(System.getProperty("ramcloud.max_multi_writes", "800"))); |
Yuta HIGUCHI | ad7dba9 | 2014-02-03 16:47:15 -0800 | [diff] [blame] | 26 | |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 27 | private static final ThreadLocal<JRamCloud> tlsRCClient = new ThreadLocal<JRamCloud>() { |
| 28 | @Override |
| 29 | protected JRamCloud initialValue() { |
Yuta HIGUCHI | 67d3a2e | 2014-02-12 18:19:50 -0800 | [diff] [blame] | 30 | return new JRamCloud(getCoordinatorUrl(config)); |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 31 | } |
| 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 HIGUCHI | 67d3a2e | 2014-02-12 18:19:50 -0800 | [diff] [blame] | 44 | 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 HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 71 | } |