Refine search
@VisibleForTesting String lookupWorkflowIdFromTaskId(String taskId) { try { ResultSet resultSet = session.execute(selectTaskLookupStatement.bind(UUID.fromString(taskId))); return Optional.ofNullable(resultSet.one()) .map(row -> row.getUUID(WORKFLOW_ID_KEY).toString()) .orElse(null); } catch (Exception e) { Monitors.error(CLASS_NAME, "lookupWorkflowIdFromTaskId"); String errorMsg = String.format("Failed to lookup workflowId from taskId: %s", taskId); LOGGER.error(errorMsg, e); throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, errorMsg, e); } } }
@Test(groups = "short") public void should_execute_prepared_statements() throws Exception { // Simple calls to all versions of the execute/executeAsync methods for prepared statements // Note: the goal is only to exercice the Session methods, PreparedStatementTest have better // prepared statement tests. String key = "execute_prepared_test"; ResultSet rs = session() .execute( String.format( Locale.US, "INSERT INTO %s (k, t, i, f) VALUES ('%s', '%s', %d, %f)", TABLE2, key, "foo", 42, 24.03f)); assertThat(rs.isExhausted()).isTrue(); PreparedStatement p = session().prepare(String.format(TestUtils.SELECT_ALL_FORMAT + " WHERE k = ?", TABLE2)); BoundStatement bs = p.bind(key); // executePrepared checkExecuteResultSet(session().execute(bs), key); checkExecuteResultSet(session().execute(bs.setConsistencyLevel(ConsistencyLevel.ONE)), key); // executePreparedAsync checkExecuteResultSet(session().executeAsync(bs).getUninterruptibly(), key); checkExecuteResultSet( session().executeAsync(bs.setConsistencyLevel(ConsistencyLevel.ONE)).getUninterruptibly(), key); }
@VisibleForTesting WorkflowMetadata getWorkflowMetadata(String workflowId) { ResultSet resultSet = session.execute(selectTotalStatement.bind(UUID.fromString(workflowId))); recordCassandraDaoRequests("getWorkflowMetadata"); return Optional.ofNullable(resultSet.one()) .map(row -> { WorkflowMetadata workflowMetadata = new WorkflowMetadata(); workflowMetadata.setTotalTasks(row.getInt(TOTAL_TASKS_KEY)); workflowMetadata.setTotalPartitions(row.getInt(TOTAL_PARTITIONS_KEY)); return workflowMetadata; }).orElseThrow(() -> new ApplicationException(ApplicationException.Code.NOT_FOUND, String.format("Workflow with id: %s not found in data store", workflowId))); }
@Test(groups = "short", expectedExceptions = InvalidQueryException.class) public void should_fail_when_prepared_on_another_cluster() throws Exception { Cluster otherCluster = Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .build(); try { PreparedStatement pst = otherCluster.connect().prepare("select * from system.peers where inet = ?"); BoundStatement bs = pst.bind().setInet(0, InetAddress.getByName("localhost")); // We expect that the error gets detected without a roundtrip to the server, so use // executeAsync session().executeAsync(bs); } finally { otherCluster.close(); } }
scanStmt.append(QueryBuilder.bindMarker()); stmt = session.prepare(scanStmt.toString()); stmt.setConsistencyLevel(readConsistencyLevel); if (trace) { logger.debug("startKey = {}, recordcount = {}", startkey, recordcount); ResultSet rs = session.execute(stmt.bind(startkey, Integer.valueOf(recordcount)));
private void removeTaskLookup(Task task) { try { recordCassandraDaoRequests("removeTaskLookup", task.getTaskType(), task.getWorkflowType()); session.execute(deleteTaskLookupStatement.bind(UUID.fromString(task.getTaskId()))); } catch (Exception e) { Monitors.error(CLASS_NAME, "removeTaskLookup"); String errorMsg = String.format("Failed to remove task lookup: %s", task.getTaskId()); LOGGER.error(errorMsg, e); throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, errorMsg); } }
@Override public boolean removeWorkflow(String workflowId) { Workflow workflow = getWorkflow(workflowId, true); boolean removed = false; // TODO: calculate number of shards and iterate if (workflow != null) { try { recordCassandraDaoRequests("removeWorkflow", "n/a", workflow.getWorkflowName()); ResultSet resultSet = session.execute(deleteWorkflowStatement.bind(UUID.fromString(workflowId), DEFAULT_SHARD_ID)); if (resultSet.wasApplied()) { removed = true; } } catch (Exception e) { Monitors.error(CLASS_NAME, "removeWorkflow"); String errorMsg = String.format("Failed to remove workflow: %s", workflowId); LOGGER.error(errorMsg, e); throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, errorMsg); } workflow.getTasks().forEach(this::removeTaskLookup); } return removed; }
session.execute(updateTaskLookupStatement.bind(UUID.fromString(workflowId), UUID.fromString(task.getTaskId()))); }); tasks.forEach(task -> { String taskPayload = toJson(task); batchStatement.add(insertTaskStatement.bind(UUID.fromString(workflowId), DEFAULT_SHARD_ID, task.getTaskId(), taskPayload)); recordCassandraDaoRequests("createTask", task.getTaskType(), task.getWorkflowType()); recordCassandraDaoPayloadSize("createTask", taskPayload.length(), task.getTaskType(), task.getWorkflowType()); }); batchStatement.add(updateTotalTasksStatement.bind(totalTasks, UUID.fromString(workflowId), DEFAULT_SHARD_ID)); session.execute(batchStatement); session.execute(updateTotalPartitionsStatement.bind(DEFAULT_TOTAL_PARTITIONS, totalTasks, UUID.fromString(workflowId)));
private void insertRecords() { int records = 30000; PreparedStatement insertStmt = session().prepare("insert into record (name, phone, value) values (?, ?, ?)"); for (int i = 0; i < records; i++) { if (i % 1000 == 0) logger.debug("Inserting record {}.", i); session().execute(insertStmt.bind("0", Integer.toString(i), "test")); } logger.debug("Inserts complete."); }
ResultSet resultSet; if (includeTasks) { resultSet = session.execute(selectWorkflowWithTasksStatement.bind(UUID.fromString(workflowId), DEFAULT_SHARD_ID)); List<Task> tasks = new ArrayList<>(); resultSet = session.execute(selectWorkflowStatement.bind(UUID.fromString(workflowId))); workflow = Optional.ofNullable(resultSet.one()) .map(row -> {
/** * Validates that a prepared statement with no variables is correctly prepared and executed. * * @jira_ticket JAVA-1132 */ @Test(groups = "short") public void should_execute_prepared_statement_with_no_variables() throws Exception { PreparedStatement ps = session().prepare("select * from system.local"); session().execute(ps.bind()); }
@Override public Task getTask(String taskId) { try { String workflowId = lookupWorkflowIdFromTaskId(taskId); if (workflowId == null) { return null; } // TODO: implement for query against multiple shards ResultSet resultSet = session.execute(selectTaskStatement.bind(UUID.fromString(workflowId), DEFAULT_SHARD_ID, taskId)); return Optional.ofNullable(resultSet.one()) .map(row -> { Task task = readValue(row.getString(PAYLOAD_KEY), Task.class); recordCassandraDaoRequests("getTask", task.getTaskType(), task.getWorkflowType()); recordCassandraDaoPayloadSize("getTask", toJson(task).length(), task.getTaskType(), task.getWorkflowType()); return task; }) .orElse(null); } catch (Exception e) { Monitors.error(CLASS_NAME, "getTask"); String errorMsg = String.format("Error getting task by id: %s", taskId); LOGGER.error(errorMsg, e); throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, errorMsg); } }
/** * Validates that a prepared statement with variables is correctly prepared and executed. * * @jira_ticket JAVA-1132 */ @Test(groups = "short") public void should_execute_prepared_statement_with_variables() throws Exception { PreparedStatement ps = session().prepare("select * from system.local where key=?"); session().execute(ps.bind("local")); } }
@Override public String createWorkflow(Workflow workflow) { try { workflow.setCreateTime(System.currentTimeMillis()); List<Task> tasks = workflow.getTasks(); workflow.setTasks(new LinkedList<>()); String payload = toJson(workflow); recordCassandraDaoRequests("createWorkflow", "n/a", workflow.getWorkflowName()); recordCassandraDaoPayloadSize("createWorkflow", payload.length(), "n/a", workflow.getWorkflowName()); session.execute(insertWorkflowStatement.bind(UUID.fromString(workflow.getWorkflowId()), 1, "", payload, 0, 1)); workflow.setTasks(tasks); return workflow.getWorkflowId(); } catch (Exception e) { Monitors.error(CLASS_NAME, "createWorkflow"); String errorMsg = String.format("Error creating workflow: %s", workflow.getWorkflowId()); LOGGER.error(errorMsg, e); throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, errorMsg, e); } }
@Test(groups = "short") public void should_set_routing_key_on_case_insensitive_keyspace_and_table() { session().execute(String.format("CREATE TABLE %s.foo (i int PRIMARY KEY)", keyspace)); PreparedStatement ps = session().prepare(String.format("INSERT INTO %s.foo (i) VALUES (?)", keyspace)); BoundStatement bs = ps.bind(1); assertThat(bs.getRoutingKey(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE)) .isNotNull(); }