Skip to content

Commit

Permalink
[HWKMETRICS-52] Make sure schema is installed when building task-queu…
Browse files Browse the repository at this point in the history
…e module
  • Loading branch information
John Sanda committed May 12, 2015
1 parent 4aef388 commit cd81352
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ public class MetricsServiceCassandra implements MetricsService {

public static final int DEFAULT_TTL = Duration.standardDays(7).toStandardSeconds().getSeconds();

private static final String SCHEMA_FILE = "/schema.cql";

private static class DataRetentionKey {
private final String tenantId;
private final MetricId metricId;
Expand Down Expand Up @@ -205,7 +203,7 @@ public void startUp(Map<String, String> params) {

if (keyspace==null||keyspace.isEmpty()) {
logger.debug("No explicit keyspace given, will default to 'hawkular'");

This comment has been minimized.

Copy link
@stefannegrea

stefannegrea May 12, 2015

Contributor

Wrong log entry

keyspace = "hawkular-metrics";
keyspace = "hawkular_metrics";
}

logger.info("Using a key space of '" + keyspace + "'");
Expand Down Expand Up @@ -801,7 +799,7 @@ private int getTTL(Metric<?> metric) {
private void updateSchemaIfNecessary(String schemaName) {
try {
SchemaManager schemaManager = new SchemaManager(session.get());
schemaManager.createSchema(schemaName, SCHEMA_FILE);
schemaManager.createSchema(schemaName);
} catch (IOException e) {
throw new RuntimeException("Schema creation failed", e);
}
Expand Down
2 changes: 1 addition & 1 deletion core/metrics-core-impl/src/main/script/schema.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ Session session = cluster.connect()
String keyspace = properties["keyspace"] ?: "hawkulartest"
SchemaManager schemaManager = new SchemaManager(session)
if (properties["resetdb"]) schemaManager.dropKeyspace(keyspace)
schemaManager.createSchema(keyspace, "/schema.cql")
schemaManager.createSchema(keyspace)
2 changes: 1 addition & 1 deletion schema-manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>org.hawkular.metrics</groupId>
<artifactId>hawkular-metrics-parent</artifactId>
<version>0.3.4-SNAPSHOT</version>
<version>0.3.2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void dropKeyspace(String keyspace) {
session.execute("DROP KEYSPACE " + keyspace);
}

public void createSchema(String keyspace, String cqlFile) throws IOException {
public void createSchema(String keyspace) throws IOException {
logger.info("Creating schema for keyspace " + keyspace);

ResultSet resultSet = session.execute("SELECT * FROM system.schema_keyspaces WHERE keyspace_name = '" +
Expand All @@ -61,7 +61,7 @@ public void createSchema(String keyspace, String cqlFile) throws IOException {

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

try (InputStream inputStream = getClass().getResourceAsStream(cqlFile);
try (InputStream inputStream = getClass().getResourceAsStream("/schema.cql");
InputStreamReader reader = new InputStreamReader(inputStream)) {
String content = CharStreams.toString(reader);

Expand Down
24 changes: 24 additions & 0 deletions task-queue/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@
<artifactId>hawkular-metrics-task-queue</artifactId>
<name>Hawkular Metrics Task Queue</name>

<properties>
<test.keyspace>hawkulartest</test.keyspace>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>schema-manager</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
Expand Down Expand Up @@ -62,4 +72,18 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<keyspace>${test.keyspace}</keyspace>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.joda.time.Duration.standardMinutes;
import static org.joda.time.Duration.standardSeconds;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
Expand Down Expand Up @@ -50,6 +51,7 @@
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.Uninterruptibles;
import org.hawkular.metrics.schema.SchemaManager;
import org.hawkular.metrics.tasks.DateTimeService;
import org.hawkular.metrics.tasks.api.Task;
import org.hawkular.metrics.tasks.api.TaskExecutionException;
Expand Down Expand Up @@ -112,16 +114,22 @@ public class TaskServiceImpl implements TaskService {
private TimeUnit timeUnit = TimeUnit.MINUTES;

public TaskServiceImpl(Session session, Queries queries, LeaseService leaseService, List<TaskType> taskTypes) {
this.session = session;
this.queries = queries;
this.leaseService = leaseService;
this.taskTypes = taskTypes;
try {
this.session = session;
this.queries = queries;
this.leaseService = leaseService;
this.taskTypes = taskTypes;
dateTimeService = new DateTimeService();
owner = InetAddress.getLocalHost().getHostName();

SchemaManager schemaManager = new SchemaManager(session);
String keyspace = System.getProperty("keyspace", "hawkular_metrics");
schemaManager.createSchema(keyspace);
} catch (UnknownHostException e) {
throw new RuntimeException("Failed to initialize owner name", e);
} catch (IOException e) {
throw new RuntimeException("Failed to initialize schema", e);
}
dateTimeService = new DateTimeService();
}

/**
Expand Down
12 changes: 10 additions & 2 deletions task-queue/src/test/java/org/hawkular/metrics/tasks/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.datastax.driver.core.Session;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.Uninterruptibles;
import org.hawkular.metrics.schema.SchemaManager;
import org.hawkular.metrics.tasks.impl.Queries;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
Expand All @@ -42,9 +43,16 @@ public class BaseTest {
protected static Queries queries;

@BeforeSuite
public static void initSuite() {
public static void initSuite() throws Exception {
Cluster cluster = Cluster.builder().addContactPoints("127.0.0.01").build();
session = cluster.connect(System.getProperty("keyspace", "hawkulartest"));
String keyspace = System.getProperty("keyspace", "hawkulartest");
session = cluster.connect("system");

SchemaManager schemaManager = new SchemaManager(session);
schemaManager.createSchema(keyspace);

session.execute("USE " + keyspace);

queries = new Queries(session);
dateTimeService = new DateTimeService();
}
Expand Down

1 comment on commit cd81352

@stefannegrea
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this cover schema updates from previous installations?

Please sign in to comment.