Skip to content

Commit

Permalink
fix existing resource skeleton and database schema for cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
varjoranta committed Dec 3, 2014
1 parent d778b18 commit 443b5b7
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 93 deletions.
5 changes: 2 additions & 3 deletions src/main/db/reaper_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
-- GRANT ALL PRIVILEGES ON DATABASE reaper_db TO reaper;

CREATE TABLE IF NOT EXISTS "cluster" (
"id" SERIAL PRIMARY KEY,
"name" TEXT PRIMARY KEY,
"partitioner" TEXT NOT NULL,
"name" TEXT NOT NULL,
"seed_hosts" TEXT[] NOT NULL
);

CREATE TABLE IF NOT EXISTS "column_family" (
"id" SERIAL PRIMARY KEY,
"cluster_id" INT NOT NULL REFERENCES "cluster" ("id"),
"cluster_name" TEXT NOT NULL REFERENCES "cluster" ("name"),
"keyspace_name" TEXT NOT NULL,
"name" TEXT NOT NULL,
"strategy" TEXT NOT NULL,
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/com/spotify/reaper/ReaperApplication.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.spotify.reaper;

import com.spotify.reaper.resources.ClusterResource;
import com.spotify.reaper.resources.AddTableResource;
import com.spotify.reaper.resources.PingResource;
import com.spotify.reaper.resources.RepairTableResource;
import com.spotify.reaper.resources.TableResource;
import com.spotify.reaper.storage.IStorage;
import com.spotify.reaper.storage.MemoryStorage;
import com.spotify.reaper.storage.PostgresStorage;
Expand Down Expand Up @@ -41,13 +40,11 @@ public void run(ReaperApplicationConfiguration config,

final PingResource pingResource = new PingResource();
final ClusterResource addClusterResource = new ClusterResource(storage);
final AddTableResource addTableResource = new AddTableResource();
final RepairTableResource repairTableResource = new RepairTableResource();
final TableResource addTableResource = new TableResource(storage);

environment.jersey().register(pingResource);
environment.jersey().register(addClusterResource);
environment.jersey().register(addTableResource);
environment.jersey().register(repairTableResource);
}

private IStorage initializeStorage(ReaperApplicationConfiguration config,
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/com/spotify/reaper/cassandra/JmxProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,16 @@ public String getPartitioner() {
return ssProxy.getPartitionerName();
}

public static String toSymbolicName(String s) {
return s.toLowerCase().replaceAll("[^a-z0-9_]", "");
}

/**
* @return Cassandra cluster name.
*/
public String getClusterName() {
checkNotNull(ssProxy, "Looks like the proxy is not connected");
return ssProxy.getClusterName();
}

public static String toSymbolicName(String s) {
return s.toLowerCase().replaceAll("[^a-z0-9_]", "");
}

public String getSymbolicName() {
return toSymbolicName(getClusterName());
return toSymbolicName(ssProxy.getClusterName());
}

/**
Expand Down
25 changes: 4 additions & 21 deletions src/main/java/com/spotify/reaper/core/Cluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@
import java.util.Set;

public class Cluster {
private Long id;
private final String partitioner; // String or actual class?

private final String name;
private final String partitioner; // Name of the partitioner class
private final Set<String> seedHosts;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getPartitioner() {
return partitioner;
}
Expand All @@ -28,26 +20,18 @@ public Set<String> getSeedHosts() {
return seedHosts;
}

private Cluster(Builder builder)
{
this.id = builder.id;
private Cluster(Builder builder) {
this.partitioner = builder.partitioner;
this.name = builder.name;
this.seedHosts = builder.seedHosts;
}


public static class Builder {
private Long id;

private String partitioner;
private String name;
private Set<String> seedHosts;

public Builder id(long id) {
this.id = id;
return this;
}

public Builder partitioner(String partitioner) {
this.partitioner = partitioner;
return this;
Expand All @@ -63,7 +47,6 @@ public Builder seedHosts(Set<String> seedHosts) {
return this;
}


public Cluster build() {
return new Cluster(this);
}
Expand Down
23 changes: 0 additions & 23 deletions src/main/java/com/spotify/reaper/resources/AddTableResource.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public ClusterResource(IStorage storage) {
@GET
@Path("/{name}")
public Response getCluster(@PathParam("name") String name) {
LOG.info("get cluster called with name: {}", name);
Cluster cluster = storage.getCluster(name);
return Response.ok().entity(cluster).build();
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/spotify/reaper/resources/PingResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.google.common.base.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
Expand All @@ -12,8 +15,11 @@
@Produces(MediaType.TEXT_PLAIN)
public class PingResource {

private static final Logger LOG = LoggerFactory.getLogger(ClusterResource.class);

@GET
public String answerPing(@QueryParam("name") Optional<String> name) {
LOG.info("ping called with name: {}", name);
return String.format("Ping %s", name.or("stranger"));
}

Expand Down

This file was deleted.

52 changes: 52 additions & 0 deletions src/main/java/com/spotify/reaper/resources/TableResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.spotify.reaper.resources;

import com.google.common.base.Optional;

import com.spotify.reaper.core.Cluster;
import com.spotify.reaper.storage.IStorage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/table")
@Produces(MediaType.APPLICATION_JSON)
public class TableResource {

private static final Logger LOG = LoggerFactory.getLogger(TableResource.class);

private final IStorage storage;

public TableResource(IStorage storage) {
this.storage = storage;
}

@GET
@Path("/{clusterName}/{keyspace}/{table}")
public Response getCluster(@PathParam("clusterName") String clusterName,
@PathParam("keyspace") String keyspace,
@PathParam("table") String table) {
LOG.info("get table called with: clusterName = {}, keyspace = {}, table = {}",
clusterName, keyspace, table);
return Response.ok().entity("not implemented yet").build();
}

@POST
public Response addTable(@QueryParam("clusterName") Optional<String> clusterName,
@QueryParam("seedHost") Optional<String> seedHost,
@QueryParam("keyspace") Optional<String> keyspace,
@QueryParam("table") Optional<String> table) {
LOG.info("add table called with: clusterName = {}, seedHost = {}, keyspace = {}, table = {}",
clusterName, seedHost, keyspace, table);
return Response.ok().entity("not implemented yet").build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ public class ClusterMapper implements ResultSetMapper<Cluster> {
public Cluster map(int index, ResultSet r, StatementContext ctx) throws SQLException {
String[] seedHosts = (String[]) r.getArray("seed_hosts").getArray();
return new Cluster.Builder()
.id(r.getLong("id"))
.partitioner(r.getString("partitioner"))
.name(r.getString("name"))
.partitioner(r.getString("partitioner"))
.seedHosts(new HashSet<String>(Arrays.asList(seedHosts)))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,19 @@
*/
public interface IStoragePostgreSQL {

static final String SQL_GET_CLUSTER = "SELECT id, partitioner, name, seed_hosts FROM cluster ";
static final String SQL_GET_CLUSTER = "SELECT name, partitioner, seed_hosts FROM cluster "
+ "WHERE name = :name";

static final String SQL_INSERT_CLUSTER = "INSERT INTO cluster (partitioner, name, seed_hosts) "
+ "VALUES (:partitioner, :name, :seedHosts)";
static final String SQL_INSERT_CLUSTER = "INSERT INTO cluster (name, partitioner, seed_hosts) "
+ "VALUES (:name, :partitioner, :seedHosts)";

static final String SQL_UPDATE_CLUSTER = "UPDATE cluster SET partitioner = :partitioner, "
+ "name = :name, seed_hosts = :seedHosts WHERE id = :id";
+ "seed_hosts = :seedHosts WHERE name = :name";

@SqlQuery(SQL_GET_CLUSTER + "WHERE name = :name")
@SqlQuery(SQL_GET_CLUSTER)
@Mapper(ClusterMapper.class)
public Cluster getCluster(@Bind("name") String clusterName);

@SqlQuery(SQL_GET_CLUSTER + "WHERE id = :id")
@Mapper(ClusterMapper.class)
public Cluster getCluster(@Bind("id") long id);

@SqlUpdate(SQL_INSERT_CLUSTER)
public int insertCluster(@BindBean Cluster newCluster);

Expand Down

0 comments on commit 443b5b7

Please sign in to comment.