/** * Retrieve an {@link AsyncBufferedMutator} for performing client-side buffering of writes. * <p> * The returned instance will use default configs. Use * {@link #getBufferedMutatorBuilder(TableName)} if you want to customize some configs. * @param tableName the name of the table * @return an {@link AsyncBufferedMutator} for the supplied tableName. */ default AsyncBufferedMutator getBufferedMutator(TableName tableName) { return getBufferedMutatorBuilder(tableName).build(); }
/** * Retrieve an {@link AsyncBufferedMutator} for performing client-side buffering of writes. * <p> * The returned instance will use default configs. Use * {@link #getBufferedMutatorBuilder(TableName, ExecutorService)} if you want to customize some * configs. * @param tableName the name of the table * @param pool the thread pool to use for executing callback * @return an {@link AsyncBufferedMutator} for the supplied tableName. */ default AsyncBufferedMutator getBufferedMutator(TableName tableName, ExecutorService pool) { return getBufferedMutatorBuilder(tableName, pool).build(); }
private void test(TableName tableName) throws InterruptedException { List<CompletableFuture<Void>> futures = new ArrayList<>(); try (AsyncBufferedMutator mutator = CONN.getBufferedMutatorBuilder(tableName).setWriteBufferSize(16 * 1024).build()) { List<CompletableFuture<Void>> fs = mutator.mutate(IntStream.range(0, COUNT / 2) .mapToObj(i -> new Put(Bytes.toBytes(i)).addColumn(CF, CQ, VALUE)) .collect(Collectors.toList())); // exceeded the write buffer size, a flush will be called directly fs.forEach(f -> f.join()); IntStream.range(COUNT / 2, COUNT).forEach(i -> { futures.add(mutator.mutate(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, VALUE))); }); // the first future should have been sent out. futures.get(0).join(); Thread.sleep(2000); // the last one should still be in write buffer assertFalse(futures.get(futures.size() - 1).isDone()); } // mutator.close will call mutator.flush automatically so all tasks should have been done. futures.forEach(f -> f.join()); AsyncTable<?> table = CONN.getTable(tableName); IntStream.range(0, COUNT).mapToObj(i -> new Get(Bytes.toBytes(i))).map(g -> table.get(g).join()) .forEach(r -> { assertArrayEquals(VALUE, r.getValue(CF, CQ)); }); }
@Test public void testPeriodicFlush() throws InterruptedException, ExecutionException { AsyncBufferedMutator mutator = CONN.getBufferedMutatorBuilder(TABLE_NAME) .setWriteBufferPeriodicFlush(1, TimeUnit.SECONDS).build(); Put put = new Put(Bytes.toBytes(0)).addColumn(CF, CQ, VALUE); CompletableFuture<?> future = mutator.mutate(put); future.get(); AsyncTable<?> table = CONN.getTable(TABLE_NAME); assertArrayEquals(VALUE, table.get(new Get(Bytes.toBytes(0))).get().getValue(CF, CQ)); }
@Test public void testNoPeriodicFlush() throws InterruptedException, ExecutionException { try (AsyncBufferedMutator mutator = CONN.getBufferedMutatorBuilder(TABLE_NAME).disableWriteBufferPeriodicFlush().build()) { Put put = new Put(Bytes.toBytes(0)).addColumn(CF, CQ, VALUE); CompletableFuture<?> future = mutator.mutate(put); Thread.sleep(2000); // assert that we have not flushed it out assertFalse(future.isDone()); mutator.flush(); future.get(); } AsyncTable<?> table = CONN.getTable(TABLE_NAME); assertArrayEquals(VALUE, table.get(new Get(Bytes.toBytes(0))).get().getValue(CF, CQ)); }
@Test public void testCancelPeriodicFlushByClose() throws InterruptedException, ExecutionException { CompletableFuture<?> future; Timeout task; try (AsyncBufferedMutatorImpl mutator = (AsyncBufferedMutatorImpl) CONN.getBufferedMutatorBuilder(TABLE_NAME) .setWriteBufferPeriodicFlush(1, TimeUnit.SECONDS).build()) { future = mutator.mutate(new Put(Bytes.toBytes(0)).addColumn(CF, CQ, VALUE)); task = mutator.periodicFlushTask; // we should have scheduled a periodic flush task assertNotNull(task); } assertTrue(task.isCancelled()); future.get(); AsyncTable<?> table = CONN.getTable(TABLE_NAME); assertArrayEquals(VALUE, table.get(new Get(Bytes.toBytes(0))).get().getValue(CF, CQ)); }
@Test public void testCancelPeriodicFlush() throws InterruptedException, ExecutionException { Put put = new Put(Bytes.toBytes(0)).addColumn(CF, CQ, VALUE); try (AsyncBufferedMutatorImpl mutator = (AsyncBufferedMutatorImpl) CONN .getBufferedMutatorBuilder(TABLE_NAME).setWriteBufferPeriodicFlush(1, TimeUnit.SECONDS) .setWriteBufferSize(10 * put.heapSize()).build()) { List<CompletableFuture<?>> futures = new ArrayList<>(); futures.add(mutator.mutate(put)); Timeout task = mutator.periodicFlushTask; // we should have scheduled a periodic flush task assertNotNull(task); for (int i = 1;; i++) { futures.add(mutator.mutate(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, VALUE))); if (mutator.periodicFlushTask == null) { break; } } assertTrue(task.isCancelled()); CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join(); AsyncTable<?> table = CONN.getTable(TABLE_NAME); for (int i = 0; i < futures.size(); i++) { assertArrayEquals(VALUE, table.get(new Get(Bytes.toBytes(i))).get().getValue(CF, CQ)); } } }
@Test public void testCancelPeriodicFlushByManuallyFlush() throws InterruptedException, ExecutionException { try (AsyncBufferedMutatorImpl mutator = (AsyncBufferedMutatorImpl) CONN.getBufferedMutatorBuilder(TABLE_NAME) .setWriteBufferPeriodicFlush(1, TimeUnit.SECONDS).build()) { CompletableFuture<?> future = mutator.mutate(new Put(Bytes.toBytes(0)).addColumn(CF, CQ, VALUE)); Timeout task = mutator.periodicFlushTask; // we should have scheduled a periodic flush task assertNotNull(task); mutator.flush(); assertTrue(task.isCancelled()); future.get(); AsyncTable<?> table = CONN.getTable(TABLE_NAME); assertArrayEquals(VALUE, table.get(new Get(Bytes.toBytes(0))).get().getValue(CF, CQ)); } }
/** * Retrieve an {@link AsyncBufferedMutator} for performing client-side buffering of writes. * <p> * The returned instance will use default configs. Use * {@link #getBufferedMutatorBuilder(TableName)} if you want to customize some configs. * @param tableName the name of the table * @return an {@link AsyncBufferedMutator} for the supplied tableName. */ default AsyncBufferedMutator getBufferedMutator(TableName tableName) { return getBufferedMutatorBuilder(tableName).build(); }
/** * Retrieve an {@link AsyncBufferedMutator} for performing client-side buffering of writes. * <p> * The returned instance will use default configs. Use * {@link #getBufferedMutatorBuilder(TableName, ExecutorService)} if you want to customize some * configs. * @param tableName the name of the table * @param pool the thread pool to use for executing callback * @return an {@link AsyncBufferedMutator} for the supplied tableName. */ default AsyncBufferedMutator getBufferedMutator(TableName tableName, ExecutorService pool) { return getBufferedMutatorBuilder(tableName, pool).build(); }
/** * Retrieve an {@link AsyncBufferedMutator} for performing client-side buffering of writes. * <p> * The returned instance will use default configs. Use * {@link #getBufferedMutatorBuilder(TableName, ExecutorService)} if you want to customize some * configs. * @param tableName the name of the table * @param pool the thread pool to use for executing callback * @return an {@link AsyncBufferedMutator} for the supplied tableName. */ default AsyncBufferedMutator getBufferedMutator(TableName tableName, ExecutorService pool) { return getBufferedMutatorBuilder(tableName, pool).build(); }
/** * Retrieve an {@link AsyncBufferedMutator} for performing client-side buffering of writes. * <p> * The returned instance will use default configs. Use * {@link #getBufferedMutatorBuilder(TableName)} if you want to customize some configs. * @param tableName the name of the table * @return an {@link AsyncBufferedMutator} for the supplied tableName. */ default AsyncBufferedMutator getBufferedMutator(TableName tableName) { return getBufferedMutatorBuilder(tableName).build(); }
private void test(TableName tableName) throws InterruptedException { List<CompletableFuture<Void>> futures = new ArrayList<>(); try (AsyncBufferedMutator mutator = CONN.getBufferedMutatorBuilder(tableName).setWriteBufferSize(16 * 1024).build()) { List<CompletableFuture<Void>> fs = mutator.mutate(IntStream.range(0, COUNT / 2) .mapToObj(i -> new Put(Bytes.toBytes(i)).addColumn(CF, CQ, VALUE)) .collect(Collectors.toList())); // exceeded the write buffer size, a flush will be called directly fs.forEach(f -> f.join()); IntStream.range(COUNT / 2, COUNT).forEach(i -> { futures.add(mutator.mutate(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, VALUE))); }); // the first future should have been sent out. futures.get(0).join(); Thread.sleep(2000); // the last one should still be in write buffer assertFalse(futures.get(futures.size() - 1).isDone()); } // mutator.close will call mutator.flush automatically so all tasks should have been done. futures.forEach(f -> f.join()); AsyncTable<?> table = CONN.getTable(tableName); IntStream.range(0, COUNT).mapToObj(i -> new Get(Bytes.toBytes(i))).map(g -> table.get(g).join()) .forEach(r -> { assertArrayEquals(VALUE, r.getValue(CF, CQ)); }); }
@Test public void testPeriodicFlush() throws InterruptedException, ExecutionException { AsyncBufferedMutator mutator = CONN.getBufferedMutatorBuilder(TABLE_NAME) .setWriteBufferPeriodicFlush(1, TimeUnit.SECONDS).build(); Put put = new Put(Bytes.toBytes(0)).addColumn(CF, CQ, VALUE); CompletableFuture<?> future = mutator.mutate(put); future.get(); AsyncTable<?> table = CONN.getTable(TABLE_NAME); assertArrayEquals(VALUE, table.get(new Get(Bytes.toBytes(0))).get().getValue(CF, CQ)); }
@Test public void testNoPeriodicFlush() throws InterruptedException, ExecutionException { try (AsyncBufferedMutator mutator = CONN.getBufferedMutatorBuilder(TABLE_NAME).disableWriteBufferPeriodicFlush().build()) { Put put = new Put(Bytes.toBytes(0)).addColumn(CF, CQ, VALUE); CompletableFuture<?> future = mutator.mutate(put); Thread.sleep(2000); // assert that we have not flushed it out assertFalse(future.isDone()); mutator.flush(); future.get(); } AsyncTable<?> table = CONN.getTable(TABLE_NAME); assertArrayEquals(VALUE, table.get(new Get(Bytes.toBytes(0))).get().getValue(CF, CQ)); }
@Test public void testCancelPeriodicFlushByClose() throws InterruptedException, ExecutionException { CompletableFuture<?> future; Timeout task; try (AsyncBufferedMutatorImpl mutator = (AsyncBufferedMutatorImpl) CONN.getBufferedMutatorBuilder(TABLE_NAME) .setWriteBufferPeriodicFlush(1, TimeUnit.SECONDS).build()) { future = mutator.mutate(new Put(Bytes.toBytes(0)).addColumn(CF, CQ, VALUE)); task = mutator.periodicFlushTask; // we should have scheduled a periodic flush task assertNotNull(task); } assertTrue(task.isCancelled()); future.get(); AsyncTable<?> table = CONN.getTable(TABLE_NAME); assertArrayEquals(VALUE, table.get(new Get(Bytes.toBytes(0))).get().getValue(CF, CQ)); }
@Test public void testCancelPeriodicFlush() throws InterruptedException, ExecutionException { Put put = new Put(Bytes.toBytes(0)).addColumn(CF, CQ, VALUE); try (AsyncBufferedMutatorImpl mutator = (AsyncBufferedMutatorImpl) CONN .getBufferedMutatorBuilder(TABLE_NAME).setWriteBufferPeriodicFlush(1, TimeUnit.SECONDS) .setWriteBufferSize(10 * put.heapSize()).build()) { List<CompletableFuture<?>> futures = new ArrayList<>(); futures.add(mutator.mutate(put)); Timeout task = mutator.periodicFlushTask; // we should have scheduled a periodic flush task assertNotNull(task); for (int i = 1;; i++) { futures.add(mutator.mutate(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, VALUE))); if (mutator.periodicFlushTask == null) { break; } } assertTrue(task.isCancelled()); CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join(); AsyncTable<?> table = CONN.getTable(TABLE_NAME); for (int i = 0; i < futures.size(); i++) { assertArrayEquals(VALUE, table.get(new Get(Bytes.toBytes(i))).get().getValue(CF, CQ)); } } }
@Test public void testCancelPeriodicFlushByManuallyFlush() throws InterruptedException, ExecutionException { try (AsyncBufferedMutatorImpl mutator = (AsyncBufferedMutatorImpl) CONN.getBufferedMutatorBuilder(TABLE_NAME) .setWriteBufferPeriodicFlush(1, TimeUnit.SECONDS).build()) { CompletableFuture<?> future = mutator.mutate(new Put(Bytes.toBytes(0)).addColumn(CF, CQ, VALUE)); Timeout task = mutator.periodicFlushTask; // we should have scheduled a periodic flush task assertNotNull(task); mutator.flush(); assertTrue(task.isCancelled()); future.get(); AsyncTable<?> table = CONN.getTable(TABLE_NAME); assertArrayEquals(VALUE, table.get(new Get(Bytes.toBytes(0))).get().getValue(CF, CQ)); } }