Skip to content

Commit

Permalink
fix: latch and state (#36)
Browse files Browse the repository at this point in the history
* fix: weird latch in exporter service

* fix: state adds new data but not rewrites all

* fix: templatecreator latch eq amount of chunks
  • Loading branch information
Dankoy committed Oct 4, 2023
1 parent 900d471 commit 6279b8f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ExporterServiceAnkiAsync implements ExporterService {
private final TemplateCreatorService templateCreatorService;
private final FilesProperties filesProperties;
private final StateService stateService;
private CountDownLatch latch = new CountDownLatch(0);
private CountDownLatch latch;

// The IoService is provided type, that's why we inject it using @Lookup annotation.
// @Lookup annotation doesn't work inside prototype bean, so had to use constructor to inject beans
Expand Down Expand Up @@ -67,35 +67,36 @@ public void export(String sourceLanguage, String targetLanguage, List<String> op
List<Vocabulary> vocabulariesFull = vocabularyService.getAll();
List<Vocabulary> filtered = stateService.filterState(vocabulariesFull);

if (!filtered.isEmpty() && filtered.size() < THREADS) {
if (!filtered.isEmpty()) {

latch = new CountDownLatch(1);
executorService.execute(
() -> asyncFunc(ankiDataList, filtered, sourceLanguage, targetLanguage, options));
if (filtered.size() < THREADS) {

} else if (filtered.size() >= THREADS) {
latch = new CountDownLatch(filtered.size());
executorService.execute(
() -> asyncFunc(ankiDataList, filtered, sourceLanguage, targetLanguage, options));

latch = new CountDownLatch(THREADS);
List<Vocabulary> oneV = filtered.subList(0, filtered.size() / 2);
List<Vocabulary> twoV = filtered.subList((filtered.size() / 2), filtered.size());
} else {

executorService.execute(
() -> asyncFunc(ankiDataList, oneV, sourceLanguage, targetLanguage, options));
executorService.execute(
() -> asyncFunc(ankiDataList, twoV, sourceLanguage, targetLanguage, options));
latch = new CountDownLatch(THREADS);
List<Vocabulary> oneV = filtered.subList(0, filtered.size() / 2);
List<Vocabulary> twoV = filtered.subList((filtered.size() / 2), filtered.size());

}
executorService.execute(
() -> asyncFunc(ankiDataList, oneV, sourceLanguage, targetLanguage, options));
executorService.execute(
() -> asyncFunc(ankiDataList, twoV, sourceLanguage, targetLanguage, options));

try {
latch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new KorvoRootException("Interrupted while waiting for task completion", e);
}
}

executorService.shutdown();
try {
latch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new KorvoRootException("Interrupted while waiting for task completion", e);
}

executorService.shutdown();

if (!filtered.isEmpty()) {
var template = templateCreatorService.create(ankiDataList);

var ioService = getIoService(
Expand All @@ -104,7 +105,8 @@ public void export(String sourceLanguage, String targetLanguage, List<String> op
filesProperties.getExportFileName());
ioService.print(template);

stateService.saveState(vocabulariesFull);
stateService.saveState(filtered);

} else {
log.info("State is the same as database. Export is not necessary.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,22 @@ public List<Vocabulary> filterState(List<Vocabulary> vocabularies) {
@Override
public void saveState(List<Vocabulary> vocabularies) {

// add new exported data to existing state

var ioServiceState = getIoService(
getFileProviderService(),
getFileNameFormatterService(),
filesProperties.getStateFileName());

final List<State> currentState = checkState();

List<State> states = vocabularies.stream()
.map(v -> new State(v.word()))
.toList();

var string = mapperService.convertToString(states);
currentState.addAll(states);

var string = mapperService.convertToString(currentState);

ioServiceState.print(string);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
public class TemplateCreatorServiceImpl implements TemplateCreatorService {

private final TemplateBuilder templateBuilder;
private CountDownLatch latch = new CountDownLatch(
Runtime.getRuntime().availableProcessors());
private CountDownLatch latch;

@Override
public String create(List<AnkiData> ankiDataList) {
Expand All @@ -38,15 +37,12 @@ public String create(List<AnkiData> ankiDataList) {
List<List<AnkiData>> splitted = splitToPartitions(ankiDataList, cores);
ExecutorService executorService = Executors.newFixedThreadPool(cores);

int toLatch = 0;
latch = new CountDownLatch(splitted.size());

for (List<AnkiData> sp : splitted) {
executorService.execute(() -> convertToDto(dtos, sp));
toLatch++;
}

toLatch = Math.min(toLatch, Runtime.getRuntime().availableProcessors());
latch = new CountDownLatch(toLatch);

try {
latch.await();
} catch (InterruptedException e) {
Expand Down

0 comments on commit 6279b8f

Please sign in to comment.