@Override public void setHeartbeat(String provisionerId, long ts) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement("UPDATE provisioners SET last_heartbeat=? WHERE id=?"); try { statement.setTimestamp(1, DBHelper.getTimestamp(ts)); statement.setString(2, provisionerId); statement.executeUpdate(); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException("Exception setting heartbeat time for provisioner " + provisionerId, e); } }
@Override public PreparedStatement createInsertStatement(Connection conn) throws SQLException { PreparedStatement statement = conn.prepareStatement( "INSERT INTO provisioners (id, last_heartbeat, capacity_total, capacity_free, provisioner) " + "VALUES (?, ?, ?, ?, ?)"); statement.setString(1, provisioner.getId()); statement.setTimestamp(2, DBHelper.getTimestamp(System.currentTimeMillis())); statement.setInt(3, provisioner.getCapacityTotal()); statement.setInt(4, provisioner.getCapacityFree()); statement.setBytes(5, provisionerBytes); return statement; } }
@Override public PreparedStatement createInsertStatement(Connection conn) throws SQLException { PreparedStatement statement = conn.prepareStatement( "INSERT INTO jobs (job_num, cluster_id, status, create_time, job) VALUES (?, ?, ?, ?, ?)"); statement.setLong(1, jobId.getJobNum()); statement.setLong(2, clusterId); statement.setString(3, clusterJob.getJobStatus().name()); statement.setTimestamp(4, DBHelper.getTimestamp(System.currentTimeMillis())); statement.setBytes(5, jobBytes); return statement; } }
@Override public void deleteTenantByName(String name) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement( "UPDATE tenants SET deleted=true, delete_time=? WHERE name=? "); statement.setTimestamp(1, DBHelper.getTimestamp(System.currentTimeMillis())); statement.setString(2, name); try { statement.executeUpdate(); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception deleting tenant {}", name); 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); } }
@Override public void delete(String name, int version) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement( "UPDATE pluginMeta SET deleted=true, delete_time=? WHERE " + "tenant_id=? AND plugin_type=? AND plugin_name=? AND resource_type=? AND name=? AND version=?"); try { statement.setTimestamp(1, DBHelper.getTimestamp(System.currentTimeMillis())); setConstantFields(statement, 2); statement.setString(6, name); statement.setInt(7, version); statement.executeUpdate(); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception deleting version {} of resource {} for tenant {}.", version, name, tenant); throw new IOException(e); } }
@Override public PreparedStatement createInsertStatement(Connection conn) throws SQLException { PreparedStatement statement = conn.prepareStatement( "INSERT INTO tenants (id, name, workers, tenant, create_time, delete_time, deleted) " + "VALUES (?, ?, ?, ?, ?, null, false)"); statement.setString(1, tenant.getId()); statement.setString(2, tenant.getSpecification().getName()); statement.setInt(3, tenant.getSpecification().getWorkers()); statement.setBytes(4, tenantBytes); statement.setTimestamp(5, DBHelper.getTimestamp(System.currentTimeMillis())); return statement; } }
@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 Set<Cluster> getExpiringClusters(long timestamp) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = conn.prepareStatement("SELECT cluster FROM clusters WHERE status IN (?, ?) AND expire_time < ?"); statement.setString(1, Cluster.Status.ACTIVE.name()); statement.setString(2, Cluster.Status.INCOMPLETE.name()); statement.setTimestamp(3, DBHelper.getTimestamp(timestamp)); try { return dbQueryExecutor.getQuerySet(statement, Cluster.class); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException(e); } }
@Override protected PreparedStatement getSetClusterStatement( Connection conn, long id, Cluster cluster, byte[] clusterBytes) throws SQLException { PreparedStatement statement = conn.prepareStatement( "UPDATE clusters SET cluster=?, owner_id=?, tenant_id=?, status=?, expire_time=?" + " WHERE id=? AND tenant_id=? AND owner_id=?"); statement.setBytes(1, clusterBytes); statement.setString(2, cluster.getAccount().getUserId()); statement.setString(3, cluster.getAccount().getTenantId()); statement.setString(4, cluster.getStatus().name()); statement.setTimestamp(5, DBHelper.getTimestamp(cluster.getExpireTime())); // where clause statement.setLong(6, id); statement.setString(7, tenantId); statement.setString(8, userId); return statement; }
@Override protected PreparedStatement getSetClusterStatement( Connection conn, long id, Cluster cluster, byte[] clusterBytes) throws SQLException { PreparedStatement statement = conn.prepareStatement( "UPDATE clusters SET cluster=?, owner_id=?, tenant_id=?, status=?, expire_time=? WHERE id=? AND tenant_id=?"); statement.setBytes(1, clusterBytes); statement.setString(2, cluster.getAccount().getUserId()); statement.setString(3, cluster.getAccount().getTenantId()); statement.setString(4, cluster.getStatus().name()); statement.setTimestamp(5, DBHelper.getTimestamp(cluster.getExpireTime())); // where clause statement.setLong(6, id); statement.setString(7, tenantId); return statement; }
private PreparedStatement getInsertClusterStatement( Connection conn, long id, Cluster cluster, byte[] clusterBytes) throws SQLException { PreparedStatement statement = conn.prepareStatement( "INSERT INTO clusters (cluster, owner_id, tenant_id, status, expire_time," + " create_time, name, id, latest_job_num) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); statement.setBytes(1, clusterBytes); statement.setString(2, cluster.getAccount().getUserId()); statement.setString(3, cluster.getAccount().getTenantId()); statement.setString(4, cluster.getStatus().name()); statement.setTimestamp(5, DBHelper.getTimestamp(cluster.getExpireTime())); statement.setTimestamp(6, DBHelper.getTimestamp(cluster.getCreateTime())); statement.setString(7, cluster.getName()); statement.setLong(8, id); String latestJobStr = cluster.getLatestJobId(); long latestJobNum = latestJobStr == null ? 0 : JobId.fromString(latestJobStr).getJobNum(); statement.setLong(9, latestJobNum); return statement; }
@Override protected PreparedStatement getSetClusterStatement( Connection conn, long id, Cluster cluster, byte[] clusterBytes) throws SQLException { PreparedStatement statement = conn.prepareStatement( "UPDATE clusters SET cluster=?, owner_id=?, tenant_id=?, status=?, expire_time=?, latest_job_num=? WHERE id=?"); statement.setBytes(1, clusterBytes); statement.setString(2, cluster.getAccount().getUserId()); statement.setString(3, cluster.getAccount().getTenantId()); statement.setString(4, cluster.getStatus().name()); statement.setTimestamp(5, DBHelper.getTimestamp(cluster.getExpireTime())); statement.setLong(6, JobId.fromString(cluster.getLatestJobId()).getJobNum()); // where clause statement.setLong(7, id); return statement; }
@Override public PreparedStatement createUpdateStatement(Connection conn) throws SQLException { PreparedStatement statement = conn.prepareStatement( "UPDATE tasks SET task=?, status=?, submit_time=?, status_time=?, type=?, " + "cluster_template_name=?, user_id=?, tenant_id=?" + " WHERE task_num=? AND job_num=? AND cluster_id=?"); statement.setBytes(1, dbQueryExecutor.toBytes(clusterTask, ClusterTask.class)); statement.setString(2, clusterTask.getStatus().name()); statement.setTimestamp(3, DBHelper.getTimestamp(clusterTask.getSubmitTime())); statement.setTimestamp(4, DBHelper.getTimestamp(clusterTask.getStatusTime())); statement.setString(5, clusterTask.getTaskName().name()); statement.setString(6, clusterTask.getClusterTemplateName()); statement.setString(7, clusterTask.getAccount().getUserId()); statement.setString(8, clusterTask.getAccount().getTenantId()); statement.setLong(9, taskId.getTaskNum()); statement.setLong(10, taskId.getJobNum()); statement.setLong(11, clusterId); return statement; }
statement.setBoolean(8, status.isLive()); statement.setBoolean(9, false); statement.setTimestamp(10, DBHelper.getTimestamp(System.currentTimeMillis())); statement.setTimestamp(11, null); statement.executeUpdate();
@Override public PreparedStatement createInsertStatement(Connection conn) throws SQLException { PreparedStatement statement = conn.prepareStatement( "INSERT INTO tasks (task_num, job_num, cluster_id, status, submit_time, task, type, " + "cluster_template_name, user_id, tenant_id)" + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); statement.setLong(1, taskId.getTaskNum()); statement.setLong(2, taskId.getJobNum()); statement.setLong(3, clusterId); statement.setString(4, clusterTask.getStatus().name()); statement.setTimestamp(5, DBHelper.getTimestamp(clusterTask.getSubmitTime())); statement.setBytes(6, taskBytes); statement.setString(7, clusterTask.getTaskName().name()); statement.setString(8, clusterTask.getClusterTemplateName()); statement.setString(9, clusterTask.getAccount().getUserId()); statement.setString(10, clusterTask.getAccount().getTenantId()); return statement; } }