private List<ClusterSummary> getSummaries(PreparedStatement statement) throws SQLException { ResultSet rs = statement.executeQuery(); try { List<ClusterSummary> summaries = Lists.newArrayList(); while (rs.next()) { Cluster cluster = dbQueryExecutor.deserializeBlob(rs.getBlob(1), Cluster.class); ClusterJob clusterJob = dbQueryExecutor.deserializeBlob(rs.getBlob(2), ClusterJob.class); summaries.add(new ClusterSummary(cluster, clusterJob)); } return ImmutableList.copyOf(summaries); } finally { rs.close(); } }
@Override public int getFreeCapacity() throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement("SELECT SUM(capacity_free) FROM provisioners"); try { return dbQueryExecutor.getNum(statement); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException("Exception getting amount of free capacity", e); } }
@Override public Provisioner getProvisioner(String id) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement("SELECT provisioner FROM provisioners WHERE id=?"); statement.setString(1, id); try { return dbQueryExecutor.getQueryItem(statement, Provisioner.class); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException("Exception getting provisioner " + id, e); } }
/** * Queries the store for a list of items, deserializing the items and returning an immutable list of them. If no items * exist, the list will be empty. Statement passed in must be closed by the caller. * * @param statement PreparedStatement of the query, ready for execution. * @param clazz Class of the items being queried. * @param <T> Type of the items being queried. * @return Immutable list of objects that were queried for. * @throws java.sql.SQLException */ public <T> ImmutableList<T> getQueryList(PreparedStatement statement, Class<T> clazz) throws SQLException { return getQueryList(statement, clazz, Integer.MAX_VALUE); }
@Override public PreparedStatement createUpdateStatement(Connection conn) throws SQLException { PreparedStatement statement = conn.prepareStatement("UPDATE users SET profile=? WHERE user_id=? AND tenant_id=?"); statement.setBytes(1, dbQueryExecutor.toBytes(profile, new TypeToken<Map<String, Object>>() { }.getType())); statement.setString(2, account.getUserId()); statement.setString(3, account.getTenantId()); return statement; }
@Override public Set<Node> getAllNodes() throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = getSelectAllNodesStatement(conn); try { return dbQueryExecutor.getQuerySet(statement, Node.class); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException("Exception getting all nodes"); } }
@Override public boolean nodeExists(String nodeId) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = getNodeExistsStatement(conn, nodeId); try { return dbQueryExecutor.hasResults(statement); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException("Exception checking existence of node` " + nodeId, e); } }
private String getNameFromDB(String id) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement("SELECT name FROM tenants WHERE id=? AND deleted=false "); try { statement.setString(1, id); return dbQueryExecutor.getString(statement); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception getting name of tenant {}", id); throw new IOException(e); } }
/** * Queries the store for a list of at most limit items, deserializing the items and performing a transform before * before placing the transformed object into the output, finally returning an immutable list of transformed items. * If no items exist, the list will be empty. Statement passed in must be closed by the caller. * * @param statement PreparedStatement of the query, ready for execution. * @param clazz Class of the items being queried in the statement. * @param transform Transform to perform to change the deserialized object in the table to the object in the output. * @param <F> Type of item being queried * @param <T> Type of item to return * @return Immutable list of transformed objects that were queried for. * @throws SQLException */ public <F, T> ImmutableList<T> getQueryList(PreparedStatement statement, Class<F> clazz, Function<F, T> transform) throws SQLException { return getQueryList(statement, clazz, transform, Integer.MAX_VALUE); }
@Override public PreparedStatement createInsertStatement(Connection conn) throws SQLException { PreparedStatement statement = conn.prepareStatement( "INSERT INTO users (user_id, tenant_id, profile) VALUES (?, ?, ?)"); statement.setString(1, account.getUserId()); statement.setString(2, account.getTenantId()); statement.setBytes(3, dbQueryExecutor.toBytes(profile, new TypeToken<Map<String, Object>>() { }.getType())); return statement; } }
@Override public Set<Node> getClusterNodes(String clusterId) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); long clusterNum = Long.parseLong(clusterId); try { PreparedStatement statement = getSelectClusterNodesStatement(conn, clusterNum); try { return dbQueryExecutor.getQuerySet(statement, Node.class); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException("Exception getting nodes for cluster " + clusterId, e); } }
@Override public boolean clusterExists(String clusterId) throws IOException { long clusterNum = Long.parseLong(clusterId); try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = getClusterExistsStatement(conn, clusterNum); try { return dbQueryExecutor.hasResults(statement); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException("Exception checking existence of cluster " + clusterId, e); } }
@Override public List<Tenant> getAllTenants() throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement("SELECT tenant FROM tenants WHERE deleted=false"); try { return dbQueryExecutor.getQueryList(statement, Tenant.class); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception getting all tenants", e); throw new IOException(e); } }
@Override public void writeTenant(Tenant tenant) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { DBPut tenantPut = new TenantDBPut(tenant, dbQueryExecutor.toBytes(tenant, Tenant.class)); tenantPut.executePut(conn); } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception writing tenant {}", tenant); throw new IOException(e); } }
@Override public Node getNode(String nodeId) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement("SELECT node FROM nodes WHERE id=? "); statement.setString(1, nodeId); try { return dbQueryExecutor.getQueryItem(statement, Node.class); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException(e); } }
/** * Queries the store for a single item, deserializing the item and returning it or null if the item does not exist. * Statement passed in must be closed by the caller. * * @param statement PreparedStatement of the query, ready for execution. * @param clazz Class of the item being queried. * @param <T> Type of the item being queried. * @return Item queried for, or null if none exists. * @throws java.sql.SQLException */ public <T> T getQueryItem(PreparedStatement statement, Class<T> clazz) throws SQLException { ResultSet rs = statement.executeQuery(); try { if (rs.next()) { Blob blob = rs.getBlob(1); return deserializeBlob(blob, clazz); } else { return null; } } finally { rs.close(); } }
@Override public Set<ClusterTask> getRunningTasks(long timestamp) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement("SELECT task FROM tasks WHERE status = ? AND submit_time < ?"); statement.setString(1, ClusterTask.Status.IN_PROGRESS.name()); statement.setTimestamp(2, DBHelper.getTimestamp(timestamp)); try { return dbQueryExecutor.getQuerySet(statement, ClusterTask.class); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException(e); } }
@Override public int getNumAssignedWorkers(String tenantID) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement( "SELECT SUM(num_assigned) FROM provisionerWorkers WHERE tenant_id=?"); try { statement.setString(1, tenantID); return dbQueryExecutor.getNum(statement); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException("Exception getting number of assigned workers for tenant " + tenantID, e); } }
@Override public boolean exists(String name) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement( "SELECT name FROM pluginMeta WHERE " + "tenant_id=? AND plugin_type=? AND plugin_name=? AND resource_type=? AND name=? AND deleted=false"); try { setConstantFields(statement); statement.setString(5, name); return dbQueryExecutor.hasResults(statement); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception checking existance of resource {} for tenant {}.", name, tenant); throw new IOException(e); } }
@Override public Collection<Provisioner> getAllProvisioners() throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement("SELECT provisioner FROM provisioners"); try { return dbQueryExecutor.getQueryList(statement, Provisioner.class); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException("Exception getting all provisioners", e); } }