private void scheduleSweepRowTask(@Output List<Future<CqlResult>> futures, int queryId, int rowIndex, AtomicInteger nextRowToQuery, List<byte[]> rows, ExecutorService executor) { if (rowIndex >= rows.size()) { return; } byte[] row = rows.get(rowIndex); Callable<CqlResult> task = () -> { CqlResult cqlResult = queryExecutor.executePrepared(queryId, ImmutableList.of(ByteBuffer.wrap(row))); if (!Thread.interrupted()) { scheduleSweepRowTask(futures, queryId, nextRowToQuery.getAndIncrement(), nextRowToQuery, rows, executor); } return cqlResult; }; try { Future<CqlResult> future = executor.submit(task); futures.set(rowIndex, future); } catch (RejectedExecutionException e) { // RejectedExecutionException are expected. // The executor is shutdown when we already fetched all the values we were interested // for the current iteration. log.trace("Rejecting row {} because executor is closed", rows.get(rowIndex)); } }
@Test public void getTimestampsForGivenRows() { String expected = "SELECT key, column1, column2 FROM \"foo__bar\"" + " WHERE key = ? LIMIT 100;"; int executorThreads = AtlasDbConstants.DEFAULT_SWEEP_CASSANDRA_READ_THREADS; executor.getTimestamps(TABLE_REF, ImmutableList.of(ROW, END_ROW), LIMIT, PTExecutors.newFixedThreadPool(executorThreads), executorThreads); verify(queryExecutor).prepare(argThat(byteBufferMatcher(expected)), eq(ROW), any()); verify(queryExecutor).executePrepared(eq(1), eq(ImmutableList.of(ByteBuffer.wrap(ROW)))); verify(queryExecutor).executePrepared(eq(1), eq(ImmutableList.of(ByteBuffer.wrap(END_ROW)))); }
@Before public void before() { CqlResult result = new CqlResult(); result.setRows(ImmutableList.of()); when(queryExecutor.execute(any(), any())).thenAnswer(invocation -> { Uninterruptibles.sleepUninterruptibly(queryDelayMillis, TimeUnit.MILLISECONDS); return result; }); CqlPreparedResult preparedResult = new CqlPreparedResult(); preparedResult.setItemId(1); when(queryExecutor.prepare(any(), any(), any())).thenReturn(preparedResult); when(queryExecutor.executePrepared(eq(1), any())).thenReturn(result); }
private void scheduleSweepRowTask(@Output List<Future<CqlResult>> futures, int queryId, int rowIndex, AtomicInteger nextRowToQuery, List<byte[]> rows, ExecutorService executor) { if (rowIndex >= rows.size()) { return; } byte[] row = rows.get(rowIndex); Callable<CqlResult> task = () -> { CqlResult cqlResult = queryExecutor.executePrepared(queryId, ImmutableList.of(ByteBuffer.wrap(row))); if (!Thread.interrupted()) { scheduleSweepRowTask(futures, queryId, nextRowToQuery.getAndIncrement(), nextRowToQuery, rows, executor); } return cqlResult; }; try { Future<CqlResult> future = executor.submit(task); futures.set(rowIndex, future); } catch (RejectedExecutionException e) { // RejectedExecutionException are expected. // The executor is shutdown when we already fetched all the values we were interested // for the current iteration. log.trace("Rejecting row {} because executor is closed", rows.get(rowIndex)); } }