@Override protected void startUp() throws Exception { if (dbConnectionPool.isEmbeddedDerbyDB()) { DBHelper.createDerbyTableIfNotExists( "CREATE TABLE users ( " + "user_id VARCHAR(256), " + "tenant_id VARCHAR(64), " + "profile BLOB )", dbConnectionPool); } }
/** * Get a {@link Connection} from the pool with auto commit on. * * @return Connection from the pool with autocommit. * @throws SQLException */ public Connection getConnection() throws SQLException { return getConnection(true); }
/** * 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 protected void startUp() throws Exception { if (dbConnectionPool.isEmbeddedDerbyDB()) { for (BaseEntityStoreView.EntityType entityType : BaseEntityStoreView.EntityType.values()) { String entityName = entityType.getId(); // immune to sql injection since it comes from the enum String createString = "CREATE TABLE " + entityName + "s ( name VARCHAR(255), version BIGINT, tenant_id VARCHAR(255), " + entityName + " BLOB, PRIMARY KEY (tenant_id, name, version))"; DBHelper.createDerbyTableIfNotExists(createString, dbConnectionPool); } } }
@Override protected void startUp() throws Exception { if (dbConnectionPool.isEmbeddedDerbyDB()) { DBHelper.createDerbyTableIfNotExists( "CREATE TABLE tenants ( " + "id VARCHAR(255), " + "name VARCHAR(255), " + "workers INT, " + "deleted BOOLEAN, " + "create_time TIMESTAMP, " + "delete_time TIMESTAMP, " + "tenant BLOB )", dbConnectionPool); } // add superadmin if it doesn't exist Tenant superadminTenant = getTenantByName(Constants.SUPERADMIN_TENANT); if (superadminTenant == null) { writeTenant(Tenant.DEFAULT_SUPERADMIN); } }
@Override protected void startUp() throws Exception { if (dbConnectionPool.isEmbeddedDerbyDB()) { DBHelper.createDerbyTableIfNotExists("CREATE TABLE provisioners (" + "id VARCHAR(255), " + "last_heartbeat TIMESTAMP, " + "capacity_total INTEGER, " + "capacity_free INTEGER, " + "provisioner BLOB, " + "PRIMARY KEY (id) )", dbConnectionPool); DBHelper.createDerbyIndex(dbConnectionPool, "provisioners_heartbeat_index", "provisioners", "last_heartbeat"); DBHelper.createDerbyTableIfNotExists("CREATE TABLE provisionerWorkers (" + "provisioner_id VARCHAR(255), " + "tenant_id VARCHAR(255), " + "num_assigned INTEGER, " + "num_live INTEGER, " + "PRIMARY KEY (tenant_id, provisioner_id) )", dbConnectionPool); } }
@Override public void writeNode(Node node) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { byte[] nodeBytes = dbQueryExecutor.toBytes(node, Node.class); DBPut nodePut = new NodeDBPut(node, nodeBytes); nodePut.executePut(conn); } finally { conn.close(); } } catch (SQLException 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 Collection<Provisioner> getTimedOutProvisioners(long idleTimestamp) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement( "SELECT provisioner FROM provisioners WHERE last_heartbeat < ?"); try { statement.setTimestamp(1, DBHelper.getTimestamp(idleTimestamp)); return dbQueryExecutor.getQueryList(statement, Provisioner.class); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException("Exception getting all provisioners", e); } }
@AfterClass public static void afterClass() { DBHelper.dropDerbyDB(); } }
@Override protected void startUp() throws Exception { if (dbConnectionPool.isEmbeddedDerbyDB()) { boolean created = DBHelper.createDerbyTableIfNotExists( "CREATE TABLE pluginMeta (" + "tenant_id VARCHAR(255), " + "plugin_type VARCHAR(16), " + "plugin_name VARCHAR(255), " + "resource_type VARCHAR(255), " + "name VARCHAR(255), " + "version INTEGER, " + "live BOOLEAN, " + "slated BOOLEAN, " + "deleted BOOLEAN, " + "create_time TIMESTAMP," + "delete_time TIMESTAMP," + "PRIMARY KEY(tenant_id, plugin_type, plugin_name, resource_type, name, version) )", dbConnectionPool); if (created) { DBHelper.createDerbyIndex(dbConnectionPool, "plugin_meta_index", "pluginMeta", "tenant_id", "plugin_type", "plugin_name", "resource_type", "name", "version"); } } }
@Override protected void startUp() throws Exception { if (dbConnectionPool.isEmbeddedDerbyDB()) { DBHelper.createDerbyTableIfNotExists( "CREATE TABLE sensitiveFields ( " + "tenant_id VARCHAR(64), " + "cluster_id VARCHAR(255), " + "fields BLOB )", dbConnectionPool); } }
@AfterClass public static void afterClass() { DBHelper.dropDerbyDB(); } }
protected void clearData() throws SQLException { Connection conn = dbConnectionPool.getConnection(); try { Statement stmt = conn.createStatement(); try { stmt.execute("DELETE FROM nodes"); } finally { stmt.close(); } } finally { conn.close(); } }
/** * 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 protected void startUp() throws Exception { if (dbConnectionPool.isEmbeddedDerbyDB()) { LOG.warn("Initializing Derby DB... Tables are not optimized for performance."); boolean created = DBHelper.createDerbyTableIfNotExists("CREATE TABLE nodes ( " + "cluster_id BIGINT, " + "id VARCHAR(64), " + "node BLOB, " + "PRIMARY KEY (id) )", dbConnectionPool ); if (created) { DBHelper.createDerbyIndex(dbConnectionPool, "nodes_cluster_index", "nodes", "cluster_id", "id"); } } }
@AfterClass public static void afterClass() { DBHelper.dropDerbyDB(); } }
public void clearData() throws SQLException { Connection conn = dbConnectionPool.getConnection(); try { Statement stmt = conn.createStatement(); try { stmt.executeUpdate("DELETE FROM pluginMeta"); } finally { stmt.close(); } } finally { conn.close(); } }
@AfterClass public static void afterClass() { DBHelper.dropDerbyDB(); } }
@AfterClass public static void afterClass() { DBHelper.dropDerbyDB(); }