@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 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 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 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 writeProvisioner(Provisioner provisioner) throws IOException { Connection conn = null; try { conn = dbConnectionPool.getConnection(false); try { DBPut provisionerPut = new ProvisionerDBPut(provisioner, dbQueryExecutor.toBytes(provisioner, Provisioner.class)); provisionerPut.executePut(conn); writeProvisionerWorkers(conn, provisioner); conn.commit(); } finally { conn.close(); } } catch (SQLException e) { LOG.error("Exception writing provisioner, will attempt to rollback.", e); if (conn != null) { try { conn.rollback(); } catch (SQLException e1) { LOG.error("Exception rolling back failed provisioner write", e); } } throw new IOException("Exception writing cluster " + provisioner.getId(), e); } }
@Override public void writeClusterJob(ClusterJob clusterJob) throws IOException { JobId jobId = JobId.fromString(clusterJob.getJobId()); long clusterId = Long.parseLong(jobId.getClusterId()); try { Connection conn = dbConnectionPool.getConnection(); try { byte[] jobBytes = dbQueryExecutor.toBytes(clusterJob, ClusterJob.class); DBPut jobPut = new ClusterJobDBPut(clusterJob, jobBytes, jobId, clusterId); jobPut.executePut(conn); } finally { conn.close(); } } catch (SQLException e) { throw new IOException(e); } }
@Override public void writeNode(final Node node) throws IllegalAccessException, IOException { if (!allowedToWrite(node)) { throw new IllegalAccessException("Not allowed to write node " + node.getId()); } 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("Exception writing node " + node.getId(), e); } }
@Override public void writeClusterTask(ClusterTask clusterTask) throws IOException { TaskId taskId = TaskId.fromString(clusterTask.getTaskId()); long clusterId = Long.parseLong(taskId.getClusterId()); try { Connection conn = dbConnectionPool.getConnection(); try { byte[] taskBytes = dbQueryExecutor.toBytes(clusterTask, ClusterTask.class); DBPut taskPut = new ClusterTaskDBPut(clusterTask, taskBytes, taskId, clusterId); taskPut.executePut(conn); } finally { conn.close(); } } catch (SQLException e) { throw new IOException(e); } }
@Override public void writeCluster(Cluster cluster) throws IllegalAccessException, IOException { if (!allowedToWrite(cluster)) { throw new IllegalAccessException("Not allowed to write cluster " + cluster.getId()); } long clusterNum = Long.parseLong(cluster.getId()); try { Connection conn = dbConnectionPool.getConnection(); try { byte[] clusterBytes = dbQueryExecutor.toBytes(cluster, Cluster.class); DBPut clusterPut = new ClusterDBPut(clusterNum, cluster, clusterBytes); clusterPut.executePut(conn); } finally { conn.close(); } } catch (SQLException e) { throw new IOException("Exception writing cluster " + cluster.getId(), e); } }
@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; }