@Override public MapKeyResults getAllKeys(final MapScope scope, final String cursor, final int limit ){ final int[] buckets = BUCKET_LOCATOR.getAllBuckets( scope.getName() ); final List<ByteBuffer> partitionKeys = new ArrayList<>(NUM_BUCKETS.length); for (int bucket : buckets) { partitionKeys.add(getMapKeyPartitionKey(scope, bucket)); } Clause in = QueryBuilder.in("key", partitionKeys); Statement statement; if( isBlank(cursor) ){ statement = QueryBuilder.select().all().from(MAP_KEYS_TABLE) .where(in) .setFetchSize(limit); }else{ statement = QueryBuilder.select().all().from(MAP_KEYS_TABLE) .where(in) .setFetchSize(limit) .setPagingState(PagingState.fromString(cursor)); } ResultSet resultSet = session.execute(statement); PagingState pagingState = resultSet.getExecutionInfo().getPagingState(); final List<String> keys = new ArrayList<>(); Iterator<Row> resultIterator = resultSet.iterator(); int size = 0; while( resultIterator.hasNext() && size < limit){ size++; keys.add((String)DataType.text().deserialize(resultIterator.next().getBytes("column1"), ProtocolVersion.NEWEST_SUPPORTED)); } return new MapKeyResults(pagingState != null ? pagingState.toString() : null, keys); }
final PagingState newPagingState = rs.getExecutionInfo().getPagingState();
@Override protected String pageState() { PagingState page = this.results.getExecutionInfo().getPagingState(); if (page == null || this.results.isExhausted()) { return null; } return page.toString(); } }
/** Coordinator management/count */ protected void addCoordinator(ResultSet rs) { InetAddress coordinator = rs.getExecutionInfo().getQueriedHost().getAddress(); Integer n = coordinators.get(coordinator); coordinators.put(coordinator, n == null ? 1 : n + 1); }
@Test(groups = "short") public void should_set_flag_on_successful_agreement() { ProtocolOptions protocolOptions = cluster().getConfiguration().getProtocolOptions(); protocolOptions.maxSchemaAgreementWaitSeconds = 10; ResultSet rs = session().execute(String.format(CREATE_TABLE, COUNTER.getAndIncrement())); assertThat(rs.getExecutionInfo().isSchemaInAgreement()).isTrue(); }
@Test(groups = "short", priority = 1) public void should_unset_flag_on_failed_agreement() { // Setting to 0 results in no query being set, so agreement fails ProtocolOptions protocolOptions = cluster().getConfiguration().getProtocolOptions(); protocolOptions.maxSchemaAgreementWaitSeconds = 0; ResultSet rs = session().execute(String.format(CREATE_TABLE, COUNTER.getAndIncrement())); assertThat(rs.getExecutionInfo().isSchemaInAgreement()).isFalse(); }
@Test(groups = "short") public void should_expose_warnings_on_execution_info() { // the default batch size warn threshold is 5 * 1024 bytes, but after CASSANDRA-10876 there must // be // multiple mutations in a batch to trigger this warning so the batch includes 2 different // inserts. ResultSet rs = session() .execute( String.format( "BEGIN UNLOGGED BATCH\n" + "INSERT INTO foo (k, v) VALUES (1, '%s')\n" + "INSERT INTO foo (k, v) VALUES (2, '%s')\n" + "APPLY BATCH", Strings.repeat("1", 2 * 1024), Strings.repeat("1", 3 * 1024))); List<String> warnings = rs.getExecutionInfo().getWarnings(); assertThat(warnings).hasSize(1); } }
@Test(groups = "short") public void should_encode_null_values() throws Exception { Map<String, ByteBuffer> payload = new HashMap<String, ByteBuffer>(); payload.put("k1", Statement.NULL_PAYLOAD_VALUE); Statement statement = new SimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1); statement.setOutgoingPayload(payload); ResultSet rows = session().execute(statement); Map<String, ByteBuffer> actual = rows.getExecutionInfo().getIncomingPayload(); assertThat(actual).isEqualTo(payload); }
@Test(groups = "short") @CassandraVersion(value = "2.0.0") public void should_reuse_wrapped_simple_statement_for_multipage_query() { loadBalancingPolicy.customStatementsHandled.set(0); for (int v = 1; v <= 100; v++) session().execute(new SimpleStatement(INSERT_MULTIPAGE_QUERY, "key_simple_multipage", v)); SimpleStatement s = new SimpleStatement(SELECT_MULTIPAGE_QUERY, "key_simple_multipage"); s.setFetchSize(1); CustomStatement customStatement = new CustomStatement(s); ResultSet rs = session().execute(customStatement); assertThat(loadBalancingPolicy.customStatementsHandled.get()).isEqualTo(1); Iterator<Row> it = rs.iterator(); assertThat(it.hasNext()).isTrue(); it.next(); assertThat(rs.getExecutionInfo().getStatement()).isEqualTo(customStatement); assertThat(loadBalancingPolicy.customStatementsHandled.get()).isEqualTo(1); assertThat(it.hasNext()).isTrue(); it.next(); assertThat(rs.getExecutionInfo().getStatement()).isEqualTo(customStatement); assertThat(loadBalancingPolicy.customStatementsHandled.get()).isEqualTo(2); }
@Test(groups = "short") public void should_set_flag_on_non_schema_altering_statement() { ProtocolOptions protocolOptions = cluster().getConfiguration().getProtocolOptions(); protocolOptions.maxSchemaAgreementWaitSeconds = 10; ResultSet rs = session().execute("select release_version from system.local"); assertThat(rs.getExecutionInfo().isSchemaInAgreement()).isTrue(); }
@Test(groups = "short") public void should_echo_custom_payload_when_executing_statement() throws Exception { Statement statement = new SimpleStatement("SELECT c2 FROM t1 where c1 = ?", 1); statement.setOutgoingPayload(payload1); ResultSet rows = session().execute(statement); Map<String, ByteBuffer> actual = rows.getExecutionInfo().getIncomingPayload(); assertThat(actual).isEqualTo(payload1); }
@Override public ListenableFuture<ResultSet> apply(ResultSet rs) throws Exception { int remainingInPage = rs.getAvailableWithoutFetching(); for (Row row : rs) { all.add(row.getInt(0)); if (--remainingInPage == 0) break; } boolean wasLastPage = rs.getExecutionInfo().getPagingState() == null; if (wasLastPage) return Futures.immediateFuture(rs); else return GuavaCompatibility.INSTANCE.transformAsync(rs.fetchMoreResults(), this); } }
@Test(groups = "short") public void should_echo_custom_payload_when_executing_batch_statement() throws Exception { Statement statement = new BatchStatement().add(new SimpleStatement("INSERT INTO t1 (c1, c2) values (1, 'foo')")); statement.setOutgoingPayload(payload1); ResultSet rows = session().execute(statement); Map<String, ByteBuffer> actual = rows.getExecutionInfo().getIncomingPayload(); assertThat(actual).isEqualTo(payload1); }
@Test(groups = "short") public void should_echo_custom_payload_when_building_statement() throws Exception { Statement statement = select("c2").from("t1").where(eq("c1", 1)).setOutgoingPayload(payload1); ResultSet rows = session().execute(statement); Map<String, ByteBuffer> actual = rows.getExecutionInfo().getIncomingPayload(); assertThat(actual).isEqualTo(payload1); }
/** * Validates that the "unsafe" paging state can be reused with the same Statement. * * @test_category paging * @expected_result {@link ResultSet} from the query with the provided raw paging state starts * from the subsequent row from the first query. */ @Test(groups = "short") public void should_complete_when_using_unsafe_paging_state() { SimpleStatement st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); ResultSet result = session().execute(st.setFetchSize(20)); int pageSize = result.getAvailableWithoutFetching(); byte[] savedPagingState = result.getExecutionInfo().getPagingStateUnsafe(); st = new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY)); result = session().execute(st.setFetchSize(20).setPagingStateUnsafe(savedPagingState)); // We have the result starting from the next page we stopped assertThat(result.one().getInt("v")).isEqualTo(pageSize); }
@Test(groups = "short") public void should_use_host_on_statement() { for (int i = 0; i < 10; i++) { int hostIndex = i % 3 + 1; Host host = TestUtils.findHost(cluster, hostIndex); // given a statement with host explicitly set. Statement statement = new SimpleStatement("select * system.local").setHost(host); // when statement is executed ResultSet result = session.execute(statement); // then the query should have been sent to the configured host. assertThat(result.getExecutionInfo().getQueriedHost()).isSameAs(host); verifyNoLbpInteractions(); } }
/** * Validates that {@link PagingState} can be reused with the same {@link BoundStatement}. * * @test_category paging * @expected_result {@link ResultSet} from the query with the provided paging state starts from * the subsequent row from the first query. */ @Test(groups = "short") @CassandraVersion("2.0.0") public void should_be_able_to_use_state_with_bound_statement() { PreparedStatement prepared = session().prepare("SELECT v from test where k=?"); BoundStatement bs = prepared.bind(KEY); ResultSet result = session().execute(bs.setFetchSize(20)); int pageSize = result.getAvailableWithoutFetching(); PagingState pagingState = result.getExecutionInfo().getPagingState(); result = session().execute(bs.setFetchSize(20).setPagingState(pagingState)); // We have the result starting from the next page we stopped assertThat(result.one().getInt("v")).isEqualTo(pageSize); }
/** * Validates that {@link PagingState} can be reused with a wrapped Statement. * * @test_category paging * @expected_result {@link ResultSet} from the query with the provided {@link PagingState} starts * from the subsequent row from the first query. */ @Test(groups = "short") public void should_use_state_with_wrapped_statement() { Statement st = new TestWrapper(new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY))); ResultSet result = session().execute(st.setFetchSize(20)); int pageSize = result.getAvailableWithoutFetching(); String savedPagingStateString = result.getExecutionInfo().getPagingState().toString(); st = new TestWrapper(new SimpleStatement(String.format("SELECT v FROM test WHERE k='%s'", KEY))); result = session() .execute( st.setFetchSize(20).setPagingState(PagingState.fromString(savedPagingStateString))); // We have the result starting from the next page we stopped assertThat(result.one().getInt("v")).isEqualTo(pageSize); }
/** * Validates that {@link PagingState} cannot be reused with a different {@link BoundStatement} * than the original, even if its source {@link PreparedStatement} was the same. * * @test_category paging * @expected_result A failure is thrown when setting paging state on a different {@link * BoundStatement}. */ @Test( groups = "short", expectedExceptions = {PagingStateException.class}) @CassandraVersion("2.0.0") public void should_not_be_able_to_use_state_with_different_bound_statement() { PreparedStatement prepared = session().prepare("SELECT v from test where k=?"); BoundStatement bs0 = prepared.bind(KEY); ResultSet result = session().execute(bs0.setFetchSize(20)); PagingState pagingState = result.getExecutionInfo().getPagingState(); BoundStatement bs1 = prepared.bind("different_key"); session().execute(bs1.setFetchSize(20).setPagingState(pagingState)); }
@Test(groups = "short") public void should_not_start_speculative_execution_if_first_execution_completes_successfully() { scassandras .node(1) .primingClient() .prime( PrimingRequest.queryBuilder() .withQuery("mock query") .withThen(then().withRows(row("result", "result1"))) .build()); long execStartCount = errors.getSpeculativeExecutions().getCount(); ResultSet rs = session.execute("mock query"); Row row = rs.one(); assertThat(row.getString("result")).isEqualTo("result1"); assertThat(errors.getSpeculativeExecutions().getCount()).isEqualTo(execStartCount); ExecutionInfo executionInfo = rs.getExecutionInfo(); assertThat(executionInfo.getTriedHosts()).containsOnly(host1); assertThat(executionInfo.getQueriedHost()).isEqualTo(host1); assertThat(executionInfo.getSpeculativeExecutions()).isEqualTo(0); assertThat(executionInfo.getSuccessfulExecutionIndex()).isEqualTo(0); }