Skip to content

Commit

Permalink
HWKALERTS-74 unrelated: tweak session management
Browse files Browse the repository at this point in the history
After rebasing on master I had a lot of trouble getting alerts to deploy
correctly in Hawkular Dist.  Schema creation was failing on what appeared
to be some sort of resource conflict.  After some investigation it looked
to me like the getSession() needed to be synchronized to avoid the
possibility of two threads trying to create the schema at the same time.
  • Loading branch information
jshaughn committed Aug 4, 2015
1 parent b5b1253 commit f9c88fc
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void initScheme(Session session, String keyspace) throws IOException {
keyspace = AlertProperties.getProperty(ALERTS_CASSANDRA_KEYSPACE, "hawkular_alerts");
}

log.debugf("Creating Schema for keyspace " + keyspace);
log.debugf("Checking Schema existence for keyspace: %s", keyspace);

ResultSet resultSet = session.execute("SELECT * FROM system.schema_keyspaces WHERE keyspace_name = '" +
keyspace + "'");
Expand All @@ -72,21 +72,28 @@ private void initScheme(Session session, String keyspace) throws IOException {
return;
}

log.infof("Creating Schema for keyspace %s", keyspace);

ImmutableMap<String, String> schemaVars = ImmutableMap.of("keyspace", keyspace);

String updatedCQL = null;
try (InputStream inputStream = CassCluster.class.getResourceAsStream("/hawkular-alerts-schema.cql");
InputStreamReader reader = new InputStreamReader(inputStream)) {
String content = CharStreams.toString(reader);

for (String cql : content.split("(?m)^-- #.*$")) {
if (!cql.startsWith("--")) {
String updatedCQL = substituteVars(cql.trim(), schemaVars);
updatedCQL = substituteVars(cql.trim(), schemaVars);
log.debugf("Executing CQL:\n" + updatedCQL + "\n");
session.execute(updatedCQL);
}
}
} catch (Exception e) {
log.errorf("Failed schema creation: %s\nEXECUTING CQL:\n%s", e, updatedCQL);
}
initialized = true;

log.infof("Done creating Schema for keyspace: " + keyspace);
}

private String substituteVars(String cql, Map<String, String> vars) {
Expand All @@ -103,7 +110,7 @@ private String substituteVars(String cql, Map<String, String> vars) {
}
}

public static Session getSession() throws Exception {
public static synchronized Session getSession() throws Exception {
if (cluster == null && session == null) {
String cqlPort = AlertProperties.getProperty(ALERTS_CASSANDRA_PORT, ALERTS_CASSANDRA_PORT_ENV, "9042");
String nodes = AlertProperties.getProperty(ALERTS_CASSANDRA_NODES, ALERTS_CASSANDRA_NODES_ENV, "127.0.0.1");
Expand Down

0 comments on commit f9c88fc

Please sign in to comment.