示例#1
0
  @Test
  public void testAllRowsReader() throws Exception {
    final AtomicLong counter = new AtomicLong(0);

    AllRowsReader<String, String> reader =
        new AllRowsReader.Builder<String, String>(keyspace, CF_STANDARD1)
            .withPageSize(3)
            .withConcurrencyLevel(2)
            //                .withPartitioner(new Murmur3Partitioner())
            .forEachRow(
                new Function<Row<String, String>, Boolean>() {
                  @Override
                  public Boolean apply(Row<String, String> row) {
                    counter.incrementAndGet();
                    LOG.info("Got a row: " + row.getKey().toString());
                    return true;
                  }
                })
            .build();

    try {
      boolean result = reader.call();
      Assert.assertEquals(counter.get(), 27);
      Assert.assertTrue(result);
    } catch (Exception e) {
      LOG.info(e.getMessage(), e);
      Assert.fail(e.getMessage());
    }
  }
示例#2
0
  @Test
  public void testAllRowsReaderWithCancel() throws Exception {
    final AtomicLong counter = new AtomicLong(0);

    AllRowsReader<String, String> reader =
        new AllRowsReader.Builder<String, String>(keyspace, CF_STANDARD1)
            .withPageSize(3)
            .withConcurrencyLevel(2)
            .forEachRow(
                new Function<Row<String, String>, Boolean>() {
                  @Override
                  public Boolean apply(Row<String, String> row) {
                    try {
                      Thread.sleep(2000);
                    } catch (InterruptedException e) {
                      Thread.currentThread().interrupt();
                      throw new RuntimeException(e);
                    }
                    counter.incrementAndGet();
                    LOG.info("Got a row: " + row.getKey().toString());
                    return true;
                  }
                })
            .build();

    Future<Boolean> future = Executors.newSingleThreadExecutor().submit(reader);

    Thread.sleep(1000);

    reader.cancel();

    try {
      boolean result = future.get();
      Assert.assertEquals(false, result);
    } catch (Exception e) {
      LOG.info("Failed to execute", e);
    }
    LOG.info("Before: " + counter.get());
    Assert.assertNotSame(28, counter.get());
    Thread.sleep(2000);
    LOG.info("After: " + counter.get());
    Assert.assertNotSame(28, counter.get());
  }
示例#3
0
  @Test
  public void testAllRowsReaderConcurrency12() throws Exception {
    final AtomicLong counter = new AtomicLong(0);

    final Map<Long, AtomicLong> threadIds = Maps.newHashMap();

    AllRowsReader<Integer, Integer> reader =
        new AllRowsReader.Builder<Integer, Integer>(keyspace, CF_ALL_ROWS)
            .withPageSize(100)
            .withConcurrencyLevel(12)
            .withColumnSlice(0)
            .forEachRow(
                new Function<Row<Integer, Integer>, Boolean>() {
                  @Override
                  public synchronized Boolean apply(Row<Integer, Integer> row) {
                    long threadId = Thread.currentThread().getId();
                    AtomicLong threadCounter = threadIds.get(threadId);
                    if (threadCounter == null) {
                      threadCounter = new AtomicLong(0);
                      threadIds.put(threadId, threadCounter);
                    }
                    threadCounter.incrementAndGet();
                    counter.incrementAndGet();
                    return true;
                  }
                })
            .build();

    try {
      Stopwatch sw = new Stopwatch().start();
      boolean result = reader.call();
      long runtimeMillis = sw.stop().elapsedMillis();

      LOG.info("Count = " + counter.get() + " runtime=" + runtimeMillis);
      LOG.info("ThreadIds (" + threadIds.size() + ") " + threadIds);
      Assert.assertEquals(threadIds.size(), 12);
      Assert.assertEquals(counter.get(), ALL_ROWS_COUNT);
      Assert.assertTrue(result);
    } catch (Exception e) {
      LOG.info(e.getMessage(), e);
      Assert.fail(e.getMessage());
    }
  }