use of java.util.function.BiConsumer in project rom-manager by Jakz.
the class Scanner method scanForRoms.
public void scanForRoms(boolean total) throws IOException {
ignoredPaths.clear();
foundFiles.clear();
clones.clear();
if (total) {
logger.i(LogTarget.romset(set), "Scanning for roms");
set.resetStatus();
} else {
logger.i(LogTarget.romset(set), "Scanning for new roms");
set.romStream().filter(r -> r.isPresent()).map(r -> r.handle().path()).forEach(ignoredPaths::add);
}
ignoredPaths.addAll(settings.getIgnoredPaths());
Path folder = settings.romsPath;
if (!canProceedWithScan())
return;
FolderScanner folderScanner = new FolderScanner(ignoredPaths, true);
final Set<Path> pathsToScan = folderScanner.scan(folder);
List<VerifierEntry> foundEntries = Collections.synchronizedList(new ArrayList<>());
/* scanning task */
{
Operation<Path, List<VerifierEntry>> operation = path -> {
logger.d(LogTarget.file(path), "> Scanning %s", path.getFileName().toString());
try {
return scanner.scanFile(path);
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (RuntimeException e) {
logger.e(LogTarget.file(path), "Exception while scanning %s", path.toString());
e.printStackTrace();
if (!SevenZip.isInitializedSuccessfully()) {
Throwable ee = SevenZip.getLastInitializationException();
ee.printStackTrace();
}
return null;
}
};
BiConsumer<Long, Float> guiProgress = (i, f) -> {
Main.progress.update(f, "Scanning " + i + " of " + pathsToScan.size() + "...");
};
Consumer<List<VerifierEntry>> callback = results -> {
foundEntries.addAll(results);
};
Runnable onComplete = () -> {
SwingUtilities.invokeLater(() -> {
Main.progress.finished();
List<VerifierEntry> transformedEntries = foundEntries.stream().collect(Collectors.toList());
HandleSet handleSet = new HandleSet(transformedEntries);
HandleSet.Stats stats = handleSet.stats();
logger.i(LogTarget.romset(set), "Found %d potential matches (%d binary, %d inside archives, %d nested inside %d archives).", stats.totalHandles, stats.binaryCount, stats.archivedCount, stats.nestedArchiveInnerCount, stats.nestedArchiveCount);
transformedEntries.forEach(h -> logger.d(LogTarget.romset(set), "> %s", h.toString()));
Runnable verifyTask = verifyTask(transformedEntries, total);
verifyTask.run();
});
};
AsyncGuiPoolWorker<Path, List<VerifierEntry>> worker = new AsyncGuiPoolWorker<>(operation, guiProgress, 1);
SwingUtilities.invokeLater(() -> {
Main.progress.show(Main.mainFrame, "Scanning " + pathsToScan.size() + " files", () -> worker.cancel());
});
worker.compute(pathsToScan, callback, onComplete);
}
}
use of java.util.function.BiConsumer in project hazelcast-jet by hazelcast.
the class SplitBrainTest method when_quorumIsLostOnMinority_then_jobRestartsUntilMerge.
@Test
public void when_quorumIsLostOnMinority_then_jobRestartsUntilMerge() {
int firstSubClusterSize = 3;
int secondSubClusterSize = 2;
int clusterSize = firstSubClusterSize + secondSubClusterSize;
StuckProcessor.executionStarted = new CountDownLatch(clusterSize * PARALLELISM);
Job[] jobRef = new Job[1];
Consumer<JetInstance[]> beforeSplit = instances -> {
MockPS processorSupplier = new MockPS(StuckProcessor::new, clusterSize);
DAG dag = new DAG().vertex(new Vertex("test", processorSupplier));
jobRef[0] = instances[0].newJob(dag, new JobConfig().setSplitBrainProtection(true));
assertOpenEventually(StuckProcessor.executionStarted);
};
Future[] minorityJobFutureRef = new Future[1];
BiConsumer<JetInstance[], JetInstance[]> onSplit = (firstSubCluster, secondSubCluster) -> {
StuckProcessor.proceedLatch.countDown();
assertTrueEventually(() -> assertEquals(clusterSize + firstSubClusterSize, MockPS.initCount.get()));
long jobId = jobRef[0].getId();
assertTrueEventually(() -> {
JetService service = getJetService(firstSubCluster[0]);
assertEquals(COMPLETED, service.getJobCoordinationService().getJobStatus(jobId));
});
JetService service2 = getJetService(secondSubCluster[0]);
assertTrueEventually(() -> {
assertEquals(STARTING, service2.getJobCoordinationService().getJobStatus(jobId));
});
MasterContext masterContext = service2.getJobCoordinationService().getMasterContext(jobId);
assertNotNull(masterContext);
minorityJobFutureRef[0] = masterContext.completionFuture();
assertTrueAllTheTime(() -> {
assertEquals(STARTING, service2.getJobCoordinationService().getJobStatus(jobId));
}, 20);
};
Consumer<JetInstance[]> 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(t instanceof TopologyChangedException));
try {
minorityJobFutureRef[0].get();
fail();
} catch (CancellationException ignored) {
} catch (Exception e) {
throw new AssertionError(e);
}
};
testSplitBrain(firstSubClusterSize, secondSubClusterSize, beforeSplit, onSplit, afterMerge);
}
use of java.util.function.BiConsumer in project hazelcast-jet by hazelcast.
the class SplitBrainTest method when_quorumIsLostOnBothSides_then_jobRestartsUntilMerge.
@Test
public void when_quorumIsLostOnBothSides_then_jobRestartsUntilMerge() {
int firstSubClusterSize = 2;
int secondSubClusterSize = 2;
int clusterSize = firstSubClusterSize + secondSubClusterSize;
StuckProcessor.executionStarted = new CountDownLatch(clusterSize * PARALLELISM);
Job[] jobRef = new Job[1];
Consumer<JetInstance[]> beforeSplit = instances -> {
MockPS processorSupplier = new MockPS(StuckProcessor::new, clusterSize);
DAG dag = new DAG().vertex(new Vertex("test", processorSupplier));
jobRef[0] = instances[0].newJob(dag, new JobConfig().setSplitBrainProtection(true));
assertOpenEventually(StuckProcessor.executionStarted);
};
BiConsumer<JetInstance[], JetInstance[]> onSplit = (firstSubCluster, secondSubCluster) -> {
StuckProcessor.proceedLatch.countDown();
long jobId = jobRef[0].getId();
assertTrueEventually(() -> {
JetService service1 = getJetService(firstSubCluster[0]);
JetService service2 = getJetService(secondSubCluster[0]);
assertEquals(RESTARTING, service1.getJobCoordinationService().getJobStatus(jobId));
assertEquals(STARTING, service2.getJobCoordinationService().getJobStatus(jobId));
});
assertTrueAllTheTime(() -> {
JetService service1 = getJetService(firstSubCluster[0]);
JetService service2 = getJetService(secondSubCluster[0]);
assertEquals(RESTARTING, service1.getJobCoordinationService().getJobStatus(jobId));
assertEquals(STARTING, service2.getJobCoordinationService().getJobStatus(jobId));
}, 20);
};
Consumer<JetInstance[]> afterMerge = instances -> {
assertTrueEventually(() -> {
assertEquals(clusterSize * 2, MockPS.initCount.get());
assertEquals(clusterSize * 2, MockPS.closeCount.get());
});
assertEquals(clusterSize, MockPS.receivedCloseErrors.size());
MockPS.receivedCloseErrors.forEach(t -> assertTrue(t instanceof TopologyChangedException));
};
testSplitBrain(firstSubClusterSize, secondSubClusterSize, beforeSplit, onSplit, afterMerge);
}
use of java.util.function.BiConsumer in project eventuate-tram-core by eventuate-tram.
the class MessageConsumerKafkaImpl method subscribe.
@Override
public void subscribe(String subscriberId, Set<String> channels, MessageHandler handler) {
BiConsumer<ConsumerRecord<String, String>, BiConsumer<Void, Throwable>> kcHandler = (record, callback) -> {
Message m = toMessage(record);
// TODO If we do that here then remove TT from higher-levels
transactionTemplate.execute(ts -> {
if (duplicateMessageDetector.isDuplicate(subscriberId, m.getId())) {
logger.trace("Duplicate message {} {}", subscriberId, m.getId());
callback.accept(null, null);
return null;
}
try {
logger.trace("Invoking handler {} {}", subscriberId, m.getId());
handler.accept(m);
} catch (Throwable t) {
logger.trace("Got exception {} {}", subscriberId, m.getId());
logger.trace("Got exception ", t);
callback.accept(null, t);
return null;
}
logger.trace("handled message {} {}", subscriberId, m.getId());
callback.accept(null, null);
return null;
});
};
EventuateKafkaConsumer kc = new EventuateKafkaConsumer(subscriberId, kcHandler, new ArrayList<>(channels), bootstrapServers);
consumers.add(kc);
kc.start();
}
use of java.util.function.BiConsumer in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class IntegrationTest method testExecutedTradesStream.
/**
* Test executed trades stream
*/
@Test(timeout = 60000)
public void testExecutedTradesStream() {
final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
// Await at least 2 callbacks
final CountDownLatch latch = new CountDownLatch(2);
try {
bitfinexClient.connect();
final BitfinexExecutedTradeSymbol symbol = new BitfinexExecutedTradeSymbol(BitfinexCurrencyPair.BTC_USD);
final QuoteManager executedTradeManager = bitfinexClient.getQuoteManager();
final BiConsumer<BitfinexExecutedTradeSymbol, ExecutedTrade> callback = (c, o) -> {
latch.countDown();
};
executedTradeManager.registerExecutedTradeCallback(symbol, callback);
executedTradeManager.subscribeExecutedTrades(symbol);
latch.await();
executedTradeManager.unsubscribeExecutedTrades(symbol);
Assert.assertTrue(executedTradeManager.removeExecutedTradeCallback(symbol, callback));
Assert.assertFalse(executedTradeManager.removeExecutedTradeCallback(symbol, callback));
} catch (Exception e) {
// Should not happen
e.printStackTrace();
Assert.assertTrue(false);
} finally {
bitfinexClient.close();
}
}
Aggregations