Skip to content

Commit

Permalink
fixed Postgres related storage issues
Browse files Browse the repository at this point in the history
* added getKeyspaces method to JmxProxy
  • Loading branch information
varjoranta committed Dec 16, 2014
1 parent c6a07bb commit 8de0139
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/main/db/reaper_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@ CREATE INDEX "repair_segment_state_idx" ON "repair_segment" USING BTREE ("state"

GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE cluster TO reaper;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE column_family TO reaper;
GRANT USAGE, SELECT ON SEQUENCE column_family_id_seq TO reaper;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE repair_run TO reaper;
GRANT USAGE, SELECT ON SEQUENCE repair_run_id_seq TO reaper;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE repair_segment TO reaper;
GRANT USAGE, SELECT ON SEQUENCE repair_segment_id_seq TO reaper;
9 changes: 9 additions & 0 deletions src/main/java/com/spotify/reaper/cassandra/JmxProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.Serializable;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -159,6 +160,14 @@ public String getClusterName() {
return toSymbolicName(ssProxy.getClusterName());
}

/**
* @return list of available keyspaces
*/
public List<String> getKeySpaces() {
checkNotNull(ssProxy, "Looks like the proxy is not connected");
return ssProxy.getKeyspaces();
}

/**
* Triggers a repair of range (beginToken, endToken] for given keyspace and column family.
*
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/spotify/reaper/core/Cluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public class Cluster {

@JsonProperty
private final String name;
private final String partitioner; // Name of the partitioner class

@JsonProperty
private final String partitioner; // Full name of the partitioner class

@JsonProperty
private final Set<String> seedHosts;

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;
import java.net.URI;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
Expand All @@ -52,7 +54,6 @@ public ClusterResource(IStorage storage) {
}

@GET
@Path("/")
public Response getClusterList() {
LOG.info("get cluster list called");
Collection<Cluster> clusters = storage.getClusters();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/spotify/reaper/storage/PostgresStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.spotify.reaper.core.RepairSegment;
import com.spotify.reaper.service.RingRange;
import com.spotify.reaper.storage.postgresql.IStoragePostgreSQL;
import com.spotify.reaper.storage.postgresql.PostgresArrayArgumentFactory;

import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;
Expand Down Expand Up @@ -82,6 +83,7 @@ public Collection<Cluster> getClusters() {
@Override
public Cluster addCluster(Cluster newCluster) {
Handle h = jdbi.open();
h.registerArgumentFactory(new PostgresArrayArgumentFactory());
IStoragePostgreSQL postgres = h.attach(IStoragePostgreSQL.class);
int rowsAdded = postgres.insertCluster(newCluster);
h.close();
Expand All @@ -95,6 +97,7 @@ public Cluster addCluster(Cluster newCluster) {
@Override
public boolean updateCluster(Cluster cluster) {
Handle h = jdbi.open();
h.registerArgumentFactory(new PostgresArrayArgumentFactory());
IStoragePostgreSQL postgres = h.attach(IStoragePostgreSQL.class);
int rowsAdded = postgres.updateCluster(cluster);
h.close();
Expand All @@ -114,6 +117,7 @@ public RepairRun getRepairRun(long id) {
@Override
public RepairRun addRepairRun(RepairRun.Builder newRepairRun) {
Handle h = jdbi.open();
h.registerArgumentFactory(new PostgresArrayArgumentFactory());
IStoragePostgreSQL postgres = h.attach(IStoragePostgreSQL.class);
long insertedId = postgres.insertRepairRun(newRepairRun.build(-1));
h.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public interface IStoragePostgreSQL {
//
static final String SQL_INSERT_REPAIR_RUN =
"INSERT INTO repair_run (cause, owner, state, creation_time, start_time, end_time) "
+ "VALUES (:cause, :owner, :state, :creationTime, :startTime, :endTime) RETURNING id";
+ "VALUES (:cause, :owner, :state, :creationTime, :startTime, :endTime)";

static final String SQL_UPDATE_REPAIR_RUN =
"UPDATE repair_run SET cause = :cause, owner = :owner, state = :state, "
Expand All @@ -101,7 +101,7 @@ public interface IStoragePostgreSQL {
static final String SQL_INSERT_COLUMN_FAMILY =
"INSERT INTO column_family (cluster_name, keyspace_name, name, segment_count, "
+ "snapshot_repair) VALUES (:clusterName, :keyspaceName, :name, :segmentCount, "
+ ":snapshotRepair) RETURNING id";
+ ":snapshotRepair)";

static final String SQL_GET_COLUMN_FAMILY =
"SELECT id, cluster_name, keyspace_name, name, segment_count, snapshot_repair "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.spotify.reaper.storage.postgresql;

import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.Argument;
import org.skife.jdbi.v2.tweak.ArgumentFactory;

import java.sql.Array;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;

/**
* Required as we are using JDBI, and it cannot do Array binding otherwise, duh.
*/
public class PostgresArrayArgumentFactory implements ArgumentFactory<Collection<String>> {

@Override
public boolean accepts(Class<?> expectedType, Object value, StatementContext ctx) {
return value instanceof Collection;
}

@Override
public Argument build(Class<?> expectedType, final Collection<String> value,
StatementContext ctx) {
return new Argument() {
public void apply(int position,
PreparedStatement statement,
StatementContext ctx) throws SQLException {
Array sqlArray = ctx.getConnection().createArrayOf("text", value.toArray());
statement.setArray(position, sqlArray);
}
};
}
}
1 change: 1 addition & 0 deletions src/test/resources/cassandra-reaper.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ logging:
level: DEBUG
loggers:
io.dropwizard: INFO
org.eclipse.jetty: INFO
appenders:
- type: console

Expand Down

0 comments on commit 8de0139

Please sign in to comment.