|
| 1 | +package org.ohdsi.webapi.service; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Optional; |
| 9 | +import org.apache.commons.collections4.map.PassiveExpiringMap; |
| 10 | +import org.ohdsi.sql.SqlTranslate; |
| 11 | +import org.ohdsi.webapi.shiro.management.datasource.SourceAccessor; |
| 12 | +import org.ohdsi.webapi.source.Source; |
| 13 | +import org.ohdsi.webapi.source.SourceDaimon; |
| 14 | +import org.ohdsi.webapi.source.SourceRepository; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.jdbc.CannotGetJdbcConnectionException; |
| 17 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 18 | +import org.springframework.stereotype.Service; |
| 19 | + |
| 20 | +/** |
| 21 | + * This code was extracted from the master branch to fix https://github.com/OHDSI/Atlas/issues/2223 issue. |
| 22 | + * There is no need to merge it into the master as the same functionality implemented there in another place. |
| 23 | + */ |
| 24 | +@Service |
| 25 | +public class SourcePriorityService extends AbstractDaoService { |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private SourceRepository sourceRepository; |
| 29 | + @Autowired |
| 30 | + private SourceAccessor sourceAccessor; |
| 31 | + |
| 32 | + private Map<Source, Boolean> connectionAvailability = Collections.synchronizedMap(new PassiveExpiringMap<>(5000)); |
| 33 | + |
| 34 | + public Source getPrioritySourceForDaimon(SourceDaimon.DaimonType daimonType) { |
| 35 | + |
| 36 | + List<Source> sourcesByDaimonPriority = sourceRepository.findAllSortedByDiamonPrioirty(daimonType); |
| 37 | + |
| 38 | + for (Source source : sourcesByDaimonPriority) { |
| 39 | + if (!(sourceAccessor.hasAccess(source) && connectionAvailability.computeIfAbsent(source, this::checkConnectionSafe))) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + return source; |
| 43 | + } |
| 44 | + |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + public Map<SourceDaimon.DaimonType, Source> getPriorityDaimons() { |
| 49 | + |
| 50 | + class SourceValidator { |
| 51 | + private Map<Integer, Boolean> checkedSources = new HashMap<>(); |
| 52 | + |
| 53 | + private boolean isSourceAvaialble(Source source) { |
| 54 | + |
| 55 | + return checkedSources.computeIfAbsent(source.getSourceId(), |
| 56 | + v -> sourceAccessor.hasAccess(source) && connectionAvailability.computeIfAbsent(source, SourcePriorityService.this::checkConnectionSafe)); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + SourceValidator sourceValidator = new SourceValidator(); |
| 61 | + Map<SourceDaimon.DaimonType, Source> priorityDaimons = new HashMap<>(); |
| 62 | + Arrays.asList(SourceDaimon.DaimonType.values()).forEach(d -> { |
| 63 | + |
| 64 | + List<Source> sources = sourceRepository.findAllSortedByDiamonPrioirty(d); |
| 65 | + Optional<Source> source = sources.stream().filter(sourceValidator::isSourceAvaialble) |
| 66 | + .findFirst(); |
| 67 | + source.ifPresent(s -> priorityDaimons.put(d, s)); |
| 68 | + }); |
| 69 | + return priorityDaimons; |
| 70 | + } |
| 71 | + |
| 72 | + private boolean checkConnectionSafe(Source source) { |
| 73 | + |
| 74 | + try { |
| 75 | + checkConnection(source); |
| 76 | + return true; |
| 77 | + } catch (CannotGetJdbcConnectionException ex) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public void checkConnection(Source source) { |
| 83 | + |
| 84 | + final JdbcTemplate jdbcTemplate = getSourceJdbcTemplate(source); |
| 85 | + jdbcTemplate.execute(SqlTranslate.translateSql("select 1;", source.getSourceDialect()).replaceAll(";$", "")); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments