first commit for new repo
[sdc/sdc-titan-cassandra.git] / src / test / java / com / thinkaurelius / titan / CassandraStorageSetup.java
1 package com.thinkaurelius.titan;
2
3 import static com.thinkaurelius.titan.diskstorage.cassandra.AbstractCassandraStoreManager.CASSANDRA_KEYSPACE;
4
5 import java.io.File;
6 import java.io.IOException;
7 import java.time.Duration;
8 import java.util.concurrent.TimeUnit;
9
10 import com.thinkaurelius.titan.diskstorage.cassandra.AbstractCassandraStoreManager;
11
12 import com.thinkaurelius.titan.diskstorage.cassandra.utils.CassandraDaemonWrapper;
13 import com.thinkaurelius.titan.diskstorage.configuration.ModifiableConfiguration;
14 import com.thinkaurelius.titan.diskstorage.configuration.WriteConfiguration;
15
16 import org.apache.commons.io.FileUtils;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import com.google.common.base.Joiner;
21 import com.google.common.base.Preconditions;
22
23 import static com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration.*;
24
25 public class CassandraStorageSetup {
26
27     public static final String CONFDIR_SYSPROP = "test.cassandra.confdir";
28     public static final String DATADIR_SYSPROP = "test.cassandra.datadir";
29
30     private static volatile Paths paths;
31
32     private static final Logger log = LoggerFactory.getLogger(CassandraStorageSetup.class);
33
34     private static synchronized Paths getPaths() {
35         if (null == paths) {
36             String yamlPath = "file://" + loadAbsoluteDirectoryPath("conf", CONFDIR_SYSPROP, true) + File.separator + "cassandra.yaml";
37             String dataPath = loadAbsoluteDirectoryPath("data", DATADIR_SYSPROP, false);
38             paths = new Paths(yamlPath, dataPath);
39         }
40         return paths;
41     }
42
43     private static ModifiableConfiguration getGenericConfiguration(String ks, String backend) {
44         ModifiableConfiguration config = buildGraphConfiguration();
45         config.set(CASSANDRA_KEYSPACE, cleanKeyspaceName(ks));
46         log.debug("Set keyspace name: {}", config.get(CASSANDRA_KEYSPACE));
47         config.set(PAGE_SIZE,500);
48         config.set(CONNECTION_TIMEOUT, Duration.ofSeconds(60L));
49         config.set(STORAGE_BACKEND, backend);
50         return config;
51     }
52
53
54     public static ModifiableConfiguration getEmbeddedConfiguration(String ks) {
55         ModifiableConfiguration config = getGenericConfiguration(ks, "embeddedcassandra");
56         config.set(STORAGE_CONF_FILE, getPaths().yamlPath);
57         return config;
58     }
59
60     public static ModifiableConfiguration getEmbeddedCassandraPartitionConfiguration(String ks) {
61         ModifiableConfiguration config = getEmbeddedConfiguration(ks);
62         config.set(IDS_FLUSH,false);
63         return config;
64     }
65
66     public static WriteConfiguration getEmbeddedGraphConfiguration(String ks) {
67         return getEmbeddedConfiguration(ks).getConfiguration();
68     }
69
70     public static WriteConfiguration getEmbeddedCassandraPartitionGraphConfiguration(String ks) {
71         return getEmbeddedConfiguration(ks).getConfiguration();
72     }
73
74     public static ModifiableConfiguration getAstyanaxConfiguration(String ks) {
75         return getGenericConfiguration(ks, "astyanax");
76     }
77
78     public static ModifiableConfiguration getAstyanaxSSLConfiguration(String ks) {
79         return enableSSL(getGenericConfiguration(ks, "astyanax"));
80     }
81
82     public static WriteConfiguration getAstyanaxGraphConfiguration(String ks) {
83         return getAstyanaxConfiguration(ks).getConfiguration();
84     }
85
86     public static ModifiableConfiguration getCassandraConfiguration(String ks) {
87         return getGenericConfiguration(ks, "cassandra");
88     }
89
90     public static WriteConfiguration getCassandraGraphConfiguration(String ks) {
91         return getCassandraConfiguration(ks).getConfiguration();
92     }
93
94     public static ModifiableConfiguration getCassandraThriftConfiguration(String ks) {
95         return getGenericConfiguration(ks, "cassandrathrift");
96     }
97
98     public static ModifiableConfiguration getCassandraThriftSSLConfiguration(String ks) {
99         return enableSSL(getGenericConfiguration(ks, "cassandrathrift"));
100     }
101
102     public static WriteConfiguration getCassandraThriftGraphConfiguration(String ks) {
103         return getCassandraThriftConfiguration(ks).getConfiguration();
104     }
105
106     /**
107      * Load cassandra.yaml and data paths from the environment or from default
108      * values if nothing is set in the environment, then delete all existing
109      * data, and finally start Cassandra.
110      * <p>
111      * This method is idempotent. Calls after the first have no effect aside
112      * from logging statements.
113      */
114     public static void startCleanEmbedded() {
115         startCleanEmbedded(getPaths());
116     }
117
118     /*
119      * Cassandra only accepts keyspace names 48 characters long or shorter made
120      * up of alphanumeric characters and underscores.
121      */
122     public static String cleanKeyspaceName(String raw) {
123         Preconditions.checkNotNull(raw);
124         Preconditions.checkArgument(0 < raw.length());
125
126         if (48 < raw.length() || raw.matches("[^a-zA-Z_]")) {
127             return "strhash" + String.valueOf(Math.abs(raw.hashCode()));
128         } else {
129             return raw;
130         }
131     }
132
133     private static ModifiableConfiguration enableSSL(ModifiableConfiguration mc) {
134         mc.set(AbstractCassandraStoreManager.SSL_ENABLED, true);
135         mc.set(STORAGE_HOSTS, new String[]{ "localhost" });
136         mc.set(AbstractCassandraStoreManager.SSL_TRUSTSTORE_LOCATION,
137                 Joiner.on(File.separator).join("target", "cassandra", "conf", "localhost-murmur-ssl", "test.truststore"));
138         mc.set(AbstractCassandraStoreManager.SSL_TRUSTSTORE_PASSWORD, "cassandra");
139         return mc;
140     }
141
142     private static void startCleanEmbedded(Paths p) {
143         if (!CassandraDaemonWrapper.isStarted()) {
144             try {
145                 FileUtils.deleteDirectory(new File(p.dataPath));
146             } catch (IOException e) {
147                 throw new RuntimeException(e);
148             }
149         }
150
151         CassandraDaemonWrapper.start(p.yamlPath);
152     }
153
154     private static String loadAbsoluteDirectoryPath(String name, String prop, boolean mustExistAndBeAbsolute) {
155         String s = System.getProperty(prop);
156
157         if (null == s) {
158             s = Joiner.on(File.separator).join(System.getProperty("user.dir"), "target", "cassandra", name, "localhost-bop");
159             log.info("Set default Cassandra {} directory path {}", name, s);
160         } else {
161             log.info("Loaded Cassandra {} directory path {} from system property {}", new Object[] { name, s, prop });
162         }
163
164         if (mustExistAndBeAbsolute) {
165             File dir = new File(s);
166             Preconditions.checkArgument(dir.isDirectory(), "Path %s must be a directory", s);
167             Preconditions.checkArgument(dir.isAbsolute(),  "Path %s must be absolute", s);
168         }
169
170         return s;
171     }
172
173     private static class Paths {
174         private final String yamlPath;
175         private final String dataPath;
176
177         public Paths(String yamlPath, String dataPath) {
178             this.yamlPath = yamlPath;
179             this.dataPath = dataPath;
180         }
181     }
182 }