use of java.util.function.BiConsumer in project hazelcast by hazelcast.
the class SplitBrainTest method when_quorumIsLostOnMinority_then_jobDoesNotRestartOnMinorityAndCancelledAfterMerge.
@Test
public void when_quorumIsLostOnMinority_then_jobDoesNotRestartOnMinorityAndCancelledAfterMerge() {
int firstSubClusterSize = 3;
int secondSubClusterSize = 2;
int clusterSize = firstSubClusterSize + secondSubClusterSize;
NoOutputSourceP.executionStarted = new CountDownLatch(clusterSize * PARALLELISM);
Job[] jobRef = new Job[1];
Consumer<HazelcastInstance[]> beforeSplit = instances -> {
MockPS processorSupplier = new MockPS(NoOutputSourceP::new, clusterSize);
DAG dag = new DAG().vertex(new Vertex("test", processorSupplier));
jobRef[0] = instances[0].getJet().newJob(dag, new JobConfig().setSplitBrainProtection(true));
assertOpenEventually(NoOutputSourceP.executionStarted);
};
Future[] minorityJobFutureRef = new Future[1];
BiConsumer<HazelcastInstance[], HazelcastInstance[]> onSplit = (firstSubCluster, secondSubCluster) -> {
NoOutputSourceP.proceedLatch.countDown();
assertTrueEventually(() -> assertEquals(clusterSize + firstSubClusterSize, MockPS.initCount.get()));
long jobId = jobRef[0].getId();
assertTrueEventually(() -> {
JetServiceBackend service = getJetServiceBackend(firstSubCluster[0]);
assertEquals(COMPLETED, service.getJobCoordinationService().getJobStatus(jobId).get());
});
JetServiceBackend service2 = getJetServiceBackend(secondSubCluster[0]);
assertTrueEventually(() -> {
MasterContext masterContext = service2.getJobCoordinationService().getMasterContext(jobId);
assertNotNull(masterContext);
minorityJobFutureRef[0] = masterContext.jobContext().jobCompletionFuture();
});
assertTrueAllTheTime(() -> {
assertStatusNotRunningOrStarting(service2.getJobCoordinationService().getJobStatus(jobId).get());
}, 20);
};
Consumer<HazelcastInstance[]> afterMerge = instances -> {
assertTrueEventually(() -> {
assertEquals(clusterSize + firstSubClusterSize, MockPS.initCount.get());
assertEquals(clusterSize + firstSubClusterSize, MockPS.closeCount.get());
});
assertEquals(clusterSize, MockPS.receivedCloseErrors.size());
MockPS.receivedCloseErrors.forEach(t -> assertTrue("received " + t, t instanceof CancellationException));
try {
minorityJobFutureRef[0].get();
fail();
} catch (CancellationException expected) {
} catch (Exception e) {
throw new AssertionError(e);
}
};
testSplitBrain(firstSubClusterSize, secondSubClusterSize, beforeSplit, onSplit, afterMerge);
}
use of java.util.function.BiConsumer in project hazelcast by hazelcast.
the class AbstractEventJournalBasicTest method allowReadingWithFutureSeq.
@Test
public void allowReadingWithFutureSeq() throws Exception {
final EventJournalTestContext<String, Integer, EJ_TYPE> context = createContext();
final EventJournalInitialSubscriberState state = subscribeToEventJournal(context.dataAdapter, partitionId);
assertEquals(0, state.getOldestSequence());
assertEquals(-1, state.getNewestSequence());
assertEventJournalSize(context.dataAdapter, 0);
final Integer value = RANDOM.nextInt();
final CountDownLatch latch = new CountDownLatch(1);
final int startSequence = 1;
final BiConsumer<ReadResultSet<EJ_TYPE>, Throwable> callback = (response, t) -> {
if (t == null) {
latch.countDown();
assertEquals(1, response.size());
final EventJournalEventAdapter<String, Integer, EJ_TYPE> journalAdapter = context.eventJournalAdapter;
final EJ_TYPE e = response.get(0);
assertEquals(ADDED, journalAdapter.getType(e));
assertEquals(value, journalAdapter.getNewValue(e));
} else {
rethrow(t);
}
};
CompletionStage<ReadResultSet<EJ_TYPE>> callbackStage = readFromEventJournal(context.dataAdapter, startSequence, 1, partitionId, TRUE_PREDICATE, IDENTITY_FUNCTION).whenCompleteAsync(callback);
assertTrueEventually(() -> {
context.dataAdapter.put(randomPartitionKey(), value);
assertTrue(latch.await(200, TimeUnit.MILLISECONDS));
}, 30);
// ensure no exception thrown from callback
callbackStage.toCompletableFuture().join();
}
use of java.util.function.BiConsumer in project hazelcast by hazelcast.
the class AbstractCompletableFutureTest method andThen_whenResultAvailable.
@Test
public void andThen_whenResultAvailable() throws Exception {
TestFutureImpl future = new TestFutureImpl();
final Object result = "result";
final BiConsumer callback = mock(BiConsumer.class);
future.complete(result);
future.whenCompleteAsync(callback, executor);
assertSame(result, future.get());
assertTrueEventually(new AssertTask() {
@Override
public void run() {
verify(callback).accept(result, null);
}
});
}
use of java.util.function.BiConsumer in project hazelcast by hazelcast.
the class AbstractCompletableFutureTest method andThen_whenCancelled.
@Test
public void andThen_whenCancelled() {
TestFutureImpl future = new TestFutureImpl();
BiConsumer callback = mock(BiConsumer.class);
future.cancel(false);
future.whenCompleteAsync(callback, executor);
verify(callback).accept(isNull(), isA(CancellationException.class));
}
use of java.util.function.BiConsumer in project DataX by alibaba.
the class DefaultGdbMapper method forStrColumn.
private static Function<Record, String> forStrColumn(final boolean numPattern, final String rule) {
final List<BiConsumer<StringBuilder, Record>> list = new ArrayList<>();
final Pattern pattern = numPattern ? STR_NUM_PATTERN : STR_DOLLAR_PATTERN;
final Matcher m = pattern.matcher(rule);
int last = 0;
while (m.find()) {
final String index = m.group(1);
// as simple integer index.
final int i = Integer.parseInt(index);
final int tmp = last;
final int start = m.start();
list.add((sb, record) -> {
sb.append(rule.subSequence(tmp, start));
if (record.getColumn(i) != null && record.getColumn(i).getByteSize() > 0) {
sb.append(record.getColumn(i).asString());
}
});
last = m.end();
}
final int tmp = last;
list.add((sb, record) -> {
sb.append(rule.subSequence(tmp, rule.length()));
});
return r -> {
final StringBuilder sb = new StringBuilder();
list.forEach(c -> c.accept(sb, r));
final String res = sb.toString();
return res.isEmpty() ? null : res;
};
}
Aggregations