use of java.util.concurrent.CompletableFuture in project hbase by apache.
the class TestAsyncTableBatch method testPartialSuccess.
@Test
public void testPartialSuccess() throws IOException, InterruptedException, ExecutionException {
Admin admin = TEST_UTIL.getAdmin();
HTableDescriptor htd = admin.getTableDescriptor(TABLE_NAME);
htd.addCoprocessor(ErrorInjectObserver.class.getName());
admin.modifyTable(TABLE_NAME, htd);
AsyncTableBase table = tableGetter.apply(TABLE_NAME);
table.putAll(Arrays.asList(SPLIT_KEYS).stream().map(k -> new Put(k).addColumn(FAMILY, CQ, k)).collect(Collectors.toList())).get();
List<CompletableFuture<Result>> futures = table.get(Arrays.asList(SPLIT_KEYS).stream().map(k -> new Get(k)).collect(Collectors.toList()));
for (int i = 0; i < SPLIT_KEYS.length - 1; i++) {
assertArrayEquals(SPLIT_KEYS[i], futures.get(i).get().getValue(FAMILY, CQ));
}
try {
futures.get(SPLIT_KEYS.length - 1).get();
} catch (ExecutionException e) {
assertThat(e.getCause(), instanceOf(RetriesExhaustedException.class));
}
}
use of java.util.concurrent.CompletableFuture in project hbase by apache.
the class TestAsyncSingleRequestRpcRetryingCaller method testLocateError.
@Test
public void testLocateError() throws IOException, InterruptedException, ExecutionException {
AtomicBoolean errorTriggered = new AtomicBoolean(false);
AtomicInteger count = new AtomicInteger(0);
HRegionLocation loc = CONN.getRegionLocator(TABLE_NAME).getRegionLocation(ROW).get();
AsyncRegionLocator mockedLocator = new AsyncRegionLocator(CONN, AsyncConnectionImpl.RETRY_TIMER) {
@Override
CompletableFuture<HRegionLocation> getRegionLocation(TableName tableName, byte[] row, RegionLocateType locateType, long timeoutNs) {
if (tableName.equals(TABLE_NAME)) {
CompletableFuture<HRegionLocation> future = new CompletableFuture<>();
if (count.getAndIncrement() == 0) {
errorTriggered.set(true);
future.completeExceptionally(new RuntimeException("Inject error!"));
} else {
future.complete(loc);
}
return future;
} else {
return super.getRegionLocation(tableName, row, locateType, timeoutNs);
}
}
@Override
void updateCachedLocation(HRegionLocation loc, Throwable exception) {
}
};
try (AsyncConnectionImpl mockedConn = new AsyncConnectionImpl(CONN.getConfiguration(), CONN.registry, CONN.registry.getClusterId().get(), User.getCurrent()) {
@Override
AsyncRegionLocator getLocator() {
return mockedLocator;
}
}) {
RawAsyncTable table = mockedConn.getRawTableBuilder(TABLE_NAME).setRetryPause(100, TimeUnit.MILLISECONDS).setMaxRetries(5).build();
table.put(new Put(ROW).addColumn(FAMILY, QUALIFIER, VALUE)).get();
assertTrue(errorTriggered.get());
errorTriggered.set(false);
count.set(0);
Result result = table.get(new Get(ROW).addColumn(FAMILY, QUALIFIER)).get();
assertArrayEquals(VALUE, result.getValue(FAMILY, QUALIFIER));
assertTrue(errorTriggered.get());
}
}
use of java.util.concurrent.CompletableFuture in project neo4j by neo4j.
the class ElectionUtil method waitForLeaderAgreement.
public static MemberId waitForLeaderAgreement(Iterable<RaftMachine> validRafts, long maxTimeMillis) throws InterruptedException, TimeoutException {
long viewCount = Iterables.count(validRafts);
Map<MemberId, MemberId> leaderViews = new HashMap<>();
CompletableFuture<MemberId> futureAgreedLeader = new CompletableFuture<>();
Collection<Runnable> destructors = new ArrayList<>();
for (RaftMachine raft : validRafts) {
destructors.add(leaderViewUpdatingListener(raft, validRafts, leaderViews, viewCount, futureAgreedLeader));
}
try {
try {
return futureAgreedLeader.get(maxTimeMillis, TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
} finally {
destructors.forEach(Runnable::run);
}
}
use of java.util.concurrent.CompletableFuture in project neo4j by neo4j.
the class ReplicatedTokenHolderTest method shouldReplicateTokenRequestForNewToken.
@Test
public void shouldReplicateTokenRequestForNewToken() throws Exception {
// given
StorageEngine storageEngine = mockedStorageEngine();
when(dependencies.resolveDependency(StorageEngine.class)).thenReturn(storageEngine);
IdGeneratorFactory idGeneratorFactory = mock(IdGeneratorFactory.class);
IdGenerator idGenerator = mock(IdGenerator.class);
when(idGenerator.nextId()).thenReturn(1L);
when(idGeneratorFactory.get(any(IdType.class))).thenReturn(idGenerator);
TokenRegistry<Token> registry = new TokenRegistry<>("Label");
int generatedTokenId = 1;
ReplicatedTokenHolder<Token> tokenHolder = new ReplicatedLabelTokenHolder(registry, (content, trackResult) -> {
CompletableFuture<Object> completeFuture = new CompletableFuture<>();
completeFuture.complete(generatedTokenId);
return completeFuture;
}, idGeneratorFactory, dependencies);
// when
Integer tokenId = tokenHolder.getOrCreateId("name1");
// then
assertThat(tokenId, equalTo(generatedTokenId));
}
use of java.util.concurrent.CompletableFuture in project jersey by jersey.
the class InMemoryConnector method apply.
@Override
public Future<?> apply(final ClientRequest request, final AsyncConnectorCallback callback) {
CompletableFuture<ClientResponse> future = new CompletableFuture<>();
try {
ClientResponse response = apply(request);
callback.response(response);
future.complete(response);
} catch (ProcessingException ex) {
future.completeExceptionally(ex);
} catch (Throwable t) {
callback.failure(t);
future.completeExceptionally(t);
}
return future;
}
Aggregations