This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
Commit 1b348d4e authored by Michael Breu's avatar Michael Breu
Browse files

Handling boot time elastic search connect exception more robust

parent 44c959eb
1 merge request!211Merge new meta data indexing
......@@ -189,7 +189,7 @@ public class ElasticsearchConfiguration extends ElasticsearchConfigurationSuppor
}
@Bean
public ElasticsearchClient getElasticsearchAPIClient() throws MalformedURLException {
ElasticsearchClient getElasticsearchAPIClient() throws MalformedURLException {
// Create the transport with a Jackson mapper
final JacksonJsonpMapper mapper = new JacksonJsonpMapper();
......
......@@ -46,6 +46,7 @@ import java.util.Spliterators;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
......@@ -55,6 +56,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.ws.rs.NotFoundException;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
......@@ -110,6 +112,10 @@ public class MetaDataRepository {
@Autowired
private StatisticsRepository statisticsRepository;
@Autowired
@Named("taskExecutor")
private Executor asyncExecutor;
/**
* contains for each Field, a list of Strings mapped to a completion. Together
* with hit counts E.g. "metadata.creator" -> Kerstin -> {"Kerstin Limbeck" ->
......@@ -254,6 +260,25 @@ public class MetaDataRepository {
@Scheduled(cron = "0 */10 * * * ?") // should run every 10 minutes
@PostConstruct
public void triggerUpdateAutocompletionCache() {
Runnable tryUpdate = () -> {
for (int i = 0; i < 10; i++) {
try {
updateAutocompletionCache();
return;
} catch (IOException e) {
LOGGER.info("Connect for update failed: {}. Retrying {}", e, i);
try {
Thread.sleep(2000L);
} catch (InterruptedException e1) {
LOGGER.info("Someone interupted update : {}. Retrying {}", e1, i);
}
}
}
};
asyncExecutor.execute(tryUpdate);
}
public void updateAutocompletionCache() throws IOException {
synchronized (this) {
Map<String, Map<String, Map<String, Integer>>> reloadedCachedCompletions = new ConcurrentHashMap<>();
......@@ -306,7 +331,10 @@ public class MetaDataRepository {
});
cachedCompletions = reloadedCachedCompletions;
} catch (co.elastic.clients.elasticsearch._types.ElasticsearchException | ElasticsearchException | ConnectException ex) {
} catch (ConnectException e) {
// just rethrow silently
throw e;
} catch (co.elastic.clients.elasticsearch._types.ElasticsearchException | ElasticsearchException ex) {
// this may happen mainly during testing, when
// test elasticsearch server is not setup correctly
LOGGER.error("Cannot update completion Cache", ex);
......
......@@ -6,6 +6,7 @@ import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import java.io.IOException;
import java.net.ConnectException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
......@@ -118,14 +119,17 @@ public class MonitoringService {
AtomicInteger exerciseCounter = new AtomicInteger(0);
try {
Map<String, Integer> languageCount = new ConcurrentHashMap<>();
final Stream<SearchResultDTO> searchResults = this.getAllSearchResults();
try {
final Stream<SearchResultDTO> searchResults = this.getAllSearchResults();
searchResults.forEach(exercise -> {
exerciseCounter.addAndGet(1);
updateLanguageCount(exercise, languageCount);
});
this.setExercisesLanguageCount(languageCount);
searchResults.forEach(exercise -> {
exerciseCounter.addAndGet(1);
updateLanguageCount(exercise, languageCount);
});
this.setExercisesLanguageCount(languageCount);
} catch (ConnectException e) {
LOGGER.warn("An Connection Exception occurred {}", e.getMessage());
}
} catch (IOException | ElasticsearchException exception) {
LOGGER.warn("An IOException occurred", exception);
return;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment